- 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
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 parseTexturePacker(data, textureAtlas) {
let atlas = {};
data.frames.forEach((frame) => {
// fix wrongly formatted JSON (e.g. last dummy object in ShoeBox)
if (frame.hasOwnProperty("filename")) {
// Source coordinates
let s = frame.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[frame.filename] = {
name : frame.filename, // frame name
texture : data.meta.image || "default", // the source texture
offset : new Vector2d(s.x, s.y),
anchorPoint : (hasTextureAnchorPoint) ? new Vector2d(originX / s.w, originY / s.h) : null,
trimmed : trimmed,
trim : trim,
width : s.w,
height : s.h,
angle : (frame.rotated === true) ? -ETA : 0
};
textureAtlas.addUVs(atlas, frame.filename, data.meta.size.w, data.meta.size.h);
}
});
return atlas;
}