minerender 2.1.0-alpha.36 → 2.1.0-alpha.37

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.
@@ -32,8 +32,8 @@ var require_dist = __commonJS({
32
32
  __assign = Object.assign || function(t) {
33
33
  for (var s, i = 1, n = arguments.length; i < n; i++) {
34
34
  s = arguments[i];
35
- for (var p12 in s) if (Object.prototype.hasOwnProperty.call(s, p12))
36
- t[p12] = s[p12];
35
+ for (var p11 in s) if (Object.prototype.hasOwnProperty.call(s, p11))
36
+ t[p11] = s[p11];
37
37
  }
38
38
  return t;
39
39
  };
@@ -1165,12 +1165,12 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1165
1165
  return a.join("/");
1166
1166
  }
1167
1167
  getFullPath() {
1168
- let p12 = [];
1168
+ let p11 = [];
1169
1169
  if (this.type) {
1170
- p12.push(this.type);
1170
+ p11.push(this.type);
1171
1171
  }
1172
- p12.push(this.path);
1173
- return p12.join("/");
1172
+ p11.push(this.path);
1173
+ return p11.join("/");
1174
1174
  }
1175
1175
  toNamespacedString() {
1176
1176
  return this.namespace + ":" + this.getFullPath();
@@ -1257,7 +1257,7 @@ var ModelMerger = class {
1257
1257
  merged.display = { ...inheritedDisplay, ...DisplayTransforms.withHandFallbacks(source.display) };
1258
1258
  }
1259
1259
  }
1260
- merged.hierarchy = models.map((m) => m.parent).filter((p12) => `${p12}`);
1260
+ merged.hierarchy = models.map((m) => m.parent).filter((p11) => `${p11}`);
1261
1261
  merged.hierarchy.push(`${merged.parent}`);
1262
1262
  return merged;
1263
1263
  }
