Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Andrey Viktorov 2013-06-13 20:53:30 +07:00
commit 54d03443cd
5 changed files with 41 additions and 32 deletions

View File

@ -413,9 +413,16 @@ function Animation.AttachmentTimeline.new ()
end end
local attachmentName = self.attachmentNames[frameIndex] local attachmentName = self.attachmentNames[frameIndex]
local attachment local slot = skeleton.slotsByName[self.slotName]
if attachmentName then attachment = skeleton:getAttachment(self.slotName, attachmentName) end if attachmentName then
skeleton:findSlot(self.slotName):setAttachment(attachment) if not slot.attachment then
slot:setAttachment(skeleton:getAttachment(self.slotName, attachmentName))
elseif slot.attachment.name ~= attachmentName then
slot:setAttachment(skeleton:getAttachment(self.slotName, attachmentName))
end
else
slot:setAttachment(nil)
end
end end
return self return self

View File

@ -35,6 +35,7 @@ function Skeleton.new (skeletonData)
data = skeletonData, data = skeletonData,
bones = {}, bones = {},
slots = {}, slots = {},
slotsByName = {},
drawOrder = {} drawOrder = {}
} }
@ -73,13 +74,10 @@ function Skeleton.new (skeletonData)
return nil return nil
end end
function self:findSlot (slotName) function self:findSlot (slotName)
if not slotName then error("slotName cannot be nil.", 2) end if not slotName then error("slotName cannot be nil.", 2) end
for i,slot in ipairs(self.slots) do return slotsByName[slotName]
if slot.data.name == slotName then return slot end end
end
return nil
end
function self:setSkin (skinName) function self:setSkin (skinName)
local newSkin local newSkin
@ -106,7 +104,7 @@ function Skeleton.new (skeletonData)
function self:getAttachment (slotName, attachmentName) function self:getAttachment (slotName, attachmentName)
if not slotName then error("slotName cannot be nil.", 2) end if not slotName then error("slotName cannot be nil.", 2) end
if not attachmentName then error("attachmentName cannot be nil.", 2) end if not attachmentName then error("attachmentName cannot be nil.", 2) end
local slotIndex = self.data:findSlotIndex(slotName) local slotIndex = skeletonData.slotNameIndices[slotName]
if slotIndex == -1 then error("Slot not found: " .. slotName, 2) end if slotIndex == -1 then error("Slot not found: " .. slotName, 2) end
if self.skin then if self.skin then
local attachment = self.skin:getAttachment(slotIndex, attachmentName) local attachment = self.skin:getAttachment(slotIndex, attachmentName)
@ -147,6 +145,7 @@ function Skeleton.new (skeletonData)
local bone = self.bones[spine.utils.indexOf(skeletonData.bones, slotData.boneData)] local bone = self.bones[spine.utils.indexOf(skeletonData.bones, slotData.boneData)]
local slot = Slot.new(slotData, self, bone) local slot = Slot.new(slotData, self, bone)
table.insert(self.slots, slot) table.insert(self.slots, slot)
self.slotsByName[slot.data.name] = slot
table.insert(self.drawOrder, slot) table.insert(self.drawOrder, slot)
end end

View File

@ -28,6 +28,7 @@ function SkeletonData.new ()
local self = { local self = {
bones = {}, bones = {},
slots = {}, slots = {},
slotNameIndices = {},
skins = {}, skins = {},
animations = {} animations = {}
} }
@ -58,10 +59,7 @@ function SkeletonData.new ()
function self:findSlotIndex (slotName) function self:findSlotIndex (slotName)
if not slotName then error("slotName cannot be nil.", 2) end if not slotName then error("slotName cannot be nil.", 2) end
for i,slot in ipairs(self.slots) do return slotNameIndices[slotName] or -1
if slot.name == slotName then return i end
end
return -1
end end
function self:findSkin (skinName) function self:findSkin (skinName)

View File

