[starling] Fixed assignment of MeshStyle and colorizing vertices. Closes #879

This commit is contained in:
badlogic 2017-04-21 10:24:18 +02:00
parent 7b40f0c4d0
commit d0a79def31
6 changed files with 286 additions and 843 deletions

View File

@ -1,5 +1,4 @@
eclipse.preferences.version=1
encoding//src/spine/examples/TankExample.as=UTF-8
encoding//src/spine/examples/TwoColorExample.as=UTF-8
encoding//src/spine/starling/SkeletonSprite.as=UTF-8
encoding/<project>=UTF-8

View File

@ -1,269 +0,0 @@
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine {
public class ConvexDecomposer {
private var convexPolygons : Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
private var convexPolygonsIndices : Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
private var indicesArray : Vector.<int> = new Vector.<int>();
private var isConcaveArray : Vector.<Boolean> = new Vector.<Boolean>();
private var triangles : Vector.<int> = new Vector.<int>();
private var polygonPool : Pool = new Pool(function() : Vector.<Number> {
return new Vector.<Number>();
});
private var polygonIndicesPool : Pool = new Pool(function() : Vector.<int> {
return new Vector.<int>();
});
public function ConvexDecomposer () {
}
public function decompose(input : Vector.<Number>) : Vector.<Vector.<Number>> {
var vertices : Vector.<Number> = input;
var vertexCount : int = input.length >> 1;
var i : int, n : int;
var indices : Vector.<int> = this.indicesArray;
indices.length = 0;
for (i = 0; i < vertexCount; i++)
indices[i] = i;
var isConcaveArray : Vector.<Boolean> = this.isConcaveArray;
isConcaveArray.length = 0;
for (i = 0, n = vertexCount; i < n; ++i)
isConcaveArray[i] = isConcave(i, vertexCount, vertices, indices);
var triangles : Vector.<int> = this.triangles;
triangles.length = 0;
while (vertexCount > 3) {
// Find ear tip.
var previous : int = vertexCount - 1, next : int = 1;
i = 0;
while (true) {
outer:
if (!isConcaveArray[i]) {
var p1 : int = indices[previous] << 1, p2 : int = indices[i] << 1, p3 : int = indices[next] << 1;
var p1x : Number = vertices[p1], p1y : Number = vertices[p1 + 1];
var p2x : Number = vertices[p2], p2y : Number = vertices[p2 + 1];
var p3x : Number = vertices[p3], p3y : Number = vertices[p3 + 1];
for (var ii : int = (next + 1) % vertexCount; ii != previous; ii = (ii + 1) % vertexCount) {
if (!isConcaveArray[ii]) continue;
var v : int = indices[ii] << 1;
var vx : int = vertices[v], vy : int = vertices[v + 1];
if (positiveArea(p3x, p3y, p1x, p1y, vx, vy)) {
if (positiveArea(p1x, p1y, p2x, p2y, vx, vy)) {
if (positiveArea(p2x, p2y, p3x, p3y, vx, vy)) break outer;
}
}
}
break;
}
if (next == 0) {
do {
if (!isConcaveArray[i]) break;
i--;
} while (i > 0);
break;
}
previous = i;
i = next;
next = (next + 1) % vertexCount;
}
// Cut ear tip.
triangles.push(indices[(vertexCount + i - 1) % vertexCount]);
triangles.push(indices[i]);
triangles.push(indices[(i + 1) % vertexCount]);
indices.splice(i, 1);
isConcaveArray.splice(i, 1);
vertexCount--;
var previousIndex : int = (vertexCount + i - 1) % vertexCount;
var nextIndex : int = i == vertexCount ? 0 : i;
isConcaveArray[previousIndex] = isConcave(previousIndex, vertexCount, vertices, indices);
isConcaveArray[nextIndex] = isConcave(nextIndex, vertexCount, vertices, indices);
}
if (vertexCount == 3) {
triangles.push(indices[2]);
triangles.push(indices[0]);
triangles.push(indices[1]);
}
var convexPolygons : Vector.<Vector.<Number>> = this.convexPolygons;
for (i = 0, n = convexPolygons.length; i < n; i++) {
this.polygonPool.free(convexPolygons[i]);
}
convexPolygons.length = 0;
var convexPolygonsIndices : Vector.<Vector.<int>> = this.convexPolygonsIndices;
for (i = 0, n = convexPolygonsIndices.length; i < n; i++) {
this.polygonIndicesPool.free(convexPolygonsIndices[i]);
}
convexPolygonsIndices.length = 0;
var polygonIndices : Vector.<int> = Vector.<int>(this.polygonIndicesPool.obtain());
polygonIndices.length = 0;
var polygon : Vector.<Number> = Vector.<Number>(this.polygonPool.obtain());
polygon.length = 0;
// Merge subsequent triangles if they form a triangle fan.
var fanBaseIndex : int = -1, lastWinding : int = 0;
var x1 : Number, y1 : Number, x2 : Number, y2 : Number, x3 : Number, y3 : Number;
var winding1 : int, winding2 : int, o : int;
for (i = 0, n = triangles.length; i < n; i += 3) {
var t1 : int = triangles[i] << 1, t2 : int = triangles[i + 1] << 1, t3 : int = triangles[i + 2] << 1;
x1 = vertices[t1];
y1 = vertices[t1 + 1];
x2 = vertices[t2];
y2 = vertices[t2 + 1];
x3 = vertices[t3];
y3 = vertices[t3 + 1];
// If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan).
var merged : Boolean = false;
if (fanBaseIndex == t1) {
o = polygon.length - 4;
winding1 = ConvexDecomposer.winding(polygon[o], polygon[o + 1], polygon[o + 2], polygon[o + 3], x3, y3);
winding2 = ConvexDecomposer.winding(x3, y3, polygon[0], polygon[1], polygon[2], polygon[3]);
if (winding1 == lastWinding && winding2 == lastWinding) {
polygon.push(x3);
polygon.push(y3);
polygonIndices.push(t3);
merged = true;
}
}
// Otherwise make this triangle the new base.
if (!merged) {
if (polygon.length > 0) {
convexPolygons.push(polygon);
convexPolygonsIndices.push(polygonIndices);
}
polygon = Vector.<Number>(this.polygonPool.obtain());
polygon.length = 0;
polygon.push(x1);
polygon.push(y1);
polygon.push(x2);
polygon.push(y2);
polygon.push(x3);
polygon.push(y3);
polygonIndices = Vector.<int>(this.polygonIndicesPool.obtain());
polygonIndices.length = 0;
polygonIndices.push(t1);
polygonIndices.push(t2);
polygonIndices.push(t3);
lastWinding = ConvexDecomposer.winding(x1, y1, x2, y2, x3, y3);
fanBaseIndex = t1;
}
}
if (polygon.length > 0) {
convexPolygons.push(polygon);
convexPolygonsIndices.push(polygonIndices);
}
// Go through the list of polygons and try to merge the remaining triangles with the found triangle fans.
for (i = 0, n = convexPolygons.length; i < n; i++) {
polygonIndices = convexPolygonsIndices[i];
if (polygonIndices.length == 0) continue;
var firstIndex : int = polygonIndices[0];
var lastIndex : int = polygonIndices[polygonIndices.length - 1];
polygon = convexPolygons[i];
o = polygon.length - 4;
var prevPrevX : Number = polygon[o], prevPrevY : Number = polygon[o + 1];
var prevX : Number = polygon[o + 2], prevY : Number = polygon[o + 3];
var firstX : Number = polygon[0], firstY : Number = polygon[1];
var secondX : Number = polygon[2], secondY : Number = polygon[3];
var currWinding : int = ConvexDecomposer.winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);
for (ii = 0; ii < n; ii++) {
if (ii == i) continue;
var otherIndices : Vector.<int>= convexPolygonsIndices[ii];
if (otherIndices.length != 3) continue;
var otherFirstIndex : int = otherIndices[0];
var otherSecondIndex : int = otherIndices[1];
var otherLastIndex : int = otherIndices[2];
var otherPoly : Vector.<Number>= convexPolygons[ii];
x3 = otherPoly[otherPoly.length - 2];
y3 = otherPoly[otherPoly.length - 1];
if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) continue;
winding1 = ConvexDecomposer.winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3);
winding2 = ConvexDecomposer.winding(x3, y3, firstX, firstY, secondX, secondY);
if (winding1 == currWinding && winding2 == currWinding) {
otherPoly.length = 0;
otherIndices.length = 0;
polygon.push(x3);
polygon.push(y3);
polygonIndices.push(otherLastIndex);
prevPrevX = prevX;
prevPrevY = prevY;
prevX = x3;
prevY = y3;
ii = 0;
}
}
}
// Remove empty polygons that resulted from the merge step above.
for (i = convexPolygons.length - 1; i >= 0; i--) {
polygon = convexPolygons[i];
if (polygon.length == 0) {
convexPolygons.splice(i, 1);
this.polygonPool.free(polygon);
}
}
return convexPolygons;
}
private static function isConcave(index : Number, vertexCount : Number, vertices : Vector.<Number>, indices : Vector.<int>) : Boolean {
var previous : int = indices[(vertexCount + index - 1) % vertexCount] << 1;
var current : int = indices[index] << 1;
var next : int = indices[(index + 1) % vertexCount] << 1;
return !positiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next], vertices[next + 1]);
}
private static function positiveArea(p1x : Number, p1y : Number, p2x : Number, p2y : Number, p3x : Number, p3y : Number) : Boolean {
return p1x * (p3y - p2y) + p2x * (p1y - p3y) + p3x * (p2y - p1y) >= 0;
}
private static function winding(p1x : Number, p1y : Number, p2x : Number, p2y : Number, p3x : Number, p3y : Number) : int {
var px : Number = p2x - p1x, py : Number = p2y - p1y;
return p3x * py - p3y * px + px * p1y - p1x * py >= 0 ? 1 : -1;
}
}
}

