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.
@@ -1692,7 +1692,6 @@ declare class Chunk {
1692
1692
  readonly y: number;
1693
1693
  readonly z: number;
1694
1694
  private readonly _blocks;
1695
- private readonly _anchor;
1696
1695
  constructor(scene: MineRenderScene, x: number, y: number, z: number);
1697
1696
  getBlockAt(x: number, y: number, z: number): Maybe<BlockInfo>;
1698
1697
  getBlockAt(pos: Vector3): Maybe<BlockInfo>;
@@ -1704,11 +1703,12 @@ declare class Chunk {
1704
1703
  setBlockAt(pos: Vector3, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1705
1704
  setBlockAt(pos: TripleArray, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1706
1705
  /**
1707
- * Set block at a _chunk_ position (0-16)
1706
+ * Set a block at integer chunk-local coordinates from 0 to 15.
1708
1707
  */
1709
1708
  setBlockInChunkAt(pos: Vector3, block?: Block, worldPos?: Vector3): Promise<Maybe<BlockInfo>>;
1710
1709
  clear(): Promise<void>;
1711
1710
  dispose(): Promise<void>;
1711
+ static isAir(block: Maybe<Block>): boolean;
1712
1712
  static chunkPosToBlockIndex(pos: Vector3): number;
1713
1713
  worldPosToChunkPos(pos: Vector3): Vector3;
1714
1714
  chunkPosToWorldPos(pos: Vector3): Vector3;
@@ -1716,23 +1716,20 @@ declare class Chunk {
1716
1716
 
1717
1717
  declare class MineRenderWorld {
1718
1718
  readonly scene: MineRenderScene;
1719
- private _size;
1720
- private _blockSize;
1721
1719
  private readonly _chunks;
1722
1720
  constructor(scene: MineRenderScene);
1723
1721
  getBlockAt(x: number, y: number, z: number): Maybe<BlockInfo>;
1724
1722
  getBlockAt(pos: Vector3): Maybe<BlockInfo>;
1725
1723
  getBlockAt(pos: TripleArray): Maybe<BlockInfo>;
1726
- setBlockAt(x: number, y: number, z: number, block: Block): Promise<Maybe<BlockInfo>>;
1727
- setBlockAt(pos: Vector3, block: Block): Promise<Maybe<BlockInfo>>;
1728
- setBlockAt(pos: TripleArray, block: Block): Promise<Maybe<BlockInfo>>;
1724
+ setBlockAt(x: number, y: number, z: number, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1725
+ setBlockAt(pos: Vector3, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1726
+ setBlockAt(pos: TripleArray, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1729
1727
  placeMultiBlock(multiblock: MultiBlockStructure, useBatches?: boolean, executor?: BatchedExecutor): Promise<void>;
1730
1728
  clear(): Promise<void>;
1731
1729
  private getOrCreateChunkAt;
1732
1730
  getChunkAt(pos: Vector3): Maybe<Chunk>;
1733
1731
  static worldToScenePosition(pos: Vector3): Vector3;
1734
1732
  static sceneToWorldPosition(pos: Vector3): Vector3;
1735
- worldPosToChunkIndex(pos: Vector3): number;
1736
1733
  worldPosToChunkKey(pos: Vector3): string;
1737
1734
  validatePosBounds(pos: Vector3): void;
1738
1735
  }
@@ -1692,7 +1692,6 @@ declare class Chunk {
1692
1692
  readonly y: number;
1693
1693
  readonly z: number;
1694
1694
  private readonly _blocks;
1695
- private readonly _anchor;
1696
1695
  constructor(scene: MineRenderScene, x: number, y: number, z: number);
1697
1696
  getBlockAt(x: number, y: number, z: number): Maybe<BlockInfo>;
1698
1697
  getBlockAt(pos: Vector3): Maybe<BlockInfo>;
@@ -1704,11 +1703,12 @@ declare class Chunk {
1704
1703
  setBlockAt(pos: Vector3, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1705
1704
  setBlockAt(pos: TripleArray, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1706
1705
  /**
1707
- * Set block at a _chunk_ position (0-16)
1706
+ * Set a block at integer chunk-local coordinates from 0 to 15.
1708
1707
  */
1709
1708
  setBlockInChunkAt(pos: Vector3, block?: Block, worldPos?: Vector3): Promise<Maybe<BlockInfo>>;
1710
1709
  clear(): Promise<void>;
1711
1710
  dispose(): Promise<void>;
1711
+ static isAir(block: Maybe<Block>): boolean;
1712
1712
  static chunkPosToBlockIndex(pos: Vector3): number;
1713
1713
  worldPosToChunkPos(pos: Vector3): Vector3;
1714
1714
  chunkPosToWorldPos(pos: Vector3): Vector3;
@@ -1716,23 +1716,20 @@ declare class Chunk {
1716
1716
 
1717
1717
  declare class MineRenderWorld {
1718
1718
  readonly scene: MineRenderScene;
1719
- private _size;
1720
- private _blockSize;
1721
1719
  private readonly _chunks;
1722
1720
  constructor(scene: MineRenderScene);
1723
1721
  getBlockAt(x: number, y: number, z: number): Maybe<BlockInfo>;
1724
1722
  getBlockAt(pos: Vector3): Maybe<BlockInfo>;
1725
1723
  getBlockAt(pos: TripleArray): Maybe<BlockInfo>;
1726
- setBlockAt(x: number, y: number, z: number, block: Block): Promise<Maybe<BlockInfo>>;
1727
- setBlockAt(pos: Vector3, block: Block): Promise<Maybe<BlockInfo>>;
1728
- setBlockAt(pos: TripleArray, block: Block): Promise<Maybe<BlockInfo>>;
1724
+ setBlockAt(x: number, y: number, z: number, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1725
+ setBlockAt(pos: Vector3, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1726
+ setBlockAt(pos: TripleArray, block: Maybe<Block>): Promise<Maybe<BlockInfo>>;
1729
1727
  placeMultiBlock(multiblock: MultiBlockStructure, useBatches?: boolean, executor?: BatchedExecutor): Promise<void>;
1730
1728
  clear(): Promise<void>;
1731
1729
  private getOrCreateChunkAt;
1732
1730
  getChunkAt(pos: Vector3): Maybe<Chunk>;
1733
1731
  static worldToScenePosition(pos: Vector3): Vector3;
1734
1732
  static sceneToWorldPosition(pos: Vector3): Vector3;
1735
- worldPosToChunkIndex(pos: Vector3): number;
1736
1733
  worldPosToChunkKey(pos: Vector3): string;
1737
1734
  validatePosBounds(pos: Vector3): void;
1738
1735
  }
@@ -7388,9 +7388,6 @@ var import_three25 = require("three");
7388
7388
  var import_three24 = require("three");
7389
7389
  var MineRenderWorld = class {
7390
7390
  constructor(scene) {
7391
- this._size = 4;
7392
- //TODO: expandable
7393
- this._blockSize = this._size * 16;
7394
7391
  this._chunks = /* @__PURE__ */ new Map();
7395
7392
  this.scene = scene;
7396
7393
  }
@@ -7401,12 +7398,8 @@ var MineRenderWorld = class {
7401
7398
  if (isTripleArray(posOrX)) {
7402
7399
  return this.getBlockAt(new import_three24.Vector3(posOrX[0], posOrX[1], posOrX[2]));
7403
7400
  }
7404
- const chunk = this.getChunkAt(posOrX);
7405
- if (typeof chunk === "undefined") {
7406
- return void 0;
7407
- }
7408
7401
  this.validatePosBounds(posOrX);
7409
- return chunk.getBlockAt(posOrX);
7402
+ return this.getChunkAt(posOrX)?.getBlockAt(posOrX);
7410
7403
  }
7411
7404
  async setBlockAt(posOrX, yOrBlock, z, block) {
7412
7405
  if (typeof posOrX == "number") {
@@ -7416,8 +7409,9 @@ var MineRenderWorld = class {
7416
7409
  return this.setBlockAt(new import_three24.Vector3(posOrX[0], posOrX[1], posOrX[2]), yOrBlock);
7417
7410
  }
7418
7411
  this.validatePosBounds(posOrX);
7419
- const chunk = this.getOrCreateChunkAt(posOrX);
7420
- return chunk.setBlockAt(posOrX, yOrBlock);
7412
+ const value = yOrBlock;
7413
+ const chunk = Chunk.isAir(value) ? this.getChunkAt(posOrX) : this.getOrCreateChunkAt(posOrX);
7414
+ return chunk?.setBlockAt(posOrX, value);
7421
7415
  }
7422
7416
  async placeMultiBlock(multiblock, useBatches = true, executor = new BatchedExecutor()) {
7423
7417
  const keys = new Set(multiblock.blocks.map((block) => AssetKey.parse("blockstates", block.type)));
@@ -7438,10 +7432,8 @@ var MineRenderWorld = class {
7438
7432
  }
7439
7433
  }
7440
7434
  async clear() {
7441
- console.log("CHUNKS", this._chunks);
7442
- for (let chunk of this._chunks.values()) {
7443
- console.log("dispose chunk", chunk);
7444
- await chunk?.dispose();
7435
+ for (const chunk of this._chunks.values()) {
7436
+ await chunk.dispose();
7445
7437
  }
7446
7438
  this._chunks.clear();
7447
7439
  }
@@ -7455,8 +7447,7 @@ var MineRenderWorld = class {
7455
7447
  return chunk;
7456
7448
  }
7457
7449
  getChunkAt(pos) {
7458
- const index = this.worldPosToChunkIndex(pos);
7459
- return this._chunks[index];
7450
+ return this._chunks.get(this.worldPosToChunkKey(pos));
7460
7451
  }
7461
7452
  static worldToScenePosition(pos) {
7462
7453
  return new import_three24.Vector3(
@@ -7472,12 +7463,6 @@ var MineRenderWorld = class {
7472
7463
  pos.z / 16
7473
7464
  );
7474
7465
  }
7475
- worldPosToChunkIndex(pos) {
7476
- const chunkX = Math.floor(pos.x / 16);
7477
- const chunkY = Math.floor(pos.y / 16);
7478
- const chunkZ = Math.floor(pos.z / 16);
7479
- return chunkZ * this._size * this._size + chunkY * this._size + chunkX;
7480
- }
7481
7466
  worldPosToChunkKey(pos) {
7482
7467
  const chunkX = Math.floor(pos.x / 16);
7483
7468
  const chunkY = Math.floor(pos.y / 16);
@@ -7485,12 +7470,9 @@ var MineRenderWorld = class {
7485
7470
  return `${chunkX}_${chunkY}_${chunkZ}`;
7486
7471
  }
7487
7472
  validatePosBounds(pos) {
7488
- if (pos.x < 0) throw new Error("x<0");
7489
- if (pos.y < 0) throw new Error("y<0");
7490
- if (pos.z < 0) throw new Error("z<0");
7491
- if (pos.x > this._blockSize) throw new Error("x>" + this._blockSize);
7492
- if (pos.y > this._blockSize) throw new Error("y>" + this._blockSize);
7493
- if (pos.z > this._blockSize) throw new Error("z>" + this._blockSize);
7473
+ if (![pos.x, pos.y, pos.z].every(Number.isInteger)) {
7474
+ throw new RangeError("Block coordinates must be integers");
7475
+ }
7494
7476
  }
7495
7477
  };
7496
7478
 
@@ -7503,16 +7485,6 @@ var Chunk = class _Chunk {
7503
7485
  this.x = x;
7504
7486
  this.y = y;
7505
7487
  this.z = z;
7506
- let anchor = new import_three25.Object3D();
7507
- this._anchor = anchor;
7508
- scene.add(anchor);
7509
- anchor.position.set(this.x * 256 + 128 - 8, this.y * 256 + 128 - 8, this.z * 256 + 128 - 8);
7510
- addBox3WireframeToObject(
7511
- new import_three25.Box3(new import_three25.Vector3(0, 0, 0), new import_three25.Vector3(256, 256, 256)),
7512
- anchor,
7513
- 16776960,
7514
- 4
7515
- );
7516
7488
  }
7517
7489
  getBlockAt(posOrX, y, z) {
7518
7490
  if (typeof posOrX == "number") {
@@ -7538,7 +7510,7 @@ var Chunk = class _Chunk {
7538
7510
  return this.setBlockInChunkAt(pos, block, worldPos);
7539
7511
  }
7540
7512
  /**
7541
- * Set block at a _chunk_ position (0-16)
7513
+ * Set a block at integer chunk-local coordinates from 0 to 15.
7542
7514
  */
7543
7515
  async setBlockInChunkAt(pos, block, worldPos) {
7544
7516
  if (typeof worldPos === "undefined") {
@@ -7551,15 +7523,13 @@ var Chunk = class _Chunk {
7551
7523
  current.object.removeFromScene();
7552
7524
  delete this._blocks[index];
7553
7525
  }
7554
- if (typeof block === "undefined" || typeof block.type === "undefined" || block.type === "air") return void 0;
7526
+ if (!block || _Chunk.isAir(block)) return void 0;
7555
7527
  const blockState = await BlockStates.get(AssetKey.parse("blockstates", block.type));
7556
7528
  if (blockState) {
7557
7529
  const blockObject = await this.scene.addBlock(blockState, {
7558
7530
  mergeMeshes: true,
7559
7531
  instanceMeshes: true,
7560
- wireframe: true,
7561
7532
  maxInstanceCount: 2e3
7562
- //TODO: setting this to high kills FPS; increase on demand somehow?
7563
7533
  });
7564
7534
  if (block.properties) {
7565
7535
  await blockObject.setState(block.properties);
@@ -7575,23 +7545,21 @@ var Chunk = class _Chunk {
7575
7545
  return void 0;
7576
7546
  }
7577
7547
  async clear() {
7578
- let promises = [];
7579
- let pos = new import_three25.Vector3();
7580
- for (let x = 0; x < 16; x++) {
7581
- for (let z = 0; z < 16; z++) {
7582
- for (let y = 0; y < 16; y++) {
7583
- promises.push(this.setBlockInChunkAt(pos.set(x, y, z), void 0));
7584
- }
7585
- }
7586
- }
7587
- return Promise.all(promises).then((ignored) => {
7588
- });
7548
+ this._blocks.forEach((block) => block.object.removeFromScene());
7549
+ this._blocks.length = 0;
7589
7550
  }
7590
7551
  async dispose() {
7591
7552
  await this.clear();
7592
- this.scene.remove(this._anchor);
7553
+ }
7554
+ static isAir(block) {
7555
+ if (typeof block === "undefined" || typeof block.type === "undefined") return true;
7556
+ const key = AssetKey.parse("blockstates", block.type);
7557
+ return key.namespace === "minecraft" && !key.type && ["air", "cave_air", "void_air"].includes(key.path);
7593
7558
  }
7594
7559
  static chunkPosToBlockIndex(pos) {
7560
+ if ([pos.x, pos.y, pos.z].some((value) => !Number.isInteger(value) || value < 0 || value >= 16)) {
7561
+ throw new RangeError("Block coordinates must be integers from 0 to 15 within a chunk");
7562
+ }
7595
7563
  return pos.y * 16 * 16 + pos.z * 16 + pos.x;
7596
7564
  }
7597
7565
  worldPosToChunkPos(pos) {