From fdee465333cef2b1dc9bdffeefa824ed79c4d977 Mon Sep 17 00:00:00 2001 From: badlogic Date: Tue, 27 Jun 2017 15:46:44 +0200 Subject: [PATCH] [lua][love][corona] Added vertex effect support. See #898 --- CHANGELOG.md | 3 + spine-corona/main.lua | 17 ++++- spine-corona/spine-corona/spine.lua | 46 +++++++++++-- spine-love/main.lua | 25 ++++++- spine-love/spine-love/spine.lua | 69 +++++++++++++++---- spine-lua/Interpolation.lua | 48 +++++++++++++ spine-lua/utils.lua | 14 ++++ spine-lua/vertexeffects/JitterEffect.lua | 64 ++++++++++++++++++ spine-lua/vertexeffects/SwirlEffect.lua | 85 ++++++++++++++++++++++++ 9 files changed, 347 insertions(+), 24 deletions(-) create mode 100644 spine-lua/Interpolation.lua create mode 100644 spine-lua/vertexeffects/JitterEffect.lua create mode 100644 spine-lua/vertexeffects/SwirlEffect.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 503c43256..91e1a8724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -179,15 +179,18 @@ * Added `SkeletonClipper` and `Triangulator`, used to implement software clipping of attachments. * `AnimationState#apply` returns boolean indicating if any timeline was applied or not. * `Animation#apply` and `Timeline#apply`` now take enums `MixPose` and `MixDirection` instead of booleans + * Added `JitterEffect` and `SwirlEffect` and support for vertex effects in Corona and Love ### Love2D * Fixed renderer to work with 3.6 changes * Added support for two color tinting. Enable it via `SkeletonRenderer.new(true)`. * Added clipping support. + * Added support for vertex effects. Set an implementation like "JitterEffect" on `Skeleton.vertexEffect`. See `main.lua` for an example. ### Corona * Fixed renderer to work with 3.6 changes. Sadly, two color tinting is not supported, as Corona doesn't let us change the vertex format needed and its doesn't allow to modify shaders in the way needed for two color tinting * Added clipping support. + * Added support for vertex effects. Set an implementation like "JitterEffect" on `SkeletonRenderer.vertexEffect`. See `main.lua` for an example ## Typescript/Javascript * **Breaking changes** diff --git a/spine-corona/main.lua b/spine-corona/main.lua index eea3a3bfa..ba50eaaa7 100644 --- a/spine-corona/main.lua +++ b/spine-corona/main.lua @@ -5,6 +5,8 @@ local spine = require "spine-corona.spine" local skeletons = {} local activeSkeleton = 1 local lastTime = 0 +local swirl = spine.SwirlEffect.new(400) +local swirlTime = 0 function loadSkeleton(atlasFile, jsonFile, x, y, scale, animation, skin) -- to load an atlas, we need to define a function that returns @@ -67,7 +69,11 @@ function loadSkeleton(atlasFile, jsonFile, x, y, scale, animation, skin) animationState:setAnimationByName(0, "walk", true) local jumpEntry = animationState:addAnimationByName(0, "jump", false, 3) animationState:addAnimationByName(0, "run", true, 0) - else + elseif atlasFile == "raptor.atlas" then + --skeleton.vertexEffect = spine.JitterEffect.new(5, 5) + skeleton.vertexEffect = swirl + animationState:setAnimationByName(0, animation, true) + else animationState:setAnimationByName(0, animation, true) end @@ -77,10 +83,10 @@ end -- table.insert(skeletons, loadSkeleton("coin.atlas", "coin-pro.json", 240, 300, 0.4, "rotate")) -- table.insert(skeletons, loadSkeleton("spineboy.atlas", "spineboy-ess.json", 240, 300, 0.4, "walk")) --- table.insert(skeletons, loadSkeleton("raptor.atlas", "raptor-pro.json", 200, 300, 0.25, "walk")) +table.insert(skeletons, loadSkeleton("raptor.atlas", "raptor-pro.json", 200, 300, 0.25, "walk")) -- table.insert(skeletons, loadSkeleton("goblins.atlas", "goblins-pro.json", 240, 300, 0.8, "walk", "goblin")) -- table.insert(skeletons, loadSkeleton("stretchyman.atlas", "stretchyman-pro.json", 40, 300, 0.5, "sneak")) -table.insert(skeletons, loadSkeleton("tank.atlas", "tank-pro.json", 400, 300, 0.2, "drive")) +-- table.insert(skeletons, loadSkeleton("tank.atlas", "tank-pro.json", 400, 300, 0.2, "drive")) -- table.insert(skeletons, loadSkeleton("vine.atlas", "vine-pro.json", 240, 300, 0.3, "grow")) local triangulator = spine.Triangulator.new() @@ -109,6 +115,11 @@ Runtime:addEventListener("enterFrame", function (event) local currentTime = event.time / 1000 local delta = currentTime - lastTime lastTime = currentTime + + swirlTime = swirlTime + delta + local percent = swirlTime % 2 + if (percent > 1) then percent = 1 - (percent - 1) end + swirl.angle = spine.Interpolation.apply(spine.Interpolation.pow2, -60, 60, percent) skeleton = skeletons[activeSkeleton].skeleton skeleton.group.isVisible = true diff --git a/spine-corona/spine-corona/spine.lua b/spine-corona/spine-corona/spine.lua index 4ce6e9dcc..edd118b36 100644 --- a/spine-corona/spine-corona/spine.lua +++ b/spine-corona/spine-corona/spine.lua @@ -65,6 +65,9 @@ spine.AtlasAttachmentLoader = require "spine-lua.AtlasAttachmentLoader" spine.Color = require "spine-lua.Color" spine.Triangulator = require "spine-lua.Triangulator" spine.SkeletonClipping = require "spine-lua.SkeletonClipping" +spine.JitterEffect = require "spine-lua.vertexeffects.JitterEffect" +spine.SwirlEffect = require "spine-lua.vertexeffects.SwirlEffect" +spine.Interpolation = require "spine-lua.Interpolation" spine.utils.readFile = function (fileName, base) if not base then base = system.ResourceDirectory end @@ -92,6 +95,14 @@ 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.tempVertex = { + x = 0, + y = 0, + u = 0, + v = 0, + light = spine.Color.newWith(1, 1, 1, 1), + dark = spine.Color.newWith(0, 0, 0, 0) + } self.clipper = spine.SkeletonClipping.new() return self end @@ -119,6 +130,8 @@ function spine.Skeleton:updateWorldTransform() local premultipliedAlpha = self.premultipliedAlpha self.batches = 0 + + if (self.vertexEffect) then self.vertexEffect:beginEffect(self) end -- Remove old drawing group, we will start anew if self.drawingGroup then self.drawingGroup:removeSelf() end @@ -217,6 +230,7 @@ function spine.Skeleton:updateWorldTransform() end self.clipper:clipEnd2() + if (self.vertexEffect) then self.vertexEffect:endEffect() end end function spine.Skeleton:flush(groupVertices, groupUvs, groupIndices, texture, color, blendMode, drawingGroup) @@ -250,13 +264,31 @@ function spine.Skeleton:batch(vertices, uvs, numVertices, indices, groupVertices i = 1 local vertexStart = #groupVertices + 1 local vertexEnd = vertexStart + numVertices * 2 - while vertexStart < vertexEnd do - groupVertices[vertexStart] = vertices[i] - groupVertices[vertexStart+1] = vertices[i+1] - groupUvs[vertexStart] = uvs[i] - groupUvs[vertexStart+1] = uvs[i+1] - vertexStart = vertexStart + 2 - i = i + 2 + if (self.vertexEffect) then + local effect = self.vertexEffect + local vertex = self.tempVertex + while vertexStart < vertexEnd do + vertex.x = vertices[i] + vertex.y = vertices[i+1] + vertex.u = uvs[i] + vertex.v = uvs[i+1] + effect:transform(vertex); + groupVertices[vertexStart] = vertex.x + groupVertices[vertexStart+1] = vertex.y + groupUvs[vertexStart] = vertex.u + groupUvs[vertexStart+1] = vertex.v + vertexStart = vertexStart + 2 + i = i + 2 + end + else + while vertexStart < vertexEnd do + groupVertices[vertexStart] = vertices[i] + groupVertices[vertexStart+1] = vertices[i+1] + groupUvs[vertexStart] = uvs[i] + groupUvs[vertexStart+1] = uvs[i+1] + vertexStart = vertexStart + 2 + i = i + 2 + end end end diff --git a/spine-love/main.lua b/spine-love/main.lua index 17e09332e..abb89a797 100644 --- a/spine-love/main.lua +++ b/spine-love/main.lua @@ -32,6 +32,8 @@ local spine = require "spine-love.spine" local skeletons = {} local activeSkeleton = 1 +local swirl = spine.SwirlEffect.new(400) +local swirlTime = 0 function loadSkeleton (jsonFile, atlasFile, animation, skin, scale, x, y) local loader = function (path) return love.graphics.newImage("data/" .. path) end @@ -53,13 +55,19 @@ function loadSkeleton (jsonFile, atlasFile, animation, skin, scale, x, y) local stateData = spine.AnimationStateData.new(skeletonData) local state = spine.AnimationState.new(stateData) state:setAnimationByName(0, animation, true) - if (jsonFile == "spineboy") then + if (jsonFile == "spineboy-ess") then stateData:setMix("walk", "jump", 0.5) stateData:setMix("jump", "run", 0.5) state:addAnimationByName(0, "jump", false, 3) state:addAnimationByName(0, "run", true, 0) end + if (jsonFile == "raptor-pro") then + swirl.centerY = -200 + skeleton.vertexEffect = swirl + -- skeleton.vertexEffect = spine.JitterEffect.new(10, 10) + end + -- set some event callbacks state.onStart = function (entry) print(entry.trackIndex.." start: "..entry.animation.name) @@ -88,6 +96,7 @@ end function love.load(arg) if arg[#arg] == "-debug" then require("mobdebug").start() end + skeletonRenderer = spine.SkeletonRenderer.new(true) table.insert(skeletons, loadSkeleton("coin-pro", "coin", "rotate", nil, 0.5, 400, 500)) table.insert(skeletons, loadSkeleton("spineboy-ess", "spineboy", "walk", nil, 0.5, 400, 500)) table.insert(skeletons, loadSkeleton("raptor-pro", "raptor", "walk", nil, 0.3, 400, 500)) @@ -95,7 +104,6 @@ function love.load(arg) table.insert(skeletons, loadSkeleton("tank-pro", "tank", "drive", nil, 0.2, 600, 500)) table.insert(skeletons, loadSkeleton("vine-pro", "vine", "grow", nil, 0.3, 400, 500)) table.insert(skeletons, loadSkeleton("stretchyman-pro", "stretchyman", "sneak", nil, 0.3, 200, 500)) - skeletonRenderer = spine.SkeletonRenderer.new(true) end function love.update (delta) @@ -105,12 +113,25 @@ function love.update (delta) state:update(delta) state:apply(skeleton) skeleton:updateWorldTransform() + + if (skeleton.vertexEffect) then + skeletonRenderer.vertexEffect = skeleton.vertexEffect + if (skeleton.vertexEffect == swirl) then + swirlTime = swirlTime + delta + local percent = swirlTime % 2 + if (percent > 1) then percent = 1 - (percent - 1) end + swirl.angle = spine.Interpolation.apply(spine.Interpolation.pow2, -60, 60, percent) + end + else + skeletonRenderer.vertexEffect = nil + end end function love.draw () love.graphics.setBackgroundColor(128, 128, 128, 255) love.graphics.setColor(255, 255, 255) local skeleton = skeletons[activeSkeleton].skeleton + skeletonRenderer:draw(skeleton) end diff --git a/spine-love/spine-love/spine.lua b/spine-love/spine-love/spine.lua index 13304cea7..c8d684a1c 100644 --- a/spine-love/spine-love/spine.lua +++ b/spine-love/spine-love/spine.lua @@ -67,6 +67,9 @@ spine.AtlasAttachmentLoader = require "spine-lua.AtlasAttachmentLoader" spine.Color = require "spine-lua.Color" spine.Triangulator = require "spine-lua.Triangulator" spine.SkeletonClipping = require "spine-lua.SkeletonClipping" +spine.JitterEffect = require "spine-lua.vertexeffects.JitterEffect" +spine.SwirlEffect = require "spine-lua.vertexeffects.SwirlEffect" +spine.Interpolation = require "spine-lua.Interpolation" spine.utils.readFile = function (fileName, base) local path = fileName @@ -139,7 +142,15 @@ function PolygonBatcher.new(vertexCount, useTwoColorTint) vertex = { 0, 0, 0, 0, 0, 0, 0, 0 }, indices = nil, useTwoColorTint = useTwoColorTint, - twoColorTintShader = twoColorTintShader + twoColorTintShader = twoColorTintShader, + tempVertex = { + x = 0, + y = 0, + u = 0, + v = 0, + light = spine.Color.newWith(1, 1, 1, 1), + dark = spine.Color.newWith(0, 0, 0, 0) + } } local indices = {} @@ -163,7 +174,7 @@ function PolygonBatcher:begin () self.drawCalls = 0 end -function PolygonBatcher:draw (texture, vertices, uvs, numVertices, indices, color, darkColor) +function PolygonBatcher:draw (texture, vertices, uvs, numVertices, indices, color, darkColor, vertexEffect) local numIndices = #indices local mesh = self.mesh @@ -207,14 +218,44 @@ function PolygonBatcher:draw (texture, vertices, uvs, numVertices, indices, colo end local v = 1 - while vertexStart < vertexEnd do - vertex[1] = vertices[v] - vertex[2] = vertices[v + 1] - vertex[3] = uvs[v] - vertex[4] = uvs[v + 1] - mesh:setVertex(vertexStart, vertex) - vertexStart = vertexStart + 1 - v = v + 2 + if (vertexEffect) then + local tempVertex = self.tempVertex + while vertexStart < vertexEnd do + tempVertex.x = vertices[v] + tempVertex.y = vertices[v + 1] + tempVertex.u = uvs[v] + tempVertex.v = uvs[v + 1] + tempVertex.light:setFrom(color) + tempVertex.dark:setFrom(darkColor) + vertexEffect:transform(tempVertex) + vertex[1] = tempVertex.x + vertex[2] = tempVertex.y + vertex[3] = tempVertex.u + vertex[4] = tempVertex.v + vertex[5] = tempVertex.light.r * 255 + vertex[6] = tempVertex.light.g * 255 + vertex[7] = tempVertex.light.b * 255 + vertex[8] = tempVertex.light.a * 255 + if (self.useTwoColorTint) then + vertex[9] = tempVertex.dark.r * 255 + vertex[10] = tempVertex.dark.g * 255 + vertex[11] = tempVertex.dark.b * 255 + vertex[12] = tempVertex.dark.a * 255 + end + mesh:setVertex(vertexStart, vertex) + vertexStart = vertexStart + 1 + v = v + 2 + end + else + while vertexStart < vertexEnd do + vertex[1] = vertices[v] + vertex[2] = vertices[v + 1] + vertex[3] = uvs[v] + vertex[4] = uvs[v + 1] + mesh:setVertex(vertexStart, vertex) + vertexStart = vertexStart + 1 + v = v + 2 + end end self.verticesLength = self.verticesLength + numVertices end @@ -255,7 +296,8 @@ function SkeletonRenderer.new (useTwoColorTint) batcher = PolygonBatcher.new(3 * 500, useTwoColorTint), premultipliedAlpha = false, useTwoColorTint = useTwoColorTint, - clipper = spine.SkeletonClipping.new() + clipper = spine.SkeletonClipping.new(), + vertexEffect = nil } setmetatable(self, SkeletonRenderer) @@ -270,6 +312,8 @@ function SkeletonRenderer:draw (skeleton) local batcher = self.batcher local premultipliedAlpha = self.premultipliedAlpha + if (self.vertexEffect) then self.vertexEffect:beginEffect(skeleton) end + local lastLoveBlendMode = love.graphics.getBlendMode() love.graphics.setBlendMode("alpha") local lastBlendMode = spine.BlendMode.normal @@ -342,7 +386,7 @@ function SkeletonRenderer:draw (skeleton) indices = self.clipper.clippedTriangles end - batcher:draw(texture, vertices, uvs, numVertices, indices, color, dark) + batcher:draw(texture, vertices, uvs, numVertices, indices, color, dark, self.vertexEffect) end self.clipper:clipEnd(slot) @@ -352,6 +396,7 @@ function SkeletonRenderer:draw (skeleton) batcher:stop() love.graphics.setBlendMode(lastLoveBlendMode) self.clipper:clipEnd2() + if (self.vertexEffect) then self.vertexEffect:endEffect(skeleton) end end spine.PolygonBatcher = PolygonBatcher diff --git a/spine-lua/Interpolation.lua b/spine-lua/Interpolation.lua new file mode 100644 index 000000000..75850d895 --- /dev/null +++ b/spine-lua/Interpolation.lua @@ -0,0 +1,48 @@ +------------------------------------------------------------------------------- +-- 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 interpolation = {} + +local math_pow = math.pow + +function interpolation.apply (func, start, _end, a) + return start + (_end - start) * func(a) +end + +function interpolation.pow2(a) + if (a <= 0.5) then return math_pow(a * 2, 2) / 2 end + return math_pow((a - 1) * 2, 2) / -2 + 1 +end + +function interpolation.pow2out(a) + return math_pow(a - 1, 2) * -1 + 1 +end + +return interpolation \ No newline at end of file diff --git a/spine-lua/utils.lua b/spine-lua/utils.lua index 3e72c1499..76e97c833 100644 --- a/spine-lua/utils.lua +++ b/spine-lua/utils.lua @@ -30,6 +30,9 @@ local utils = {} +local math_sqrt = math.sqrt +local math_random = math.random + utils.degRad = math.pi / 180 function tablePrint (tt, indent, done) @@ -153,4 +156,15 @@ function utils.mod(a, b) end end +function utils.randomTriangular(min, max) + return utils.randomTriangularWith(min, max, (min + max) * 0.5) +end + +function utils.randomTriangularWith(min, max, mode) + local u = math.random() + local d = max - min + if (u <= (mode - min) / d) then return min + math_sqrt(u * d * (mode - min)) end + return max - math_sqrt((1 - u) * d * (max - mode)) +end + return utils diff --git a/spine-lua/vertexeffects/JitterEffect.lua b/spine-lua/vertexeffects/JitterEffect.lua new file mode 100644 index 000000000..a62dbace3 --- /dev/null +++ b/spine-lua/vertexeffects/JitterEffect.lua @@ -0,0 +1,64 @@ +------------------------------------------------------------------------------- +-- 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 utils = require "spine-lua.utils" + +local setmetatable = setmetatable +local math_min = math.min +local math_max = math.max +local ipairs = ipairs +local table_insert = table.insert +local table_remove = table.remove + +local JitterEffect = {} +JitterEffect.__index = JitterEffect + +function JitterEffect.new (jitterX, jitterY) + local self = { + jitterX = jitterX, + jitterY = jitterY + } + setmetatable(self, JitterEffect) + + return self +end + +function JitterEffect:beginEffect (skeleton) +end + +function JitterEffect:transform (vertex) + vertex.x = vertex.x + utils.randomTriangular(-self.jitterX, self.jitterY) + vertex.y = vertex.y + utils.randomTriangular(-self.jitterX, self.jitterY) +end + +function JitterEffect:endEffect () +end + +return JitterEffect \ No newline at end of file diff --git a/spine-lua/vertexeffects/SwirlEffect.lua b/spine-lua/vertexeffects/SwirlEffect.lua new file mode 100644 index 000000000..3312d14c3 --- /dev/null +++ b/spine-lua/vertexeffects/SwirlEffect.lua @@ -0,0 +1,85 @@ +------------------------------------------------------------------------------- +-- 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 utils = require "spine-lua.utils" +local interpolation = require "spine-lua.Interpolation" + +local setmetatable = setmetatable +local math_min = math.min +local math_max = math.max +local ipairs = ipairs +local table_insert = table.insert +local table_remove = table.remove +local utils_deg_rad = utils.degRad +local math_sqrt = math.sqrt +local math_sin = math.sin +local math_cos = math.cos + +local SwirlEffect = {} +SwirlEffect.__index = SwirlEffect + +function SwirlEffect.new (radius) + local self = { + worldX = 0, + worldY = 0, + centerX = 0, + centerY = 0, + radius = radius, + angle = 0, + interpolation = interpolation.pow2 + } + setmetatable(self, SwirlEffect) + + return self +end + +function SwirlEffect:beginEffect (skeleton) + self.worldX = skeleton.x + self.centerX + self.worldY = skeleton.y + self.centerY + self.angleRad = self.angle * utils_deg_rad +end + +function SwirlEffect:transform (vertex) + local x = vertex.x - self.worldX + local y = vertex.y - self.worldY + local dist = math_sqrt(x * x + y * y) + if (dist < self.radius) then + local theta = interpolation.apply(self.interpolation, 0, self.angleRad, (self.radius - dist) / self.radius) + local cos = math_cos(theta) + local sin = math_sin(theta) + vertex.x = cos * x - sin * y + self.worldX + vertex.y = sin * x + cos * y + self.worldY + end +end + +function SwirlEffect:endEffect () +end + +return SwirlEffect \ No newline at end of file