-
Notifications
You must be signed in to change notification settings - Fork 7
/
jsonem_models.js
223 lines (183 loc) · 5.88 KB
/
jsonem_models.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(function() {
const CODEC = new Codec("jsonem_entity", {
name: "JsonEM Entity",
load_filter: {
extensions: ["json"],
type: "json"
},
extension: "json",
remember: true,
load(model, file) {
//console.log(`Opening Model: ${JSON.stringify(model)}`)
let texture = model.texture;
let bones = model.bones;
Project.texture_width = texture.width;
Project.texture_height = texture.height;
for (key in bones) {
addBone(undefined, [0, 0, 0], key, bones[key]);
}
},
compile() {
let compiled = {};
compiled.texture = {};
compiled.texture.width = Project.texture_width;
compiled.texture.height = Project.texture_height;
compiled.bones = {};
for (node of Outliner.root) {
if (node instanceof Group) {
compiled.bones[node.name] = compileBone(node, [0, 0, 0]);
}
}
//console.log(`Exporting Model: ${JSON.stringify(compiled)}`)
return JSON.stringify(compiled, null, 4);
},
export_action: new Action("export_jsonem", {
name: "Export JsonEM Java Entity Model",
icon: "icon-format_java",
category: "file",
click: () => CODEC.export()
})
});
const FORMAT = new ModelFormat({
id: "jsonem_model",
icon: "icon-format_java",
name: "JsonEM Java Entity Model",
description: "Entity model for Minecraft Java Edition through JsonEM.",
show_on_start_screen: true,
box_uv: true,
optional_box_uv: false,
single_texture: true,
bone_rig: true,
centered_grid: true,
rotate_cubes: false,
integer_size: false,
locators: false,
canvas_limit: false,
rotation_limit: false,
display_mode: true,
animation_mode: false,
codec: CODEC,
onActivation() {
MenuBar.addAction(CODEC.export_action, "file.export")
},
onDeactivation() {
CODEC.export_action.delete();
}
})
Plugin.register("jsonem_models", {
title: "JsonEM Model Support",
author: "FoundationGames",
description: "Create models to be used with https://github.com/FoundationGames/JsonEM",
icon: "icon-format_java",
version: "1.4",
variant: "both"
})
function flipCoords(vec) {
return [-vec[0], -vec[1], vec[2]];
}
function flipY(vec) {
return [vec[0], -vec[1], vec[2]];
}
function isZero(vec) {
for (c of vec) {
if (c != 0) {
return false;
}
}
return true;
}
function addBone(parent, pOrigin, key, bone) {
let gopts = {name: key, children: []};
let origin = pOrigin;
if ("transform" in bone) {
if ("origin" in bone.transform) {
let bor = bone.transform.origin;
origin = [origin[0] + bor[0], origin[1] + bor[1], origin[2] + bor[2]];
}
if ("rotation" in bone.transform) {
let rot = bone.transform.rotation;
gopts.rotation = [-Math.radToDeg(rot[0]), -Math.radToDeg(rot[1]), Math.radToDeg(rot[2])];
}
}
gopts.origin = flipCoords(origin);
let group = new Group(gopts);
if (parent !== undefined) {
group.addTo(parent);
}
group = group.init();
for (cuboid of bone.cuboids) {
let copts = {name: "cube"};
if ("name" in cuboid) {
copts.name = cuboid.name;
}
if ("dilation" in cuboid) {
copts.inflate = cuboid.dilation[0];
}
if ("mirror" in cuboid) {
copts.mirror_uv = cuboid.mirror;
}
let pos = cuboid.offset;
pos = [pos[0] + origin[0], pos[1] + origin[1], pos[2] + origin[2]]
let size = cuboid.dimensions;
copts.to = flipCoords([pos[0], pos[1], pos[2] + size[2]]);
copts.from = flipCoords([pos[0] + size[0], pos[1] + size[1], pos[2]]);
copts.uv_offset = cuboid.uv;
new Cube(copts).addTo(group).init();
}
for (ckey in bone.children) {
addBone(group, origin, ckey, bone.children[ckey]);
}
}
function compileBone(bone, pOrigin) {
let compiled = {};
let bOrigin = flipCoords(bone.origin); // bone origin
let origin = [bOrigin[0] - pOrigin[0], bOrigin[1] - pOrigin[1], bOrigin[2] - pOrigin[2]]; // origin to write to json
if (!isZero(origin)) {
if (!("transform" in compiled)) compiled.transform = {};
compiled.transform.origin = origin;
}
let brot = bone.rotation;
let rotation = [Math.degToRad(-brot[0]), Math.degToRad(-brot[1]), Math.degToRad(brot[2])];
if (!isZero(rotation)) {
if (!("transform" in compiled)) compiled.transform = {};
compiled.transform.rotation = rotation;
}
compiled.cuboids = [];
let children = {}
for (node of bone.children) {
if (node instanceof Group) {
if (node.parent !== bone) {
continue;
}
children[node.name] = compileBone(node, bOrigin);
}
if (node instanceof Cube) {
if (node.parent !== bone) {
continue;
}
let cuboid = {}
if (node.name !== "cube") {
cuboid.name = node.name;
}
let to = node.to;
let from = node.from;
let size = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
let pos = flipCoords([from[0] + size[0], from[1] + size[1], from[2]]);
cuboid.offset = [pos[0] - bOrigin[0], pos[1] - bOrigin[1], pos[2] - bOrigin[2]];
cuboid.dimensions = size;
cuboid.uv = node.uv_offset;
if (node.mirror_uv) {
cuboid.mirror = true;
}
if (node.inflate > 0) {
cuboid.dilation = [node.inflate, node.inflate, node.inflate];
}
compiled.cuboids.push(cuboid);
}
}
if (children != {}) {
compiled.children = children;
}
return compiled;
}
})();