From f8332b7773f0e1684dcfd7378c423f745960ff26 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Fri, 4 Apr 2025 19:22:21 +0200 Subject: [PATCH] [csharp][unity] Port of commit 68d262b5: Improved rendering performance when an attachment is fully inside a clipping attachment. Reuse the original vertices rather than clipping's triangle soup. --- spine-csharp/src/Skeleton.cs | 3 +- spine-csharp/src/SkeletonClipping.cs | 43 ++++++++++--------- spine-csharp/src/package.json | 2 +- .../Mesh Generation/MeshGenerator.cs | 4 +- spine-unity/Assets/Spine/package.json | 4 +- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/spine-csharp/src/Skeleton.cs b/spine-csharp/src/Skeleton.cs index 5f9f1e49e..a4db4ce12 100644 --- a/spine-csharp/src/Skeleton.cs +++ b/spine-csharp/src/Skeleton.cs @@ -736,8 +736,7 @@ namespace Spine { } if (vertices != null) { - if (clipper != null && clipper.IsClipping) { - clipper.ClipTriangles(vertices, triangles, triangles.Length); + if (clipper != null && clipper.IsClipping && clipper.ClipTriangles(vertices, triangles, triangles.Length)) { vertices = clipper.ClippedVertices.Items; verticesLength = clipper.ClippedVertices.Count; } diff --git a/spine-csharp/src/SkeletonClipping.cs b/spine-csharp/src/SkeletonClipping.cs index fd2573c26..c2408992f 100644 --- a/spine-csharp/src/SkeletonClipping.cs +++ b/spine-csharp/src/SkeletonClipping.cs @@ -78,7 +78,7 @@ namespace Spine { clippingPolygon.Clear(); } - public void ClipTriangles (float[] vertices, int[] triangles, int trianglesLength) { + public bool ClipTriangles (float[] vertices, int[] triangles, int trianglesLength) { ExposedList clipOutput = this.clipOutput, clippedVertices = this.clippedVertices; ExposedList clippedTriangles = this.clippedTriangles; ExposedList[] polygons = clippingPolygons.Items; @@ -87,24 +87,25 @@ namespace Spine { int index = 0; clippedVertices.Clear(); clippedTriangles.Clear(); + float[] clipOutputItems = null; for (int i = 0; i < trianglesLength; i += 3) { - int vertexOffset = triangles[i] << 1; - float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1]; + int v = triangles[i] << 1; + float x1 = vertices[v], y1 = vertices[v + 1]; - vertexOffset = triangles[i + 1] << 1; - float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1]; + v = triangles[i + 1] << 1; + float x2 = vertices[v], y2 = vertices[v + 1]; - vertexOffset = triangles[i + 2] << 1; - float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1]; + v = triangles[i + 2] << 1; + float x3 = vertices[v], y3 = vertices[v + 1]; for (int p = 0; p < polygonsCount; p++) { int s = clippedVertices.Count; if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) { + clipOutputItems = clipOutput.Items; int clipOutputLength = clipOutput.Count; if (clipOutputLength == 0) continue; int clipOutputCount = clipOutputLength >> 1; - float[] clipOutputItems = clipOutput.Items; float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items; for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) { float x = clipOutputItems[ii], y = clipOutputItems[ii + 1]; @@ -140,9 +141,10 @@ namespace Spine { } } } + return clipOutputItems != null; } - public void ClipTriangles (float[] vertices, int[] triangles, int trianglesLength, float[] uvs) { + public bool ClipTriangles (float[] vertices, int[] triangles, int trianglesLength, float[] uvs) { ExposedList clipOutput = this.clipOutput, clippedVertices = this.clippedVertices; ExposedList clippedTriangles = this.clippedTriangles; ExposedList[] polygons = clippingPolygons.Items; @@ -152,30 +154,30 @@ namespace Spine { clippedVertices.Clear(); clippedUVs.Clear(); clippedTriangles.Clear(); - + float[] clipOutputItems = null; for (int i = 0; i < trianglesLength; i += 3) { - int vertexOffset = triangles[i] << 1; - float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1]; - float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1]; + int v = triangles[i] << 1; + float x1 = vertices[v], y1 = vertices[v + 1]; + float u1 = uvs[v], v1 = uvs[v + 1]; - vertexOffset = triangles[i + 1] << 1; - float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1]; - float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1]; + v = triangles[i + 1] << 1; + float x2 = vertices[v], y2 = vertices[v + 1]; + float u2 = uvs[v], v2 = uvs[v + 1]; - vertexOffset = triangles[i + 2] << 1; - float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1]; - float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1]; + v = triangles[i + 2] << 1; + float x3 = vertices[v], y3 = vertices[v + 1]; + float u3 = uvs[v], v3 = uvs[v + 1]; for (int p = 0; p < polygonsCount; p++) { int s = clippedVertices.Count; if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) { + clipOutputItems = clipOutput.Items; int clipOutputLength = clipOutput.Count; if (clipOutputLength == 0) continue; float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1; float d = 1 / (d0 * d2 + d1 * (y1 - y3)); int clipOutputCount = clipOutputLength >> 1; - float[] clipOutputItems = clipOutput.Items; float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items; float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items; for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) { @@ -226,6 +228,7 @@ namespace Spine { } } } + return clipOutputItems != null; } ///Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping diff --git a/spine-csharp/src/package.json b/spine-csharp/src/package.json index ced3135bc..510728638 100644 --- a/spine-csharp/src/package.json +++ b/spine-csharp/src/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-csharp", "displayName": "spine-csharp Runtime", "description": "This plugin provides the spine-csharp core runtime.", - "version": "4.2.36", + "version": "4.3.0", "unity": "2018.3", "author": { "name": "Esoteric Software", diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs index 769860926..4541e9055 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs @@ -682,8 +682,8 @@ namespace Spine.Unity { color.b = (byte)(skeletonB * slot.B * c.b * 255); } - if (useClipping && clipper.IsClipping) { - clipper.ClipTriangles(workingVerts, attachmentTriangleIndices, attachmentIndexCount, uvs); + if (useClipping && clipper.IsClipping + && clipper.ClipTriangles(workingVerts, attachmentTriangleIndices, attachmentIndexCount, uvs)) { workingVerts = clipper.ClippedVertices.Items; attachmentVertexCount = clipper.ClippedVertices.Count >> 1; attachmentTriangleIndices = clipper.ClippedTriangles.Items; diff --git a/spine-unity/Assets/Spine/package.json b/spine-unity/Assets/Spine/package.json index 6ee911457..f441579df 100644 --- a/spine-unity/Assets/Spine/package.json +++ b/spine-unity/Assets/Spine/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-unity", "displayName": "spine-unity Runtime", "description": "This plugin provides the spine-unity runtime core.", - "version": "4.2.99", + "version": "4.3.0", "unity": "2018.3", "author": { "name": "Esoteric Software", @@ -14,7 +14,7 @@ "com.unity.modules.animation" : "1.0.0", "com.unity.modules.physics" : "1.0.0", "com.unity.modules.physics2d" : "1.0.0", - "com.esotericsoftware.spine.spine-csharp": "4.2.0" + "com.esotericsoftware.spine.spine-csharp": "4.3.0" }, "repository": { "type": "git",