@@ -7191,6 +7191,87 @@ var Skins = class {
7191
7191
  // src/world/Chunk.ts
7192
7192
  import { Vector3 as Vector311 } from "three";
7193
7193
 
7194
+ // src/world/ChunkData.ts
7195
+ var ChunkData = class _ChunkData {
7196
+ constructor() {
7197
+ this.indices = new Uint16Array(4096);
7198
+ this.palette = [void 0];
7199
+ this.paletteIds = /* @__PURE__ */ new Map();
7200
+ this.freeIds = [];
7201
+ this.nbt = /* @__PURE__ */ new Map();
7202
+ }
7203
+ get(index) {
7204
+ _ChunkData.validateIndex(index);
7205
+ const entry = this.palette[this.indices[index]];
7206
+ return entry ? _ChunkData.copyBlock(entry.state, this.nbt.get(index)) : void 0;
7207
+ }
7208
+ snapshot(index) {
7209
+ _ChunkData.validateIndex(index);
7210
+ const entry = this.palette[this.indices[index]];
7211
+ return entry ? _ChunkData.copyBlock.bind(void 0, entry.state, this.nbt.get(index)) : void 0;
7212
+ }
7213
+ set(index, block) {
7214
+ _ChunkData.validateIndex(index);
7215
+ let state;
7216
+ let key;
7217
+ let nbt;
7218
+ if (block && !_ChunkData.isAir(block)) {
7219
+ const type = AssetKey.parse("blockstates", block.type).toNamespacedString();
7220
+ const properties = Object.fromEntries(Object.keys(block.properties ?? {}).sort().map((name) => [name, block.properties[name]]));
7221
+ state = Object.freeze(Object.keys(properties).length ? { type, properties: Object.freeze(properties) } : { type });
7222
+ key = JSON.stringify([type, properties]);
7223
+ if (block.nbt !== void 0) nbt = structuredClone(block.nbt);
7224
+ }
7225
+ const previousId = this.indices[index];
7226
+ if (previousId !== 0) {
7227
+ const previous = this.palette[previousId];
7228
+ if (--previous.references === 0) {
7229
+ this.paletteIds.delete(previous.key);
7230
+ this.palette[previousId] = void 0;
7231
+ this.freeIds.push(previousId);
7232
+ }
7233
+ }
7234
+ let id = 0;
7235
+ if (state && key !== void 0) {
7236
+ const existingId = this.paletteIds.get(key);
7237
+ if (existingId !== void 0) {
7238
+ id = existingId;
7239
+ this.palette[id].references++;
7240
+ } else {
7241
+ id = this.freeIds.pop() ?? this.palette.length;
7242
+ this.palette[id] = { key, state, references: 1 };
7243
+ this.paletteIds.set(key, id);
7244
+ }
7245
+ }
7246
+ this.indices[index] = id;
7247
+ if (nbt !== void 0) this.nbt.set(index, nbt);
7248
+ else this.nbt.delete(index);
7249
+ }
7250
+ clear() {
7251
+ this.indices.fill(0);
7252
+ this.palette.length = 1;
7253
+ this.paletteIds.clear();
7254
+ this.freeIds.length = 0;
7255
+ this.nbt.clear();
7256
+ }
7257
+ static isAir(block) {
7258
+ if (typeof block === "undefined" || typeof block.type === "undefined") return true;
7259
+ const key = AssetKey.parse("blockstates", block.type);
7260
+ return key.namespace === "minecraft" && !key.type && ["air", "cave_air", "void_air"].includes(key.path);
7261
+ }
7262
+ static copyBlock(state, nbt) {
7263
+ const block = { type: state.type };
7264
+ if (state.properties) block.properties = { ...state.properties };
7265
+ if (nbt !== void 0) block.nbt = structuredClone(nbt);
7266
+ return block;
7267
+ }
7268
+ static validateIndex(index) {
7269
+ if (!Number.isInteger(index) || index < 0 || index >= 4096) {
7270
+ throw new RangeError("Block index must be an integer from 0 to 4095");
7271
+ }
7272
+ }
7273
+ };
7274
+
7194
7275
  // src/world/MineRenderWorld.ts
7195
7276
  import { Vector3 as Vector310 } from "three";
7196
7277
  var MineRenderWorld = class {
@@ -7301,10 +7382,10 @@ var MineRenderWorld = class {
7301
7382
  };
7302
7383
 
7303
7384
  // src/world/Chunk.ts
7304
- var p11 = prefix("Chunk");
7305
7385
  var Chunk = class _Chunk {
7306
7386
  constructor(scene, x, y, z) {
7307
- this._blocks = [];
7387
+ this.data = new ChunkData();
7388
+ this.renderedBlocks = /* @__PURE__ */ new Map();
7308
7389
  this.scene = scene;
7309
7390
  this.x = x;
7310
7391
  this.y = y;
@@ -7319,7 +7400,7 @@ var Chunk = class _Chunk {
7319
7400
  }
7320
7401
  const pos = this.worldPosToChunkPos(posOrX);
7321
7402
  const index = _Chunk.chunkPosToBlockIndex(pos);
7322
- return this._blocks[index];
7403
+ return this.renderedBlocks.get(index);
7323
7404
  }
7324
7405
  async setBlockAt(posOrX, yOrBlock, z, block) {
7325
7406
  if (typeof posOrX == "number") {
@@ -7341,44 +7422,47 @@ var Chunk = class _Chunk {
7341
7422
  worldPos = this.chunkPosToWorldPos(pos);
7342
7423
  }
7343
7424
  const index = _Chunk.chunkPosToBlockIndex(pos);
7344
- const current = this._blocks[index];
7345
- if (typeof current !== "undefined") {
7346
- console.debug(p11, "deleting existing block at", pos, worldPos, index);
7347
- current.object.removeFromScene();
7348
- delete this._blocks[index];
7349
- }
7350
- if (!block || _Chunk.isAir(block)) return void 0;
7351
- const blockState = await BlockStates.get(AssetKey.parse("blockstates", block.type));
7352
- if (blockState) {
7353
- const blockObject = await this.scene.addBlock(blockState, {
7425
+ this.data.set(index, block);
7426
+ const readBlock = this.data.snapshot(index);
7427
+ this.renderedBlocks.get(index)?.object.removeFromScene();
7428
+ this.renderedBlocks.delete(index);
7429
+ if (!readBlock) return void 0;
7430
+ const stored = readBlock();
7431
+ let object;
7432
+ try {
7433
+ const blockState = await BlockStates.get(AssetKey.parse("blockstates", stored.type));
7434
+ if (!blockState) {
7435
+ this.data.set(index, void 0);
7436
+ return void 0;
7437
+ }
7438
+ object = await this.scene.addBlock(blockState, {
7354
7439
  mergeMeshes: true,
7355
7440
  instanceMeshes: true,
7356
7441
  maxInstanceCount: 2e3
7357
7442
  });
7358
- if (block.properties) {
7359
- await blockObject.setState(block.properties);
7360
- }
7361
- const scenePos = MineRenderWorld.worldToScenePosition(worldPos);
7362
- blockObject.setPosition(scenePos);
7363
- this._blocks[index] = {
7364
- block,
7365
- object: blockObject
7366
- };
7367
- return this._blocks[index];
7443
+ if (stored.properties) await object.setState(stored.properties);
7444
+ object.setPosition(MineRenderWorld.worldToScenePosition(worldPos));
7445
+ const info = { get block() {
7446
+ return readBlock();
7447
+ }, object };
7448
+ this.renderedBlocks.set(index, info);
7449
+ return info;
7450
+ } catch (error) {
7451
+ this.data.set(index, void 0);
7452
+ object?.removeFromScene();
7453
+ throw error;
7368
7454
  }
7369
- return void 0;
7370
7455
  }
7371
7456
  async clear() {
7372
- this._blocks.forEach((block) => block.object.removeFromScene());
7373
- this._blocks.length = 0;
7457
+ for (const info of this.renderedBlocks.values()) info.object.removeFromScene();
7458
+ this.renderedBlocks.clear();
7459
+ this.data.clear();
7374
7460
  }
7375
7461
  async dispose() {
7376
7462
  await this.clear();
7377
7463
  }
7378
7464
  static isAir(block) {
7379
- if (typeof block === "undefined" || typeof block.type === "undefined") return true;
7380
- const key = AssetKey.parse("blockstates", block.type);
7381
- return key.namespace === "minecraft" && !key.type && ["air", "cave_air", "void_air"].includes(key.path);
7465
+ return ChunkData.isAir(block);
7382
7466
  }
7383
7467
  static chunkPosToBlockIndex(pos) {
7384
7468
  if ([pos.x, pos.y, pos.z].some((value) => !Number.isInteger(value) || value < 0 || value >= 16)) {
@@ -7425,6 +7509,7 @@ export {
7425
7509
  Caching,
7426
7510
  CanvasImage,
7427
7511
  Chunk,
7512
+ ChunkData,
7428
7513
  CubeFace,
7429
7514
  CubeFaceIndex,
7430
7515
  DEFAULT_ELEMENTS,