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.
@@ -952,7 +952,11 @@ var _Materials = class _Materials {
952
952
 
953
953
  if (SHADE) {
954
954
 
955
- vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
955
+ #ifdef USE_INSTANCING
956
+ vec3 N = vec3( modelMatrix * instanceMatrix * vec4(normal, 0.0) );
957
+ #else
958
+ vec3 N = vec3( modelMatrix * vec4(normal, 0.0) );
959
+ #endif
956
960
 
957
961
  float yLight = (1.0+N.y) * 0.5;
958
962
  light = yLight * (1.0-AMBIENT) + N.x*N.x * XFAC + N.z*N.z * ZFAC + AMBIENT;
@@ -4218,6 +4222,16 @@ function isModelObject(obj) {
4218
4222
  var import_ts_deepmerge6 = __toESM(require_dist());
4219
4223
  import { Euler as Euler3, Matrix4 as Matrix46, Vector3 as Vector36 } from "three";
4220
4224
  var p10 = prefix("BlockObject");
4225
+ function matchesCondition(condition, state) {
4226
+ return Object.entries(condition).every(([key, value]) => {
4227
+ if (Array.isArray(value)) {
4228
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4229
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4230
+ return false;
4231
+ }
4232
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4233
+ });
4234
+ }
4221
4235
  var _BlockObject = class _BlockObject extends SceneObject {
4222
4236
  constructor(blockState, options) {
4223
4237
  super(options);
@@ -4246,15 +4260,9 @@ var _BlockObject = class _BlockObject extends SceneObject {
4246
4260
  if (this.blockState.variants) {
4247
4261
  await this.setState(Object.keys(this.blockState.variants)[0]);
4248
4262
  } else if (this.blockState.multipart) {
4249
- for (let part of this.blockState.multipart) {
4250
- if (part.when && !("OR" in part.when)) {
4251
- const k = Object.keys(part.when)[0];
4252
- if (k) {
4253
- await this.setState(k, part.when[k].split("|")[0]);
4254
- break;
4255
- }
4256
- }
4257
- }
4263
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4264
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4265
+ await this.setState(state);
4258
4266
  }
4259
4267
  }
4260
4268
  } else {
@@ -4314,52 +4322,8 @@ var _BlockObject = class _BlockObject extends SceneObject {
4314
4322
  console.debug(p10, "Missing apply for blockState part", part);
4315
4323
  continue;
4316
4324
  }
4317
- if (!part.when) {
4325
+ if (!part.when || matchesCondition(part.when, state)) {
4318
4326
  out.push(this.getSingleVariant(part.apply));
4319
- } else {
4320
- let matches = true;
4321
- if ("OR" in part.when) {
4322
- const or = part.when.OR;
4323
- let anyMatch = false;
4324
- for (let o of or) {
4325
- for (let k in o) {
4326
- const split = o[k].split("|");
4327
- let m = false;
4328
- for (let s of split) {
4329
- if (`${state[k]}` === `${s}`) {
4330
- m = true;
4331
- break;
4332
- }
4333
- }
4334
- if (m) {
4335
- anyMatch = true;
4336
- break;
4337
- }
4338
- }
4339
- }
4340
- if (!anyMatch) {
4341
- matches = false;
4342
- }
4343
- } else {
4344
- for (let k in part.when) {
4345
- const split = part.when[k].split("|");
4346
- let m = false;
4347
- for (let s of split) {
4348
- if (`${state[k]}` === `${s}`) {
4349
- m = true;
4350
- break;
4351
- }
4352
- }
4353
- if (!m) {
4354
- matches = false;
4355
- break;
4356
- }
4357
- }
4358
- }
4359
- if (matches) {
4360
- const variants = part.apply;
4361
- out.push(this.getSingleVariant(variants));
4362
- }
4363
4327
  }
4364
4328
  }
4365
4329
  }
@@ -4389,11 +4353,21 @@ var _BlockObject = class _BlockObject extends SceneObject {
4389
4353
  }
4390
4354
  }
4391
4355
  getSingleVariant(variants) {
4392
- if (Array.isArray(variants)) {
4393
- return variants[0];
4394
- } else {
4395
- return variants;
4396
- }
4356
+ if (!Array.isArray(variants)) return variants;
4357
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4358
+ const total = variants.reduce((sum, variant) => {
4359
+ const weight = variant.weight ?? 1;
4360
+ if (!Number.isInteger(weight) || weight < 1) {
4361
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4362
+ }
4363
+ return sum + weight;
4364
+ }, 0);
4365
+ let choice = Math.random() * total;
4366
+ for (const variant of variants) {
4367
+ choice -= variant.weight ?? 1;
4368
+ if (choice < 0) return variant;
4369
+ }
4370
+ return variants[variants.length - 1];
4397
4371
  }
4398
4372
  // @deprecated
4399
4373
  async createAVariant(variants, instanceInfo) {