From b06a485ad87e7015f6656c1ac65116d47189a866 Mon Sep 17 00:00:00 2001 From: Davide Tantillo Date: Fri, 6 Jun 2025 14:34:00 +0200 Subject: [PATCH] [haxe] Port to 4.3 - fix clipping. --- .../src/starlingExamples/VineExample.hx | 7 ++- .../spine-haxe/spine/SkeletonClipping.hx | 44 +++++++++---------- .../spine-haxe/spine/flixel/SkeletonSprite.hx | 23 +++++----- .../spine/starling/SkeletonSprite.hx | 17 ++++--- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/spine-haxe/example/src/starlingExamples/VineExample.hx b/spine-haxe/example/src/starlingExamples/VineExample.hx index d19bb9e07..5178f69cb 100644 --- a/spine-haxe/example/src/starlingExamples/VineExample.hx +++ b/spine-haxe/example/src/starlingExamples/VineExample.hx @@ -54,10 +54,12 @@ class VineExample extends Scene { var skeletonSprite = new SkeletonSprite(skeletondata, animationStateData); skeletonSprite.skeleton.updateWorldTransform(Physics.none); var bounds = skeletonSprite.skeleton.getBounds(); - skeletonSprite.scale = Starling.current.stage.stageWidth / bounds.width; + skeletonSprite.scale = Starling.current.stage.stageWidth / bounds.width / 10; skeletonSprite.x = Starling.current.stage.stageWidth / 2; skeletonSprite.y = Starling.current.stage.stageHeight * 0.5; + skeletonSprite.state.setAnimationByName(0, "grow", true); + addChild(skeletonSprite); juggler.add(skeletonSprite); @@ -67,7 +69,8 @@ class VineExample extends Scene { public function onTouch(e:TouchEvent) { var touch = e.getTouch(this); if (touch != null && touch.phase == TouchPhase.ENDED) { - SceneManager.getInstance().switchScene(new SackExample()); + SceneManager.getInstance().switchScene(new CelestialCircusExample()); + // SceneManager.getInstance().switchScene(new SackExample()); } } } diff --git a/spine-haxe/spine-haxe/spine/SkeletonClipping.hx b/spine-haxe/spine-haxe/spine/SkeletonClipping.hx index cf86599c3..1ec8b2733 100644 --- a/spine-haxe/spine-haxe/spine/SkeletonClipping.hx +++ b/spine-haxe/spine-haxe/spine/SkeletonClipping.hx @@ -157,7 +157,7 @@ class SkeletonClipping { return clipOutputItems != null; } - public function clipTriangles(vertices:Array, triangles:Array, trianglesLength:Float, uvs:Array = null, stride:Int = 0):Bool { + public function clipTriangles(vertices:Array, triangles:Array, trianglesLength:Float, uvs:Array = null):Bool { if (uvs == null) { return clipTrianglesNoRender(vertices, triangles, trianglesLength); } @@ -170,17 +170,17 @@ class SkeletonClipping { var i:Int = 0; var clipOutputItems:Array = null; while (i < trianglesLength) { - var t:Int = triangles[i]; - var u1:Float = uvs[t << 1], v1:Float = uvs[(t << 1) + 1]; - var x1:Float = vertices[t * stride], y1:Float = vertices[t * stride + 1]; + var vertexOffset:Int = triangles[i] << 1; + var x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1]; + var u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1]; - t = triangles[i + 1]; - var u2:Float = uvs[t << 1], v2:Float = uvs[(t << 1) + 1]; - var x2:Float = vertices[t * stride], y2:Float = vertices[t * stride + 1]; + vertexOffset = triangles[i + 1] << 1; + var x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1]; + var u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1]; - t = triangles[i + 2]; - var u3:Float = uvs[t << 1], v3:Float = uvs[(t << 1) + 1]; - var x3:Float = vertices[t * stride], y3:Float = vertices[t * stride + 1]; + vertexOffset = triangles[i + 2] << 1; + var x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1]; + var u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1]; for (p in 0...polygonsCount) { var s:Int = clippedVertices.length; @@ -192,28 +192,24 @@ class SkeletonClipping { var clipOutputLength:Int = clipOutput.length; if (clipOutputLength == 0) continue; - var d0:Float = y2 - y3, - d1:Float = x3 - x2, - d2:Float = x1 - x3, - d4:Float = y3 - y1; - var d:Float = 1 / (d0 * d2 + d1 * (y1 - y3)); + var d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1; + var d = 1 / (d0 * d2 + d1 * (y1 - y3)); var clipOutputCount:Int = clipOutputLength >> 1; clippedVerticesItems = clippedVertices; - clippedVerticesItems.resize(s + clipOutputLength * stride); + clippedVerticesItems.resize(s + clipOutputLength); clippedUvsItems = clippedUvs; clippedUvsItems.resize(s + clipOutputLength); var ii:Int = 0; while (ii < clipOutputLength) { - var x:Float = clipOutputItems[ii], - y:Float = clipOutputItems[ii + 1]; + var x = clipOutputItems[ii], y = clipOutputItems[ii + 1]; clippedVerticesItems[s] = x; clippedVerticesItems[s + 1] = y; - var c0:Float = x - x3, c1:Float = y - y3; - var a:Float = (d0 * c0 + d1 * c1) * d; - var b:Float = (d4 * c0 + d2 * c1) * d; - var c:Float = 1 - a - b; + var c0 = x - x3, c1 = y - y3; + var a = (d0 * c0 + d1 * c1) * d; + var b = (d4 * c0 + d2 * c1) * d; + var c = 1 - a - b; clippedUvsItems[s] = u1 * a + u2 * b + u3 * c; clippedUvsItems[s + 1] = v1 * a + v2 * b + v3 * c; s += 2; @@ -234,7 +230,7 @@ class SkeletonClipping { index += clipOutputCount + 1; } else { clippedVerticesItems = clippedVertices; - clippedVerticesItems.resize(s + 3 * stride); + clippedVerticesItems.resize(s + 3 * 2); clippedVerticesItems[s] = x1; clippedVerticesItems[s + 1] = y1; clippedVerticesItems[s + 2] = x2; @@ -243,7 +239,7 @@ class SkeletonClipping { clippedVerticesItems[s + 5] = y3; clippedUvsItems = clippedUvs; - clippedUvsItems.resize(s + 3 * 2); + clippedUvsItems.resize(s + 3); clippedUvsItems[s] = u1; clippedUvsItems[s + 1] = v1; clippedUvsItems[s + 2] = u2; diff --git a/spine-haxe/spine-haxe/spine/flixel/SkeletonSprite.hx b/spine-haxe/spine-haxe/spine/flixel/SkeletonSprite.hx index 286804530..3f141255c 100644 --- a/spine-haxe/spine-haxe/spine/flixel/SkeletonSprite.hx +++ b/spine-haxe/spine-haxe/spine/flixel/SkeletonSprite.hx @@ -199,7 +199,9 @@ class SkeletonSprite extends FlxObject var vertexSize:Int = twoColorTint ? 12 : 8; _tempMatrix = getTransformMatrix(); for (slot in drawOrder) { - var clippedVertexSize:Int = clipper.isClipping() ? 2 : vertexSize; + // no two tint color support and tint is passed as parameter to mesh, so vertex size is 2 + // var clippedVertexSize = clipper.isClipping() ? 2 : vertexSize; + var clippedVertexSize = 2; if (!slot.bone.active) { clipper.clipEnd(slot); continue; @@ -212,9 +214,8 @@ class SkeletonSprite extends FlxObject var region:RegionAttachment = cast(attachment, RegionAttachment); numVertices = 4; numFloats = clippedVertexSize << 2; - if (numFloats > worldVertices.length) { + if (numFloats > worldVertices.length) worldVertices.resize(numFloats); - } region.computeWorldVertices(slot, worldVertices, 0, clippedVertexSize); mesh = getFlixelMeshFromRendererAttachment(region); @@ -238,6 +239,7 @@ class SkeletonSprite extends FlxObject attachmentColor = meshAttachment.color; } else if (Std.isOfType(attachment, ClippingAttachment)) { var clip:ClippingAttachment = cast(attachment, ClippingAttachment); + clipper.clipEnd(slot); clipper.clipStart(skeleton, slot, clip); continue; } else { @@ -256,12 +258,9 @@ class SkeletonSprite extends FlxObject ); mesh.alpha = skeleton.color.a * pose.color.a * attachmentColor.a * alpha; - if (clipper.isClipping()) { - clipper.clipTriangles(worldVertices, triangles, triangles.length, uvs); - + if (clipper.isClipping() && clipper.clipTriangles(worldVertices, triangles, triangles.length, uvs)) { mesh.indices = Vector.ofArray(clipper.clippedTriangles); mesh.uvtData = Vector.ofArray(clipper.clippedUvs); - if (angle == 0) { mesh.vertices = Vector.ofArray(clipper.clippedVertices); mesh.x = x + offsetX; @@ -278,21 +277,19 @@ class SkeletonSprite extends FlxObject } } } else { - var v = 0; var n = numFloats; var i = 0; mesh.vertices.length = numVertices; - while (v < n) { + while (i < n) { if (angle == 0) { - mesh.vertices[i] = worldVertices[v]; - mesh.vertices[i + 1] = worldVertices[v + 1]; + mesh.vertices[i] = worldVertices[i]; + mesh.vertices[i + 1] = worldVertices[i + 1]; } else { - _tempPoint.setTo(worldVertices[v], worldVertices[v + 1]); + _tempPoint.setTo(worldVertices[i], worldVertices[i + 1]); _tempPoint = _tempMatrix.transformPoint(_tempPoint); mesh.vertices[i] = _tempPoint.x; mesh.vertices[i + 1] = _tempPoint.y; } - v += 8; i += 2; } if (angle == 0) { diff --git a/spine-haxe/spine-haxe/spine/starling/SkeletonSprite.hx b/spine-haxe/spine-haxe/spine/starling/SkeletonSprite.hx index cb5a8c660..3a155b982 100644 --- a/spine-haxe/spine-haxe/spine/starling/SkeletonSprite.hx +++ b/spine-haxe/spine-haxe/spine/starling/SkeletonSprite.hx @@ -118,8 +118,8 @@ class SkeletonSprite extends DisplayObject implements IAnimatable { var worldVertices:Array = _tempVertices; var pose = slot.pose; var attachment = pose.attachment; - if (Std.isOfType(pose, RegionAttachment)) { - var region:RegionAttachment = cast(pose, RegionAttachment); + if (Std.isOfType(attachment, RegionAttachment)) { + var region:RegionAttachment = cast(attachment, RegionAttachment); verticesLength = 8; verticesCount = verticesLength >> 1; if (worldVertices.length < verticesLength) @@ -146,8 +146,8 @@ class SkeletonSprite extends DisplayObject implements IAnimatable { indexData = mesh.getIndexData(); attachmentColor = region.color; uvs = region.uvs; - } else if (Std.isOfType(pose, MeshAttachment)) { - var meshAttachment:MeshAttachment = cast(pose, MeshAttachment); + } else if (Std.isOfType(attachment, MeshAttachment)) { + var meshAttachment:MeshAttachment = cast(attachment, MeshAttachment); verticesLength = meshAttachment.worldVerticesLength; verticesCount = verticesLength >> 1; if (worldVertices.length < verticesLength) @@ -175,8 +175,9 @@ class SkeletonSprite extends DisplayObject implements IAnimatable { indexData = mesh.getIndexData(); attachmentColor = meshAttachment.color; uvs = meshAttachment.uvs; - } else if (Std.isOfType(pose, ClippingAttachment)) { - var clip:ClippingAttachment = cast(pose, ClippingAttachment); + } else if (Std.isOfType(attachment, ClippingAttachment)) { + var clip:ClippingAttachment = cast(attachment, ClippingAttachment); + clipper.clipEnd(slot); clipper.clipStart(skeleton, slot, clip); continue; } else { @@ -197,9 +198,7 @@ class SkeletonSprite extends DisplayObject implements IAnimatable { dark = Color.rgb(Std.int(pose.darkColor.r * 255), Std.int(pose.darkColor.g * 255), Std.int(pose.darkColor.b * 255)); } - if (clipper.isClipping()) { - clipper.clipTriangles(worldVertices, indices, indices.length, uvs); - + if (clipper.isClipping() && clipper.clipTriangles(worldVertices, indices, indices.length, uvs)) { // Need to create a new mesh here, see https://github.com/EsotericSoftware/spine-runtimes/issues/1125 mesh = new SkeletonMesh(mesh.texture); indexData = mesh.getIndexData();