View File

@ -76,30 +76,26 @@ package spine.examples {
this.setRequiresRedraw();
addChild(skeleton);
// Starling.juggler.add(skeleton);
Starling.juggler.add(skeleton);
addEventListener(TouchEvent.TOUCH, onClick);
}
private function onClick(event : TouchEvent) : void {
var touch : Touch = event.getTouch(this);
if (touch && touch.phase == TouchPhase.BEGAN) {
skeleton.state.update(gunGrabCount++ % 2 == 1 ? 0 : 0.1);
skeleton.state.apply(skeleton.skeleton);
skeleton.skeleton.updateWorldTransform();
this.setRequiresRedraw();
// if (gunGrabCount < 2) {
// if (gunGrabbed)
// skeleton.skeleton.setToSetupPose();
// else
// skeleton.state.setAnimationByName(1, "gungrab", false);
// gunGrabbed = !gunGrabbed;
// gunGrabCount++;
// } else {
// var parent: DisplayObjectContainer = this.parent;
// this.removeFromParent(true);
// parent.addChild(new TankExample());
// }
if (touch && touch.phase == TouchPhase.BEGAN) {
if (gunGrabCount < 2) {
if (gunGrabbed)
skeleton.skeleton.setToSetupPose();
else
skeleton.state.setAnimationByName(1, "gungrab", false);
gunGrabbed = !gunGrabbed;
gunGrabCount++;
} else {
var parent: DisplayObjectContainer = this.parent;
this.removeFromParent(true);
parent.addChild(new TankExample());
}
}
}
}

