-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
spine.lua
496 lines (442 loc) · 15.8 KB
/
spine.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
-------------------------------------------------------------------------------
-- 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.
-------------------------------------------------------------------------------
local setmetatable = setmetatable
spine = {}
spine.utils = require "spine-lua.utils"
spine.SkeletonJson = require "spine-lua.SkeletonJson"
spine.SkeletonData = require "spine-lua.SkeletonData"
spine.BoneData = require "spine-lua.BoneData"
spine.SlotData = require "spine-lua.SlotData"
spine.IkConstraintData = require "spine-lua.IkConstraintData"
spine.Skin = require "spine-lua.Skin"
spine.Attachment = require "spine-lua.attachments.Attachment"
spine.BoundingBoxAttachment = require "spine-lua.attachments.BoundingBoxAttachment"
spine.RegionAttachment = require "spine-lua.attachments.RegionAttachment"
spine.MeshAttachment = require "spine-lua.attachments.MeshAttachment"
spine.VertexAttachment = require "spine-lua.attachments.VertexAttachment"
spine.PathAttachment = require "spine-lua.attachments.PathAttachment"
spine.PointAttachment = require "spine-lua.attachments.PointAttachment"
spine.Skeleton = require "spine-lua.Skeleton"
spine.Bone = require "spine-lua.Bone"
spine.Slot = require "spine-lua.Slot"
spine.IkConstraint = require "spine-lua.IkConstraint"
spine.AttachmentType = require "spine-lua.attachments.AttachmentType"
spine.AttachmentLoader = require "spine-lua.AttachmentLoader"
spine.Animation = require "spine-lua.Animation"
spine.AnimationStateData = require "spine-lua.AnimationStateData"
spine.AnimationState = require "spine-lua.AnimationState"
spine.EventData = require "spine-lua.EventData"
spine.Event = require "spine-lua.Event"
spine.SkeletonBounds = require "spine-lua.SkeletonBounds"
spine.BlendMode = require "spine-lua.BlendMode"
spine.TextureAtlas = require "spine-lua.TextureAtlas"
spine.TextureRegion = require "spine-lua.TextureRegion"
spine.TextureAtlasRegion = require "spine-lua.TextureAtlasRegion"
spine.AtlasAttachmentLoader = require "spine-lua.AtlasAttachmentLoader"
spine.Color = require "spine-lua.Color"
spine.utils.readFile = function (fileName, base)
local path = fileName
if base then path = base .. '/' .. path end
return love.filesystem.read(path)
end
local json = require "spine-love.dkjson"
spine.utils.readJSON = function (text)
return json.decode(text)
end
local PolygonBatcher = {}
PolygonBatcher.__index = PolygonBatcher
function PolygonBatcher.new(vertexCount, useTwoColorTint)
if useTwoColorTint == nil then useTwoColorTint = false end
local vertexFormat
local twoColorTintShader = nil
if useTwoColorTint then
vertexFormat = {
{"VertexPosition", "float", 2}, -- The x,y position of each vertex.
{"VertexTexCoord", "float", 2}, -- The u,v texture coordinates of each vertex.
{"VertexColor", "byte", 4}, -- The r,g,b,a light color of each vertex.
{"VertexColor2", "byte", 4} -- The r,g,b,a dark color of each vertex.
}
local vertexcode = [[
attribute vec4 VertexColor2;
varying vec4 color2;
vec4 position(mat4 transform_projection, vec4 vertex_position) {
color2 = VertexColor2;
return transform_projection * vertex_position;
}
]]
local pixelcode = [[
varying vec4 color2;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 texColor = Texel(texture, texture_coords);
float alpha = texColor.a * color.a;
vec4 outputColor;
outputColor.a = alpha;
outputColor.rgb = (1.0 - texColor.rgb) * color2.rgb * alpha + texColor.rgb * color.rgb;
return outputColor;
}
]]
twoColorTintShader = love.graphics.newShader(pixelcode, vertexcode)
else
vertexFormat = {
{"VertexPosition", "float", 2}, -- The x,y position of each vertex.
{"VertexTexCoord", "float", 2}, -- The u,v texture coordinates of each vertex.
{"VertexColor", "byte", 4} -- The r,g,b,a light color of each vertex.
}
end
local self = {
mesh = love.graphics.newMesh(vertexFormat, vertexCount, "triangles", "dynamic"),
maxVerticesLength = vertexCount,
maxIndicesLength = vertexCount * 3,
verticesLength = 0,
indicesLength = 0,
lastTexture = nil,
isDrawing = false,
drawCalls = 0,
vertex = { 0, 0, 0, 0, 0, 0, 0, 0 },
indices = nil,
useTwoColorTint = useTwoColorTint,
twoColorTintShader = twoColorTintShader
}
local indices = {}
local i = 1
local maxIndicesLength = self.maxIndicesLength
while i <= maxIndicesLength do
indices[i] = 1
i = i + 1
end
self.indices = indices;
setmetatable(self, PolygonBatcher)
return self
end
function PolygonBatcher:begin ()
if self.isDrawing then error("PolygonBatcher is already drawing. Call PolygonBatcher:stop() before calling PolygonBatcher:begin().", 2) end
self.lastTexture = nil
self.isDrawing = true
self.drawCalls = 0
end
function PolygonBatcher:draw (texture, vertices, numVertices, indices)
local numIndices = #indices
local mesh = self.mesh
if texture ~= self.lastTexture then
self:flush()
self.lastTexture = texture
mesh:setTexture(texture)
elseif self.verticesLength + numVertices >= self.maxVerticesLength or self.indicesLength + numIndices > self.maxIndicesLength then
self:flush()
end
local i = 1
local indexStart = self.indicesLength + 1
local offset = self.verticesLength
local indexEnd = indexStart + numIndices
local meshIndices = self.indices
while indexStart < indexEnd do
meshIndices[indexStart] = indices[i] + offset
indexStart = indexStart + 1
i = i + 1
end
self.indicesLength = self.indicesLength + numIndices
i = 1
local vertexStart = self.verticesLength + 1
local vertexEnd = vertexStart + numVertices
local vertex = self.vertex
if not self.useTwoColorTint then
while vertexStart < vertexEnd do
vertex[1] = vertices[i]
vertex[2] = vertices[i+1]
vertex[3] = vertices[i+2]
vertex[4] = vertices[i+3]
vertex[5] = vertices[i+4] * 255
vertex[6] = vertices[i+5] * 255
vertex[7] = vertices[i+6] * 255
vertex[8] = vertices[i+7] * 255
mesh:setVertex(vertexStart, vertex)
vertexStart = vertexStart + 1
i = i + 8
end
else
while vertexStart < vertexEnd do
vertex[1] = vertices[i]
vertex[2] = vertices[i+1]
vertex[3] = vertices[i+2]
vertex[4] = vertices[i+3]
vertex[5] = vertices[i+4] * 255
vertex[6] = vertices[i+5] * 255
vertex[7] = vertices[i+6] * 255
vertex[8] = vertices[i+7] * 255
vertex[9] = vertices[i+8] * 255
vertex[10] = vertices[i+9] * 255
vertex[11] = vertices[i+10] * 255
vertex[12] = vertices[i+11] * 255
mesh:setVertex(vertexStart, vertex)
vertexStart = vertexStart + 1
i = i + 12
end
end
self.verticesLength = self.verticesLength + numVertices
end
function PolygonBatcher:flush ()
if self.verticesLength == 0 then return end
local mesh = self.mesh
mesh:setVertexMap(self.indices)
mesh:setDrawRange(1, self.indicesLength)
if not self.useTwoColorTint then
love.graphics.draw(mesh, 0, 0)
else
love.graphics.setShader(self.twoColorTintShader)
love.graphics.draw(mesh, 0, 0)
love.graphics.setShader()
end
self.verticesLength = 0
self.indicesLength = 0
self.drawCalls = self.drawCalls + 1
end
function PolygonBatcher:stop ()
if not self.isDrawing then error("PolygonBatcher is not drawing. Call PolygonBatcher:begin() first.", 2) end
if self.verticesLength > 0 then self:flush() end
self.lastTexture = nil
self.isDrawing = false
end
local SkeletonRenderer = {}
SkeletonRenderer.__index = SkeletonRenderer
SkeletonRenderer.QUAD_TRIANGLES = { 1, 2, 3, 3, 4, 1 }
function SkeletonRenderer.new (useTwoColorTint)
if not useTwoColorTint then useTwoColorTint = false end
local self = {
batcher = PolygonBatcher.new(3 * 500, useTwoColorTint),
premultipliedAlpha = false,
useTwoColorTint = useTwoColorTint
}
setmetatable(self, SkeletonRenderer)
return self
end
local worldVertices = spine.utils.newNumberArray(10000 * 12)
local tmpColor = spine.Color.newWith(0, 0, 0, 0)
local tmpColor2 = spine.Color.newWith(0, 0, 0, 0)
function SkeletonRenderer:draw (skeleton)
local batcher = self.batcher
local premultipliedAlpha = self.premultipliedAlpha
local lastLoveBlendMode = love.graphics.getBlendMode()
love.graphics.setBlendMode("alpha")
local lastBlendMode = spine.BlendMode.normal
batcher:begin()
local drawOrder = skeleton.drawOrder
for i, slot in ipairs(drawOrder) do
local attachment = slot.attachment
local vertices = nil
local indics = nil
local texture = nil
local color = tmpColor
if attachment then
if attachment.type == spine.AttachmentType.region then
numVertices = 4
vertices = self:computeRegionVertices(slot, attachment, premultipliedAlpha, color)
indices = SkeletonRenderer.QUAD_TRIANGLES
texture = attachment.region.renderObject.texture
elseif attachment.type == spine.AttachmentType.mesh then
numVertices = attachment.worldVerticesLength / 2
vertices = self:computeMeshVertices(slot, attachment, premultipliedAlpha, color)
indices = attachment.triangles
texture = attachment.region.renderObject.texture
end
if texture then
local slotBlendMode = slot.data.blendMode
if lastBlendMode ~= slotBlendMode then
if slotBlendMode == spine.BlendMode.normal then
love.graphics.setBlendMode("alpha")
elseif slotBlendMode == spine.BlendMode.additive then
love.graphics.setBlendMode("additive")
elseif slotBlendMode == spine.BlendMode.multiply then
love.graphics.setBlendMode("multiply")
elseif slotBlendMode == spine.BlendMode.screen then
love.graphics.setBlendMode("screen")
end
lastBlendMode = slotBlendMode
batcher:stop()
batcher:begin()
end
batcher:draw(texture, vertices, numVertices, indices)
end
end
end
batcher:stop()
love.graphics.setBlendMode(lastLoveBlendMode)
end
function SkeletonRenderer: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 dark = tmpColor2
if slot.darkColor then dark = slot.darkColor
else dark:set(0, 0, 0, 0) end
local vertices = worldVertices
if not self.useTwoColorTint then
region:computeWorldVertices(slot.bone, vertices, 0, 8)
else
region:computeWorldVertices(slot.bone, vertices, 0, 12)
end
local uvs = region.uvs
if not self.useTwoColorTint then
vertices[3] = uvs[1]
vertices[4] = uvs[2]
vertices[5] = color.r
vertices[6] = color.g
vertices[7] = color.b
vertices[8] = color.a
vertices[11] = uvs[3]
vertices[12] = uvs[4]
vertices[13] = color.r
vertices[14] = color.g
vertices[15] = color.b
vertices[16] = color.a
vertices[19] = uvs[5]
vertices[20] = uvs[6]
vertices[21] = color.r
vertices[22] = color.g
vertices[23] = color.b
vertices[24] = color.a
vertices[27] = uvs[7]
vertices[28] = uvs[8]
vertices[29] = color.r
vertices[30] = color.g
vertices[31] = color.b
vertices[32] = color.a
else
vertices[3] = uvs[1]
vertices[4] = uvs[2]
vertices[5] = color.r
vertices[6] = color.g
vertices[7] = color.b
vertices[8] = color.a
vertices[9] = dark.r
vertices[10] = dark.g
vertices[11] = dark.b
vertices[12] = 0
vertices[15] = uvs[3]
vertices[16] = uvs[4]
vertices[17] = color.r
vertices[18] = color.g
vertices[19] = color.b
vertices[20] = color.a
vertices[21] = dark.r
vertices[22] = dark.g
vertices[23] = dark.b
vertices[24] = 0
vertices[27] = uvs[5]
vertices[28] = uvs[6]
vertices[29] = color.r
vertices[30] = color.g
vertices[31] = color.b
vertices[32] = color.a
vertices[33] = dark.r
vertices[34] = dark.g
vertices[35] = dark.b
vertices[36] = 0
vertices[39] = uvs[7]
vertices[40] = uvs[8]
vertices[41] = color.r
vertices[42] = color.g
vertices[43] = color.b
vertices[44] = color.a
vertices[45] = dark.r
vertices[46] = dark.g
vertices[47] = dark.b
vertices[48] = 0
end
return vertices
end
function SkeletonRenderer: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
local dark = tmpColor2
if slot.darkColor then dark = slot.darkColor
else dark:set(0, 0, 0, 0) end
if not self.useTwoColorTint then
mesh:computeWorldVertices(slot, 0, mesh.worldVerticesLength, vertices, 0, 8)
else
mesh:computeWorldVertices(slot, 0, mesh.worldVerticesLength, vertices, 0, 12)
end
local uvs = mesh.uvs
local i = 1
local n = numVertices + 1
local u = 1
local v = 3
if not self.useTwoColorTint then
while i < n do
vertices[v] = uvs[u]
vertices[v + 1] = uvs[u + 1]
vertices[v + 2] = color.r
vertices[v + 3] = color.g
vertices[v + 4] = color.b
vertices[v + 5] = color.a
i = i + 1
u = u + 2
v = v + 8
end
else
while i < n do
vertices[v] = uvs[u]
vertices[v + 1] = uvs[u + 1]
vertices[v + 2] = color.r
vertices[v + 3] = color.g
vertices[v + 4] = color.b
vertices[v + 5] = color.a
vertices[v + 6] = dark.r
vertices[v + 7] = dark.g
vertices[v + 8] = dark.b
vertices[v + 9] = 0
i = i + 1
u = u + 2
v = v + 12
end
end
return vertices
end
spine.PolygonBatcher = PolygonBatcher
spine.SkeletonRenderer = SkeletonRenderer
return spine