@ -29,7 +29,6 @@ local SlotData = require "spine-lua.SlotData"
local Skin = require "spine-lua.Skin" local Skin = require "spine-lua.Skin"
local AttachmentLoader = require "spine-lua.AttachmentLoader" local AttachmentLoader = require "spine-lua.AttachmentLoader"
local Animation = require "spine-lua.Animation" local Animation = require "spine-lua.Animation"
local TIMELINE_SCALE = "scale" local TIMELINE_SCALE = "scale"
local TIMELINE_ROTATE = "rotate" local TIMELINE_ROTATE = "rotate"
local TIMELINE_TRANSLATE = "translate" local TIMELINE_TRANSLATE = "translate"
@ -102,8 +101,9 @@ function SkeletonJson.new (attachmentLoader)
end end
slotData.attachmentName = slotMap["attachment"] slotData.attachmentName = slotMap["attachment"]
table.insert(skeletonData.slots, slotData)
skeletonData.slotNameIndices[slotData.name] = #skeletonData.slots
table.insert(skeletonData.slots, slotData)
end end
end end
@ -113,7 +113,7 @@ function SkeletonJson.new (attachmentLoader)
for skinName,skinMap in pairs(map) do for skinName,skinMap in pairs(map) do
local skin = Skin.new(skinName) local skin = Skin.new(skinName)
for slotName,slotMap in pairs(skinMap) do for slotName,slotMap in pairs(skinMap) do
local slotIndex = skeletonData:findSlotIndex(slotName) local slotIndex = skeletonData.slotNameIndices[slotName]
for attachmentName,attachmentMap in pairs(slotMap) do for attachmentName,attachmentMap in pairs(slotMap) do
local attachment = readAttachment(attachmentName, attachmentMap, self.scale) local attachment = readAttachment(attachmentName, attachmentMap, self.scale)
if attachment then if attachment then
@ -213,7 +213,7 @@ function SkeletonJson.new (attachmentLoader)
local slotsMap = map["slots"] local slotsMap = map["slots"]
if slotsMap then if slotsMap then
for slotName,timelineMap in pairs(slotsMap) do for slotName,timelineMap in pairs(slotsMap) do
local slotIndex = skeletonData:findSlotIndex(slotName) local slotIndex = skeletonData.slotNameIndices[slotName]
for timelineName,values in pairs(timelineMap) do for timelineName,values in pairs(timelineMap) do
if timelineName == TIMELINE_COLOR then if timelineName == TIMELINE_COLOR then

View File

@ -36,8 +36,9 @@ public class SkeletonComponent : MonoBehaviour {
public String initialSkinName; public String initialSkinName;
public float timeScale = 1; public float timeScale = 1;
private Mesh mesh; private Mesh mesh;
private int lastVertexCount;
private Vector3[] vertices; private Vector3[] vertices;
private Color[] colors; private Color32[] colors;
private Vector2[] uvs; private Vector2[] uvs;
private int[] triangles; private int[] triangles;
private float[] vertexPositions = new float[8]; private float[] vertexPositions = new float[8];
@ -99,11 +100,13 @@ public class SkeletonComponent : MonoBehaviour {
// Ensure mesh data is the right size. // Ensure mesh data is the right size.
Vector3[] vertices = this.vertices; Vector3[] vertices = this.vertices;
bool newTriangles = quadCount > vertices.Length / 4; int vertexCount = quadCount * 4;
bool newTriangles = vertexCount > vertices.Length;
if (newTriangles) { if (newTriangles) {
this.vertices = vertices = new Vector3[quadCount * 4]; // Not enough vertices, increase size.
colors = new Color[quadCount * 4]; this.vertices = vertices = new Vector3[vertexCount];
uvs = new Vector2[quadCount * 4]; colors = new Color32[vertexCount];
uvs = new Vector2[vertexCount];
triangles = new int[quadCount * 6]; triangles = new int[quadCount * 6];
mesh.Clear(); mesh.Clear();
@ -118,15 +121,17 @@ public class SkeletonComponent : MonoBehaviour {
triangles[index + 5] = vertex + 1; triangles[index + 5] = vertex + 1;
} }
} else { } else {
// Too many vertices, zero the extra.
Vector3 zero = new Vector3(0, 0, 0); Vector3 zero = new Vector3(0, 0, 0);
for (int i = quadCount * 4, n = vertices.Length; i < n; i++) for (int i = vertexCount, n = lastVertexCount; i < n; i++)
vertices[i] = zero; vertices[i] = zero;
} }
lastVertexCount = vertexCount;
// Setup mesh. // Setup mesh.
float[] vertexPositions = this.vertexPositions; float[] vertexPositions = this.vertexPositions;
int vertexIndex = 0; int vertexIndex = 0;
Color color = new Color(); Color32 color = new Color32();
for (int i = 0, n = drawOrder.Count; i < n; i++) { for (int i = 0, n = drawOrder.Count; i < n; i++) {
Slot slot = drawOrder[i]; Slot slot = drawOrder[i];
RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
@ -139,10 +144,10 @@ public class SkeletonComponent : MonoBehaviour {
vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0);
vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0); vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0);
color.a = skeleton.A * slot.A; color.a = (byte)(skeleton.A * slot.A * 255);
color.r = skeleton.R * slot.R * color.a; color.r = (byte)(skeleton.R * slot.R * color.a);
color.g = skeleton.G * slot.G * color.a; color.g = (byte)(skeleton.G * slot.G * color.a);
color.b = skeleton.B * slot.B * color.a; color.b = (byte)(skeleton.B * slot.B * color.a);
colors[vertexIndex] = color; colors[vertexIndex] = color;
colors[vertexIndex + 1] = color; colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color; colors[vertexIndex + 2] = color;
@ -157,7 +162,7 @@ public class SkeletonComponent : MonoBehaviour {
vertexIndex += 4; vertexIndex += 4;
} }
mesh.vertices = vertices; mesh.vertices = vertices;
mesh.colors = colors; mesh.colors32 = colors;
mesh.uv = uvs; mesh.uv = uvs;
if (newTriangles) mesh.triangles = triangles; if (newTriangles) mesh.triangles = triangles;
} }