View File

@ -1,277 +0,0 @@
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine.starling {
import spine.attachments.ClippingAttachment;
import spine.SkeletonClipping;
import spine.Bone;
import spine.Skeleton;
import spine.SkeletonData;
import spine.Slot;
import spine.atlas.AtlasRegion;
import spine.attachments.Attachment;
import spine.attachments.MeshAttachment;
import spine.attachments.RegionAttachment;
import starling.display.BlendMode;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.rendering.IndexData;
import starling.rendering.Painter;
import starling.rendering.VertexData;
import starling.utils.Color;
import starling.utils.MatrixUtil;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class SkeletonSprite extends DisplayObject {
static private var _tempPoint : Point = new Point();
static private var _tempMatrix : Matrix = new Matrix();
static private var _tempVertices : Vector.<Number> = new Vector.<Number>(8);
static internal var blendModes : Vector.<String> = new <String>[BlendMode.NORMAL, BlendMode.ADD, BlendMode.MULTIPLY, BlendMode.SCREEN];
private var _skeleton : Skeleton;
public var batchable : Boolean = true;
private var _smoothing : String = "bilinear";
private static var _twoColorStyle : TwoColorMeshStyle;
private static var clipper: SkeletonClipping = new SkeletonClipping();
private static var QUAD_INDICES : Vector.<uint> = new <uint>[0, 1, 2, 2, 3, 0];
public function SkeletonSprite(skeletonData : SkeletonData) {
Bone.yDown = true;
_skeleton = new Skeleton(skeletonData);
_skeleton.updateWorldTransform();
if (_twoColorStyle == null) {
_twoColorStyle = new TwoColorMeshStyle();
}
}
override public function render(painter : Painter) : void {
var clipper: SkeletonClipping = SkeletonSprite.clipper;
painter.state.alpha *= skeleton.color.a;
var originalBlendMode : String = painter.state.blendMode;
var r : Number = skeleton.color.r * 255;
var g : Number = skeleton.color.g * 255;
var b : Number = skeleton.color.b * 255;
var drawOrder : Vector.<Slot> = skeleton.drawOrder;
var ii : int, iii : int;
var attachmentColor: spine.Color;
var rgb : uint, a : Number;
var dark : uint;
var mesh : SkeletonMesh;
var verticesLength : int, verticesCount : int, indicesLength : int;
var indexData : IndexData, indices : Vector.<uint>, vertexData : VertexData;
var uvs : Vector.<Number>;
for (var i : int = 0, n : int = drawOrder.length; i < n; ++i) {
var worldVertices : Vector.<Number> = _tempVertices;
var slot : Slot = drawOrder[i];
if (slot.attachment is RegionAttachment) {
var region : RegionAttachment = slot.attachment as RegionAttachment;
verticesLength = 4 * 2;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
mesh = region.rendererObject as SkeletonMesh;
indices = QUAD_INDICES;
if (mesh == null) {
if (region.rendererObject is Image)
region.rendererObject = mesh = new SkeletonMesh(Image(region.rendererObject).texture);
if (region.rendererObject is AtlasRegion)
region.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(region.rendererObject).rendererObject).texture);
mesh.setStyle(_twoColorStyle);
}
indexData = mesh.getIndexData();
attachmentColor = region.color;
uvs = region.uvs;
} else if (slot.attachment is MeshAttachment) {
var meshAttachment : MeshAttachment = MeshAttachment(slot.attachment);
verticesLength = meshAttachment.worldVerticesLength;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
meshAttachment.computeWorldVertices(slot, 0, meshAttachment.worldVerticesLength, worldVertices, 0, 2);
mesh = meshAttachment.rendererObject as SkeletonMesh;
indices = meshAttachment.triangles;
if (mesh == null) {
if (meshAttachment.rendererObject is Image)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(meshAttachment.rendererObject).texture);
if (meshAttachment.rendererObject is AtlasRegion)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(meshAttachment.rendererObject).rendererObject).texture);
mesh.setStyle(_twoColorStyle);
}
indexData = mesh.getIndexData();
attachmentColor = meshAttachment.color;
uvs = meshAttachment.uvs;
} else if (slot.attachment is ClippingAttachment) {
var clip : ClippingAttachment = ClippingAttachment(slot.attachment);
clipper.clipStart(slot, clip);
continue;
} else {
continue;
}
a = slot.color.a * attachmentColor.a;
rgb = Color.rgb(r * slot.color.r * attachmentColor.r, g * slot.color.g * attachmentColor.g, b * slot.color.b * attachmentColor.b);
if (slot.darkColor == null) dark = Color.rgb(0, 0, 0);
else dark = Color.rgb(slot.darkColor.r * 255, slot.darkColor.g * 255, slot.darkColor.b * 255);
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, indices, indices.length, uvs);
verticesCount = clipper.clippedVertices.length >> 1;
worldVertices = clipper.clippedVertices;
uvs = clipper.clippedUvs;
indices = clipper.clippedTriangles;
}
trace ("slot: " + slot.data.name);
trace("vertices: " + worldVertices);
trace("uvs:" + uvs);
trace("indices:" + indices);
trace("rgb: " + rgb);
trace("a: " + a);
trace("dark: " + dark);
if (indices.length > 0) {
// Mesh doesn't retain the style, can't find the reason why
mesh.setStyle(_twoColorStyle);
indicesLength = indices.length;
for (ii = 0; ii < indicesLength; ii++) {
indexData.setIndex(ii, indices[ii]);
}
indexData.numIndices = indicesLength;
indexData.trim();
vertexData = mesh.getVertexData();
vertexData.colorize("color", rgb, a);
vertexData.colorize("color2", dark);
for (ii = 0, iii = 0; ii < verticesCount; ii++, iii += 2) {
mesh.setVertexPosition(ii, worldVertices[iii], worldVertices[iii + 1]);
mesh.setTexCoords(ii, uvs[iii], uvs[iii + 1]);
}
vertexData.numVertices = verticesCount;
painter.state.blendMode = blendModes[slot.data.blendMode.ordinal];
painter.batchMesh(mesh);
}
clipper.clipEndWithSlot(slot);
}
painter.state.blendMode = originalBlendMode;
clipper.clipEnd();
trace("==============");
}
override public function hitTest(localPoint : Point) : DisplayObject {
var minX : Number = Number.MAX_VALUE, minY : Number = Number.MAX_VALUE;
var maxX : Number = -Number.MAX_VALUE, maxY : Number = -Number.MAX_VALUE;
var slots : Vector.<Slot> = skeleton.slots;
var worldVertices : Vector.<Number> = _tempVertices;
var empty : Boolean = true;
for (var i : int = 0, n : int = slots.length; i < n; ++i) {
var slot : Slot = slots[i];
var attachment : Attachment = slot.attachment;
if (!attachment) continue;
var verticesLength : int;
if (attachment is RegionAttachment) {
var region : RegionAttachment = RegionAttachment(slot.attachment);
verticesLength = 8;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
} else if (attachment is MeshAttachment) {
var mesh : MeshAttachment = MeshAttachment(attachment);
verticesLength = mesh.worldVerticesLength;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
mesh.computeWorldVertices(slot, 0, verticesLength, worldVertices, 0, 2);
} else
continue;
if (verticesLength != 0)
empty = false;
for (var ii : int = 0; ii < verticesLength; ii += 2) {
var x : Number = worldVertices[ii], y : Number = worldVertices[ii + 1];
minX = minX < x ? minX : x;
minY = minY < y ? minY : y;
maxX = maxX > x ? maxX : x;
maxY = maxY > y ? maxY : y;
}
}
if (empty)
return null;
var temp : Number;
if (maxX < minX) {
temp = maxX;
maxX = minX;
minX = temp;
}
if (maxY < minY) {
temp = maxY;
maxY = minY;
minY = temp;
}
if (localPoint.x >= minX && localPoint.x < maxX && localPoint.y >= minY && localPoint.y < maxY)
return this;
return null;
}
override public function getBounds(targetSpace : DisplayObject, resultRect : Rectangle = null) : Rectangle {
if (!resultRect)
resultRect = new Rectangle();
if (targetSpace == this)
resultRect.setTo(0, 0, 0, 0);
else if (targetSpace == parent)
resultRect.setTo(x, y, 0, 0);
else {
getTransformationMatrix(targetSpace, _tempMatrix);
MatrixUtil.transformCoords(_tempMatrix, 0, 0, _tempPoint);
resultRect.setTo(_tempPoint.x, _tempPoint.y, 0, 0);
}
return resultRect;
}
public function get skeleton() : Skeleton {
return _skeleton;
}
public function get smoothing() : String {
return _smoothing;
}
public function set smoothing(smoothing : String) : void {
_smoothing = smoothing;
}
}
}

