- 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
import { ETA } from "../../../math/math.js";
import Vector2d from "../../../math/vector2.js";
/**
* parse the given data and return a corresponding atlas
* @param {Object} data - atlas data information. See {@link loader.getJSON}
* @param {TextureAtlas} textureAtlas - the texture atlas class calling the parser
* @returns {Object} the corresponding Atlas
* @ignore
*/
export function parseAseprite(data, textureAtlas) {
let atlas = {};
const frames = data.frames;
for (const name in frames) {
let frame = frames[name].frame;
let trimmed = !!frame.trimmed;
let trim;
if (trimmed) {
trim = {
x : frame.spriteSourceSize.x,
y : frame.spriteSourceSize.y,
w : frame.spriteSourceSize.w,
h : frame.spriteSourceSize.h
};
}
let originX, originY;
// Pixel-based offset origin from the top-left of the source frame
let hasTextureAnchorPoint = (frame.sourceSize && frame.pivot);
if (hasTextureAnchorPoint) {
originX = (frame.sourceSize.w * frame.pivot.x) - ((trimmed) ? trim.x : 0);
originY = (frame.sourceSize.h * frame.pivot.y) - ((trimmed) ? trim.y : 0);
}
atlas[name] = {
name : name, // frame name
texture : data.meta.image || "default", // the source texture
offset : new Vector2d(frame.x, frame.y),
anchorPoint : (hasTextureAnchorPoint) ? new Vector2d(originX / frame.w, originY / frame.h) : null,
trimmed : trimmed,
trim : trim,
width : frame.w,
height : frame.h,
angle : (frame.rotated === true) ? -ETA : 0
};
textureAtlas.addUVs(atlas, name, data.meta.size.w, data.meta.size.h);
}
const anims = {};
for (const name in data.meta.frameTags) {
const anim = data.meta.frameTags[name];
// aseprite provide a range from [from] to [to], so build the corresponding index array
const indexArray = Array.from({ length: anim.to - anim.from + 1 }, (_, i) => anim.from + i);
anims[name] = {
name: anim.name,
index: indexArray,
// aseprite provide animation speed between frame, melonJS expect total duration for a given animation
speed: 10 * (indexArray.length - 1),
// only "forward" is supported for now
direction: anim.direction
};
}
atlas.anims = anims;
return atlas;
}