-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvs_toolkit.js
More file actions
454 lines (388 loc) · 22.8 KB
/
Copy pathvs_toolkit.js
File metadata and controls
454 lines (388 loc) · 22.8 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
(function(){
let exportVsAction;
let importVsAction;
function r4(num) {
return Math.round(num * 10000) / 10000;
}
Plugin.register('vs_toolkit', {
title: 'Vintage Story Toolkit',
icon: 'build',
author: 'El Neuman',
description: 'Allows to import and export VintageStory models.',
version: '1.1.8',
variant: "both",
onload() {
let m = new THREE.Mesh();
function rotationToLocal(rotation) {
m.rotation.set(0,0,0, "ZYX");
m.rotateOnWorldAxis(new THREE.Vector3(1,0,0), Math.degToRad(rotation[0]));
m.rotateOnWorldAxis(new THREE.Vector3(0,1,0), Math.degToRad(rotation[1]));
m.rotateOnWorldAxis(new THREE.Vector3(0,0,1), Math.degToRad(rotation[2]));
m.rotation.reorder("XYZ");
return [
Math.radToDeg(m.rotation.x),
Math.radToDeg(m.rotation.y),
Math.radToDeg(m.rotation.z),
];
}
exportVsAction = new Action("exportVsModel", {
name: "Export Vintage Story Model",
icon: "park",
click: function(){
const worldcenter = (typeof Format !== 'undefined' && Format.name === "Java Block/Item") ? 0 : 8;
function getGroupGlobalFrom(group) {
if (group.vsFrom) return group.vsFrom;
if (group.parent && group.parent !== "root") {
return getGroupGlobalFrom(group.parent);
}
return [-worldcenter, 0, -worldcenter];
}
let VSjson = {
editor: {
allAngles: false,
entityTextureMode: false
},
textureWidth: Project.texture_width,
textureHeight: Project.texture_height,
textureSizes: {},
textures: {},
elements: [],
animations:[]
};
if (Project.textures.length > 0) {
for (let i = 0; i < Project.textures.length; i++) {
let texName = Project.textures[i].name.replace(".png", "");
let texWidth = Project.textures[i].uv_width || Project.texture_width;
let texHeight = Project.textures[i].uv_height || Project.texture_height;
VSjson.textureSizes[texName] = [texWidth, texHeight];
VSjson.textures[texName] = texName;
}
}
let elemALL = [];
let axis = ["x", "y", "z"];
let faces = ["north","east","south","west","up","down"];
for (let i = 0; i < Group.all.length; i++) {
let group = Group.all[i];
let pFrom = group.parent && group.parent !== "root" ? getGroupGlobalFrom(group.parent) : [-worldcenter, 0, -worldcenter];
let globalFrom = group.vsFrom || pFrom;
let globalTo = group.vsTo || pFrom;
let elem = {
uuid: group.uuid,
name: group.name,
rotationOrigin: [r4(group.origin[0] - pFrom[0]), r4(group.origin[1] - pFrom[1]), r4(group.origin[2] - pFrom[2])],
from: [r4(globalFrom[0] - pFrom[0]), r4(globalFrom[1] - pFrom[1]), r4(globalFrom[2] - pFrom[2])],
to: [r4(globalTo[0] - pFrom[0]), r4(globalTo[1] - pFrom[1]), r4(globalTo[2] - pFrom[2])],
parentUuid: group.parent === "root" ? null : group.parent.uuid,
faces: {north: { texture: "#", uv: [0,0,0,0] }, east: { texture: "#", uv: [0,0,0,0] }, south: { texture: "#", uv: [0,0,0,0] }, west: { texture: "#", uv: [0,0,0,0] }, up: { texture: "#", uv: [0,0,0,0] }, down: { texture: "#", uv: [0,0,0,0] }},
}
let r = new THREE.Euler(0,0,0,"XYZ");
if(group.rotation) {
r = new THREE.Euler(Math.degToRad(group.rotation[0]), Math.degToRad(group.rotation[1]), Math.degToRad(group.rotation[2]), "XYZ");
}
r = [Math.radToDeg(r.x), Math.radToDeg(r.y), Math.radToDeg(r.z)];
axis.forEach((a, index) => {
let roundedRot = r4(r[index]);
if (roundedRot !== 0) {
elem["rotation" + a.toUpperCase()] = roundedRot;
}
});
elemALL.push(elem);
}
for (let i = 0; i < Cube.all.length; i++) {
let cube = Cube.all[i];
let pFrom = cube.parent && cube.parent !== "root" ? getGroupGlobalFrom(cube.parent) : [-worldcenter, 0, -worldcenter];
let elem = {
uuid: cube.uuid,
name : cube.name,
from: [r4(cube.from[0] - pFrom[0]), r4(cube.from[1] - pFrom[1]), r4(cube.from[2] - pFrom[2])],
to: [r4(cube.to[0] - pFrom[0]), r4(cube.to[1] - pFrom[1]), r4(cube.to[2] - pFrom[2])],
rotationOrigin: [r4(cube.origin[0] - pFrom[0]), r4(cube.origin[1] - pFrom[1]), r4(cube.origin[2] - pFrom[2])],
faces: {
north: { texture: "#0", uv: [0,0,0,0] }, east: { texture: "#0", uv: [0,0,0,0] }, south: { texture: "#0", uv: [0,0,0,0] },
west: { texture: "#0", uv: [0,0,0,0] }, up: { texture: "#0", uv: [0,0,0,0] }, down: { texture: "#0", uv: [0,0,0,0] }
},
parentUuid: cube.parent === "root" ? null : cube.parent.uuid
};
let r = cube.mesh.rotation.clone().reorder("XYZ");
r = [Math.radToDeg(r.x), Math.radToDeg(r.y), Math.radToDeg(r.z)];
axis.forEach((a, index) => {
let roundedRot = r4(r[index]);
if (roundedRot !== 0) {
elem["rotation" + a.toUpperCase()] = roundedRot;
}
});
faces.forEach(face => {
if (Project.textures.length > 0) {
if (!Format.single_texture) {
let foundTex = Project.textures.find(e => e.uuid == cube.faces[face].texture);
if (foundTex && foundTex !== false) {
elem.faces[face].texture = "#" + foundTex.name.replace(".png", "");
} else {
elem.faces[face].texture = "#missing";
}
} else {
let foundTex = cube.faces[face].getTexture();
if (foundTex) {
elem.faces[face].texture = "#" + foundTex.name.replace(".png", "");
} else {
elem.faces[face].texture = "#missing";
}
}
}
elem.faces[face].autoUv = Project.box_uv;
elem.faces[face].uv = [r4(cube.faces[face].uv[0]), r4(cube.faces[face].uv[1]), r4(cube.faces[face].uv[2]), r4(cube.faces[face].uv[3])];
if (cube.faces[face].rotation !== 0) {
elem.faces[face].rotation = cube.faces[face].rotation;
}
});
elemALL.push(elem);
}
VSjson.elements = list_to_tree(elemALL);
if (typeof Format !== 'undefined' && Format.animation_mode) {
for (let i = 0; i < Animation.all.length; i++) {
let bbAnim = Animation.all[i];
let anim = {
name: bbAnim.name,
code: (bbAnim.name).toLowerCase().replace(/ /g, "_"),
onActivityStopped: "EaseOut",
onAnimationEnd: bbAnim.loop === "hold" ? "Hold" : (bbAnim.loop === "once" ? "Stop" : "Repeat"),
quantityframes: Math.round(bbAnim.length * 30),
keyframes: []
};
let frameMap = {};
Object.keys(bbAnim.animators).forEach(key => {
let animator = bbAnim.animators[key];
let kfs = animator.keyframes;
if (!kfs || kfs.length === 0) {
kfs = [];
['rotation', 'position', 'scale'].forEach(ch => {
if (animator[ch] && Array.isArray(animator[ch])) kfs = kfs.concat(animator[ch]);
});
}
if (!kfs || kfs.length === 0 || !animator.group) return;
let groupName = animator.group.name || animator.group;
if (typeof groupName !== 'string') groupName = groupName.name;
kfs.forEach(kf => {
let frameNum = Math.round(kf.time * 30);
if (!frameMap[frameNum]) frameMap[frameNum] = {};
if (!frameMap[frameNum][groupName]) frameMap[frameNum][groupName] = {};
let elemA = frameMap[frameNum][groupName];
let dp = kf.data_points[0];
if (!dp) return;
let x = parseFloat(dp.x) || 0;
let y = parseFloat(dp.y) || 0;
let z = parseFloat(dp.z) || 0;
if (kf.channel === "rotation") {
let rl = rotationToLocal([x, y, z]);
elemA.rotationX = r4(rl[0]);
elemA.rotationY = r4(rl[1]);
elemA.rotationZ = r4(rl[2]);
} else if (kf.channel === "position") {
elemA.offsetX = r4(x);
elemA.offsetY = r4(y);
elemA.offsetZ = r4(z);
} else if (kf.channel === "scale") {
elemA.stretchX = r4(x);
elemA.stretchY = r4(y);
elemA.stretchZ = r4(z);
}
});
});
Object.keys(frameMap).forEach(frameStr => {
anim.keyframes.push({
frame: parseInt(frameStr),
elements: frameMap[frameStr]
});
});
anim.keyframes.sort((a, b) => a.frame - b.frame);
VSjson.animations.push(anim);
}
}
if (isApp) {
Blockbench.export({
type: 'VintageStory Model',
extensions: ['json'],
name: (Project.name !== '' ? Project.name: "model"),
content: autoStringify(VSjson),
savetype: 'json'
});
} else {
var blob = new Blob([autoStringify(VSjson)], {type: "text/plain;charset=utf-8"});
saveAs(blob, (Project.name !== '' ? Project.name: "model") + ".json");
}
}
});
importVsAction = new Action("importVsModel", {
name: "Import Vintage Story Model",
icon: "unarchive",
click: function() {
const worldcenter = (typeof Format !== 'undefined' && Format.name === "Java Block/Item") ? 0 : 8;
Blockbench.import({
type: 'Vintage Story Model',
extensions: ['json']
}, function(files) {
if (!files || files.length === 0) return;
let file = files[0];
let vsModel;
try {
vsModel = JSON.parse(file.content);
} catch(e) {
Blockbench.showMessageBox({title: 'Error', message: 'Неверный или поврежденный JSON файл.'});
return;
}
if (vsModel.textureWidth) Project.texture_width = vsModel.textureWidth;
if (vsModel.textureHeight) Project.texture_height = vsModel.textureHeight;
let textureMap = {};
if (vsModel.textures) {
for (let key in vsModel.textures) {
let texPath = vsModel.textures[key];
let tex = new Texture({
id: key,
name: key,
folder: typeof texPath === 'string' ? texPath : 'block'
}).add();
textureMap[key] = tex;
}
}
function parseElements(elements, parentGroup, accumulatedOffset) {
if (!elements) return;
if (!accumulatedOffset) accumulatedOffset = [-worldcenter, 0, -worldcenter];
elements.forEach(el => {
let isCube = el.from && el.to;
let hasRotation = el.rotationX || el.rotationY || el.rotationZ;
let hasChildren = el.children && el.children.length > 0;
let localFrom = isCube ? el.from : (el.from || [0,0,0]);
let localTo = isCube ? el.to : (el.to || [0,0,0]);
let bbFrom = [
r4(localFrom[0] + accumulatedOffset[0]),
r4(localFrom[1] + accumulatedOffset[1]),
r4(localFrom[2] + accumulatedOffset[2])
];
let bbTo = [
r4(localTo[0] + accumulatedOffset[0]),
r4(localTo[1] + accumulatedOffset[1]),
r4(localTo[2] + accumulatedOffset[2])
];
let rotOrigin;
if (el.rotationOrigin) {
rotOrigin = [
r4(el.rotationOrigin[0] + accumulatedOffset[0]),
r4(el.rotationOrigin[1] + accumulatedOffset[1]),
r4(el.rotationOrigin[2] + accumulatedOffset[2])
];
} else if (isCube) {
rotOrigin = [
(bbFrom[0] + bbTo[0]) / 2,
(bbFrom[1] + bbTo[1]) / 2,
(bbFrom[2] + bbTo[2]) / 2
];
} else {
rotOrigin = [8 - worldcenter, 0, 8 - worldcenter];
}
let bbRot = [0, 0, 0];
if (hasRotation) {
let vsEuler = new THREE.Euler(
Math.degToRad(el.rotationX || 0),
Math.degToRad(el.rotationY || 0),
Math.degToRad(el.rotationZ || 0),
"XYZ"
);
vsEuler.reorder("ZYX");
bbRot = [
r4(Math.radToDeg(vsEuler.x)),
r4(Math.radToDeg(vsEuler.y)),
r4(Math.radToDeg(vsEuler.z))
];
}
let group = null;
if (hasChildren || !isCube) {
group = new Group({
name: el.name || 'group',
origin: rotOrigin,
rotation: bbRot
}).init();
group.vsFrom = bbFrom;
group.vsTo = bbTo;
if (parentGroup) group.addTo(parentGroup);
}
if (isCube) {
let cubeConfig = {
name: el.name || 'cube',
from: bbFrom,
to: bbTo,
origin: rotOrigin,
rotation: group ? [0, 0, 0] : bbRot
};
let cube = new Cube(cubeConfig);
if (el.faces) {
for (let f in el.faces) {
if (cube.faces[f] && el.faces[f]) {
let vFace = el.faces[f];
if (vFace.uv) {
cube.faces[f].uv = [vFace.uv[0], vFace.uv[1], vFace.uv[2], vFace.uv[3]];
}
if (vFace.rotation) {
cube.faces[f].rotation = vFace.rotation;
}
if (vFace.texture) {
let texKey = vFace.texture.replace('#', '');
if (textureMap[texKey]) {
cube.faces[f].texture = textureMap[texKey].uuid;
}
}
}
}
}
cube.init();
if (group) {
cube.addTo(group);
} else if (parentGroup) {
cube.addTo(parentGroup);
}
}
if (hasChildren) {
parseElements(el.children, group || parentGroup, bbFrom);
}
});
}
Undo.initEdit({elements: Cube.all, outliner: true, textures: Texture.all});
parseElements(vsModel.elements, null, [-worldcenter, 0, -worldcenter]);
Canvas.updateAll();
Undo.finishEdit('Import Vintage Story Model');
});
}
});
MenuBar.addAction(exportVsAction, "file.export");
MenuBar.addAction(importVsAction, "file.import");
},
onunload() {
exportVsAction.delete();
importVsAction.delete();
}
});
function list_to_tree(list) {
var map = {}, node, roots = [], i;
for (i = 0; i < list.length; i ++) {
map[list[i].uuid] = i;
list[i].children = [];
}
for (i = 0; i < list.length; i ++) {
node = list[i];
if (node.parentUuid !== null) {
if (list[map[node.parentUuid]]) list[map[node.parentUuid]].children.push(node);
} else {
roots.push(node);
}
}
for (i = 0; i < list.length; i ++) {
node = list[i];
delete node.parentUuid;
delete node.uuid;
delete node.parent;
if (node.children && node.children.length === 0) {
delete node.children;
}
}
return roots;
}
})()