Merge pull request #524 from ggcrunchy/master

Added requires in attachment loader
This commit is contained in:
Nathan Sweet 2016-03-05 09:24:44 +01:00
commit 70d0bc1b38
5 changed files with 47 additions and 40 deletions

View File

@ -604,7 +604,7 @@ function Animation.FfdTimeline.new ()
function self:apply (skeleton, lastTime, time, firedEvents, alpha) function self:apply (skeleton, lastTime, time, firedEvents, alpha)
local slot = skeleton.slots[self.slotIndex] local slot = skeleton.slots[self.slotIndex]
if slot.attachment ~= attachment then return end if slot.attachment ~= self.attachment then return end
local frames = self.frames local frames = self.frames
if time < frames[0] then return end -- Time is before first frame. if time < frames[0] then return end -- Time is before first frame.
@ -612,7 +612,7 @@ function Animation.FfdTimeline.new ()
local frameVertices = self.frameVertices local frameVertices = self.frameVertices
local vertexCount = #frameVertices[0] local vertexCount = #frameVertices[0]
local vertices = slot.attachmentVertices local vertices = slot.attachmentVertices
if #vertices < vertexCount then if not vertices or #vertices < vertexCount then
vertices = {} vertices = {}
slot.attachmentVertices = vertices slot.attachmentVertices = vertices
end end
@ -620,16 +620,15 @@ function Animation.FfdTimeline.new ()
alpha = 1 -- Don't mix from uninitialized slot vertices. alpha = 1 -- Don't mix from uninitialized slot vertices.
end end
slot.attachmentVerticesCount = vertexCount slot.attachmentVerticesCount = vertexCount
if time >= frames[#frames - 1] then -- Time is after last frame.
if time >= frames[#frames] then -- Time is after last frame. local lastVertices = frameVertices[#frames - 1]
local lastVertices = frameVertices[#frames]
if alpha < 1 then if alpha < 1 then
for i = 0, vertexCount do for i = 1, vertexCount do
local vertex = vertices[i] local vertex = vertices[i]
vertices[i] = vertex + (lastVertices[i] - vertex) * alpha vertices[i] = vertex + (lastVertices[i] - vertex) * alpha
end end
else else
for i = 0, vertexCount do for i = 1, vertexCount do
vertices[i] = lastVertices[i] vertices[i] = lastVertices[i]
end end
end end
@ -647,13 +646,13 @@ function Animation.FfdTimeline.new ()
local nextVertices = frameVertices[frameIndex] local nextVertices = frameVertices[frameIndex]
if alpha < 1 then if alpha < 1 then
for i = 0, vertexCount do for i = 1, vertexCount do
local prev = prevVertices[i] local prev = prevVertices[i]
local vertices = vertices[i] local vertex = vertices[i]
vertices[i] = vertices + (prev + (nextVertices[i] - prev) * percent - vertices) * alpha vertices[i] = vertex + (prev + (nextVertices[i] - prev) * percent - vertex) * alpha
end end
else else
for i = 0, vertexCount do for i = 1, vertexCount do
local prev = prevVertices[i] local prev = prevVertices[i]
vertices[i] = prev + (nextVertices[i] - prev) * percent vertices[i] = prev + (nextVertices[i] - prev) * percent
end end

View File

@ -32,6 +32,8 @@
local AttachmentType = require "spine-lua.AttachmentType" local AttachmentType = require "spine-lua.AttachmentType"
local RegionAttachment = require "spine-lua.RegionAttachment" local RegionAttachment = require "spine-lua.RegionAttachment"
local BoundingBoxAttachment = require "spine-lua.BoundingBoxAttachment" local BoundingBoxAttachment = require "spine-lua.BoundingBoxAttachment"
local MeshAttachment = require "spine-lua.MeshAttachment"
local SkinningMeshAttachment = require "spine-lua.SkinnedMeshAttachment"
local AttachmentLoader = {} local AttachmentLoader = {}
function AttachmentLoader.new () function AttachmentLoader.new ()

View File

@ -34,7 +34,7 @@ local AttachmentType = require "spine-lua.AttachmentType"
local MeshAttachment = {} local MeshAttachment = {}
function MeshAttachment.new (name) function MeshAttachment.new (name)
if not name then error("name cannot be nil", 2) end if not name then error("name cannot be nil", 2) end
local self = { local self = {
name = name, name = name,
type = AttachmentType.mesh, type = AttachmentType.mesh,
@ -46,7 +46,7 @@ function MeshAttachment.new (name)
r = 1, g = 1, b = 1, a = 1, r = 1, g = 1, b = 1, a = 1,
path = nil, path = nil,
rendererObject = nil, rendererObject = nil,
regionU = 0, regionV = 0, regionU2 = 0, regionV2 = 0, regionRotate = false, regionU = 0, regionV = 0, regionU2 = 1, regionV2 = 1, regionRotate = false,
regionOffsetX = 0, regionOffsetY = 0, regionOffsetX = 0, regionOffsetY = 0,
regionWidth = 0, regionHeight = 0, regionWidth = 0, regionHeight = 0,
regionOriginalWidth = 0, regionOriginalHeight = 0, regionOriginalWidth = 0, regionOriginalHeight = 0,
@ -75,12 +75,13 @@ function MeshAttachment.new (name)
function self:computeWorldVertices (x, y, slot, worldVertices) function self:computeWorldVertices (x, y, slot, worldVertices)
local bone = slot.bone local bone = slot.bone
x,y=slot.bone.skeleton.x,slot.bone.skeleton.y
x = x + bone.worldX x = x + bone.worldX
y = y + bone.worldY y = y + bone.worldY
local m00, m01, m10, m11 = bone.m00, bone.m01, bone.m10, bone.m11 local m00, m01, m10, m11 = bone.m00, bone.m01, bone.m10, bone.m11
local vertices = self.vertices local vertices = self.vertices
local verticesCount = vertices.length local verticesCount = #vertices
if #slot.attachmentVertices == verticesCount then vertices = slot.attachmentVertices end if slot.attachmentVertices and #slot.attachmentVertices == verticesCount then vertices = slot.attachmentVertices end
for i = 1, verticesCount, 2 do for i = 1, verticesCount, 2 do
local vx = vertices[i] local vx = vertices[i]
local vy = vertices[i + 1] local vy = vertices[i + 1]

View File

@ -178,9 +178,8 @@ function SkeletonJson.new (attachmentLoader)
end end
if skin.name == "default" then if skin.name == "default" then
skeletonData.defaultSkin = skin skeletonData.defaultSkin = skin
else
table.insert(skeletonData.skins, skin)
end end
table.insert(skeletonData.skins, skin)
end end
end end
@ -266,19 +265,21 @@ function SkeletonJson.new (attachmentLoader)
return mesh return mesh
elseif type == AttachmentType.skinnedmesh then elseif type == AttachmentType.skinnedmesh then
local mesh = self.attachmentLoader.newSkinnedMeshAttachment(skin, name, path) local mesh = self.attachmentLoader.newSkinningMeshAttachment(skin, name, path)
if not mesh then return null end if not mesh then return null end
mesh.path = path mesh.path = path
local uvs = getArray(map, "uvs", 1) local uvs = getArray(map, "uvs", 1)
vertices = getArray(map, "vertices", 1) local vertices = getArray(map, "vertices", 1)
local weights = {} local weights = {}
local bones = {} local bones = {}
for i = 1, vertices do local i, n = 1, #vertices
while i < n do
local boneCount = vertices[i] local boneCount = vertices[i]
i = i + 1 i = i + 1
table.insert(bones, boneCount) table.insert(bones, boneCount)
for ii = 1, i + boneCount * 4 do local nn = i + boneCount * 4
while i < nn do
table.insert(bones, vertices[i]) table.insert(bones, vertices[i])
table.insert(weights, vertices[i + 1] * scale) table.insert(weights, vertices[i + 1] * scale)
table.insert(weights, vertices[i + 2] * scale) table.insert(weights, vertices[i + 2] * scale)
@ -468,22 +469,21 @@ function SkeletonJson.new (attachmentLoader)
local ffd = map["ffd"] local ffd = map["ffd"]
if ffd then if ffd then
for skinName,slotMap in pairs(ffd) do for skinName,slotMap in pairs(ffd) do
local skin = skeletonData.findSkin(skinName) local skin = skeletonData:findSkin(skinName)
for slotName,meshMap in pairs(slotMap) do for slotName,meshMap in pairs(slotMap) do
local slotIndex = skeletonData.findSlotIndex(slotName) local slotIndex = skeletonData:findSlotIndex(slotName)
for meshName,values in pairs(meshMap) do for meshName,values in pairs(meshMap) do
local timeline = Animation.FfdTimeline.new() local timeline = Animation.FfdTimeline.new()
local attachment = skin:getAttachment(slotIndex, meshName) local attachment = skin:getAttachment(slotIndex, meshName)
if not attachment then error("FFD attachment not found: " .. meshName) end if not attachment then error("FFD attachment not found: " .. meshName) end
timeline.slotIndex = slotIndex timeline.slotIndex = slotIndex
timeline.attachment = attachment timeline.attachment = attachment
local isMesh = attachment.type == AttachmentType.mesh local isMesh = attachment.type == AttachmentType.mesh
local vertexCount local vertexCount
if isMesh then if isMesh then
vertexCount = attachment.vertices.length vertexCount = #attachment.vertices
else else
vertexCount = attachment.weights.length / 3 * 2 vertexCount = #attachment.weights / 3 * 2
end end
local frameIndex = 0 local frameIndex = 0
@ -494,12 +494,18 @@ function SkeletonJson.new (attachmentLoader)
vertices = attachment.vertices vertices = attachment.vertices
else else
vertices = {} vertices = {}
vertices.length = vertexCount for i = 1, vertexCount do
vertices[i] = 0
end
end end
else else
local verticesValue = valueMap["vertices"] local verticesValue = valueMap["vertices"]
local vertices = {} local scale = self.scale
vertices = {}
local start = valueMap["offset"] or 0 local start = valueMap["offset"] or 0
for ii = 1, start do
vertices[ii] = 0
end
if scale == 1 then if scale == 1 then
for ii = 1, #verticesValue do for ii = 1, #verticesValue do
vertices[ii + start] = verticesValue[ii] vertices[ii + start] = verticesValue[ii]
@ -518,7 +524,6 @@ function SkeletonJson.new (attachmentLoader)
vertices[vertexCount] = 0 vertices[vertexCount] = 0
end end
end end
timeline:setFrame(frameIndex, valueMap["time"], vertices) timeline:setFrame(frameIndex, valueMap["time"], vertices)
readCurve(timeline, frameIndex, valueMap) readCurve(timeline, frameIndex, valueMap)
frameIndex = frameIndex + 1 frameIndex = frameIndex + 1

View File

@ -37,7 +37,7 @@ function SkinnedMeshAttachment.new (name)
local self = { local self = {
name = name, name = name,
type = AttachmentType.mesh, type = AttachmentType.skinnedmesh,
bones = nil, bones = nil,
weights = nil, weights = nil,
uvs = nil, uvs = nil,
@ -47,7 +47,7 @@ function SkinnedMeshAttachment.new (name)
r = 1, g = 1, b = 1, a = 1, r = 1, g = 1, b = 1, a = 1,
path = nil, path = nil,
rendererObject = nil, rendererObject = nil,
regionU = 0, regionV = 0, regionU2 = 0, regionV2 = 0, regionRotate = false, regionU = 0, regionV = 0, regionU2 = 1, regionV2 = 1, regionRotate = false,
regionOffsetX = 0, regionOffsetY = 0, regionOffsetX = 0, regionOffsetY = 0,
regionWidth = 0, regionHeight = 0, regionWidth = 0, regionHeight = 0,
regionOriginalWidth = 0, regionOriginalHeight = 0, regionOriginalWidth = 0, regionOriginalHeight = 0,
@ -75,21 +75,21 @@ function SkinnedMeshAttachment.new (name)
end end
function self:computeWorldVertices (x, y, slot, worldVertices) function self:computeWorldVertices (x, y, slot, worldVertices)
local skeletonBones = slot.skeleton.bones local skeletonBones = slot.bone.skeleton.bones
x,y=slot.bone.skeleton.x,slot.bone.skeleton.y
local weights = self.weights local weights = self.weights
local bones = self.bones local bones = self.bones
local w, v, b, f = 1, 1, 1, 1
local w, v, b, f = 0, 0, 0, 0 local n = #bones
local n = bones.length
local wx, wy, bone, vx, vy, weight local wx, wy, bone, vx, vy, weight
if #slot.attachmentVertices == 0 then if slot.attachmentVerticesCount == 0 then
while v < n do while v <= n do
wx = 0 wx = 0
wy = 0 wy = 0
local nn = bones[v] + v local nn = bones[v] + v
v = v + 1 v = v + 1
while v <= nn do while v <= nn do
bone = skeletonBones[bones[v]] bone = skeletonBones[bones[v] + 1]
vx = weights[b] vx = weights[b]
vy = weights[b + 1] vy = weights[b + 1]
weight = weights[b + 2] weight = weights[b + 2]
@ -104,13 +104,13 @@ function SkinnedMeshAttachment.new (name)
end end
else else
local ffd = slot.attachmentVertices local ffd = slot.attachmentVertices
while v < n do while v <= n do
wx = 0 wx = 0
wy = 0 wy = 0
local nn = bones[v] + v local nn = bones[v] + v
v = v + 1 v = v + 1
while v <= nn do while v <= nn do
bone = skeletonBones[bones[v]] bone = skeletonBones[bones[v] + 1]
vx = weights[b] + ffd[f] vx = weights[b] + ffd[f]
vy = weights[b + 1] + ffd[f + 1] vy = weights[b + 1] + ffd[f + 1]
weight = weights[b + 2] weight = weights[b + 2]