minerender 2.1.0-alpha.15 → 2.1.0-alpha.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -689,16 +689,17 @@ interface BlockStateVariant {
689
689
  y?: number;
690
690
  x?: number;
691
691
  uvlock?: boolean;
692
+ weight?: number;
692
693
  }
693
694
  interface BlockStateMultipart {
694
- when?: MultipartCondition | {
695
- OR: MultipartCondition[];
696
- };
697
- apply?: BlockStateVariant;
698
- }
699
- interface MultipartCondition {
700
- [key: string]: string;
695
+ when?: MultipartCondition;
696
+ apply?: BlockStateVariant | BlockStateVariant[];
701
697
  }
698
+ type MultipartCondition = Record<string, string> | {
699
+ OR: MultipartCondition[];
700
+ } | {
701
+ AND: MultipartCondition[];
702
+ };
702
703
 
703
704
  declare const DEFAULT_ROOT = "https://assets.mcasset.cloud/1.21.11";
704
705
  declare const DEFAULT_NAMESPACE = "minecraft";
@@ -689,16 +689,17 @@ interface BlockStateVariant {
689
689
  y?: number;
690
690
  x?: number;
691
691
  uvlock?: boolean;
692
+ weight?: number;
692
693
  }
693
694
  interface BlockStateMultipart {
694
- when?: MultipartCondition | {
695
- OR: MultipartCondition[];
696
- };
697
- apply?: BlockStateVariant;
698
- }
699
- interface MultipartCondition {
700
- [key: string]: string;
695
+ when?: MultipartCondition;
696
+ apply?: BlockStateVariant | BlockStateVariant[];
701
697
  }
698
+ type MultipartCondition = Record<string, string> | {
699
+ OR: MultipartCondition[];
700
+ } | {
701
+ AND: MultipartCondition[];
702
+ };
702
703
 
703
704
  declare const DEFAULT_ROOT = "https://assets.mcasset.cloud/1.21.11";
704
705
  declare const DEFAULT_NAMESPACE = "minecraft";
@@ -1108,7 +1108,11 @@ var _Materials = class _Materials {
1108
1108
 
1109
1109
  if (SHADE) {
1110
1110
 
1111
- vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
1111
+ #ifdef USE_INSTANCING
1112
+ vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
1113
+ #else
1114
+ vec3 N = vec3( modelMatrix * vec4(normal, 0.0) );
1115
+ #endif
1112
1116
 
1113
1117
  float yLight = (1.0+N.y) * 0.5;
1114
1118
  light = yLight * (1.0-AMBIENT) + N.x*N.x * XFAC + N.z*N.z * ZFAC + AMBIENT;
@@ -4364,6 +4368,16 @@ function isModelObject(obj) {
4364
4368
  var import_ts_deepmerge6 = __toESM(require_dist());
4365
4369
  var import_three16 = require("three");
4366
4370
  var p10 = prefix("BlockObject");
4371
+ function matchesCondition(condition, state) {
4372
+ return Object.entries(condition).every(([key, value]) => {
4373
+ if (Array.isArray(value)) {
4374
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4375
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4376
+ return false;
4377
+ }
4378
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4379
+ });
4380
+ }
4367
4381
  var _BlockObject = class _BlockObject extends SceneObject {
4368
4382
  constructor(blockState, options) {
4369
4383
  super(options);
@@ -4392,15 +4406,9 @@ var _BlockObject = class _BlockObject extends SceneObject {
4392
4406
  if (this.blockState.variants) {
4393
4407
  await this.setState(Object.keys(this.blockState.variants)[0]);
4394
4408
  } else if (this.blockState.multipart) {
4395
- for (let part of this.blockState.multipart) {
4396
- if (part.when && !("OR" in part.when)) {
4397
- const k = Object.keys(part.when)[0];
4398
- if (k) {
4399
- await this.setState(k, part.when[k].split("|")[0]);
4400
- break;
4401
- }
4402
- }
4403
- }
4409
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4410
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4411
+ await this.setState(state);
4404
4412
  }
4405
4413
  }
4406
4414
  } else {
@@ -4460,52 +4468,8 @@ var _BlockObject = class _BlockObject extends SceneObject {
4460
4468
  console.debug(p10, "Missing apply for blockState part", part);
4461
4469
  continue;
4462
4470
  }
4463
- if (!part.when) {
4471
+ if (!part.when || matchesCondition(part.when, state)) {
4464
4472
  out.push(this.getSingleVariant(part.apply));
4465
- } else {
4466
- let matches = true;
4467
- if ("OR" in part.when) {
4468
- const or = part.when.OR;
4469
- let anyMatch = false;
4470
- for (let o of or) {
4471
- for (let k in o) {
4472
- const split = o[k].split("|");
4473
- let m = false;
4474
- for (let s of split) {
4475
- if (`${state[k]}` === `${s}`) {
4476
- m = true;
4477
- break;
4478
- }
4479
- }
4480
- if (m) {
4481
- anyMatch = true;
4482
- break;
4483
- }
4484
- }
4485
- }
4486
- if (!anyMatch) {
4487
- matches = false;
4488
- }
4489
- } else {
4490
- for (let k in part.when) {
4491
- const split = part.when[k].split("|");
4492
- let m = false;
4493
- for (let s of split) {
4494
- if (`${state[k]}` === `${s}`) {
4495
- m = true;
4496
- break;
4497
- }
4498
- }
4499
- if (!m) {
4500
- matches = false;
4501
- break;
4502
- }
4503
- }
4504
- }
4505
- if (matches) {
4506
- const variants = part.apply;
4507
- out.push(this.getSingleVariant(variants));
4508
- }
4509
4473
  }
4510
4474
  }
4511
4475
  }
@@ -4535,11 +4499,21 @@ var _BlockObject = class _BlockObject extends SceneObject {
4535
4499
  }
4536
4500
  }
4537
4501
  getSingleVariant(variants) {
4538
- if (Array.isArray(variants)) {
4539
- return variants[0];
4540
- } else {
4541
- return variants;
4542
- }
4502
+ if (!Array.isArray(variants)) return variants;
4503
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4504
+ const total = variants.reduce((sum, variant) => {
4505
+ const weight = variant.weight ?? 1;
4506
+ if (!Number.isInteger(weight) || weight < 1) {
4507
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4508
+ }
4509
+ return sum + weight;
4510
+ }, 0);
4511
+ let choice = Math.random() * total;
4512
+ for (const variant of variants) {
4513
+ choice -= variant.weight ?? 1;
4514
+ if (choice < 0) return variant;
4515
+ }
4516
+ return variants[variants.length - 1];
4543
4517
  }
4544
4518
  // @deprecated
4545
4519
  async createAVariant(variants, instanceInfo) {