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.
@@ -957,7 +957,11 @@ var Materials = class _Materials {
957
957
 
958
958
  if (SHADE) {
959
959
 
960
- vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
960
+ #ifdef USE_INSTANCING
961
+ vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
962
+ #else
963
+ vec3 N = vec3( modelMatrix * vec4(normal, 0.0) );
964
+ #endif
961
965
 
962
966
  float yLight = (1.0+N.y) * 0.5;
963
967
  light = yLight * (1.0-AMBIENT) + N.x*N.x * XFAC + N.z*N.z * ZFAC + AMBIENT;
@@ -4260,6 +4264,16 @@ function isModelObject(obj) {
4260
4264
  var import_ts_deepmerge6 = __toESM(require_dist());
4261
4265
  import { Euler as Euler3, Matrix4 as Matrix46, Vector3 as Vector36 } from "three";
4262
4266
  var p10 = prefix("BlockObject");
4267
+ function matchesCondition(condition, state) {
4268
+ return Object.entries(condition).every(([key, value]) => {
4269
+ if (Array.isArray(value)) {
4270
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4271
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4272
+ return false;
4273
+ }
4274
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4275
+ });
4276
+ }
4263
4277
  var BlockObject = class _BlockObject extends SceneObject {
4264
4278
  constructor(blockState, options) {
4265
4279
  super(options);
@@ -4293,15 +4307,9 @@ var BlockObject = class _BlockObject extends SceneObject {
4293
4307
  if (this.blockState.variants) {
4294
4308
  await this.setState(Object.keys(this.blockState.variants)[0]);
4295
4309
  } else if (this.blockState.multipart) {
4296
- for (let part of this.blockState.multipart) {
4297
- if (part.when && !("OR" in part.when)) {
4298
- const k = Object.keys(part.when)[0];
4299
- if (k) {
4300
- await this.setState(k, part.when[k].split("|")[0]);
4301
- break;
4302
- }
4303
- }
4304
- }
4310
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4311
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4312
+ await this.setState(state);
4305
4313
  }
4306
4314
  }
4307
4315
  } else {
@@ -4361,52 +4369,8 @@ var BlockObject = class _BlockObject extends SceneObject {
4361
4369
  console.debug(p10, "Missing apply for blockState part", part);
4362
4370
  continue;
4363
4371
  }
4364
- if (!part.when) {
4372
+ if (!part.when || matchesCondition(part.when, state)) {
4365
4373
  out.push(this.getSingleVariant(part.apply));
4366
- } else {
4367
- let matches = true;
4368
- if ("OR" in part.when) {
4369
- const or = part.when.OR;
4370
- let anyMatch = false;
4371
- for (let o of or) {
4372
- for (let k in o) {
4373
- const split = o[k].split("|");
4374
- let m = false;
4375
- for (let s of split) {
4376
- if (`${state[k]}` === `${s}`) {
4377
- m = true;
4378
- break;
4379
- }
4380
- }
4381
- if (m) {
4382
- anyMatch = true;
4383
- break;
4384
- }
4385
- }
4386
- }
4387
- if (!anyMatch) {
4388
- matches = false;
4389
- }
4390
- } else {
4391
- for (let k in part.when) {
4392
- const split = part.when[k].split("|");
4393
- let m = false;
4394
- for (let s of split) {
4395
- if (`${state[k]}` === `${s}`) {
4396
- m = true;
4397
- break;
4398
- }
4399
- }
4400
- if (!m) {
4401
- matches = false;
4402
- break;
4403
- }
4404
- }
4405
- }
4406
- if (matches) {
4407
- const variants = part.apply;
4408
- out.push(this.getSingleVariant(variants));
4409
- }
4410
4374
  }
4411
4375
  }
4412
4376
  }
@@ -4436,11 +4400,21 @@ var BlockObject = class _BlockObject extends SceneObject {
4436
4400
  }
4437
4401
  }
4438
4402
  getSingleVariant(variants) {
4439
- if (Array.isArray(variants)) {
4440
- return variants[0];
4441
- } else {
4442
- return variants;
4443
- }
4403
+ if (!Array.isArray(variants)) return variants;
4404
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4405
+ const total = variants.reduce((sum, variant) => {
4406
+ const weight = variant.weight ?? 1;
4407
+ if (!Number.isInteger(weight) || weight < 1) {
4408
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4409
+ }
4410
+ return sum + weight;
4411
+ }, 0);
4412
+ let choice = Math.random() * total;
4413
+ for (const variant of variants) {
4414
+ choice -= variant.weight ?? 1;
4415
+ if (choice < 0) return variant;
4416
+ }
4417
+ return variants[variants.length - 1];
4444
4418
  }
4445
4419
  // @deprecated
4446
4420
  async createAVariant(variants, instanceInfo) {