minerender 2.1.0-alpha.39 → 2.1.0-alpha.40

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.
@@ -4667,19 +4667,19 @@ var _BlockObject = class _BlockObject extends SceneObject {
4667
4667
  for (let k in defaultState) {
4668
4668
  state[k] = defaultState[k].default;
4669
4669
  }
4670
- await this.setState(state);
4670
+ this._setState(state);
4671
4671
  } else {
4672
4672
  if (this.blockState.variants) {
4673
- await this.setState(Object.keys(this.blockState.variants)[0]);
4673
+ this._setState(Object.keys(this.blockState.variants)[0]);
4674
4674
  } else if (this.blockState.multipart) {
4675
4675
  const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4676
4676
  const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4677
- await this.setState(state);
4677
+ this._setState(state);
4678
4678
  }
4679
4679
  }
4680
- } else {
4681
- await this.recreateModels();
4682
4680
  }
4681
+ if (this.options.initialState !== void 0) this._setState(this.options.initialState);
4682
+ await this.recreateModels();
4683
4683
  }
4684
4684
  dispose() {
4685
4685
  this.clearModels();
@@ -9370,12 +9370,26 @@ var MineRenderWorld = class {
9370
9370
  if (isTripleArray(posOrX)) {
9371
9371
  return this.setBlockAt(new Vector311(posOrX[0], posOrX[1], posOrX[2]), yOrBlock);
9372
9372
  }
9373
- this.validatePosBounds(posOrX);
9374
- const value = yOrBlock;
9375
- const chunk = Chunk.isAir(value) ? this.getChunkAt(posOrX) : this.getOrCreateChunkAt(posOrX);
9376
- return chunk?.setBlockAt(posOrX, value);
9373
+ return this.placeBlock(posOrX, yOrBlock);
9374
+ }
9375
+ async placeBlock(pos, value, onBlocksChanged) {
9376
+ this.validatePosBounds(pos);
9377
+ const chunk = Chunk.isAir(value) ? this.getChunkAt(pos) : this.getOrCreateChunkAt(pos);
9378
+ return chunk?.setBlockInChunkAt(chunk.worldPosToChunkPos(pos), value, pos, onBlocksChanged);
9377
9379
  }
9378
9380
  async placeMultiBlock(multiblock, useBatches = true, executor) {
9381
+ const changes = /* @__PURE__ */ new Map();
9382
+ try {
9383
+ await this.placeBlocks(multiblock, useBatches, executor, changes);
9384
+ } finally {
9385
+ await this.updateCulling([...changes.values()]);
9386
+ }
9387
+ }
9388
+ async placeBlocks(multiblock, useBatches, executor, changes) {
9389
+ const collect = async (positions2) => {
9390
+ for (const pos of positions2) changes.set(pos.toArray().join(","), pos);
9391
+ };
9392
+ const place = (block) => this.placeBlock(new Vector311(...block.position), block, collect);
9379
9393
  const keys = /* @__PURE__ */ new Map();
9380
9394
  for (const block of multiblock.blocks) {
9381
9395
  if (Chunk.isAir(block)) continue;
@@ -9384,7 +9398,7 @@ var MineRenderWorld = class {
9384
9398
  }
9385
9399
  await BlockStates.getAll(keys.values());
9386
9400
  if (!useBatches) {
9387
- for (const block of multiblock.blocks) await this.setBlockAt(block.position, block);
9401
+ for (const block of multiblock.blocks) await place(block);
9388
9402
  return;
9389
9403
  }
9390
9404
  const positions = /* @__PURE__ */ new Map();
@@ -9400,7 +9414,7 @@ var MineRenderWorld = class {
9400
9414
  for (let i = 0; i < groups.length; i += queue.batch) {
9401
9415
  const results = await Promise.allSettled(groups.slice(i, i + queue.batch).map(
9402
9416
  (blocks) => queue.submit(async () => {
9403
- for (const block of blocks) await this.setBlockAt(block.position, block);
9417
+ for (const block of blocks) await place(block);
9404
9418
  })
9405
9419
  ));
9406
9420
  const failure = results.find((result) => result.status === "rejected");
@@ -9412,25 +9426,32 @@ var MineRenderWorld = class {
9412
9426
  }
9413
9427
  /** Replaces one chunk column's blocks. Entity NBT remains available on the parsed chunk. */
9414
9428
  async placeChunk(chunk, executor) {
9415
- const previous = [...this._chunks.entries()].filter(([, section]) => section.x === chunk.x && section.z === chunk.z);
9416
- for (const [key, section] of previous) {
9417
- this._chunks.delete(key);
9418
- await section.dispose();
9419
- }
9420
- for (const section of chunk.sections) {
9421
- const blocks = [];
9422
- for (let index = 0; index < 4096; index++) {
9423
- const block = section.data.get(index);
9424
- if (block) blocks.push({
9425
- ...block,
9426
- position: [
9427
- chunk.x * 16 + index % 16,
9428
- section.y * 16 + Math.floor(index / 256),
9429
- chunk.z * 16 + Math.floor(index / 16) % 16
9430
- ]
9429
+ const changes = /* @__PURE__ */ new Map();
9430
+ try {
9431
+ const previous = [...this._chunks.entries()].filter(([, section]) => section.x === chunk.x && section.z === chunk.z);
9432
+ for (const [key, section] of previous) {
9433
+ this._chunks.delete(key);
9434
+ await section.clear(async (positions) => {
9435
+ for (const pos of positions) changes.set(pos.toArray().join(","), pos);
9431
9436
  });
9432
9437
  }
9433
- await this.placeMultiBlock({ size: [16, 16, 16], blocks }, true, executor);
9438
+ for (const section of chunk.sections) {
9439
+ const blocks = [];
9440
+ for (let index = 0; index < 4096; index++) {
9441
+ const block = section.data.get(index);
9442
+ if (block) blocks.push({
9443
+ ...block,
9444
+ position: [
9445
+ chunk.x * 16 + index % 16,
9446
+ section.y * 16 + Math.floor(index / 256),
9447
+ chunk.z * 16 + Math.floor(index / 16) % 16
9448
+ ]
9449
+ });
9450
+ }
9451
+ await this.placeBlocks({ size: [16, 16, 16], blocks }, true, executor, changes);
9452
+ }
9453
+ } finally {
9454
+ await this.updateCulling([...changes.values()]);
9434
9455
  }
9435
9456
  }
9436
9457
  async clear() {
@@ -9553,7 +9574,7 @@ var Chunk = class _Chunk {
9553
9574
  /**
9554
9575
  * Set a block at integer chunk-local coordinates from 0 to 15.
9555
9576
  */
9556
- async setBlockInChunkAt(pos, block, worldPos) {
9577
+ async setBlockInChunkAt(pos, block, worldPos, onBlocksChanged = this.onBlocksChanged) {
9557
9578
  if (typeof worldPos === "undefined") {
9558
9579
  worldPos = this.chunkPosToWorldPos(pos);
9559
9580
  }
@@ -9574,9 +9595,9 @@ var Chunk = class _Chunk {
9574
9595
  object = await this.scene.addBlock(blockState, {
9575
9596
  mergeMeshes: true,
9576
9597
  instanceMeshes: true,
9577
- maxInstanceCount: 2e3
9598
+ maxInstanceCount: 2e3,
9599
+ initialState: stored.properties
9578
9600
  });
9579
- if (stored.properties) await object.setState(stored.properties);
9580
9601
  object.setPosition(MineRenderWorld.worldToScenePosition(worldPos));
9581
9602
  const info = { get block() {
9582
9603
  return readBlock();
@@ -9588,17 +9609,17 @@ var Chunk = class _Chunk {
9588
9609
  object?.removeFromScene();
9589
9610
  throw error;
9590
9611
  } finally {
9591
- await this.onBlocksChanged?.([worldPos]);
9612
+ await onBlocksChanged?.([worldPos]);
9592
9613
  }
9593
9614
  }
9594
- async clear() {
9615
+ async clear(onBlocksChanged = this.onBlocksChanged) {
9595
9616
  const positions = [...this.renderedBlocks.keys()].map((index) => this.chunkPosToWorldPos(
9596
9617
  new Vector312(index % 16, Math.floor(index / 256), Math.floor(index / 16) % 16)
9597
9618
  ));
9598
9619
  for (const info of this.renderedBlocks.values()) info.object.removeFromScene();
9599
9620
  this.renderedBlocks.clear();
9600
9621
  this.data.clear();
9601
- await this.onBlocksChanged?.(positions);
9622
+ await onBlocksChanged?.(positions);
9602
9623
  }
9603
9624
  async dispose() {
9604
9625
  await this.clear();