View File

@ -1,279 +1,273 @@
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine.starling {
import spine.attachments.ClippingAttachment;
import spine.SkeletonClipping;
import spine.Bone;
import spine.Skeleton;
import spine.SkeletonData;
import spine.Slot;
import spine.atlas.AtlasRegion;
import spine.attachments.Attachment;
import spine.attachments.MeshAttachment;
import spine.attachments.RegionAttachment;
import starling.display.BlendMode;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.rendering.IndexData;
import starling.rendering.Painter;
import starling.rendering.VertexData;
import starling.utils.Color;
import starling.utils.MatrixUtil;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class SkeletonSprite extends DisplayObject {
static private var _tempPoint : Point = new Point();
static private var _tempMatrix : Matrix = new Matrix();
static private var _tempVertices : Vector.<Number> = new Vector.<Number>(8);
static internal var blendModes : Vector.<String> = new <String>[BlendMode.NORMAL, BlendMode.ADD, BlendMode.MULTIPLY, BlendMode.SCREEN];
private var _skeleton : Skeleton;
public var batchable : Boolean = true;
private var _smoothing : String = "bilinear";
private static var _twoColorStyle : TwoColorMeshStyle;
private static var clipper: SkeletonClipping = new SkeletonClipping();
private static var QUAD_INDICES : Vector.<uint> = new <uint>[0, 1, 2, 2, 3, 0];
public function SkeletonSprite(skeletonData : SkeletonData) {
Bone.yDown = true;
_skeleton = new Skeleton(skeletonData);
_skeleton.updateWorldTransform();
if (_twoColorStyle == null) {
_twoColorStyle = new TwoColorMeshStyle();
}
}
override public function render(painter : Painter) : void {
var clipper: SkeletonClipping = SkeletonSprite.clipper;
painter.state.alpha *= skeleton.color.a;
var originalBlendMode : String = painter.state.blendMode;
var r : Number = skeleton.color.r * 255;
var g : Number = skeleton.color.g * 255;
var b : Number = skeleton.color.b * 255;
var drawOrder : Vector.<Slot> = skeleton.drawOrder;
var ii : int, iii : int;
var attachmentColor: spine.Color;
var rgb : uint, a : Number;
var dark : uint;
var mesh : SkeletonMesh;
var verticesLength : int, verticesCount : int, indicesLength : int;
var indexData : IndexData, indices : Vector.<uint>, vertexData : VertexData;
var uvs : Vector.<Number>;
for (var i : int = 0, n : int = drawOrder.length; i < n; ++i) {
var worldVertices : Vector.<Number> = _tempVertices;
var slot : Slot = drawOrder[i];
if (slot.attachment is RegionAttachment) {
var region : RegionAttachment = slot.attachment as RegionAttachment;
verticesLength = 4 * 2;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
mesh = region.rendererObject as SkeletonMesh;
indices = QUAD_INDICES;
if (mesh == null) {
if (region.rendererObject is Image)
region.rendererObject = mesh = new SkeletonMesh(Image(region.rendererObject).texture);
if (region.rendererObject is AtlasRegion)
region.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(region.rendererObject).rendererObject).texture);
indexData = mesh.getIndexData();
for (ii = 0; ii < indices.length; ii++)
indexData.setIndex(ii, indices[ii]);
indexData.numIndices = indices.length;
indexData.trim();
}
indexData = mesh.getIndexData();
attachmentColor = region.color;
uvs = region.uvs;
} else if (slot.attachment is MeshAttachment) {
var meshAttachment : MeshAttachment = MeshAttachment(slot.attachment);
verticesLength = meshAttachment.worldVerticesLength;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
meshAttachment.computeWorldVertices(slot, 0, meshAttachment.worldVerticesLength, worldVertices, 0, 2);
mesh = meshAttachment.rendererObject as SkeletonMesh;
indices = meshAttachment.triangles;
if (mesh == null) {
if (meshAttachment.rendererObject is Image)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(meshAttachment.rendererObject).texture);
if (meshAttachment.rendererObject is AtlasRegion)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(meshAttachment.rendererObject).rendererObject).texture);
mesh.setStyle(_twoColorStyle);
indexData = mesh.getIndexData();
indicesLength = meshAttachment.triangles.length;
for (ii = 0; ii < indicesLength; ii++) {
indexData.setIndex(ii, indices[ii]);
}
indexData.numIndices = indicesLength;
indexData.trim();
}
indexData = mesh.getIndexData();
attachmentColor = meshAttachment.color;
uvs = meshAttachment.uvs;
} else if (slot.attachment is ClippingAttachment) {
var clip : ClippingAttachment = ClippingAttachment(slot.attachment);
clipper.clipStart(slot, clip);
continue;
} else {
continue;
}
a = slot.color.a * attachmentColor.a;
rgb = Color.rgb(r * slot.color.r * attachmentColor.r, g * slot.color.g * attachmentColor.g, b * slot.color.b * attachmentColor.b);
if (slot.darkColor == null) dark = Color.rgb(0, 0, 0);
else dark = Color.rgb(slot.darkColor.r * 255, slot.darkColor.g * 255, slot.darkColor.b * 255);
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, indices, indices.length, uvs);
verticesCount = clipper.clippedVertices.length >> 1;
worldVertices = clipper.clippedVertices;
uvs = clipper.clippedUvs;
indices = clipper.clippedTriangles;
indicesLength = indices.length;
for (ii = 0; ii < indicesLength; ii++) {
indexData.setIndex(ii, indices[ii]);
}
indexData.numIndices = indicesLength;
indexData.trim();
}
// Mesh doesn't retain the style, can't find the reason why
mesh.setStyle(_twoColorStyle);
vertexData = mesh.getVertexData();
vertexData.colorize("color", rgb, a);
vertexData.colorize("color2", dark);
for (ii = 0, iii = 0; ii < verticesCount; ii++, iii += 2) {
mesh.setVertexPosition(ii, worldVertices[iii], worldVertices[iii + 1]);
mesh.setTexCoords(ii, uvs[iii], uvs[iii + 1]);
}
vertexData.numVertices = verticesCount;
painter.state.blendMode = blendModes[slot.data.blendMode.ordinal];
painter.batchMesh(mesh);
clipper.clipEndWithSlot(slot);
}
painter.state.blendMode = originalBlendMode;
clipper.clipEnd();
}
override public function hitTest(localPoint : Point) : DisplayObject {
var minX : Number = Number.MAX_VALUE, minY : Number = Number.MAX_VALUE;
var maxX : Number = -Number.MAX_VALUE, maxY : Number = -Number.MAX_VALUE;
var slots : Vector.<Slot> = skeleton.slots;
var worldVertices : Vector.<Number> = _tempVertices;
var empty : Boolean = true;
for (var i : int = 0, n : int = slots.length; i < n; ++i) {
var slot : Slot = slots[i];
var attachment : Attachment = slot.attachment;
if (!attachment) continue;
var verticesLength : int;
if (attachment is RegionAttachment) {
var region : RegionAttachment = RegionAttachment(slot.attachment);
verticesLength = 8;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
} else if (attachment is MeshAttachment) {
var mesh : MeshAttachment = MeshAttachment(attachment);
verticesLength = mesh.worldVerticesLength;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
mesh.computeWorldVertices(slot, 0, verticesLength, worldVertices, 0, 2);
} else
continue;
if (verticesLength != 0)
empty = false;
for (var ii : int = 0; ii < verticesLength; ii += 2) {
var x : Number = worldVertices[ii], y : Number = worldVertices[ii + 1];
minX = minX < x ? minX : x;
minY = minY < y ? minY : y;
maxX = maxX > x ? maxX : x;
maxY = maxY > y ? maxY : y;
}
}
if (empty)
return null;
var temp : Number;
if (maxX < minX) {
temp = maxX;
maxX = minX;
minX = temp;
}
if (maxY < minY) {
temp = maxY;
maxY = minY;
minY = temp;
}
if (localPoint.x >= minX && localPoint.x < maxX && localPoint.y >= minY && localPoint.y < maxY)
return this;
return null;
}
override public function getBounds(targetSpace : DisplayObject, resultRect : Rectangle = null) : Rectangle {
if (!resultRect)
resultRect = new Rectangle();
if (targetSpace == this)
resultRect.setTo(0, 0, 0, 0);
else if (targetSpace == parent)
resultRect.setTo(x, y, 0, 0);
else {
getTransformationMatrix(targetSpace, _tempMatrix);
MatrixUtil.transformCoords(_tempMatrix, 0, 0, _tempPoint);
resultRect.setTo(_tempPoint.x, _tempPoint.y, 0, 0);
}
return resultRect;
}
public function get skeleton() : Skeleton {
return _skeleton;
}
public function get smoothing() : String {
return _smoothing;
}
public function set smoothing(smoothing : String) : void {
_smoothing = smoothing;
}
}
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine.starling {
import spine.attachments.ClippingAttachment;
import spine.SkeletonClipping;
import spine.Bone;
import spine.Skeleton;
import spine.SkeletonData;
import spine.Slot;
import spine.atlas.AtlasRegion;
import spine.attachments.Attachment;
import spine.attachments.MeshAttachment;
import spine.attachments.RegionAttachment;
import starling.display.BlendMode;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.rendering.IndexData;
import starling.rendering.Painter;
import starling.rendering.VertexData;
import starling.utils.Color;
import starling.utils.MatrixUtil;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class SkeletonSprite extends DisplayObject {
static private var _tempPoint : Point = new Point();
static private var _tempMatrix : Matrix = new Matrix();
static private var _tempVertices : Vector.<Number> = new Vector.<Number>(8);
static internal var blendModes : Vector.<String> = new <String>[BlendMode.NORMAL, BlendMode.ADD, BlendMode.MULTIPLY, BlendMode.SCREEN];
private var _skeleton : Skeleton;
public var batchable : Boolean = true;
private var _smoothing : String = "bilinear";
private static var clipper: SkeletonClipping = new SkeletonClipping();
private static var QUAD_INDICES : Vector.<uint> = new <uint>[0, 1, 2, 2, 3, 0];
public function SkeletonSprite(skeletonData : SkeletonData) {
Bone.yDown = true;
_skeleton = new Skeleton(skeletonData);
_skeleton.updateWorldTransform();
}
override public function render(painter : Painter) : void {
var clipper: SkeletonClipping = SkeletonSprite.clipper;
painter.state.alpha *= skeleton.color.a;
var originalBlendMode : String = painter.state.blendMode;
var r : Number = skeleton.color.r * 255;
var g : Number = skeleton.color.g * 255;
var b : Number = skeleton.color.b * 255;
var drawOrder : Vector.<Slot> = skeleton.drawOrder;
var ii : int, iii : int;
var attachmentColor: spine.Color;
var rgb : uint, a : Number;
var dark : uint;
var mesh : SkeletonMesh;
var verticesLength : int, verticesCount : int, indicesLength : int;
var indexData : IndexData, indices : Vector.<uint>, vertexData : VertexData;
var uvs : Vector.<Number>;
for (var i : int = 0, n : int = drawOrder.length; i < n; ++i) {
var worldVertices : Vector.<Number> = _tempVertices;
var slot : Slot = drawOrder[i];
if (slot.attachment is RegionAttachment) {
var region : RegionAttachment = slot.attachment as RegionAttachment;
verticesLength = 4 * 2;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
mesh = region.rendererObject as SkeletonMesh;
indices = QUAD_INDICES;
if (mesh == null) {
if (region.rendererObject is Image)
region.rendererObject = mesh = new SkeletonMesh(Image(region.rendererObject).texture);
if (region.rendererObject is AtlasRegion)
region.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(region.rendererObject).rendererObject).texture);
mesh.setStyle(new TwoColorMeshStyle());
indexData = mesh.getIndexData();
for (ii = 0; ii < indices.length; ii++)
indexData.setIndex(ii, indices[ii]);
indexData.numIndices = indices.length;
indexData.trim();
}
indexData = mesh.getIndexData();
attachmentColor = region.color;
uvs = region.uvs;
} else if (slot.attachment is MeshAttachment) {
var meshAttachment : MeshAttachment = MeshAttachment(slot.attachment);
verticesLength = meshAttachment.worldVerticesLength;
verticesCount = verticesLength >> 1;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
meshAttachment.computeWorldVertices(slot, 0, meshAttachment.worldVerticesLength, worldVertices, 0, 2);
mesh = meshAttachment.rendererObject as SkeletonMesh;
indices = meshAttachment.triangles;
if (mesh == null) {
if (meshAttachment.rendererObject is Image)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(meshAttachment.rendererObject).texture);
if (meshAttachment.rendererObject is AtlasRegion)
meshAttachment.rendererObject = mesh = new SkeletonMesh(Image(AtlasRegion(meshAttachment.rendererObject).rendererObject).texture);
mesh.setStyle(new TwoColorMeshStyle());
indexData = mesh.getIndexData();
indicesLength = meshAttachment.triangles.length;
for (ii = 0; ii < indicesLength; ii++) {
indexData.setIndex(ii, indices[ii]);
}
indexData.numIndices = indicesLength;
indexData.trim();
}
indexData = mesh.getIndexData();
attachmentColor = meshAttachment.color;
uvs = meshAttachment.uvs;
} else if (slot.attachment is ClippingAttachment) {
var clip : ClippingAttachment = ClippingAttachment(slot.attachment);
clipper.clipStart(slot, clip);
continue;
} else {
continue;
}
a = slot.color.a * attachmentColor.a;
rgb = Color.rgb(r * slot.color.r * attachmentColor.r, g * slot.color.g * attachmentColor.g, b * slot.color.b * attachmentColor.b);
if (slot.darkColor == null) dark = Color.rgb(0, 0, 0);
else dark = Color.rgb(slot.darkColor.r * 255, slot.darkColor.g * 255, slot.darkColor.b * 255);
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, indices, indices.length, uvs);
verticesCount = clipper.clippedVertices.length >> 1;
worldVertices = clipper.clippedVertices;
uvs = clipper.clippedUvs;
indices = clipper.clippedTriangles;
indicesLength = indices.length;
for (ii = 0; ii < indicesLength; ii++) {
indexData.setIndex(ii, indices[ii]);
}
indexData.numIndices = indicesLength;
indexData.trim();
}
vertexData = mesh.getVertexData();
vertexData.numVertices = verticesCount;
vertexData.colorize("color", rgb, a);
vertexData.colorize("color2", dark);
for (ii = 0, iii = 0; ii < verticesCount; ii++, iii += 2) {
mesh.setVertexPosition(ii, worldVertices[iii], worldVertices[iii + 1]);
mesh.setTexCoords(ii, uvs[iii], uvs[iii + 1]);
}
painter.state.blendMode = blendModes[slot.data.blendMode.ordinal];
painter.batchMesh(mesh);
clipper.clipEndWithSlot(slot);
}
painter.state.blendMode = originalBlendMode;
clipper.clipEnd();
}
override public function hitTest(localPoint : Point) : DisplayObject {
var minX : Number = Number.MAX_VALUE, minY : Number = Number.MAX_VALUE;
var maxX : Number = -Number.MAX_VALUE, maxY : Number = -Number.MAX_VALUE;
var slots : Vector.<Slot> = skeleton.slots;
var worldVertices : Vector.<Number> = _tempVertices;
var empty : Boolean = true;
for (var i : int = 0, n : int = slots.length; i < n; ++i) {
var slot : Slot = slots[i];
var attachment : Attachment = slot.attachment;
if (!attachment) continue;
var verticesLength : int;
if (attachment is RegionAttachment) {
var region : RegionAttachment = RegionAttachment(slot.attachment);
verticesLength = 8;
region.computeWorldVertices(slot.bone, worldVertices, 0, 2);
} else if (attachment is MeshAttachment) {
var mesh : MeshAttachment = MeshAttachment(attachment);
verticesLength = mesh.worldVerticesLength;
if (worldVertices.length < verticesLength) worldVertices.length = verticesLength;
mesh.computeWorldVertices(slot, 0, verticesLength, worldVertices, 0, 2);
} else
continue;
if (verticesLength != 0)
empty = false;
for (var ii : int = 0; ii < verticesLength; ii += 2) {
var x : Number = worldVertices[ii], y : Number = worldVertices[ii + 1];
minX = minX < x ? minX : x;
minY = minY < y ? minY : y;
maxX = maxX > x ? maxX : x;
maxY = maxY > y ? maxY : y;
}
}
if (empty)
return null;
var temp : Number;
if (maxX < minX) {
temp = maxX;
maxX = minX;
minX = temp;
}
if (maxY < minY) {
temp = maxY;
maxY = minY;
minY = temp;
}
if (localPoint.x >= minX && localPoint.x < maxX && localPoint.y >= minY && localPoint.y < maxY)
return this;
return null;
}
override public function getBounds(targetSpace : DisplayObject, resultRect : Rectangle = null) : Rectangle {
if (!resultRect)
resultRect = new Rectangle();
if (targetSpace == this)
resultRect.setTo(0, 0, 0, 0);
else if (targetSpace == parent)
resultRect.setTo(x, y, 0, 0);
else {
getTransformationMatrix(targetSpace, _tempMatrix);
MatrixUtil.transformCoords(_tempMatrix, 0, 0, _tempPoint);
resultRect.setTo(_tempPoint.x, _tempPoint.y, 0, 0);
}
return resultRect;
}
public function get skeleton() : Skeleton {
return _skeleton;
}
public function get smoothing() : String {
return _smoothing;
}
public function set smoothing(smoothing : String) : void {
_smoothing = smoothing;
}
}
}