Skip to content

Commit

Permalink
[lua] Fixed SkeletonClipping. [corona] Refactored rendering, should b…
Browse files Browse the repository at this point in the history
…e a bit faster as it avoids a bunch of copies. Fixed RegionAttachment to be in alignement with reference implementation
  • Loading branch information
badlogic committed May 4, 2017
1 parent 1fafbd0 commit 8123422
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 99 deletions.
14 changes: 10 additions & 4 deletions spine-corona/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,25 @@ end

-- table.insert(skeletons, loadSkeleton("test.atlas", "test.json", 240, 300, 0.4, "animation"))
table.insert(skeletons, loadSkeleton("coin.atlas", "coin.json", 240, 300, 0.4, "rotate"))
--[[table.insert(skeletons, loadSkeleton("spineboy.atlas", "spineboy.json", 240, 300, 0.4, "walk"))
table.insert(skeletons, loadSkeleton("spineboy.atlas", "spineboy.json", 240, 300, 0.4, "walk"))
table.insert(skeletons, loadSkeleton("raptor.atlas", "raptor.json", 200, 300, 0.25, "walk"))
table.insert(skeletons, loadSkeleton("goblins.atlas", "goblins-mesh.json", 240, 300, 0.8, "walk", "goblin"))
table.insert(skeletons, loadSkeleton("stretchyman.atlas", "stretchyman.json", 40, 300, 0.5, "sneak"))
table.insert(skeletons, loadSkeleton("tank.atlas", "tank.json", 400, 300, 0.2, "drive"))
table.insert(skeletons, loadSkeleton("vine.atlas", "vine.json", 240, 300, 0.3, "animation"))]]--
table.insert(skeletons, loadSkeleton("vine.atlas", "vine.json", 240, 300, 0.3, "animation"))

local triangulator = spine.Triangulator.new()
local polygon = { 411, 219, 199, 230, 161, 362, 534, 407, 346, 305, 596, 265 }
local indices = triangulator:triangulate(polygon)
print(indices)
print(triangulator:decompose(polygon, indices))

local skeletonClipping = spine.SkeletonClipping.new()
local polygon2 = {0, 0, 100, 0, 100, 100, 0, 100 }
skeletonClipping:makeClockwise(polygon2)
print(polygon2)


