* [Lua] Adding Spine atlas parser

* [Corona] Adding ability to load atlas files
* [Corona] Added example of using atlas

# Documentation:

## Lua runtime: `spine.Atlas.parse`

`Atlas.lua` introduces `spine.Atlas.parse(atlasPath, baseDir)` funciton. This function can be use across Lua runtimes to implement their their own image sheet bindings. It will return Array of `Page`s or `nil` if error.

### `Page` properties:
* `name` - filename of the page (`"raptor.png"`)
* `size` - dimmensions of original image (`[1022,1022]`)
* `format` - texture format (`"RGBA8888"`)
* `filter` - filter for texture scalin up and down (`["Linear","Linear"]`)
* `wrap` - wrap modes (`"none"`)
* `regions` - Array of `Region`s on this page

### `Region` properties:
* `name` - region name (`"torso"`)
* `rotate` - is image rotated in texture (`true`)
* `xy` - image frame position on texture (`[610,647]`)
* `size` - image frame size on texture (`[54,91]`)
* `splits` - (optional)
* `pad` - (optional)
* `orig` - original image size (`[54,91]`)
* `offset` - offset if image was trimmed (`[0,0]`)
* `index` - `-1`

## Corona runtime: `spine.GetAtlasSprites`

`spine.lua` introduces `spine.GetAtlasSprites(atlasName, baseDir)`, which returns `Sprites` table. This funciton would parse atlas, locate atlas textures, load them to Corona's image sheets and create convinience bindings and functions. User can use convenience functions, or write own wrappers.

### `Sprites` table
#### Convenience functions:
* `ATLAS_HELPER_createImage` - can be assigned to `skeleton.createImage`
* `ATLAS_HELPER_createMesh` - can be assigned to `skeleton.createMesh`
* `ATLAS_HELPER_setupSkeleton` - use this on skeleton to assing both creation functions (`sprites.setupSkeleton(skeleton)`)

### Region name bindings:
All other elements of `Sprites` table would contain binding `sprites["region_name"] = sheetReference`, where each `sheetReference` contains following elements:

* `frame` - number of frame in image sheet (`42`)
* `region` - reference to `Region` table
* `page` -  reference to `Page` table. `Page` table would also get new property - `sheet`, reference to Corona's image sheet where sprites can be accessed.

As result, to create image for `attachment` code would look something like `display.newImage( sprites[attachment.name].page.sheet, sprites[attachment.name].frame )`. For more rubust example see `ATLAS_HELPER_createImage` in `spine-corona/spine.lua`.

# Known Issues:

* Corona would not work with padded/cropped atlases.
* Possible (but unprobable) name collision with region names and `ATLAS_HELPER_*` functions.
This commit is contained in:
Vlad Shcherban 2016-06-23 11:07:12 -04:00
parent a98f76f13f
commit 559fcbfcb9
11 changed files with 5595 additions and 0 deletions

View File

