forked from JannisX11/blockbench-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsmodel.js
More file actions
628 lines (585 loc) · 16.8 KB
/
Copy pathcsmodel.js
File metadata and controls
628 lines (585 loc) · 16.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
(function() {
var import_action, export_action;
Plugin.register('csmodel', {
title: 'CraftStudio Model Format',
icon: 'star',
author: 'JannisX11',
description: 'Allows to import and export CraftStudio Models (.csmodel).',
about: `To **import** a model from CraftStudio, go to the CraftStudio project settings and export a cspack file.
Open this file in an archive manager (like 7zip) and extract the model file.
Import the file into Blockbench using the import menu.
\nTo **export** a file, export a .csmodel file from Blockbench and drop it into an existing .cspack file into the Models folder.
Make sure it is using the same file name as the old model in the pack. Import the .cspack into CraftStudio and select the models you want to import.`,
tags: ["CraftStudio"],
version: '0.1.3',
min_version: '3.8.0',
variant: 'both',
onload() {
function toArrayBuffer(buf) {
var ab = new ArrayBuffer(buf.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
}
let getNativeSize = function(cube, face) {
var side = face.direction;
var size = {};
if (side == 'north' || side == 'south') {
return [cube.size('0'),
cube.size('1')]
} else if (side == 'east' || side == 'west') {
return [cube.size('2'),
cube.size('1')]
} else if (side == 'up' || side == 'down') {
return [cube.size('0'),
cube.size('2')]
}
}
var textDecoder;
class BinaryReader {
constructor(buffer) {
this.view = new DataView(buffer);
this.cursor = 0;
this.textDecoder = new TextDecoder();
}
ReadUInt8() {
var val;
val = this.view.getUint8(this.cursor);
this.cursor += 1;
return val;
}
ReadUInt16() {
var val;
val = this.view.getUint16(this.cursor, true);
this.cursor += 2;
return val;
}
ReadInt32() {
var val;
val = this.view.getInt32(this.cursor, true);
this.cursor += 4;
return val;
}
ReadUInt32() {
var val;
val = this.view.getUint32(this.cursor, true);
this.cursor += 4;
return val;
}
ReadFloat32() {
var val;
val = this.view.getFloat32(this.cursor, true);
this.cursor += 4;
return val;
}
ReadFloat64() {
var val;
val = this.view.getFloat64(this.cursor, true);
this.cursor += 8;
return val;
}
ReadBoolean() {
return this.ReadUInt8() !== 0;
}
Read7BitEncodedInt() {
var bitIndex, num, returnValue;
returnValue = 0;
bitIndex = 0;
while (true) {
if (bitIndex !== 35) {
num = this.ReadUInt8();
returnValue |= (num & 127) << bitIndex;
bitIndex += 7;
} else {
throw new Error("Invalid 7-bit encoded int");
}
if ((num & 128) === 0) {
break;
}
}
return returnValue;
}
ReadString() {
var length, val;
length = this.Read7BitEncodedInt();
val = this.DecodeString(new Uint8Array(this.view.buffer.slice(this.cursor, this.cursor + length)));
this.cursor += length;
return val;
}
ReadPoint() {
return {
x: this.ReadInt32(),
y: this.ReadInt32()
};
}
ReadVector2() {
return new THREE.Vector2(this.ReadFloat32(), this.ReadFloat32());
}
ReadVector3() {
return new THREE.Vector3(this.ReadFloat32(), this.ReadFloat32(), this.ReadFloat32());
}
ReadIntVector3() {
return new THREE.Vector3(this.ReadInt32(), this.ReadInt32(), this.ReadInt32());
}
ReadQuaternion() {
var w;
w = this.ReadFloat32();
return new THREE.Quaternion(this.ReadFloat32(), this.ReadFloat32(), this.ReadFloat32(), w);
}
ReadBytes(length) {
var bytes;
bytes = new Uint8Array(this.view.buffer.slice(this.cursor, this.cursor + length));
this.cursor += length;
return bytes;
}
DecodeString(array) {
return this.textDecoder.decode(array);
};
};
class BinaryWriter {
constructor(minimal_length) {
this.array = new Uint8Array(minimal_length);
this.buffer = this.array.buffer;
this.view = new DataView(this.buffer);
this.cursor = 0;
this.textEncoder = new TextEncoder();
}
expand(n) {
var new_length = this.cursor+1+n;
if (new_length > this.buffer.byteLength) {
var oldArray = this.array;
this.array = new Uint8Array(new_length);
this.buffer = this.array.buffer;
this.array.set(oldArray);
this.view = new DataView(this.buffer)
}
}
WriteUInt8(value) {
this.expand(1);
this.view.setUint8(this.cursor, value);
this.cursor += 1;
}
WriteUInt16(value) {
this.expand(2);
this.view.setUint16(this.cursor, value, true);
this.cursor += 2;
}
WriteInt32(value) {
this.expand(4);
this.view.setInt32(this.cursor, value, true);
this.cursor += 4;
}
WriteUInt32(value) {
this.expand(4);
this.view.setUint32(this.cursor, value, true);
this.cursor += 4;
}
WriteFloat32(value) {
this.expand(4);
this.view.setFloat32(this.cursor, value, true);
this.cursor += 4;
}
WriteFloat64(value) {
this.expand(8);
this.view.setFloat64(this.cursor, value, true);
this.cursor += 8;
}
WriteBoolean(value) {
this.WriteUInt8(value ? 1 : 0)
}
Write7BitEncodedInt(value) {
while (value >= 0x80) {
this.WriteUInt8(value | 0x80);
value = value >> 7;
}
this.WriteUInt8(value);
}
WriteString(string) {
var array = this.EncodeString(string);
this.Write7BitEncodedInt(array.byteLength);
this.WriteBytes(array);
}
WritePoint(point) {
this.expand(8);
this.view.setInt32(this.cursor, point.x, true);
this.cursor += 4;
this.view.setInt32(this.cursor, point.y, true);
this.cursor += 4;
}
WriteVector2(vector) {
this.expand(8);
this.view.setFloat32(this.cursor, vector.x, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, vector.y, true);
this.cursor += 4;
}
WriteVector3(vector) {
this.expand(12);
this.view.setFloat32(this.cursor, vector.x, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, vector.y, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, vector.z, true);
this.cursor += 4;
}
WriteIntVector3(vector) {
this.expand(12);
this.view.setInt32(this.cursor, vector.x, true);
this.cursor += 4;
this.view.setInt32(this.cursor, vector.y, true);
this.cursor += 4;
this.view.setInt32(this.cursor, vector.z, true);
this.cursor += 4;
}
WriteQuaternion(quat) {
this.expand(16);
this.view.setFloat32(this.cursor, quat.w, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, quat.x, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, quat.y, true);
this.cursor += 4;
this.view.setFloat32(this.cursor, quat.z, true);
this.cursor += 4;
}
WriteBytes(array) {
this.expand(array.byteLength);
this.array.set(array, this.cursor);
this.cursor += array.byteLength;
}
EncodeString(string) {
return this.textEncoder.encode(string);
}
};
var oppositeFaces = {
'south': 'north',
'north': 'south',
'east': 'west',
'down': 'up',
'west': 'east',
'up': 'down',
}
var faceAxes = {
'north': 2,
'east': 0,
'up': 1,
'south': 2,
'west': 0,
'down': 1,
}
var codec = new Codec('csmodel', {
name: 'CraftStudio',
extension: 'csmodel',
remember: false,
compile(options) {
var writer = new BinaryWriter(100);
var fake_cube = new Cube({
from: [0, 0, 0],
to: [0, 0, 0],
})
var ids = {};
var id = -1;
var offsets = {};
writer.WriteUInt8(0);//asset type
writer.WriteUInt16(5);//format version
writer.WriteUInt16(0);//next unused node id
writer.WriteUInt16(0);//node count
function processGroup(group, parent) {
//get main cube
var main_cube;
for (var child of group.children) {
if (child instanceof Cube && child.rotation.allEqual(0)) {
main_cube = child;
break;
}
}
if (!main_cube) {
//add fake 0x0x0 cube
processCube(group, parent);
} else {
processCube(main_cube, parent, group)
}
ids[group.uuid] = id;
for (var child of group.children) {
if (child instanceof Cube && child != main_cube) {
processCube(child, group, false)
} else if (child instanceof Group) {
processGroup(child, group)
}
}
}
function processCube(obj, parent, group) {
id++;
var cube = (obj instanceof Cube) ? obj : fake_cube;
if (group) obj = group
writer.WriteUInt16(id);
if (parent && ids[parent.uuid] !== undefined) {
writer.WriteUInt16(ids[parent.uuid]);
} else {
writer.WriteUInt16(-1);
}
writer.WriteString(obj.name);
var size = cube.size(undefined, true);
var center = [
cube.from[0] + size[0]/2,
cube.from[1] + size[1]/2,
cube.from[2] + size[2]/2,
]
var pivot = obj.origin
var pos = new THREE.Vector3().fromArray(pivot);
if (parent) {
pos.sub(new THREE.Vector3().fromArray(parent.origin));
} else {
pos.add(new THREE.Vector3().fromArray(obj.origin));
}
if (parent && ids[parent.uuid]) {
pos.sub(offsets[ids[parent.uuid]]);
}
var offset = new THREE.Vector3(center[0] - pivot[0], center[1] - pivot[1], center[2] - pivot[2]);
var euler = new THREE.Euler().setFromDegreeArray(obj.rotation);
var rotation = new THREE.Quaternion().setFromEuler(euler);
var scale = new THREE.Vector3(
((size[0]+2*cube.inflate) / size[0]) * (size[0] < 0 ?-1:1),
((size[1]+2*cube.inflate) / size[1]) * (size[1] < 0 ?-1:1),
((size[2]+2*cube.inflate) / size[2]) * (size[2] < 0 ?-1:1)
);
offsets[id] = new THREE.Vector3().copy(offset);
writer.WriteVector3(pos.divideScalar(16));
writer.WriteVector3(offset.divideScalar(16));
writer.WriteVector3(scale);
writer.WriteQuaternion(rotation);
writer.WriteUInt16(Math.abs(size[0]));
writer.WriteUInt16(Math.abs(size[1]));
writer.WriteUInt16(Math.abs(size[2]));
writer.WriteUInt8(2);
for (var face_key in oppositeFaces) {
var face = cube.faces[face_key];
writer.WritePoint({
x: face.uv[0],
y: face.uv[1]
})
}
for (var face_key in oppositeFaces) {
var code = 0;
var x_m = face_key != 'up' && face_key != 'down';
var y_m = face_key != 'up';
if (face.rotation == 90) code += 1;
if (face.rotation == 180) code += 2;
if (face.rotation == 270) code += 4;
if (face.uv[0] > face.uv[2] != x_m) code += 8;
if (face.uv[1] > face.uv[3] != y_m) code += 16;
writer.WriteUInt8(code);
}
}
Outliner.root.forEach(obj => {
if (obj instanceof Group) {
processGroup(obj);
} else if (obj instanceof Cube) {
processCube(obj)
}
})
writer.view.setUint16(5, id+1, true);
if (Texture.getDefault()) {
var src = Texture.getDefault().getBase64();
} else {
var src = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAHklEQVQ4T2P8////fwYKAOOoAQyjYcAwGgYMwyIMACUfP9Eb4ExuAAAAAElFTkSuQmCC'
}
var array = Uint8Array.from(atob(src), c => c.charCodeAt(0));
writer.WriteUInt32(array.length);
writer.WriteBytes(array);
writer.WriteUInt16(0);
return writer.array;
},
parse(arraybuffer) {
newProject(Formats.bedrock)
var reader = new BinaryReader(arraybuffer);
var nodes = {};
var face_info = {};
var node_data = {};
reader.ReadUInt8();//asset type
reader.ReadUInt16();//format version
reader.ReadUInt16();//next unused node id
var node_count = reader.ReadUInt16();
for (var i = 0; i < node_count; i++) {
(function() {
var id = reader.ReadUInt16();
var parent_id = reader.ReadUInt16();
var name = reader.ReadString();
var pos = reader.ReadVector3().multiplyScalar(16);
var offset = reader.ReadVector3().multiplyScalar(16);
node_data[id] = {
name,
pos: new THREE.Vector3().copy(pos),
offset: new THREE.Vector3().copy(offset)
}
var scale = reader.ReadVector3();
var rotation = new THREE.Euler().setFromQuaternion(reader.ReadQuaternion(), 'ZYX').toArray();
rotation.forEach((n, i) => {
rotation[i] = Math.radToDeg(n);
})
var size = [
reader.ReadUInt16() * scale.x,
reader.ReadUInt16() * scale.y,
reader.ReadUInt16() * scale.z
]
var uv_mode = reader.ReadUInt8();
var faces = {
south: {offset: reader.ReadPoint()},
north: {offset: reader.ReadPoint()},
east: {offset: reader.ReadPoint()},
down: {offset: reader.ReadPoint()},
west: {offset: reader.ReadPoint()},
up: {offset: reader.ReadPoint()},
}
faces.south.transform = reader.ReadUInt8();
faces.north.transform = reader.ReadUInt8();
faces.east.transform = reader.ReadUInt8();
faces.down.transform = reader.ReadUInt8();
faces.west.transform = reader.ReadUInt8();
faces.up.transform = reader.ReadUInt8();
if (nodes[parent_id]) {
pos.add(new THREE.Vector3().fromArray(nodes[parent_id].origin))
pos.add(node_data[parent_id].offset)
}
var from = [
pos.x + offset.x - size[0]/2,
pos.y + offset.y - size[1]/2,
pos.z + offset.z - size[2]/2,
]
var to = [
from[0] + size[0],
from[1] + size[1],
from[2] + size[2],
]
var cube = new Cube({
name,
origin: pos.toArray(),
from, to,
rotation,
box_uv: false
}).init();
nodes[id] = cube;
face_info[cube.uuid] = faces;
faces.scale = scale;
faces.cube = cube;
if (nodes[parent_id]) {
var parent = nodes[parent_id];
if (parent instanceof Cube) {
var parent_cube = parent;
parent = nodes[parent_id] = new Group({
name: parent_cube.name,
origin: parent_cube.origin,
rotation: parent_cube.rotation
}).addTo(parent_cube).init();
parent.createUniqueName();
parent_cube.addTo(parent);
parent_cube.extend({rotation: [0, 0, 0]})
}
cube.addTo(parent);
}
})()
}
var image = reader.ReadBytes(reader.ReadUInt32());
var i = 0;
let txt = '';
while (i < image.length) {
let sub = image.subarray(i, Math.clamp(i+1024, 0, image.length));
let substr = String.fromCharCode.apply(null, sub);
txt += substr;
i += 1024;
}
let url = 'data:image/png;base64,' + btoa(txt);
var tex = new Texture({name: 'texture'}).fromDataURL(url).add();
tex.load_callback = function() {
Project.texture_width = tex.width;
Project.texture_height = tex.height;
Project.box_uv = false;
for (var uuid in face_info) {
var info = face_info[uuid];
var cube = info.cube;
for (var face_key in cube.faces) {
var face = cube.faces[face_key];
face.extend({uv: [
info[face_key].offset.x,
info[face_key].offset.y,
0,0
]});
var native_size = getNativeSize(cube, face);
if (face.direction == 'south' || face.direction == 'north') {
face.uv_size = [
native_size[0]/info.scale.x,
native_size[1]/info.scale.y
]
} else if (face.direction == 'up' || face.direction == 'down') {
face.uv_size = [
native_size[0]/info.scale.x,
native_size[1]/info.scale.z
]
} else {
face.uv_size = [
native_size[0]/info.scale.z,
native_size[1]/info.scale.y
]
}
var code = info[face_key].transform;
if (code >= 16) {
var size = face.uv[3] - face.uv[1];
face.uv[3] -= size*2;
code -= 16;
}
if (code >= 8) {
var size = face.uv[2] - face.uv[0];
face.uv[2] -= size*2;
code -= 8;
}
switch (code) {
case 4: face.rotation = 270; break;
case 2: face.rotation = 180; break;
case 1: face.rotation = 90; break;
}
}
}
Canvas.updateAll()
}
}
})
import_action = new Action('import_csmodel', {
name: 'Import CraftStudio Model',
description: '',
icon: 'star',
category: 'file',
click() {
Blockbench.import({
extensions: ['csmodel'],
type: 'CraftStudio Model',
readtype: 'binary',
resource_id: 'craftstudio_files'
}, files => {
codec.parse(files[0].content);
csname = files[0].name.replace(".csmodel","").replace(/\s+/g, "_").toLowerCase();
Project.name = csname;
Project.model_identifier = csname;
Texture.all.last().name = csname;
})
}
})
export_action = new Action('export_csmodel', {
name: 'Export CraftStudio Model',
description: '',
icon: 'star',
category: 'file',
click() {
codec.export();
}
})
MenuBar.addAction(import_action, 'file.import')
MenuBar.addAction(export_action, 'file.export')
},
onunload() {
import_action.delete();
export_action.delete();
}
});
})()