minerender 2.1.0-alpha.23 → 2.1.0-alpha.24

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.
@@ -137,7 +137,6 @@ __export(index_node_exports, {
137
137
  BUILTIN_GENERATED: () => BUILTIN_GENERATED,
138
138
  BasicAssetKey: () => BasicAssetKey,
139
139
  BatchedExecutor: () => BatchedExecutor,
140
- BlockEntityModels: () => BlockEntityModels,
141
140
  BlockObject: () => BlockObject,
142
141
  BlockStates: () => BlockStates,
143
142
  BlockTints: () => BlockTints,
@@ -157,7 +156,6 @@ __export(index_node_exports, {
157
156
  DisplayPosition: () => DisplayPosition,
158
157
  DisplayTransforms: () => DisplayTransforms,
159
158
  Entities: () => Entities,
160
- EntityModels: () => EntityModels,
161
159
  EntityObject: () => EntityObject,
162
160
  Env: () => Env,
163
161
  Geometries: () => Geometries,
@@ -211,7 +209,6 @@ __export(index_node_exports, {
211
209
  addWireframeToObject: () => addWireframeToObject,
212
210
  applyElementRotation: () => applyElementRotation,
213
211
  applyGenericRotation: () => applyGenericRotation,
214
- applyModelPartRotation: () => applyModelPartRotation,
215
212
  axisToVec3: () => axisToVec3,
216
213
  base64decode: () => base64decode,
217
214
  base64encode: () => base64encode,
@@ -562,7 +559,7 @@ var Caching = class _Caching {
562
559
  this.blockTintCache = _Caching.createAssetCache();
563
560
  }
564
561
  static {
565
- this.entityModelsCache = _Caching.createAssetCache();
562
+ this.entityModelCache = _Caching.createAssetCache();
566
563
  }
567
564
  static createAssetCache() {
568
565
  return import_loading_cache.Caches.builder().expireAfterWrite(import_time.Time.minutes(10)).expireAfterAccess(import_time.Time.minutes(5)).expirationInterval(import_time.Time.seconds(30)).buildAsync();
@@ -587,7 +584,7 @@ var Caching = class _Caching {
587
584
  this.listAssetCache,
588
585
  this.defaultBlockStatesCache,
589
586
  this.blockTintCache,
590
- this.entityModelsCache
587
+ this.entityModelCache
591
588
  ];
592
589
  }
593
590
  static clear() {
@@ -1697,14 +1694,6 @@ function applyElementRotation(rotation, geometry) {
1697
1694
  }
1698
1695
  geometry.translate(origin.x, origin.y, origin.z);
1699
1696
  }
1700
- function applyModelPartRotation(modelPart, geometry) {
1701
- const origin = new import_three8.Vector3(modelPart.pivotX, modelPart.pivotY, modelPart.pivotZ);
1702
- geometry.translate(-origin.x, -origin.y, -origin.z);
1703
- geometry.rotateX(toRadians(modelPart.pitch));
1704
- geometry.rotateY(toRadians(modelPart.yaw));
1705
- geometry.rotateZ(toRadians(modelPart.roll));
1706
- geometry.translate(origin.x, origin.y, origin.z);
1707
- }
1708
1697
  function applyGenericRotation(axis, rotation, obj) {
1709
1698
  switch (axis) {
1710
1699
  case "x" /* X */:
@@ -2761,18 +2750,18 @@ var MinecraftCubeTexture = class {
2761
2750
  }
2762
2751
  getUvEast(w, h, l) {
2763
2752
  return [
2764
- [this.mapW(this.u), this.mapH(this.v + l + h)],
2765
- [this.mapW(this.u + l), this.mapH(this.v + l + h)],
2766
- [this.mapW(this.u), this.mapH(this.v + l)],
2767
- [this.mapW(this.u + l), this.mapH(this.v + l)]
2753
+ [this.mapW(this.u + l + w + l), this.mapH(this.v + l + h)],
2754
+ [this.mapW(this.u + l + w), this.mapH(this.v + l + h)],
2755
+ [this.mapW(this.u + l + w + l), this.mapH(this.v + l)],
2756
+ [this.mapW(this.u + l + w), this.mapH(this.v + l)]
2768
2757
  ];
2769
2758
  }
2770
2759
  getUvWest(w, h, l) {
2771
2760
  return [
2772
- [this.mapW(this.u + l + w), this.mapH(this.v + l + h)],
2773
- [this.mapW(this.u + l + w + l), this.mapH(this.v + l + h)],
2774
- [this.mapW(this.u + l + w), this.mapH(this.v + l)],
2775
- [this.mapW(this.u + l + w + l), this.mapH(this.v + l)]
2761
+ [this.mapW(this.u + l), this.mapH(this.v + l + h)],
2762
+ [this.mapW(this.u), this.mapH(this.v + l + h)],
2763
+ [this.mapW(this.u + l), this.mapH(this.v + l)],
2764
+ [this.mapW(this.u), this.mapH(this.v + l)]
2776
2765
  ];
2777
2766
  }
2778
2767
  getUvUp(w, h, l) {
@@ -2785,26 +2774,26 @@ var MinecraftCubeTexture = class {
2785
2774
  }
2786
2775
  getUvDown(w, h, l) {
2787
2776
  return [
2788
- [this.mapW(this.u + l), this.mapH(this.v + l)],
2789
- [this.mapW(this.u + l + w), this.mapH(this.v + l)],
2790
2777
  [this.mapW(this.u + l), this.mapH(this.v)],
2791
- [this.mapW(this.u + l + w), this.mapH(this.v)]
2778
+ [this.mapW(this.u + l + w), this.mapH(this.v)],
2779
+ [this.mapW(this.u + l), this.mapH(this.v + l)],
2780
+ [this.mapW(this.u + l + w), this.mapH(this.v + l)]
2792
2781
  ];
2793
2782
  }
2794
2783
  getUvSouth(w, h, l) {
2795
2784
  return [
2796
- [this.mapW(this.u + l + w + l), this.mapH(this.v + l + h)],
2797
2785
  [this.mapW(this.u + l + w + l + w), this.mapH(this.v + l + h)],
2798
- [this.mapW(this.u + l + w + l), this.mapH(this.v + l)],
2799
- [this.mapW(this.u + l + w + l + w), this.mapH(this.v + l)]
2786
+ [this.mapW(this.u + l + w + l), this.mapH(this.v + l + h)],
2787
+ [this.mapW(this.u + l + w + l + w), this.mapH(this.v + l)],
2788
+ [this.mapW(this.u + l + w + l), this.mapH(this.v + l)]
2800
2789
  ];
2801
2790
  }
2802
2791
  getUvNorth(w, h, l) {
2803
2792
  return [
2804
- [this.mapW(this.u + l), this.mapH(this.v + l + h)],
2805
2793
  [this.mapW(this.u + l + w), this.mapH(this.v + l + h)],
2806
- [this.mapW(this.u + l), this.mapH(this.v + l)],
2807
- [this.mapW(this.u + l + w), this.mapH(this.v + l)]
2794
+ [this.mapW(this.u + l), this.mapH(this.v + l + h)],
2795
+ [this.mapW(this.u + l + w), this.mapH(this.v + l)],
2796
+ [this.mapW(this.u + l), this.mapH(this.v + l)]
2808
2797
  ];
2809
2798
  }
2810
2799
  getFaceUv(face, w, h, l) {
@@ -2823,10 +2812,12 @@ var MinecraftCubeTexture = class {
2823
2812
  return this.getUvNorth(w, h, l);
2824
2813
  }
2825
2814
  }
2826
- toUvArray(w, h, l) {
2815
+ toUvArray(w, h, l, mirror = false) {
2827
2816
  const uv = [];
2828
2817
  for (let i = 0; i < CUBE_FACES.length; i++) {
2829
- UVMapper.setFaceUvInArray(uv, i * 4, this.getFaceUv(CUBE_FACES[i], w, h, l));
2818
+ const face = mirror && i < 2 ? CUBE_FACES[1 - i] : CUBE_FACES[i];
2819
+ const corners = this.getFaceUv(face, w, h, l);
2820
+ UVMapper.setFaceUvInArray(uv, i * 4, mirror ? [corners[1], corners[0], corners[3], corners[2]] : corners);
2830
2821
  }
2831
2822
  return uv;
2832
2823
  }
@@ -2856,69 +2847,51 @@ function shutdown() {
2856
2847
  Caching.end();
2857
2848
  }
2858
2849
 
2859
- // src/assets/Entities.ts
2860
- var Entities = class {
2861
- static async getBlockEntityModels() {
2862
- const key = AssetKey.parse("models", "minerender:blockEntityModels");
2863
- console.log(key);
2864
- return Caching.entityModelsCache.get(key.serialize(), () => {
2865
- return AssetLoader.get(key, "json" /* JSON */);
2866
- });
2867
- }
2868
- static async getEntityModels() {
2869
- const key = AssetKey.parse("models", "minerender:entityModels");
2870
- console.log(key);
2871
- return Caching.entityModelsCache.get(key.serialize(), () => {
2872
- return AssetLoader.get(key, "json" /* JSON */);
2873
- });
2850
+ // src/error/MineRenderError.ts
2851
+ var MineRenderError = class _MineRenderError extends Error {
2852
+ constructor(msg) {
2853
+ super(msg);
2854
+ Object.setPrototypeOf(this, _MineRenderError.prototype);
2874
2855
  }
2875
- // BlockEntity names are hardcoded
2876
- static async getBlockList() {
2877
- const models = await this.getBlockEntityModels();
2878
- if (!models) {
2879
- return [];
2880
- }
2881
- return Object.keys(models);
2856
+ get name() {
2857
+ return "MineRenderError";
2882
2858
  }
2859
+ };
2860
+
2861
+ // src/assets/Entities.ts
2862
+ var Entities = class {
2883
2863
  static async getEntityList() {
2884
- const models = await this.getEntityModels();
2885
- if (!models) {
2886
- return [];
2887
- }
2888
- return Object.keys(models);
2889
- }
2890
- static async getBlock(modelKey, textureKey) {
2891
- const models = await this.getBlockEntityModels();
2892
- if (!models) {
2893
- return void 0;
2894
- }
2895
- if (!textureKey) {
2896
- textureKey = modelKey;
2897
- }
2898
- const baseKey = modelKey.path.includes("/") ? new BasicAssetKey(modelKey.namespace, modelKey.path.split("/")[0]) : modelKey;
2899
- return {
2900
- key: textureKey,
2901
- parts: models[baseKey.toNamespacedString()]
2864
+ const collect = async (path) => {
2865
+ const prefix2 = path ? `${path}/` : "";
2866
+ const key = new AssetKey(DEFAULT_NAMESPACE, `${prefix2}_list`, void 0, void 0, "entity-models", ".json");
2867
+ const list = await Caching.listAssetCache.get(key.serialize(), () => AssetLoader.get(key, "list" /* LIST */));
2868
+ if (!list) return [];
2869
+ const files = list.files.filter((file) => file.endsWith(".json") && file !== "_list.json").map((file) => `${prefix2}${file.slice(0, -5)}`);
2870
+ const children = await Promise.all(list.directories.map((directory) => collect(`${prefix2}${directory}`)));
2871
+ return [...files, ...children.flat()];
2902
2872
  };
2873
+ return collect("");
2903
2874
  }
2904
- static async getEntity(modelKey, textureKey) {
2905
- const models = await this.getEntityModels();
2906
- if (!models) {
2907
- return void 0;
2908
- }
2909
- if (!textureKey) {
2910
- textureKey = modelKey;
2875
+ static async getEntity(modelKey, textureKey, options) {
2876
+ const path = isAssetKey(modelKey) ? modelKey.getFullPath() : modelKey.path;
2877
+ const key = new AssetKey(modelKey.namespace, path, void 0, void 0, "entity-models", ".json");
2878
+ const model = await Caching.entityModelCache.get(key.serialize(), () => AssetLoader.get(key, "json" /* JSON */));
2879
+ if (!model) return void 0;
2880
+ const layerName = options?.layer ?? "main";
2881
+ const layer = model.layers[layerName];
2882
+ if (!layer) {
2883
+ throw new MineRenderError(`Entity ${model.id} has no layer "${layerName}". Available layers: ${Object.keys(model.layers).join(", ")}`);
2911
2884
  }
2912
- const baseKey = modelKey.path.includes("/") ? new BasicAssetKey(modelKey.namespace, modelKey.path.split("/")[0]) : modelKey;
2913
- return {
2914
- key: textureKey,
2915
- parts: models[baseKey.toNamespacedString()]
2916
- };
2885
+ return { key: textureKey ?? modelKey, layer, id: model.id };
2886
+ }
2887
+ /** @deprecated Use getEntityList(); block entities share the entity dataset. */
2888
+ static getBlockList() {
2889
+ return this.getEntityList();
2890
+ }
2891
+ /** @deprecated Use getEntity(); block entities share the entity dataset. */
2892
+ static getBlock(modelKey, textureKey, options) {
2893
+ return this.getEntity(modelKey, textureKey, options);
2917
2894
  }
2918
- };
2919
- var EntityModels = class {
2920
- };
2921
- var BlockEntityModels = class {
2922
2895
  };
2923
2896
 
2924
2897
  // src/renderer/SceneObject.ts
@@ -2967,17 +2940,6 @@ function isInstanceReference(obj) {
2967
2940
  return obj.isInstanceReference;
2968
2941
  }
2969
2942
 
2970
- // src/error/MineRenderError.ts
2971
- var MineRenderError = class _MineRenderError extends Error {
2972
- constructor(msg) {
2973
- super(msg);
2974
- Object.setPrototypeOf(this, _MineRenderError.prototype);
2975
- }
2976
- get name() {
2977
- return "MineRenderError";
2978
- }
2979
- };
2980
-
2981
2943
  // src/renderer/SceneObject.ts
2982
2944
  var import_ts_deepmerge3 = __toESM(require_dist());
2983
2945
  var p6 = prefix("SceneObject");
@@ -3111,15 +3073,8 @@ var SceneObject = class _SceneObject extends import_three10.Object3D {
3111
3073
  return this.toggleObjectVisibility(this.getMeshByName(name), visible);
3112
3074
  }
3113
3075
  iterateAllMeshes(cb) {
3114
- this.children.forEach((obj) => {
3115
- if (obj.isMesh) {
3116
- cb(obj);
3117
- }
3118
- obj.children.forEach((obj1) => {
3119
- if (obj1.isMesh) {
3120
- cb(obj1);
3121
- }
3122
- });
3076
+ this.traverse((obj) => {
3077
+ if (isMesh(obj)) cb(obj);
3123
3078
  });
3124
3079
  }
3125
3080
  //</editor-fold>
@@ -3357,17 +3312,16 @@ function isSceneObject(obj) {
3357
3312
  // src/entity/scene/EntityObject.ts
3358
3313
  var import_ts_deepmerge4 = __toESM(require_dist());
3359
3314
  var import_three12 = require("three");
3360
- var THREE3 = __toESM(require("three"));
3361
- var EntityObject = class extends SceneObject {
3315
+ var EntityObject = class _EntityObject extends SceneObject {
3362
3316
  constructor(entity, options) {
3363
3317
  super();
3364
3318
  this.entity = entity;
3365
3319
  this.isEntityObject = true;
3366
3320
  this.meshesCreated = false;
3367
- this.options = (0, import_ts_deepmerge4.default)({}, SceneObject.DEFAULT_OPTIONS, options ?? {});
3321
+ this.options = (0, import_ts_deepmerge4.default)({}, _EntityObject.DEFAULT_OPTIONS, options ?? {});
3368
3322
  }
3369
3323
  static {
3370
- this.DEFAULT_OPTIONS = (0, import_ts_deepmerge4.default)({}, SceneObject.DEFAULT_OPTIONS, {});
3324
+ this.DEFAULT_OPTIONS = (0, import_ts_deepmerge4.default)({}, SceneObject.DEFAULT_OPTIONS, { flip: true });
3371
3325
  }
3372
3326
  async init() {
3373
3327
  this.createMeshes();
@@ -3377,10 +3331,10 @@ var EntityObject = class extends SceneObject {
3377
3331
  super.dispose();
3378
3332
  }
3379
3333
  get textureKey() {
3334
+ const key = this.entity.key;
3380
3335
  return new AssetKey(
3381
- this.entity.key?.namespace ?? DEFAULT_NAMESPACE,
3382
- this.entity.key.path,
3383
- //TODO: texture may differ from entity name; most of them are in subdirectories for multiple variants etc.
3336
+ key.namespace,
3337
+ isAssetKey(key) ? key.getFullPath() : key.path,
3384
3338
  "textures",
3385
3339
  "entity",
3386
3340
  "assets",
@@ -3390,36 +3344,41 @@ var EntityObject = class extends SceneObject {
3390
3344
  async loadTextures() {
3391
3345
  this.imageData = await ModelTextures.get(this.textureKey);
3392
3346
  }
3393
- //TODO: abstract this, since extracted model data can be used for entities, blocks, players
3394
3347
  createMeshes(force = false) {
3395
3348
  if (this.meshesCreated && !force) return;
3396
- const mat = Materials.MISSING_TEXTURE;
3397
- for (let partName in this.entity.parts) {
3398
- const part = this.entity.parts[partName];
3399
- const texture = new MinecraftCubeTexture(part.textureOffsetU, part.textureOffsetV, part.textureWidth, part.textureHeight);
3400
- const partAnchor = new import_three12.Object3D();
3401
- partAnchor.position.x = part.pivotX;
3402
- partAnchor.position.y = part.pivotY;
3403
- partAnchor.position.z = part.pivotZ;
3404
- for (let cube of part.cubes) {
3405
- const w = cube.maxX - cube.minX;
3406
- const h = cube.maxY - cube.minY;
3407
- const l = cube.maxZ - cube.minZ;
3408
- const uv = texture.toUvArray(w, h, l);
3409
- const cubeGeo = this._getBoxGeometryForDimensionsAndUv(w, h, l, uv).clone();
3410
- cubeGeo.applyMatrix4(new THREE3.Matrix4().makeTranslation(w / 2, h / 2, l / 2));
3411
- cubeGeo.applyMatrix4(new THREE3.Matrix4().makeTranslation(cube.minX, cube.minY, cube.minZ));
3412
- applyModelPartRotation(part, cubeGeo);
3413
- const mesh = this.createMesh(partName, cubeGeo, mat);
3414
- partAnchor.add(mesh);
3415
- if (this.options.wireframe) {
3416
- addWireframeToMesh(cubeGeo, mesh);
3417
- }
3418
- }
3419
- this.add(partAnchor);
3420
- }
3349
+ const modelRoot = new import_three12.Object3D();
3350
+ if (this.options.flip) modelRoot.scale.set(-1, -1, 1);
3351
+ this.add(modelRoot);
3352
+ this.createPart("root", this.entity.layer.root, modelRoot, this.entity.layer.texture, Materials.MISSING_TEXTURE);
3421
3353
  this.meshesCreated = true;
3422
3354
  }
3355
+ createPart(name, part, parent, textureSize, material) {
3356
+ const anchor = this.createGroup(name);
3357
+ anchor.position.fromArray(part.pose.offset);
3358
+ anchor.rotation.set(...part.pose.rotation, "ZYX");
3359
+ anchor.scale.fromArray(part.pose.scale ?? [1, 1, 1]);
3360
+ parent.add(anchor);
3361
+ const size = part.texture ?? textureSize;
3362
+ for (const cube of part.cubes) {
3363
+ const [width, height, depth] = cube.size;
3364
+ const [growX, growY, growZ] = cube.grow ?? [0, 0, 0];
3365
+ const texture = new MinecraftCubeTexture(...cube.uv, ...size);
3366
+ const uv = size[0] === 0 || size[1] === 0 ? new Array(48).fill(0) : texture.toUvArray(width, height, depth, cube.mirror);
3367
+ const geometry = this._getBoxGeometryForDimensionsAndUv(
3368
+ width + growX * 2,
3369
+ height + growY * 2,
3370
+ depth + growZ * 2,
3371
+ uv
3372
+ ).clone();
3373
+ geometry.translate(cube.origin[0] + width / 2, cube.origin[1] + height / 2, cube.origin[2] + depth / 2);
3374
+ const mesh = this.createMesh(name, geometry, material);
3375
+ anchor.add(mesh);
3376
+ if (this.options.wireframe) addWireframeToMesh(geometry, mesh);
3377
+ }
3378
+ for (const [childName, child] of Object.entries(part.children)) {
3379
+ this.createPart(childName, child, anchor, size, material);
3380
+ }
3381
+ }
3423
3382
  async applyTextures() {
3424
3383
  const assetKeyStr = this.textureKey.serialize();
3425
3384
  const keyStr = `entity:${assetKeyStr}`;
@@ -7328,7 +7287,6 @@ var Chunk = class _Chunk {
7328
7287
  BUILTIN_GENERATED,
7329
7288
  BasicAssetKey,
7330
7289
  BatchedExecutor,
7331
- BlockEntityModels,
7332
7290
  BlockObject,
7333
7291
  BlockStates,
7334
7292
  BlockTints,
@@ -7348,7 +7306,6 @@ var Chunk = class _Chunk {
7348
7306
  DisplayPosition,
7349
7307
  DisplayTransforms,
7350
7308
  Entities,
7351
- EntityModels,
7352
7309
  EntityObject,
7353
7310
  Env,
7354
7311
  Geometries,
@@ -7402,7 +7359,6 @@ var Chunk = class _Chunk {
7402
7359
  addWireframeToObject,
7403
7360
  applyElementRotation,
7404
7361
  applyGenericRotation,
7405
- applyModelPartRotation,
7406
7362
  axisToVec3,
7407
7363
  base64decode,
7408
7364
  base64encode,