@ -0,0 +1,194 @@
spineboy.png
format: RGBA8888
filter: Linear,Linear
repeat: none
eye_indifferent
rotate: true
xy: 389, 5
size: 56, 53
orig: 56, 53
offset: 0, 0
index: -1
eye_surprised
rotate: false
xy: 580, 34
size: 56, 53
orig: 56, 53
offset: 0, 0
index: -1
front_bracer
rotate: false
xy: 732, 85
size: 35, 48
orig: 35, 48
offset: 0, 0
index: -1
front_fist_closed
rotate: false
xy: 556, 91
size: 45, 49
orig: 45, 49
offset: 0, 0
index: -1
front_fist_open
rotate: false
xy: 668, 32
size: 52, 52
orig: 52, 52
offset: 0, 0
index: -1
front_foot
rotate: false
xy: 924, 201
size: 76, 41
orig: 76, 41
offset: 0, 0
index: -1
front_foot_bend1
rotate: false
xy: 845, 200
size: 77, 42
orig: 77, 42
offset: 0, 0
index: -1
front_foot_bend2
rotate: false
xy: 778, 186
size: 65, 56
orig: 65, 56
offset: 0, 0
index: -1
front_shin
rotate: true
xy: 444, 91
size: 49, 110
orig: 49, 110
offset: 0, 0
index: -1
front_thigh
rotate: true
xy: 603, 89
size: 29, 67
orig: 29, 67
offset: 0, 0
index: -1
front_upper_arm
rotate: true
xy: 672, 86
size: 32, 58
orig: 32, 58
offset: 0, 0
index: -1
goggles
rotate: false
xy: 444, 142
size: 157, 100
orig: 157, 100
offset: 0, 0
index: -1
gun
rotate: false
xy: 603, 120
size: 126, 122
orig: 126, 122
offset: 0, 0
index: -1
head
rotate: false
xy: 279, 63
size: 163, 179
orig: 163, 179
offset: 0, 0
index: -1
mouth_grind
rotate: false
xy: 845, 163
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
mouth_oooo
rotate: false
xy: 842, 126
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
mouth_smile
rotate: false
xy: 769, 97
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
muzzle
rotate: false
xy: 2, 2
size: 275, 240
orig: 277, 240
offset: 0, 0
index: -1
neck
rotate: false
xy: 903, 173
size: 22, 25
orig: 22, 25
offset: 0, 0
index: -1
rear_bracer
rotate: false
xy: 722, 40
size: 34, 43
orig: 34, 43
offset: 0, 0
index: -1
rear_foot
rotate: false
xy: 444, 11
size: 68, 36
orig: 68, 36
offset: 0, 0
index: -1
rear_foot_bend1
rotate: false
xy: 444, 49
size: 70, 40
orig: 70, 40
offset: 0, 0
index: -1
rear_foot_bend2
rotate: false
xy: 778, 134
size: 62, 50
orig: 62, 50
offset: 0, 0
index: -1
rear_shin
rotate: false
xy: 731, 135
size: 45, 107
orig: 45, 107
offset: 0, 0
index: -1
rear_thigh
rotate: true
xy: 516, 50
size: 39, 62
orig: 39, 62
offset: 0, 0
index: -1
rear_upper_arm
rotate: false
xy: 638, 35
size: 28, 52
orig: 28, 52
offset: 0, 0
index: -1
torso
rotate: true
xy: 279, 2
size: 59, 108
orig: 59, 108
offset: 0, 0
index: -1

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,87 @@
-- This example shows simple usage of displaying a skeleton with queued animations.
local spine = require "spine-corona.spine"
local json = spine.SkeletonJson.new()
json.scale = 0.6
local skeletonData = json:readSkeletonDataFile("examples/spineboy-atlas/spineboy.json")
local skeleton = spine.Skeleton.new(skeletonData)
local sprites = spine.GetAtlasSprites( "examples/spineboy-atlas/spineboy.atlas" )
sprites.ATLAS_HELPER_setup(skeleton)
skeleton.group.x = display.contentWidth * 0.75
skeleton.group.y = display.contentHeight * 0.9
skeleton.flipX = false
skeleton.flipY = false
skeleton.debug = true -- Omit or set to false to not draw debug lines on top of the images.
skeleton.debugAabb = true
skeleton:setToSetupPose()
local bounds = spine.SkeletonBounds.new()
-- AnimationStateData defines crossfade durations between animations.
local stateData = spine.AnimationStateData.new(skeletonData)
stateData:setMix("walk", "jump", 0.2)
stateData:setMix("jump", "run", 0.2)
-- AnimationState has a queue of animations and can apply them with crossfading.
local state = spine.AnimationState.new(stateData)
-- state:setAnimationByName(0, "test")
state:setAnimationByName(0, "walk", true)
state:addAnimationByName(0, "jump", false, 3)
state:addAnimationByName(0, "run", true, 0)
state.onStart = function (trackIndex)
-- print(trackIndex.." start: "..state:getCurrent(trackIndex).animation.name)
end
state.onEnd = function (trackIndex)
-- print(trackIndex.." end: "..state:getCurrent(trackIndex).animation.name)
end
state.onComplete = function (trackIndex, loopCount)
-- print(trackIndex.." complete: "..state:getCurrent(trackIndex).animation.name..", "..loopCount)
end
state.onEvent = function (trackIndex, event)
-- print(trackIndex.." event: "..state:getCurrent(trackIndex).animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'")
end
local lastTime = 0
local touchX = 999999
local touchY = 999999
local headSlot = skeleton:findSlot("head")
Runtime:addEventListener("enterFrame", function (event)
-- Compute time in seconds since last frame.
local currentTime = event.time / 1000
local delta = currentTime - lastTime
lastTime = currentTime
-- Bounding box hit detection.
bounds:update(skeleton, true)
if bounds:containsPoint(touchX, touchY) then
headSlot.g = 0;
headSlot.b = 0;
else
headSlot.g = 1;
headSlot.b = 1;
end
-- Update the state with the delta time, apply it, and update the world transforms.
state:update(delta)
state:apply(skeleton)
skeleton:updateWorldTransform()
end)
Runtime:addEventListener("touch", function (event)
if event.phase ~= "ended" and event.phase ~= "cancelled" then
-- Make the coordinates relative to the skeleton's group.
touchX = event.x - skeleton.group.x
touchY = skeleton.group.y - event.y
else
touchX = 999999
touchY = 999999
end
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

View File

@ -1,4 +1,5 @@
require "examples.spineboy.spineboy"
-- require "examples.spineboy-atlas.spineboy"
-- require "examples.spineboy.spineboy-mesh"
-- require "examples.goblins.goblins"
-- require "examples.dragon.dragon"

View File

@ -54,6 +54,7 @@ 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.Atlas = require "spine-lua.Atlas"
spine.utils.readFile = function (fileName, base)
if not base then base = system.ResourceDirectory end
@ -327,4 +328,91 @@ function spine.Skeleton.new (skeletonData, group)
return self
end
function spine.GetAtlasSprites ( name, location )
local dir = name
if dir:match(".-/.-") then
dir = string.gsub(dir, "(.*/)(.*)", "%1")
end
if string.sub(dir, -1) ~= "/" then
dir = dir .. "/"
end
local pages = spine.Atlas.parse( name, location )
local sprites = {}
for _,page in ipairs(pages) do
local options = { frames={} }
if page.size then
options.sheetContentWidth, options.sheetContentHeight = unpack( page.size )
end
for i,region in ipairs(page.regions) do
sprites[region.name] = { frame=i, region=region, page=page }
local frame = { }
frame.x, frame.y = unpack(region.xy)
frame.width, frame.height = unpack(region.size)
if region.rotate then
frame.width, frame.height = frame.height, frame.width
end
if region.offset[1] ~= 0 or region.offset[2] ~= 0 or region.size[1] ~= region.orig[1] or region.size[2] ~= region.orig[2] then
print("WARNING: Corona currently does not srtipping whitespaces in Spine atlases (atlas: " .. name .. ", sprite: " .. region.name .. ")")
end
table.insert( options.frames, frame )
end
local oldMag, oldMin = display.getDefault( "magTextureFilter" ), display.getDefault( "minTextureFilter" )
local newMag, newMin = "linear", "linear"
if string.find("Nearest", page.filter[1]) then
newMin = "nearest"
end
if string.find("Nearest", page.filter[2]) then
newMag = "nearest"
end
display.setDefault( "magTextureFilter", newMag )
display.setDefault( "minTextureFilter", newMin )
page.sheet = graphics.newImageSheet( dir..page.name, options )
display.setDefault( "magTextureFilter", oldMag )
display.setDefault( "minTextureFilter", oldMin )
end
function sprites.ATLAS_HELPER_createImage(_, attachment)
local sprite = sprites[attachment.name]
local obj = display.newRect( 0, 0, 100, 100 )
if sprite then
obj.fill = { type="image", sheet=sprite.page.sheet, frame=sprite.frame}
if sprite.region.rotate then
local fill = obj.fill
if fill then
fill.rotation = 90
end
end
end
return obj
end
function sprites.ATLAS_HELPER_createMesh(_, attachment, meshParameters)
local sprite = sprites[attachment.name]
local obj = display.newMesh(meshParameters)
if sprite then
obj.fill = { type="image", sheet=sprite.page.sheet, frame=sprite.frame}
if sprite.region.rotate then
local fill = obj.fill
if fill then
fill.rotation = 90
end
end
end
return obj
end
function sprites.ATLAS_HELPER_setup(skeleton)
skeleton.createImage = sprites.ATLAS_HELPER_createImage
skeleton.createMesh = sprites.ATLAS_HELPER_createMesh
end
return sprites
end
return spine

View File

@ -0,0 +1,194 @@
spineboy.png
format: RGBA8888
filter: Linear,Linear
repeat: none
eye_indifferent
rotate: true
xy: 389, 5
size: 56, 53
orig: 56, 53
offset: 0, 0
index: -1
eye_surprised
rotate: false
xy: 580, 34
size: 56, 53
orig: 56, 53
offset: 0, 0
index: -1
front_bracer
rotate: false
xy: 732, 85
size: 35, 48
orig: 35, 48
offset: 0, 0
index: -1
front_fist_closed
rotate: false
xy: 556, 91
size: 45, 49
orig: 45, 49
offset: 0, 0
index: -1
front_fist_open
rotate: false
xy: 668, 32
size: 52, 52
orig: 52, 52
offset: 0, 0
index: -1
front_foot
rotate: false
xy: 924, 201
size: 76, 41
orig: 76, 41
offset: 0, 0
index: -1
front_foot_bend1
rotate: false
xy: 845, 200
size: 77, 42
orig: 77, 42
offset: 0, 0
index: -1
front_foot_bend2
rotate: false
xy: 778, 186
size: 65, 56
orig: 65, 56
offset: 0, 0
index: -1
front_shin
rotate: true
xy: 444, 91
size: 49, 110
orig: 49, 110
offset: 0, 0
index: -1
front_thigh
rotate: true
xy: 603, 89
size: 29, 67
orig: 29, 67
offset: 0, 0
index: -1
front_upper_arm
rotate: true
xy: 672, 86
size: 32, 58
orig: 32, 58
offset: 0, 0
index: -1
goggles
rotate: false
xy: 444, 142
size: 157, 100
orig: 157, 100
offset: 0, 0
index: -1
gun
rotate: false
xy: 603, 120
size: 126, 122
orig: 126, 122
offset: 0, 0
index: -1
head
rotate: false
xy: 279, 63
size: 163, 179
orig: 163, 179
offset: 0, 0
index: -1
mouth_grind
rotate: false
xy: 845, 163
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
mouth_oooo
rotate: false
xy: 842, 126
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
mouth_smile
rotate: false
xy: 769, 97
size: 56, 35
orig: 56, 35
offset: 0, 0
index: -1
muzzle
rotate: false
xy: 2, 2
size: 275, 240
orig: 277, 240
offset: 0, 0
index: -1
neck
rotate: false
xy: 903, 173
size: 22, 25
orig: 22, 25
offset: 0, 0
index: -1
rear_bracer
rotate: false
xy: 722, 40
size: 34, 43
orig: 34, 43
offset: 0, 0
index: -1
rear_foot
rotate: false
xy: 444, 11
size: 68, 36
orig: 68, 36
offset: 0, 0
index: -1
rear_foot_bend1
rotate: false
xy: 444, 49
size: 70, 40
orig: 70, 40
offset: 0, 0
index: -1
rear_foot_bend2
rotate: false
xy: 778, 134
size: 62, 50
orig: 62, 50
offset: 0, 0
index: -1
rear_shin
rotate: false
xy: 731, 135
size: 45, 107
orig: 45, 107
offset: 0, 0
index: -1
rear_thigh
rotate: true
xy: 516, 50
size: 39, 62
orig: 39, 62
offset: 0, 0
index: -1
rear_upper_arm
rotate: false
xy: 638, 35
size: 28, 52
orig: 28, 52
offset: 0, 0
index: -1
torso
rotate: true
xy: 279, 2
size: 59, 108
orig: 59, 108
offset: 0, 0
index: -1

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,87 @@
-- This example shows simple usage of displaying a skeleton with queued animations.
local spine = require "spine-corona.spine"
local atlasLoader = require "spine-atlas-loader"
local sprites = atlasLoader.load( "spineboy.atlas", "examples/spineboy-atlas/" )
local json = spine.SkeletonJson.new()
json.scale = 0.6
local skeletonData = json:readSkeletonDataFile("examples/spineboy-atlas/spineboy.json")
local skeleton = spine.Skeleton.new(skeletonData)
sprites.setupImageFunctions(skeleton)
skeleton.group.x = display.contentWidth * 0.75
skeleton.group.y = display.contentHeight * 0.9
skeleton.flipX = false
skeleton.flipY = false
skeleton.debug = true -- Omit or set to false to not draw debug lines on top of the images.
skeleton.debugAabb = true
skeleton:setToSetupPose()
local bounds = spine.SkeletonBounds.new()
-- AnimationStateData defines crossfade durations between animations.
local stateData = spine.AnimationStateData.new(skeletonData)
stateData:setMix("walk", "jump", 0.2)
stateData:setMix("jump", "run", 0.2)
-- AnimationState has a queue of animations and can apply them with crossfading.
local state = spine.AnimationState.new(stateData)
-- state:setAnimationByName(0, "test")
state:setAnimationByName(0, "walk", true)
state:addAnimationByName(0, "jump", false, 3)
state:addAnimationByName(0, "run", true, 0)
state.onStart = function (trackIndex)
print(trackIndex.." start: "..state:getCurrent(trackIndex).animation.name)
end
state.onEnd = function (trackIndex)
print(trackIndex.." end: "..state:getCurrent(trackIndex).animation.name)
end
state.onComplete = function (trackIndex, loopCount)
print(trackIndex.." complete: "..state:getCurrent(trackIndex).animation.name..", "..loopCount)
end
state.onEvent = function (trackIndex, event)
print(trackIndex.." event: "..state:getCurrent(trackIndex).animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'")
end
local lastTime = 0
local touchX = 999999
local touchY = 999999
local headSlot = skeleton:findSlot("head")
Runtime:addEventListener("enterFrame", function (event)
-- Compute time in seconds since last frame.
local currentTime = as * event.time / 1000
local delta = currentTime - lastTime
lastTime = currentTime
-- Bounding box hit detection.
bounds:update(skeleton, true)
if bounds:containsPoint(touchX, touchY) then
headSlot.g = 0;
headSlot.b = 0;
else
headSlot.g = 1;
headSlot.b = 1;
end
-- Update the state with the delta time, apply it, and update the world transforms.
state:update(delta)
state:apply(skeleton)
skeleton:updateWorldTransform()
end)
Runtime:addEventListener("touch", function (event)
if event.phase ~= "ended" and event.phase ~= "cancelled" then
-- Make the coordinates relative to the skeleton's group.
touchX = event.x - skeleton.group.x
touchY = skeleton.group.y - event.y
else
touchX = 999999
touchY = 999999
end
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

106
spine-lua/Atlas.lua Normal file
View File

@ -0,0 +1,106 @@
-------------------------------------------------------------------------------
-- Spine Runtimes Software License
-- Version 2.3
--
-- Copyright (c) 2013-2015, 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 (the "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 otherwise create derivative works, improvements of the
-- Software or develop new applications using the Software 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; LOSS OF USE, DATA, OR PROFITS;
-- OR BUSINESS INTERRUPTION) 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 Atlas = {}
function Atlas.parse(atlasPath, atlasBase)
local function parseIntTuple4( l )
local a,b,c,d = string.match( l , " ? ?%a+: ([+-]?%d+), ?([+-]?%d+), ?([+-]?%d+), ?([+-]?%d+)" )
local a,b,c,d = tonumber( a ), tonumber( b ), tonumber( c ), tonumber( d )
return a and b and c and d and {a, b, c ,d}
end
local function parseIntTuple2( l )
local a,b = string.match( l , " ? ?%a+: ([+-]?%d+), ?([+-]?%d+)" )
local a,b = tonumber( a ), tonumber( b )
return a and b and {a, b}
end
if not atlasPath then
error("Error: " .. atlasPath .. ".atlas" .. " doesn't exist!")
return nil
end
local atlasLines = spine.utils.readFile( atlasPath, atlasBase )
if not atlasLines then
error("Error: " .. atlasPath .. ".atlas" .. " unable to read!")
return nil
end
local pages = {}
local it = string.gmatch(atlasLines, "(.-)\r?\n") -- iterate over lines
for l in it do
if #l == 0 then
l = it()
if l then
local page = { name = l }
l = it()
page.size = parseIntTuple2( l )
if page.size then
l = it()
end
page.format = string.match( l, "%a+: (.+)" )
page.filter = {string.match( it(), "%a+: (.+),(.+)" )}
page.wrap = string.match( it(), "%a+: (.+)" )
page.regions = {}
table.insert( pages, page )
else
break
end
else
local region = {name = l}
region.rotate = string.match( it(), "%a+: (.+)" ) == "true"
region.xy = parseIntTuple2( it() )
region.size = parseIntTuple2( it() )
l = it()
region.splits = parseIntTuple4(l)
if region.splits then
l = it()
region.pad = parseIntTuple4(l)
if region.pad then
l = it()
end
end
region.orig = parseIntTuple2( l )
region.offset = parseIntTuple2( it() )
region.index = tonumber( string.match( it() , "%a+: ([+-]?%d+)" ) )
table.insert( pages[#pages].regions, region )
end
end
return pages
end
return Atlas