Skip to content

Commit

Permalink
feat: each chunk must specify its data ordering
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the IVoxelMap.ILocalMapData format changed
  • Loading branch information
piellardj committed Oct 2, 2024
1 parent 41e6cfb commit bd2881f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib/terrain/voxelmap/board/board-renderable-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class BoardRenderableFactory extends VoxelsRenderableFactoryCpuWorker {
size: chunkSize,
isEmpty: chunkIsEmpty,
data: chunkData,
dataOrdering: 'zyx',
};
}

Expand Down
4 changes: 1 addition & 3 deletions src/lib/terrain/voxelmap/i-voxelmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ interface ILocalMapData {
* - bit 1: 1 if the voxel should be displayed as checkerboard, 0 otherwise
* - bits 2-13: ID of the material
* Use the helper "voxelmapDataPacking" to do this encoding and be future-proof.
*
* The elements should be ordered by coordinates as follow by Z first, then Y then X.
* For example, for a portion of the map between (0,0,0) and (2,2,2): (0,0,0) then (1,0,0) then (0,1,0) then (1,1,0) then (0,1,1) then (1,1,1)
*/
readonly data: Uint16Array;
readonly dataOrdering: VoxelsChunkOrdering;

/** Should be:
* - true if there are no voxels in the data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from '../../../../libs/three-usage';
import { processAsap } from '../../../../helpers/async/async-sync';
import { vec3ToString } from '../../../../helpers/string';
import { type IVoxelMap } from '../../i-voxelmap';
import { type VoxelsChunkOrdering, type IVoxelMap } from '../../i-voxelmap';
import { type VoxelsRenderable } from '../../voxelsRenderable/voxels-renderable';
import {
type VoxelsChunkData,
Expand All @@ -19,6 +19,7 @@ type VertexData = {
type LocalMapData = {
readonly size: THREE.Vector3;
readonly data: Uint16Array;
readonly dataOrdering: VoxelsChunkOrdering;
readonly isEmpty: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ class VoxelsRenderableFactoryCpu extends VoxelsRenderableFactory {
if (voxelsChunkData.isEmpty) {
return [];
}

if (voxelsChunkData.dataOrdering !== this.serializableFactory.voxelsChunkOrdering) {
throw new Error(
`Invalid data ordering: expected "${this.serializableFactory.voxelsChunkOrdering}" but received "${voxelsChunkData.dataOrdering}".`
);
}

const buffer = await this.buildBuffer(voxelsChunkData);
return this.assembleGeometryAndMaterials(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class VoxelsComputerGpu {
private readonly localCacheBuffer: GPUBuffer;
private readonly buffer: FaceBuffer;

private voxelsChunkOrdering: VoxelsChunkOrdering;

private readonly workgroupSize = 256;

private readonly promiseThrottler = new PromisesQueue(1);
Expand All @@ -59,6 +61,8 @@ class VoxelsComputerGpu {
) {
this.device = device;

this.voxelsChunkOrdering = voxelsChunkOrdering;

const checkerboardPattern = {
x: +checkerboardType.includes('x'),
y: +checkerboardType.includes('y'),
Expand Down Expand Up @@ -237,6 +241,12 @@ class VoxelsComputerGpu {
}

public async computeBuffer(voxelsChunkData: VoxelsChunkData): Promise<ComputationOutputs> {
if (voxelsChunkData.dataOrdering !== this.voxelsChunkOrdering) {
throw new Error(
`Invalid data ordering: expected "${this.voxelsChunkOrdering}" but received "${voxelsChunkData.dataOrdering}".`
);
}

return this.promiseThrottler.run(async () => {
this.device.queue.writeBuffer(
this.localCacheBuffer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as THREE from '../../../../libs/three-usage';
import { nextPowerOfTwo } from '../../../../helpers/math';
import { vec3ToString } from '../../../../helpers/string';
import { type PackedUintFragment } from '../../../../helpers/uint-packing';
import { type IVoxelMaterial } from '../../i-voxelmap';
import * as THREE from '../../../../libs/three-usage';
import { type IVoxelMaterial, type VoxelsChunkOrdering } from '../../i-voxelmap';
import { type VoxelsMaterialUniforms, type VoxelsMaterials } from '../voxels-material';
import { VoxelsRenderable } from '../voxels-renderable';

Expand All @@ -24,6 +24,7 @@ type VertexData = {
type VoxelsChunkData = {
readonly size: THREE.Vector3;
readonly data: Uint16Array;
readonly dataOrdering: VoxelsChunkOrdering;
readonly isEmpty: boolean;
};

Expand Down Expand Up @@ -185,9 +186,9 @@ abstract class VoxelsRenderableFactoryBase {

export {
VoxelsRenderableFactoryBase,
type CheckerboardType,
type GeometryAndMaterial,
type Parameters,
type VertexData,
type VoxelsChunkData,
type CheckerboardType,
};
1 change: 1 addition & 0 deletions src/test/map/trees/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Tree {
size,
isEmpty: false,
data: new Uint16Array(size.x * size.y * size.z),
dataOrdering: 'zyx',
};
this.indexFactor = { x: 1, y: size.x, z: size.x * size.y };
this.offset = new THREE.Vector3(-this.radiusXZ, 0, -this.radiusXZ);
Expand Down
3 changes: 2 additions & 1 deletion src/test/map/voxel-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ class VoxelMap implements IVoxelMap, IHeightmap {
}
}

const result = {
const result: ILocalMapData = {
data,
dataOrdering: 'zyx',
isEmpty,
};

Expand Down

0 comments on commit bd2881f

Please sign in to comment.