skar

hi all,

i’m trying to update the Codea implementation of the Lua runtime - basically the current one in the resources link only works with older runtime 2.5 , updating there is one place i’m stuck on - the Codea implementation uses the RegionAttachment and MeshAttachment

updateWorldVertices(slot)

but the newest runtime doesn’t have this function anymore, it’s now

computeWorldVertices (bone, worldVertices

in the old function the vertices were part of the RegionAttachment/MeshAttachment self variables and they were returned by the function which are used in Codea to build out it’s mesh

so what is the way to get these vertices now? does this implementation need to be reworked entirely or can i get it to work with just a few changes? the older runtime still works it’s just has a bug i’d like to see if the new ones fixed it

old runtime -
function RegionAttachment:updateWorldVertices (slot, premultipliedAlpha)
local skeleton = slot.bone.skeleton
local skeletonColor = skeleton.color
local slotColor = slot.color
local regionColor = self.color
local alpha = skeletonColor.a * slotColor.a * regionColor.a
local multiplier = alpha
if premultipliedAlpha then multiplier = 1 end
local color = self.tempColor
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 = self.vertices
local offset = self.offset
local bone = slot.bone
local x = bone.worldX
local y = bone.worldY
local a = bone.a
local b = bone.b
local c = bone.c
local d = bone.d
local offsetX = 0
local offsetY = 0

offsetX = offset[OX1]
offsetY = offset[OY1]
vertices[X1] = offsetX * a + offsetY * b + x -- br
vertices[Y1] = offsetX * c + offsetY * d + y
vertices[C1R] = color.r
vertices[C1G] = color.g
vertices[C1B] = color.b
vertices[C1A] = color.a

offsetX = offset[OX2]
offsetY = offset[OY2]
vertices[X2] = offsetX * a + offsetY * b + x -- bl
vertices[Y2] = offsetX * c + offsetY * d + y
vertices[C2R] = color.r
vertices[C2G] = color.g
vertices[C2B] = color.b
vertices[C2A] = color.a

offsetX = offset[OX3]
offsetY = offset[OY3]
vertices[X3] = offsetX * a + offsetY * b + x -- ul
vertices[Y3] = offsetX * c + offsetY * d + y
vertices[C3R] = color.r
vertices[C3G] = color.g
vertices[C3B] = color.b
vertices[C3A] = color.a

offsetX = offset[OX4]
offsetY = offset[OY4]
vertices[X4] = offsetX * a + offsetY * b + x -- ur
vertices[Y4] = offsetX * c + offsetY * d + y
vertices[C4R] = color.r
vertices[C4G] = color.g
vertices[C4B] = color.b
vertices[C4A] = color.a

return vertices
end
new runtime
function RegionAttachment:computeWorldVertices (bone, worldVertices, offset, stride)
offset = offset + 1
local vertexOffset = self.offset
local x = bone.worldX
local y = bone.worldY
local a = bone.a
local b = bone.b
local c = bone.c
local d = bone.d
local offsetX = 0
local offsetY = 0

offsetX = vertexOffset[7]
offsetY = vertexOffset[8]
worldVertices[offset] = offsetX * a + offsetY * b + x -- br
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
offset = offset + stride

offsetX = vertexOffset[1]
offsetY = vertexOffset[2]
worldVertices[offset] = offsetX * a + offsetY * b + x -- bl
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
offset = offset + stride

offsetX = vertexOffset[3]
offsetY = vertexOffset[4]
worldVertices[offset] = offsetX * a + offsetY * b + x -- ul
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
offset = offset + stride

offsetX = vertexOffset[5]
offsetY = vertexOffset[6]
worldVertices[offset] = offsetX * a + offsetY * b + x -- ur
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
end
codea implementation
if attachment.type == spine.AttachmentType.region then
texture = attachment.region.renderObject.texture
vertices = attachment:updateWorldVertices(slot, true)
triangles = QUAD_TRIANGLES
elseif attachment.type == spine.AttachmentType.mesh then
texture = attachment.region.renderObject.texture
vertices = attachment:updateWorldVertices(slot, true)
triangles = attachment.triangles
end

if texture and vertices and triangles then
pushStyle()

local faces = {}
local uvs = {}
local colors = {}
local blend_mode = slot.data.blendMode

if blend_mode == spine.BlendMode.additive then blendMode(ADDITIVE)
elseif blend_mode == spine.BlendMode.multiply then blendMode(MULTIPLY)
elseif blend_mode == spine.BlendMode.screen then blendMode(ONE, ONE_MINUS_SRC_COLOR)
else blendMode(NORMAL) end -- blend_mode == spine.BlendMode.normal and undefined

-- triangulate and supply to GPU
for j, id in ipairs(triangles) do -- listed in cw order
local pos = id * 8 - 8
local vert = vec2(vertices[pos + 1], vertices[pos + 2])
local uv = vec2(vertices[pos + 3], 1 - vertices[pos + 4]) -- flip y
local r = vertices[pos + 5] * 255
local g = vertices[pos + 6] * 255
local b = vertices[pos + 7] * 255
local a = vertices[pos + 8] * 255
table.insert(faces, vert)
table.insert(uvs, uv)
table.insert(colors, color(r, g, b, a))
end

self.mesh:clear()
self.mesh.texture = texture
self.mesh.vertex_buffer:set(faces)
self.mesh.texture_buffer:set(uvs)
self.mesh.color_buffer:set(colors)
self.mesh:draw()


---

i got this to work with the latest runtime
local texture, triangles, uvs
local vertices = {}

if attachment.type == spine.AttachmentType.region then
texture = attachment.region.renderObject.texture
uvs = attachment.uvs
--vertices = attachment:updateWorldVertices(slot, true)
attachment:computeWorldVertices(slot.bone, vertices, 0, 2)
triangles = QUAD_TRIANGLES
elseif attachment.type == spine.AttachmentType.mesh then
texture = attachment.region.renderObject.texture
--vertices = attachment:updateWorldVertices(slot, true)
uvs = attachment.uvs
attachment:computeWorldVertices(slot, 0, attachment.worldVerticesLength, vertices, 0, 2)
triangles = attachment.triangles
end
if texture and vertices and triangles then
pushStyle()
local faces = {}
local uvz = {}
local colors = {}
local blend_mode = slot.data.blendMode

if blend_mode == spine.BlendMode.additive then blendMode(ADDITIVE)
elseif blend_mode == spine.BlendMode.multiply then blendMode(MULTIPLY)
elseif blend_mode == spine.BlendMode.screen then blendMode(ONE, ONE_MINUS_SRC_COLOR)
else blendMode(NORMAL) end -- blend_mode == spine.BlendMode.normal and undefined

-- triangulate and supply to GPU

for j, id in ipairs(triangles) do -- listed in cw order
--print(j, id)
local pos = id * 2 - 2
local vert = vec2(vertices[pos + 1], vertices[pos + 2])
local uv = vec2(uvs[pos + 1], 1 - uvs[pos + 2]) -- flip y
--[[
local r = vertices[pos + 5] * 255
local g = vertices[pos + 6] * 255
local b = vertices[pos + 7] * 255
local a = vertices[pos + 8] * 255
]]
table.insert(faces, vert)
table.insert(uvz, uv)
--table.insert(colors, colorL(r, g, b, a))
end

self.mesh:clear()
self.mesh.texture = texture
self.mesh.vertices = faces
--self.mesh.vertex_buffer:set(faces)
self.mesh.texture_buffer:set(uvz)
--self.mesh.color_buffer:set(colors)
self.mesh:draw()
basically just had to figure out that vertices are now built from being passed in and the color is no longer part of the values

the bug that i saw is gone with the update BUT i tested out rendering 20 spine boys riding raptors and the performance was sluggish, looks like i’ll stuck to png frames and didn’t need the runtime after all :|
skar
  • Сообщения: 7

Nate

Sorry for the late response on the weekend.

You can see how computeWorldVertices works in spine-solar2d (which used to be spine-corona):
https://github.com/EsotericSoftware/spine-runtimes/blob/4.0/spine-solar2d/spine-solar2d/spine.lua#L171

In general Lua doesn't provide great performance, but there are things you may be able to do to improve it. For example, in the code I linked a single worldVertices table is created once and used for all region and mesh attachments. There may be other optimizations you can do and some may depend on your game toolkit.

Please note 4.0 will be our last revision of the spine-lua release. We won't have a Lua runtime starting with 4.1, as we don't have enough Lua users to warrant the effort it takes to maintain.
Аватара пользователя
Nate

Nate
  • Сообщения: 11345


Вернуться в Runtimes