local bounds = spine.SkeletonBounds.new()
skeletons[1].skeleton:updateWorldTransform()
bounds:update(skeletons[1].skeleton, true)
Expand All @@ -110,7 +116,7 @@ Runtime:addEventListener("enterFrame", function (event)
state = skeletons[activeSkeleton].state

state:update(delta)
-- state:apply(skeleton)
state:apply(skeleton)
skeleton:updateWorldTransform()

-- uncomment if you want to know how many batches a skeleton renders to
Expand All @@ -120,7 +126,7 @@ end)
Runtime:addEventListener("key", function(event)
if activeSkeleton == 2 and event.phase == "down" then
state = skeletons[activeSkeleton].state
state:setAnimationByName(0, "Jump", false)
state:setAnimationByName(0, "jump", false)
state:addAnimationByName(0, "walk", true, 0)
end
return false
Expand Down
114 changes: 43 additions & 71 deletions spine-corona/spine-corona/spine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ spine.Skeleton.new = function(skeletonData, group)
self.batches = 0
self.tempColor = spine.Color.newWith(1, 1, 1, 1)
self.tempColor2 = spine.Color.newWith(-1, 1, 1, 1)
self.clipper = spine.SkeletonClipping.new()
return self
end

Expand Down Expand Up @@ -140,25 +141,49 @@ function spine.Skeleton:updateWorldTransform()
local lastTexture = nil
local blendMode = nil
local lastBlendMode = nil
local renderable = {
vertices = nil,
uvs = nil
}

for i,slot in ipairs(drawOrder) do
local attachment = slot.attachment
local vertices = nil
local uvs = nil
local numVertices = 0
local indices = nil
if attachment then
if attachment.type == spine.AttachmentType.region then
numVertices = 4
vertices = self:computeRegionVertices(slot, attachment, premultipliedAlpha, color)
vertices = worldVertices
attachment:computeWorldVertices(slot.bone, vertices, 0, 2)
uvs = attachment.uvs
indices = QUAD_TRIANGLES
texture = attachment.region.renderObject.texture
blendMode = toCoronaBlendMode(slot.data.blendMode)
elseif attachment.type == spine.AttachmentType.mesh then
numVertices = attachment.worldVerticesLength / 2
vertices = self:computeMeshVertices(slot, attachment, premultipliedAlpha, color)
vertices = worldVertices
attachment:computeWorldVertices(slot, 0, attachment.worldVerticesLength, vertices, 0, 2)
uvs = attachment.uvs
indices = attachment.triangles
texture = attachment.region.renderObject.texture
blendMode = toCoronaBlendMode(slot.data.blendMode)
elseif attachment.type == spine.AttachmentType.clipping then
self.clipper:clipStart(slot, attachment)
end

local skeleton = slot.bone.skeleton
local skeletonColor = skeleton.color
local slotColor = slot.color
local attachmentColor = attachment.color
local alpha = skeletonColor.a * slotColor.a * attachmentColor.a
local multiplier = alpha
if premultipliedAlpha then multiplier = 1 end
color:set(skeletonColor.r * slotColor.r * attachmentColor.r * multiplier,
skeletonColor.g * slotColor.g * attachmentColor.g * multiplier,
skeletonColor.b * slotColor.b * attachmentColor.b * multiplier,
alpha)

if texture and vertices and indices then
if not lastTexture then lastTexture = texture end
Expand All @@ -174,80 +199,27 @@ function spine.Skeleton:updateWorldTransform()
groupUvs = {}
groupIndices = {}
end

if self.clipper:isClipping() then
self.clipper:clipTriangles(vertices, uvs, indices, #indices)
vertices = self.clipper.clippedVertices
numVertices = #vertices / 2
uvs = self.clipper.clippedUVs
indices = self.clipper.clippedTriangles
end

self:batch(vertices, numVertices, indices, groupVertices, groupUvs, groupIndices)
self:batch(vertices, uvs, numVertices, indices, groupVertices, groupUvs, groupIndices)
end

self.clipper:clipEnd(slot)
end
end

if #groupVertices > 0 then
self:flush(groupVertices, groupUvs, groupIndices, texture, color, blendMode, drawingGroup)
end
end

function spine.Skeleton:computeRegionVertices(slot, region, pma, color)
local skeleton = slot.bone.skeleton
local skeletonColor = skeleton.color
local slotColor = slot.color
local regionColor = region.color
local alpha = skeletonColor.a * slotColor.a * regionColor.a
local multiplier = alpha
if pma then multiplier = 1 end
color:set(skeletonColor.r * slotColor.r * regionColor.r * multiplier,
skeletonColor.g * slotColor.g * regionColor.g * multiplier,
skeletonColor.b * slotColor.b * regionColor.b * multiplier,
alpha)

local vertices = worldVertices
region:computeWorldVertices(slot.bone, vertices, 0, 4)

local uvs = region.uvs

vertices[3] = uvs[1]
vertices[4] = uvs[2]

vertices[7] = uvs[3]
vertices[8] = uvs[4]

vertices[11] = uvs[5]
vertices[12] = uvs[6]

vertices[15] = uvs[7]
vertices[16] = uvs[8]

return vertices
end

function spine.Skeleton:computeMeshVertices(slot, mesh, pma, color)
local skeleton = slot.bone.skeleton
local skeletonColor = skeleton.color
local slotColor = slot.color
local meshColor = mesh.color
local alpha = skeletonColor.a * slotColor.a * meshColor.a
local multiplier = alpha
if pma then multiplier = 1 end
color:set(skeletonColor.r * slotColor.r * meshColor.r * multiplier,
skeletonColor.g * slotColor.g * meshColor.g * multiplier,
skeletonColor.b * slotColor.b * meshColor.b * multiplier,
alpha)

local numVertices = mesh.worldVerticesLength / 2
local vertices = worldVertices
mesh:computeWorldVertices(slot, 0, mesh.worldVerticesLength, vertices, 0, 4)

local uvs = mesh.uvs
local i = 1
local n = numVertices + 1
local u = 1
local v = 3
while i < n do
vertices[v] = uvs[u]
vertices[v + 1] = uvs[u + 1]
i = i + 1
u = u + 2
v = v + 4
end
return vertices
self.clipper:clipEnd2()
end

function spine.Skeleton:flush(groupVertices, groupUvs, groupIndices, texture, color, blendMode, drawingGroup)
Expand All @@ -265,7 +237,7 @@ function spine.Skeleton:flush(groupVertices, groupUvs, groupIndices, texture, co
self.batches = self.batches + 1
end

function spine.Skeleton:batch(vertices, numVertices, indices, groupVertices, groupUvs, groupIndices)
function spine.Skeleton:batch(vertices, uvs, numVertices, indices, groupVertices, groupUvs, groupIndices)
local numIndices = #indices
local i = 1
local indexStart = #groupIndices + 1
Expand All @@ -284,10 +256,10 @@ function spine.Skeleton:batch(vertices, numVertices, indices, groupVertices, gro
while vertexStart < vertexEnd do
groupVertices[vertexStart] = vertices[i]
groupVertices[vertexStart+1] = vertices[i+1]
groupUvs[vertexStart] = vertices[i+2]
groupUvs[vertexStart+1] = vertices[i+3]
groupUvs[vertexStart] = uvs[i]
groupUvs[vertexStart+1] = uvs[i+1]
vertexStart = vertexStart + 2
i = i + 4
i = i + 2
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,9 @@ public static void main (String[] args) {

Array<FloatArray> polys = triangulator.decompose(polygon, triangles);
System.out.println(polys);

FloatArray poly2 = new FloatArray(new float[] {0, 0, 100, 0, 100, 100, 0, 100 });
SkeletonClipping.makeClockwise(poly2);
System.out.println(poly2);
}
}
Loading

0 comments on commit 8123422

Please sign in to comment.