Skip to content

Commit 663df0e

Browse files
committed
[unity] Fixed SpineVisualElement not rendering back-faces by providing a Flip Back Faces property (UI Elements). Closes #3016.
1 parent 092decd commit 663df0e

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,33 @@ void ResizeOptionalUVBuffer (ref ExposedList<Vector2> uvBuffer, int vertexCount)
12301230
}
12311231
}
12321232
}
1233+
1234+
public void FlipBackfaceWindingOrder () {
1235+
int submeshCount = submeshes.Count;
1236+
ExposedList<int>[] submeshesItems = submeshes.Items;
1237+
Vector3[] vertexPositions = vertexBuffer.Items;
1238+
1239+
for (int s = 0; s < submeshCount; ++s) {
1240+
ExposedList<int> submeshIndices = submeshesItems[s];
1241+
int indexCount = submeshIndices.Count;
1242+
int[] indices = submeshIndices.Items;
1243+
for (int i = 0; i < indexCount; i += 3) {
1244+
int iA = indices[i];
1245+
int iB = indices[i + 1];
1246+
int iC = indices[i + 2];
1247+
Vector3 a = vertexPositions[iA];
1248+
Vector3 b = vertexPositions[iB];
1249+
Vector3 c = vertexPositions[iC];
1250+
Vector2 d1 = new Vector2(b.x - a.x, b.y - a.y);
1251+
Vector2 d2 = new Vector2(c.x - b.x, c.y - b.y);
1252+
float z = d1.x * d2.y - d1.y * d2.x;
1253+
if (z < 0.0f) {
1254+
indices[i + 1] = iC;
1255+
indices[i + 2] = iB;
1256+
}
1257+
}
1258+
}
1259+
}
12331260
#endregion
12341261

12351262
#region Step 3 : Transfer vertex and triangle data to UnityEngine.Mesh

spine-unity/Assets/Spine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.esotericsoftware.spine.spine-unity",
33
"displayName": "spine-unity Runtime",
44
"description": "This plugin provides the spine-unity runtime core and examples. Spine Examples can be installed via the Samples tab.",
5-
"version": "4.3.40",
5+
"version": "4.3.41",
66
"unity": "2018.3",
77
"author": {
88
"name": "Esoteric Software",

spine-unity/Modules/com.esotericsoftware.spine.ui-toolkit/Runtime/SpineVisualElement.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public string InitialSkinName {
9595
}
9696
public string initialSkinName;
9797

98+
/// <summary>Flip indices of back-faces to correct winding order during mesh generation.
99+
/// UI Elements otherwise does not draw back-faces.</summary>
100+
[UxmlAttribute] public bool flipBackFaces { get; set; } = true;
98101
[UxmlAttribute] public bool startingLoop { get; set; } = true;
99102
[UxmlAttribute] public float timeScale { get; set; } = 1.0f;
100103

@@ -335,6 +338,8 @@ protected void GenerateVisualContents (MeshGenerationContext context) {
335338
// clipping is done, vertex counts are final.
336339

337340
PrepareUISubmesh(uiSubmesh, meshGenerator.VertexCount, meshGenerator.SubmeshIndexCount(0));
341+
if (flipBackFaces)
342+
meshGenerator.FlipBackfaceWindingOrder();
338343
meshGenerator.FillVertexData(ref uiSubmesh.verticesSlice);
339344
meshGenerator.FillTrianglesSingleSubmesh(ref uiSubmesh.indicesSlice);
340345

spine-unity/Modules/com.esotericsoftware.spine.ui-toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.esotericsoftware.spine.ui-toolkit",
33
"displayName": "Spine UI Toolkit [Experimental]",
44
"description": "This plugin provides UI Toolkit integration for the spine-unity runtime.\n\nPrerequisites:\nIt requires a working installation of the spine-unity runtime, version 4.3.0 or newer and Unity 6000.0.16 or newer (requires [this bugfix](https://issuetracker.unity3d.com/issues/some-default-uxmlconverters-are-dependent-on-the-current-culture)).\n(See http://esotericsoftware.com/git/spine-runtimes/spine-unity)",
5-
"version": "4.3.0-preview.3",
5+
"version": "4.3.0-preview.4",
66
"unity": "6000.0",
77
"author": {
88
"name": "Esoteric Software",

0 commit comments

Comments
 (0)