-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspine-json.js
More file actions
49 lines (44 loc) · 1.68 KB
/
spine-json.js
File metadata and controls
49 lines (44 loc) · 1.68 KB
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
const json2patch = (buffer, data, atlas) => {
const skelBin = new SkeletonBinary(buffer, atlas, 1);
skelBin.buildJson();
let input = JSON.parse(data);
atlas = atlas[0];
// revert back to original skeleton data from skel
input.skeleton = skelBin.json.skeleton;
// remove spine version from json
if(input.skeleton.spine == '3.8.87'){
console.log(`LOG: Spine version removed from data`);
delete input.skeleton.spine;
}
// patch attachment
for(let s in input.skins){
let atts = input.skins[s].attachments;
for(let a in Object.keys(atts)){
let attItem = Object.keys(atts)[a];
let att = atts[attItem];
for(let a2 in Object.keys(att)){
let attName = Object.keys(att)[a2];
let attData = att[attName];
if(!attData.type || attData.type == 'region'){
let findImage = atlas.regions.find(v => {
return v.name == attName;
});
if(attData.width != findImage.orig[0] || attData.height != findImage.orig[1]){
if(!attData.scaleX){
attData.scaleX = 1;
}
if(!attData.scaleY){
attData.scaleY = 1;
}
attData.scaleX = +(attData.scaleX * (attData.width / findImage.orig[0])).toFixed(4);
attData.scaleY = +(attData.scaleY * (attData.height / findImage.orig[1])).toFixed(4);
}
}
}
}
}
return input;
};
export {
json2patch
};