-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockGeoMaker.js
762 lines (728 loc) · 31.8 KB
/
BlockGeoMaker.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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// this looks interesting: https://github.com/PrismarineJS/minecraft-data/blob/master/data/bedrock/1.20.71/blockCollisionShapes.json (Object.fromEntries(Object.entries(d.blocks).map(([name, indices])=>[name,indices.map(i=>d.shapes[i])])))
// READ: this also looks pretty comprehensive: https://github.com/MCBE-Development-Wiki/mcbe-dev-home/blob/main/docs/misc/enums/block_shape.md
// https://github.com/bricktea/MCStructure/blob/main/docs/1.16.201/enums/B.md
import { awaitAllEntries, clamp, hexColorToClampedTriplet, JSONSet } from "./essential.js";
// https://wiki.bedrock.dev/visuals/material-creations.html#overlay-color-in-render-controllers
// https://wiki.bedrock.dev/documentation/materials.html#entity-alphatest
export default class BlockGeoMaker {
/** variant numbers tied to specific blocks. they will always have these variant indices. */
static #eigenvariants = {
"grass_block": 0, // for the tints; this makes it look like the forest or flower forest biomes
"grass_path": 0, // down texture uses dirt instead of flattened_dirt :/
"unpowered_repeater": 0,
"powered_repeater": 1,
"unpowered_comparator": 0,
"powered_comparator": 1,
"daylight_detector": 0,
"daylight_detector_inverted": 1,
"furnace": 0, // these are strange
"lit_furnace": 0,
"blast_furnace": 0,
"lit_blast_furnace": 0,
"smoker": 0,
"lit_smoker": 0,
"normal_stone_stairs": 0,
"stone_button": 0,
"stone_pressure_plate": 0,
"stone_brick_stairs": 0,
"bubble_column": 0,
"wooden_button": 0,
"wooden_pressure_plate": 0,
"wooden_door": 0,
"spruce_door": 1,
"birch_door": 2,
"jungle_door": 3,
"acacia_door": 4,
"dark_oak_door": 5,
"iron_door": 6,
"oak_leaves": 0,
"spruce_leaves": 0,
"birch_leaves": 0,
"jungle_leaves": 0,
"acacia_leaves": 0,
"dark_oak_leaves": 0,
"carved_pumpkin": 0,
"lit_pumpkin": 1,
"pumpkin": 2
};
static #REDSTONE_DUST_TINTS = function() {
// net.minecraft.world.level.block.RedStoneWireBlock
let cols = [];
for(let i = 0; i < 16; i++) {
let f = i / 15;
let r = f * 0.6 + (f > 0? 0.4 : 0.3);
let g = clamp(f * f * 0.7 - 0.5, 0, 1);
cols[i] = [r, g, 0];
}
return cols;
}();
config;
textureRefs;
#individualBlockShapes;
#blockShapePatterns;
#blockShapeGeos;
#globalBlockStateRotations;
#blockShapeBlockStateRotations;
#blockNameBlockStateRotations;
#blockNamePatternBlockStateRotations;
#globalBlockStateTextureVariants;
#blockShapeBlockStateTextureVariants;
#blockNameBlockStateTextureVariants;
#blockNamePatternBlockStateTextureVariants;
#cachedBlockShapes;
constructor(config) {
return (async () => {
this.config = config;
this.textureRefs = new JSONSet();
let { blockShapes, blockShapeGeos, blockStateDefs } = await awaitAllEntries({
blockShapes: fetch("data/blockShapes.json").then(res => res.jsonc()),
blockShapeGeos: fetch("data/blockShapeGeos.json").then(res => res.jsonc()),
blockStateDefs: fetch("data/blockStateDefinitions.json").then(res => res.jsonc())
});
this.#individualBlockShapes = blockShapes["individual_blocks"];
this.#blockShapePatterns = Object.entries(blockShapes["patterns"]).map(([rule, blockShape]) => [new RegExp(rule), blockShape]); // store regular expressions from the start to avoid recompiling them every time
this.#blockShapeGeos = blockShapeGeos;
// console.log(this.#blockShapeGeos)
// block-state-driven rotations/texture variants can either be global, based on block shape, based on specific block names, or based on regular expressions for block names, hence the many variables.
this.#globalBlockStateRotations = blockStateDefs["rotations"]["*"];
this.#blockShapeBlockStateRotations = [];
Object.entries(blockStateDefs["rotations"]["block_shapes"] ?? {}).forEach(([blockShapes, rotationDefs]) => {
blockShapes.split(",").forEach(blockShape => {
this.#blockShapeBlockStateRotations[blockShape] = rotationDefs;
});
});
this.#blockNameBlockStateRotations = [];
this.#blockNamePatternBlockStateRotations = [];
Object.entries(blockStateDefs["rotations"]["block_names"] ?? {}).forEach(([blockNames, rotationDefs]) => {
if(blockNames.startsWith("/") && blockNames.endsWith("/")) {
this.#blockNamePatternBlockStateRotations.push([new RegExp(blockNames.slice(1, -1)), rotationDefs]);
} else {
blockNames.split(",").forEach(blockName => {
this.#blockNameBlockStateRotations[blockName] = rotationDefs; // todo: make these Maps?
});
}
});
this.#globalBlockStateTextureVariants = blockStateDefs["texture_variants"]["*"];
this.#blockShapeBlockStateTextureVariants = [];
Object.entries(blockStateDefs["texture_variants"]["block_shapes"] ?? {}).forEach(([blockShapes, textureVariantDefs]) => {
blockShapes.split(",").forEach(blockShape => {
this.#blockShapeBlockStateTextureVariants[blockShape] = textureVariantDefs;
});
});
this.#blockNameBlockStateTextureVariants = [];
this.#blockNamePatternBlockStateTextureVariants = [];
Object.entries(blockStateDefs["texture_variants"]["block_names"] ?? {}).forEach(([blockNames, textureVariantDefs]) => {
if(blockNames.startsWith("/") && blockNames.endsWith("/")) {
this.#blockNamePatternBlockStateTextureVariants.push([new RegExp(blockNames.slice(1, -1)), textureVariantDefs]);
} else {
blockNames.split(",").forEach(blockName => {
this.#blockNameBlockStateTextureVariants[blockName] = textureVariantDefs;
});
}
});
this.#cachedBlockShapes = new Map();
return this;
})();
}
/**
* Makes a bone template (i.e. unpositioned, nameless bone with geometry) from a block. Texture UVs are unresolved, and are indices for the textureRefs property.
* @param {Block} block
* @returns {BoneTemplate}
*/
makeBoneTemplate(block) {
let blockName = block["name"];
let blockShape = this.#getBlockShape(blockName);
let boneCubes = this.#makeBoneCubes(block, blockShape);
if(boneCubes.length == 0) {
console.debug(`No cubes are being rendered for block ${blockName}`);
}
let bone = {
"cubes": boneCubes
};
let blockShapeSpecificRotations = this.#blockShapeBlockStateRotations[blockShape];
let blockNameSpecificRotations = this.#blockNameBlockStateRotations[blockName];
Object.entries(block["states"] ?? {}).forEach(([blockStateName, blockStateValue]) => {
let rotations = blockNameSpecificRotations?.[blockStateName] ?? this.#blockNamePatternBlockStateRotations.find(([pattern, rotations]) => pattern.test(blockName) && blockName in rotations)?.[1] ?? blockShapeSpecificRotations?.[blockStateName] ?? this.#globalBlockStateRotations[blockStateName]; // order: block name (exact match), block name (regular expression pattern), block shape, global
if(!rotations) {
return; // this block state doesn't control rotation
}
if(!(blockStateValue in rotations)) {
console.error(`Block state value ${blockStateValue} for rotation block state ${blockStateName} not found...`);
return;
}
if(bone["rotation"]) {
console.debug(`Multiple rotation block states for block ${block["name"]}; adding them all together!`);
bone["rotation"] = bone["rotation"].map((x, i) => x + rotations[blockStateValue][i]);
} else {
bone["rotation"] = rotations[blockStateValue];
bone["pivot"] = [8, 8, 8];
}
});
return bone;
}
/**
* Absolutely positions a bone template.
* @param {BoneTemplate} boneTemplate
* @param {Vec3} blockPos The position where the bone will be moved to.
* @returns {BoneTemplate}
*/
positionBoneTemplate(boneTemplate, blockPos) {
let bone = structuredClone(boneTemplate);
bone["cubes"].forEach(boneCube => {
boneCube["origin"] = boneCube["origin"].map((x, i) => x + blockPos[i]);
if("pivot" in boneCube) {
boneCube["pivot"] = boneCube["pivot"].map((x, i) => x + blockPos[i]);
}
boneCube["extra_rots"]?.forEach(extraRot => {
extraRot["pivot"] = extraRot["pivot"].map((x, i) => x + blockPos[i]);
});
});
if("pivot" in bone) {
bone["pivot"] = bone["pivot"].map((x, i) => x + blockPos[i]);
}
return bone;
}
/**
* Gets the block shape for a specific block.
* @param {String} blockName
* @returns {String}
*/
#getBlockShape(blockName) {
if(this.#cachedBlockShapes.has(blockName)) {
return this.#cachedBlockShapes.get(blockName);
}
let individualBlockShape = this.#individualBlockShapes[blockName];
if(individualBlockShape) {
return individualBlockShape;
}
let matchingBlockShape = this.#blockShapePatterns.find(([pattern]) => pattern.test(blockName))?.[1]; // could use .filter to catch double matches but that's a skill issue
let blockShape = matchingBlockShape ?? "block";
this.#cachedBlockShapes.set(blockName, blockShape);
return blockShape;
}
/**
* Makes the cubes in a bone from a block.
* @param {Block} block
* @param {String} blockShape
* @returns {Array}
*/
#makeBoneCubes(block, blockShape) {
let specialTexture;
if(blockShape.includes("{")) {
[, blockShape, specialTexture] = blockShape.match(/^(\w+)\{(textures\/[\w\/]+)\}$/);
}
let unfilteredCubes = structuredClone(this.#blockShapeGeos[blockShape]);
if(!unfilteredCubes) {
console.error(`Could not find geometry for block shape ${blockShape}; defaulting to "block"`);
unfilteredCubes = structuredClone(this.#blockShapeGeos["block"]);
}
let filteredCubes = [];
while(unfilteredCubes.length) {
// For each unfiltered cube, we add it to filteredCubes if we've checked the "if" flag. If there are copies, we then add them back to unfilteredCubes.
let cube = unfilteredCubes.shift();
if("block_states" in cube) {
let blockOverride = structuredClone(block);
for(let blockStateName in cube["block_states"]) {
if(typeof cube["block_states"][blockStateName] == "string") {
cube["block_states"][blockStateName] = this.#interpolateInBlockValues(block, cube["block_states"][blockStateName]);
}
}
blockOverride["states"] = { ...blockOverride["states"], ...cube["block_states"] };
cube["block_override"] = blockOverride;
}
if("if" in cube) {
if(!this.#checkBlockStateConditional(cube["block_override"] ?? block, cube["if"])) {
continue;
}
delete cube["if"]; // later on, when determining if a bone cube is mergeable, we only allow cubes with "pos" and "size" keys. I am lazy so it only checks if there are exactly 2 keys in the cube. hence, we need to delete the "if" key here.
}
if("terrain_texture" in cube) {
cube["terrain_texture"] = this.#interpolateInBlockValues(cube["block_override"] ?? block, cube["terrain_texture"]);
}
if("copy" in cube) {
let copiedCubes = structuredClone(this.#blockShapeGeos[cube["copy"]]);
if(!copiedCubes) {
console.error(`Could not find geometry for block shape ${blockShape}; defaulting to "block"`);
copiedCubes = structuredClone(this.#blockShapeGeos["block"]);
}
let fieldsToCopy = Object.keys(cube).filter(field => !["copy", "rot", "pivot", "translate"].includes(field));
copiedCubes.forEach(copiedCube => {
if("translate" in cube) {
copiedCube["translate"] = (copiedCube["translate"] ?? [0, 0, 0]).map((x, i) => x + cube["translate"][i]);
}
fieldsToCopy.forEach(field => { // copy all fields from this cube onto the new ones
if(typeof copiedCube[field] == "object") {
copiedCube[field] = { ...cube[field], ...copiedCube[field] }; // fields on the copied cubes still take priority over the "parent" cube (the one that's copying it)
} else {
copiedCube[field] ??= cube[field];
}
});
if("rot" in cube) {
if("rot" in copiedCube) {
// maths for combining both rotations is hard so we handle it differently and create a list of extra rotations.
// HoloPrint.js will create a wrapper bone for each rotation
copiedCube["extra_rots"] ??= [];
copiedCube["extra_rots"].unshift({
"rot": cube["rot"],
"pivot": cube["pivot"] ?? [8, 8, 8]
});
} else {
copiedCube["rot"] = cube["rot"];
copiedCube["pivot"] = cube["pivot"] ?? copiedCube["pivot"];
}
}
});
unfilteredCubes.push(...copiedCubes);
} else {
filteredCubes.push(cube);
}
}
// add easy property accessors. I could make a class if I wanted to
filteredCubes.forEach(cube => {
Object.defineProperties(cube, Object.fromEntries(["x", "y", "z", "w", "h", "d"].map((prop, i) => [prop, {
get() {
return (i < 3? this["pos"] : this["size"])[i % 3];
},
set(value) {
(i < 3? this["pos"] : this["size"])[i % 3] = value;
}
}])));
});
let cubes = this.#mergeCubes(filteredCubes);
let blockName = block["name"];
let variant = this.#getTextureVariant(block);
let variantWithoutEigenvariant;
let boneCubes = [];
cubes.forEach(cube => {
// In MCBE most non-full-block textures look at where the part that is being rendered is in relation to the entire cube space it's in - like it's being projected onto a full face then cut out. Kinda hard to explain sorry, I recommend messing around with fence textures so you understand how it works.
let westUvOffset = [cube.z, 16 - cube.y - cube.h];
let eastUvOffset = [16 - cube.z - cube.d, 16 - cube.y - cube.h];
let downUvOffset = [16 - cube.x - cube.w, 16 - cube.z - cube.d];
let upUvOffset = [16 - cube.x - cube.w, cube.z];
let northUvOffset = [cube.x, 16 - cube.y - cube.h];
let southUvOffset = [16 - cube.x - cube.w, 16 - cube.y - cube.h];
let boneCube = {
"origin": cube["pos"],
"size": cube["size"],
"uv": {
"west": {
"uv": cube["uv"]?.["west"] ?? cube["uv"]?.["side"] ?? cube["uv"]?.["*"] ?? westUvOffset,
"uv_size": cube["uv_sizes"]?.["west"] ?? cube["uv_sizes"]?.["side"] ?? cube["uv_sizes"]?.["*"] ?? [cube.d, cube.h]
},
"east": {
"uv": cube["uv"]?.["east"] ?? cube["uv"]?.["side"] ?? cube["uv"]?.["*"] ?? eastUvOffset,
"uv_size": cube["uv_sizes"]?.["east"] ?? cube["uv_sizes"]?.["side"] ?? cube["uv_sizes"]?.["*"] ?? [cube.d, cube.h]
},
"down": {
"uv": cube["uv"]?.["down"] ?? cube["uv"]?.["*"] ?? downUvOffset,
"uv_size": cube["uv_sizes"]?.["down"] ?? cube["uv_sizes"]?.["*"] ?? [cube.w, cube.d]
},
"up": {
"uv": cube["uv"]?.["up"] ?? cube["uv"]?.["*"] ?? upUvOffset,
"uv_size": cube["uv_sizes"]?.["up"] ?? cube["uv_sizes"]?.["*"] ?? [cube.w, cube.d]
},
"north": {
"uv": cube["uv"]?.["north"] ?? cube["uv"]?.["side"] ?? cube["uv"]?.["*"] ?? northUvOffset,
"uv_size": cube["uv_sizes"]?.["north"] ?? cube["uv_sizes"]?.["side"] ?? cube["uv_sizes"]?.["*"] ?? [cube.w, cube.h]
},
"south": {
"uv": cube["uv"]?.["south"] ?? cube["uv"]?.["side"] ?? cube["uv"]?.["*"] ?? southUvOffset,
"uv_size": cube["uv_sizes"]?.["south"] ?? cube["uv_sizes"]?.["side"] ?? cube["uv_sizes"]?.["*"] ?? [cube.w, cube.h]
}
}
};
let croppable = false;
// When the size of a cube in a direction is 0, we can remove all faces but 1. Because we have DisableCulling in the material, this single face will render from the back as well.
// On a side note, if there wasn't the DisableCulling material state and we rendered both faces on opposite sides, the texture wouldn't be mirrored on the other side, so this is another bug fix ig
if(cube.w == 0) {
// 0 width: only render west
["east", "down", "up", "north", "south"].forEach(faceName => delete boneCube["uv"][faceName]);
croppable = true;
}
if(cube.h == 0) {
// 0 height: only render down
["west", "east", "up", "north", "south"].forEach(faceName => delete boneCube["uv"][faceName]);
croppable = true;
}
if(cube.d == 0) {
// 0 depth: only render north
["west", "east", "down", "up", "south"].forEach(faceName => delete boneCube["uv"][faceName]);
croppable = true;
}
let cubeVariant;
if("variant" in cube) {
cubeVariant = cube["variant"];
} else if(cube["ignore_eigenvariant"]) {
if("block_override" in cube) {
cubeVariant = this.#getTextureVariant(cube["block_override"], true);
} else {
if(variantWithoutEigenvariant == undefined) {
variantWithoutEigenvariant = this.#getTextureVariant(block, true);
}
cubeVariant = variantWithoutEigenvariant;
}
} else if("block_override" in cube) {
cubeVariant = this.#getTextureVariant(cube["block_override"]);
} else {
cubeVariant = variant; // default variant for this block
}
let textureSize = cube["texture_size"] ?? [16, 16];
// add generic keys to all faces, and convert texture references into indices
for(let faceName in boneCube["uv"]) {
let isSideFace = ["west", "east", "north", "south"].includes(faceName);
let face = boneCube["uv"][faceName];
let textureFace = cube["textures"]?.[faceName] ?? (isSideFace? cube["textures"]?.["side"] : undefined) ?? cube["textures"]?.["*"] ?? faceName;
if(textureFace == "none") {
delete boneCube["uv"][faceName];
continue;
}
textureFace = this.#interpolateInBlockValues(cube["block_override"] ?? block, textureFace);
let textureRef = {
"uv": face["uv"].map((x, i) => x / textureSize[i]),
"uv_size": face["uv_size"].map((x, i) => x / textureSize[i]),
"block_name": blockName,
"texture_face": textureFace,
"variant": cubeVariant,
"croppable": croppable
};
if(textureFace == "#tex") {
if(specialTexture) {
textureRef["texture_path_override"] = specialTexture;
} else {
console.error(`No #tex for block ${blockName} and blockshape ${blockShape}!`);
}
} else if(/^textures\/.+[^/]$/.test(textureFace)) { // file path
textureRef["texture_path_override"] = textureFace;
} else {
let terrainTextureOverride = cube["terrain_texture"];
if(terrainTextureOverride) {
delete textureRef["block_name"];
delete textureRef["texture_face"];
textureRef["terrain_texture_override"] = terrainTextureOverride;
}
}
if("texture_path_override" in textureRef) {
delete textureRef["block_name"];
delete textureRef["texture_face"];
delete textureRef["variant"];
}
if("tint" in cube) {
let tint = cube["tint"];
tint = this.#interpolateInBlockValues(cube["block_override"] ?? block, tint);
if(tint[0] == "#") {
textureRef["tint"] = hexColorToClampedTriplet(tint);
} else {
// this is from cauldrons; colour is a 32-bit ARGB colour
let colorCode = 4294967296 + Number(tint); // 4294967296 = 2 ** 32
textureRef["tint"] = [colorCode >> 16 & 0xFF, colorCode >> 8 & 0xFF, colorCode & 0xFF].map(x => x / 255);
}
}
if(blockName == "redstone_wire") {
textureRef["tint"] = BlockGeoMaker.#REDSTONE_DUST_TINTS[block["states"]["redstone_signal"]];
}
this.textureRefs.add(textureRef);
let flipTextureHorizontally = cube["flip_textures_horizontally"]?.includes(faceName) || (isSideFace && cube["flip_textures_horizontally"]?.includes("side")) || cube["flip_textures_horizontally"]?.includes("*");
let flipTextureVertically = cube["flip_textures_vertically"]?.includes(faceName) || (isSideFace && cube["flip_textures_vertically"]?.includes("side")) || cube["flip_textures_vertically"]?.includes("*");
boneCube["uv"][faceName] = {
"index": this.textureRefs.indexOf(textureRef),
"flip_horizontally": (faceName == "down" || faceName == "up") ^ flipTextureHorizontally, // in MC the down/up faces are rotated 180 degrees compared to how they are in geometry; this can be faked by flipping both axes. I don't want to use uv_rotation since that's a 1.21 thing and I want support back to 1.16.
"flip_vertically": (faceName == "down" || faceName == "up") ^ flipTextureVertically
};
}
if("rot" in cube) {
boneCube["rotation"] = cube["rot"];
boneCube["pivot"] = cube["pivot"] ?? [8, 8, 8]; // we set the pivot for this cube to be the center of it. if we don't specify this it would look at the pivot for the bone, which would be different if we're doing our fancy animations.
}
if("extra_rots" in cube) {
boneCube["extra_rots"] = cube["extra_rots"];
}
if("translate" in cube) {
boneCube["origin"] = boneCube["origin"].map((x, i) => x + cube["translate"][i]);
if("rot" in cube) {
boneCube["pivot"] = boneCube["pivot"].map((x, i) => x + cube["translate"][i]);
}
if("extra_rots" in cube) {
boneCube["extra_rots"].forEach(extraRot => {
extraRot["pivot"] = extraRot["pivot"].map((x, i) => x + cube["translate"][i]);
});
}
}
boneCube = this.#scaleBoneCube(boneCube);
boneCubes.push(boneCube);
});
return boneCubes;
}
/**
* Merges cubes together greedily.
* @param {Array<Object>} cubes
* @returns {Array<Object>}
*/
#mergeCubes(cubes) {
let unmergeableCubes = [];
let mergeableCubes = [];
cubes.forEach(cube => {
if(!cube["size"].some(x => x == 0) && Object.keys(cube).length == 2) { // 2 keys: pos and size
mergeableCubes.push(cube);
} else {
unmergeableCubes.push(cube);
}
});
let mergedCubes = [];
mergeableCubes.forEach(cube1 => { // this is the cube we're trying to add to mergedCubes
tryMerging: while(true) {
for(let [i, cube2] of mergedCubes.entries()) {
if(this.#tryMergeCubesOneWay(cube1, cube2)) {
console.debug("Merged cube", cube2, "into", cube1);
mergedCubes.splice(i, 1);
continue tryMerging; // since boneCube1 has been mutated, this stops the current comparison of boneCube1 with the already merged cubes, and makes it start again.
} else if(this.#tryMergeCubesOneWay(cube2, cube1)) {
console.debug("Merged cube", cube1, "into", cube2);
mergedCubes.splice(i, 1);
cube1 = cube2;
continue tryMerging; // boneCube2 has been mutated, so we removed it from mergedCubes and swap it out for boneCube1, then restart trying to merge it.
}
}
break; // we'll only get to here when it's checked all combinations between the already merged cubes and
}
mergedCubes.push(cube1);
});
return [...unmergeableCubes, ...mergedCubes];
}
/**
* Gets the index of the variant to use in terrain_texture.json for a block.
* @param {Block} block
* @param {Boolean} [ignoreEigenvariant]
* @returns {Number}
*/
#getTextureVariant(block, ignoreEigenvariant = false) {
let blockName = block["name"];
let eigenvariantExists = blockName in BlockGeoMaker.#eigenvariants;
if(!ignoreEigenvariant && eigenvariantExists) {
let variant = BlockGeoMaker.#eigenvariants[blockName];
console.debug(`Using eigenvariant ${variant} for block ${blockName}`);
return variant;
} else if(ignoreEigenvariant && !eigenvariantExists) {
console.warn(`Cannot ignore eigenvariant of ${blockName} as it doesn't exist!`);
}
if(!("states" in block)) {
return -1;
}
let blockShape = this.#getBlockShape(blockName); // In copied block shapes, we want to look at the original block shape's texture variants, not the copied's. E.g. With candle_cake, we don't want the cake that is copied to look at the texture variants for cake (which includes bite_counter).
let blockShapeSpecificVariants = this.#blockShapeBlockStateTextureVariants[blockShape];
if(blockShapeSpecificVariants?.["#exclusive_add"]) {
let variant = 0;
Object.entries(block["states"]).forEach(([blockStateName, blockStateValue]) => {
if(blockStateName in blockShapeSpecificVariants) {
let blockStateVariants = blockShapeSpecificVariants[blockStateName];
if(!(blockStateValue in blockStateVariants)) {
console.error(`Block state value ${blockStateValue} for texture-variating block state ${blockStateName} not found...`);
return;
}
variant += blockStateVariants[blockStateValue];
}
});
return variant;
}
let blockNameSpecificVariants = this.#blockNameBlockStateTextureVariants[blockName] ?? this.#blockNamePatternBlockStateTextureVariants.find(([pattern]) => pattern.test(blockName))?.[1];
if(blockNameSpecificVariants?.["#exclusive_add"]) {
let variant = 0;
Object.entries(block["states"]).forEach(([blockStateName, blockStateValue]) => {
if(blockStateName in blockNameSpecificVariants) {
let blockStateVariants = blockNameSpecificVariants[blockStateName];
if(!(blockStateValue in blockStateVariants)) {
console.error(`Block state value ${blockStateValue} for texture-variating block state ${blockStateName} not found...`);
return;
}
variant += blockStateVariants[blockStateValue];
}
});
return variant;
}
let variant = -1;
Object.entries(block["states"]).forEach(([blockStateName, blockStateValue]) => {
let blockStateVariants = blockNameSpecificVariants?.[blockStateName] ?? blockShapeSpecificVariants?.[blockStateName] ?? this.#globalBlockStateTextureVariants[blockStateName];
if(blockStateVariants == undefined) {
return;
}
if(!(blockStateValue in blockStateVariants)) {
console.error(`Block state value ${blockStateValue} for texture-variating block state ${blockStateName} not found...`);
return;
}
let newVariant = blockStateVariants[blockStateValue];
if(variant != -1) {
console.warn(`Multiple texture-variating block states for block ${block["name"]}; using ${blockStateName}`);
}
variant = newVariant;
});
return variant;
}
/** Scales a bone cube towards (8, 8, 8).
* @param {Object} boneCube
* @returns {Object}
*/
#scaleBoneCube(boneCube) {
boneCube["origin"] = boneCube["origin"].map(x => (x - 8) * this.config.SCALE + 8);
boneCube["size"] = boneCube["size"].map(x => x * this.config.SCALE);
if("pivot" in boneCube) {
boneCube["pivot"] = boneCube["pivot"].map(x => (x - 8) * this.config.SCALE + 8);
}
if("extra_rots" in boneCube) {
boneCube["extra_rots"].forEach(extraRot => {
extraRot["pivot"] = extraRot["pivot"].map(x => (x - 8) * this.config.SCALE + 8);
});
}
return boneCube;
}
#checkBlockStateConditional(block, conditional) {
let trimmedConditional = conditional.replaceAll(/\s/g, "");
let booleanOperations = trimmedConditional.match(/&&|\|\|/g) ?? []; // can have multiple separated by ?? or ||
let booleanValues = trimmedConditional.split(/&&|\|\|/).map(booleanExpression => {
let match = booleanExpression.match(/^((?:entity\.)?[\w:&-?]+)(==|>|<|>=|<=|!=)(-?\w+)$/); // Despite the Minecraft Wiki and Microsoft creator documentation saying there can be boolean block states, they're stored as bytes in NBT
if(!match) {
console.error(`Incorrectly formatted block state expression "${booleanExpression}" from conditional "${conditional}"\n(Match: ${JSON.stringify(match)})`);
return true; // If we miss the error message more geometry will draw more attention to it :D
}
let [, blockStateTerm, comparisonOperator, expectedBlockState] = match;
let blockStateOperation = blockStateTerm.match(/^(entity\.)?([\w:]+)(?:(\?\?|&)(-?\d+))?$/);
if(!blockStateOperation) {
console.error(`Incorrectly formed block state term: ${blockStateTerm}`);
return true;
}
let [, usingBlockEntityData, blockStateName, blockStateOperator, blockStateOperandString] = blockStateOperation;
let dataObjectName = usingBlockEntityData? "block_entity_data" : "states";
if(!(dataObjectName in block)) {
console.error(`No ${dataObjectName} in block ${block["name"]}!`);
return true;
}
let dataObject = block[dataObjectName];
if(blockStateOperator != "??" && !(blockStateName in dataObject)) {
console.error(`Cannot find ${dataObjectName} ${blockStateName} on block ${block["name"]}`);
return true;
}
let actualBlockState = dataObject[blockStateName];
if(blockStateOperator) {
let blockStateOperand = Number(blockStateOperandString);
if(Number.isNaN(blockStateOperand)) {
console.error(`${dataObjectName} operand ${blockStateOperand} is not a number!`);
return true;
}
actualBlockState = function() {
switch(blockStateOperator) {
case "&": return actualBlockState & blockStateOperand;
case "??": return actualBlockState ?? blockStateOperand;
}
console.error(`Unknown ${dataObjectName} operator ${blockStateOperator} in term ${blockStateTerm}!`);
return actualBlockState;
}();
}
switch(comparisonOperator) {
case "==": return actualBlockState == expectedBlockState;
case ">": return actualBlockState > expectedBlockState;
case "<": return actualBlockState < expectedBlockState;
case ">=": return actualBlockState >= expectedBlockState;
case "<=": return actualBlockState <= expectedBlockState;
case "!=": return actualBlockState != expectedBlockState;
}
console.error(`Unknown ${dataObjectName} comparison operator ${comparisonOperator} in expression ${booleanExpression}`);
return true;
});
// do && before || to match JS operator precedence
let andRes = [booleanValues[0]];
booleanOperations.forEach((booleanOperation, i) => {
if(booleanOperation == "&&") {
andRes[andRes.length - 1] &&= booleanValues[i + 1];
} else {
andRes.push(booleanValues[i + 1])
}
});
let orRes = andRes.some(x => x); // only || operations remain
return orRes;
}
/**
* Substitutes values from a block into a particular expression.
* @param {Block} block
* @param {String} fullExpression
* @returns {String}
*/
#interpolateInBlockValues(block, fullExpression) {
let wholeStringValue;
let substitutedExpression = fullExpression.replaceAll(/\${([^}]+)}/g, (bracketedExpression, expression) => {
if(wholeStringValue != undefined) return;
let match = expression.replaceAll(/\s/g, "").match(/^(#block_name|#block_states|#block_entity_data)((?:\.\w+|\[-?\d+\])*)(\[(-?\d+):(-?\d*)\]|\[:(-?\d+)\])?(?:\?\?(.+))?$/);
if(!match) {
console.error(`Wrongly formatted expression: ${bracketedExpression}`);
return "";
}
let [, specialVar, propertyChain, ...slicingAndDefault] = match;
let value = function() {
switch(specialVar) {
case "#block_name": return block["name"];
case "#block_states": return block["states"];
case "#block_entity_data": return block["block_entity_data"];
}
console.error(`Unknown special variable: ${specialVar}`);
}();
propertyChain.match(/\.\w+|\[-?\d+\]/g)?.forEach(property => {
let keys = property.match(/^\.(\w+)|\[(\d+)\]$/);
value = value?.[keys[1] ?? keys[2]];
});
if(slicingAndDefault[0] != undefined) {
value = value?.slice(slicingAndDefault[1], slicingAndDefault[3] ?? (slicingAndDefault[2] == ""? undefined : slicingAndDefault[2]));
}
if(value == undefined || value === "") {
if(slicingAndDefault[4] != undefined) {
let defaultValue = slicingAndDefault[4];
if(/SET_WHOLE_STRING\([^)]+\)/.test(defaultValue)) {
wholeStringValue = defaultValue.match(/\(([^)]+)\)/)[1];
return;
} else {
value = defaultValue;
}
} else {
console.error(`Nothing for ${specialVar}${propertyChain} in block:`, block);
return "";
}
}
console.debug(`Changed ${bracketedExpression} to ${value}!`, block);
return value;
});
return wholeStringValue ?? substitutedExpression;
}
/**
* Unpurely tries to merge the cube into the first if the second is more positive than the first.
* @param {Object} cube1
* @param {Object} cube2
* @returns {Boolean} If the second cube was merged into the first.
*/
#tryMergeCubesOneWay(cube1, cube2) {
if(cube1.x + cube1.w == cube2.x) { // bone cube 2 is to the right of bone cube 1
if(cube1.y == cube2.y && cube1.z == cube2.z && cube1.h == cube2.h && cube1.d == cube2.d) {
cube1.w += cube2.w; // grow cube 1...
return true;
}
} else if(cube1.y + cube1.h == cube2.y) { // bone cube 2 is above bone cube 1
if(cube1.x == cube2.x && cube1.z == cube2.z && cube1.w == cube2.w && cube1.d == cube2.d) {
cube1.h += cube2.h;
return true;
}
} else if(cube1.z + cube1.d == cube2.z) { // bone cube 2 is behind bone cube 1
if(cube1.x == cube2.x && cube1.y == cube2.y && cube1.w == cube2.w && cube1.h == cube2.h) {
cube1.d += cube2.d;
return true;
}
}
return false;
}
}
/**
* @typedef {import("./HoloPrint.js").Vec3} Vec3
*/
/**
* @typedef {import("./HoloPrint.js").Block} Block
*/
/**
* @typedef {import("./HoloPrint.js").BoneTemplate} BoneTemplate
*/
/**
* @typedef {import("./HoloPrint.js").Bone} Bone
*/