minerender 2.1.0-alpha.34 → 2.1.0-alpha.35

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.
@@ -7243,15 +7243,12 @@ var Skins = class {
7243
7243
  };
7244
7244
 
7245
7245
  // src/world/Chunk.ts
7246
- import { Box3 as Box32, Object3D as Object3D6, Vector3 as Vector311 } from "three";
7246
+ import { Vector3 as Vector311 } from "three";
7247
7247
 
7248
7248
  // src/world/MineRenderWorld.ts
7249
7249
  import { Vector3 as Vector310 } from "three";
7250
7250
  var MineRenderWorld = class {
7251
7251
  constructor(scene) {
7252
- this._size = 4;
7253
- //TODO: expandable
7254
- this._blockSize = this._size * 16;
7255
7252
  this._chunks = /* @__PURE__ */ new Map();
7256
7253
  this.scene = scene;
7257
7254
  }
@@ -7262,12 +7259,8 @@ var MineRenderWorld = class {
7262
7259
  if (isTripleArray(posOrX)) {
7263
7260
  return this.getBlockAt(new Vector310(posOrX[0], posOrX[1], posOrX[2]));
7264
7261
  }
7265
- const chunk = this.getChunkAt(posOrX);
7266
- if (typeof chunk === "undefined") {
7267
- return void 0;
7268
- }
7269
7262
  this.validatePosBounds(posOrX);
7270
- return chunk.getBlockAt(posOrX);
7263
+ return this.getChunkAt(posOrX)?.getBlockAt(posOrX);
7271
7264
  }
7272
7265
  async setBlockAt(posOrX, yOrBlock, z, block) {
7273
7266
  if (typeof posOrX == "number") {
@@ -7277,8 +7270,9 @@ var MineRenderWorld = class {
7277
7270
  return this.setBlockAt(new Vector310(posOrX[0], posOrX[1], posOrX[2]), yOrBlock);
7278
7271
  }
7279
7272
  this.validatePosBounds(posOrX);
7280
- const chunk = this.getOrCreateChunkAt(posOrX);
7281
- return chunk.setBlockAt(posOrX, yOrBlock);
7273
+ const value = yOrBlock;
7274
+ const chunk = Chunk.isAir(value) ? this.getChunkAt(posOrX) : this.getOrCreateChunkAt(posOrX);
7275
+ return chunk?.setBlockAt(posOrX, value);
7282
7276
  }
7283
7277
  async placeMultiBlock(multiblock, useBatches = true, executor = new BatchedExecutor()) {
7284
7278
  const keys = new Set(multiblock.blocks.map((block) => AssetKey.parse("blockstates", block.type)));
@@ -7299,10 +7293,8 @@ var MineRenderWorld = class {
7299
7293
  }
7300
7294
  }
7301
7295
  async clear() {
7302
- console.log("CHUNKS", this._chunks);
7303
- for (let chunk of this._chunks.values()) {
7304
- console.log("dispose chunk", chunk);
7305
- await chunk?.dispose();
7296
+ for (const chunk of this._chunks.values()) {
7297
+ await chunk.dispose();
7306
7298
  }
7307
7299
  this._chunks.clear();
7308
7300
  }
@@ -7316,8 +7308,7 @@ var MineRenderWorld = class {
7316
7308
  return chunk;
7317
7309
  }
7318
7310
  getChunkAt(pos) {
7319
- const index = this.worldPosToChunkIndex(pos);
7320
- return this._chunks[index];
7311
+ return this._chunks.get(this.worldPosToChunkKey(pos));
7321
7312
  }
7322
7313
  static worldToScenePosition(pos) {
7323
7314
  return new Vector310(
@@ -7333,12 +7324,6 @@ var MineRenderWorld = class {
7333
7324
  pos.z / 16
7334
7325
  );
7335
7326
  }
7336
- worldPosToChunkIndex(pos) {
7337
- const chunkX = Math.floor(pos.x / 16);
7338
- const chunkY = Math.floor(pos.y / 16);
7339
- const chunkZ = Math.floor(pos.z / 16);
7340
- return chunkZ * this._size * this._size + chunkY * this._size + chunkX;
7341
- }
7342
7327
  worldPosToChunkKey(pos) {
7343
7328
  const chunkX = Math.floor(pos.x / 16);
7344
7329
  const chunkY = Math.floor(pos.y / 16);
@@ -7346,12 +7331,9 @@ var MineRenderWorld = class {
7346
7331
  return `${chunkX}_${chunkY}_${chunkZ}`;
7347
7332
  }
7348
7333
  validatePosBounds(pos) {
7349
- if (pos.x < 0) throw new Error("x<0");
7350
- if (pos.y < 0) throw new Error("y<0");
7351
- if (pos.z < 0) throw new Error("z<0");
7352
- if (pos.x > this._blockSize) throw new Error("x>" + this._blockSize);
7353
- if (pos.y > this._blockSize) throw new Error("y>" + this._blockSize);
7354
- if (pos.z > this._blockSize) throw new Error("z>" + this._blockSize);
7334
+ if (![pos.x, pos.y, pos.z].every(Number.isInteger)) {
7335
+ throw new RangeError("Block coordinates must be integers");
7336
+ }
7355
7337
  }
7356
7338
  };
7357
7339
 
@@ -7364,16 +7346,6 @@ var Chunk = class _Chunk {
7364
7346
  this.x = x;
7365
7347
  this.y = y;
7366
7348
  this.z = z;
7367
- let anchor = new Object3D6();
7368
- this._anchor = anchor;
7369
- scene.add(anchor);
7370
- anchor.position.set(this.x * 256 + 128 - 8, this.y * 256 + 128 - 8, this.z * 256 + 128 - 8);
7371
- addBox3WireframeToObject(
7372
- new Box32(new Vector311(0, 0, 0), new Vector311(256, 256, 256)),
7373
- anchor,
7374
- 16776960,
7375
- 4
7376
- );
7377
7349
  }
7378
7350
  getBlockAt(posOrX, y, z) {
7379
7351
  if (typeof posOrX == "number") {
@@ -7399,7 +7371,7 @@ var Chunk = class _Chunk {
7399
7371
  return this.setBlockInChunkAt(pos, block, worldPos);
7400
7372
  }
7401
7373
  /**
7402
- * Set block at a _chunk_ position (0-16)
7374
+ * Set a block at integer chunk-local coordinates from 0 to 15.
7403
7375
  */
7404
7376
  async setBlockInChunkAt(pos, block, worldPos) {
7405
7377
  if (typeof worldPos === "undefined") {
@@ -7412,15 +7384,13 @@ var Chunk = class _Chunk {
7412
7384
  current.object.removeFromScene();
7413
7385
  delete this._blocks[index];
7414
7386
  }
7415
- if (typeof block === "undefined" || typeof block.type === "undefined" || block.type === "air") return void 0;
7387
+ if (!block || _Chunk.isAir(block)) return void 0;
7416
7388
  const blockState = await BlockStates.get(AssetKey.parse("blockstates", block.type));
7417
7389
  if (blockState) {
7418
7390
  const blockObject = await this.scene.addBlock(blockState, {
7419
7391
  mergeMeshes: true,
7420
7392
  instanceMeshes: true,
7421
- wireframe: true,
7422
7393
  maxInstanceCount: 2e3
7423
- //TODO: setting this to high kills FPS; increase on demand somehow?
7424
7394
  });
7425
7395
  if (block.properties) {
7426
7396
  await blockObject.setState(block.properties);
@@ -7436,23 +7406,21 @@ var Chunk = class _Chunk {
7436
7406
  return void 0;
7437
7407
  }
7438
7408
  async clear() {
7439
- let promises = [];
7440
- let pos = new Vector311();
7441
- for (let x = 0; x < 16; x++) {
7442
- for (let z = 0; z < 16; z++) {
7443
- for (let y = 0; y < 16; y++) {
7444
- promises.push(this.setBlockInChunkAt(pos.set(x, y, z), void 0));
7445
- }
7446
- }
7447
- }
7448
- return Promise.all(promises).then((ignored) => {
7449
- });
7409
+ this._blocks.forEach((block) => block.object.removeFromScene());
7410
+ this._blocks.length = 0;
7450
7411
  }
7451
7412
  async dispose() {
7452
7413
  await this.clear();
7453
- this.scene.remove(this._anchor);
7414
+ }
7415
+ static isAir(block) {
7416
+ if (typeof block === "undefined" || typeof block.type === "undefined") return true;
7417
+ const key = AssetKey.parse("blockstates", block.type);
7418
+ return key.namespace === "minecraft" && !key.type && ["air", "cave_air", "void_air"].includes(key.path);
7454
7419
  }
7455
7420
  static chunkPosToBlockIndex(pos) {
7421
+ if ([pos.x, pos.y, pos.z].some((value) => !Number.isInteger(value) || value < 0 || value >= 16)) {
7422
+ throw new RangeError("Block coordinates must be integers from 0 to 15 within a chunk");
7423
+ }
7456
7424
  return pos.y * 16 * 16 + pos.z * 16 + pos.x;
7457
7425
  }
7458
7426
  worldPosToChunkPos(pos) {