Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/change chunk format #50

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
type ILocalMapData,
type IVoxelMap,
type IVoxelMaterial,
type VoxelsChunkOrdering,
type VoxelsChunkSize,
} from './terrain/voxelmap/i-voxelmap';
export {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/terrain/terrain-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class TerrainViewer {
}

private updateHeightmap(): void {
const completeChunksColumns = this.voxelmapViewer.getCompleteChunksColumns();

const heightmapContainer = this.heightmapViewer.container;
if (this.parameters.lod.enabled) {
if (!heightmapContainer.parent) {
Expand All @@ -95,6 +93,7 @@ class TerrainViewer {
this.heightmapViewer.wireframe = this.parameters.lod.wireframe;

if (this.heightmapViewerNeedsUpdate) {
const completeChunksColumns = this.voxelmapViewer.getCompleteChunksColumns();
this.heightmapViewer.setHiddenPatches(completeChunksColumns);
this.heightmapViewerNeedsUpdate = false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/terrain/voxelmap/board/board-renderable-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BoardRenderableFactory extends VoxelsRenderableFactoryCpuWorker {
voxelMaterialsList: params.voxelMaterialsList,
maxVoxelsChunkSize: { xz: 128, y: 16 },
workersPoolSize: 1,
voxelsChunkOrdering: 'zyx',
});
}

Expand Down Expand Up @@ -108,6 +109,7 @@ class BoardRenderableFactory extends VoxelsRenderableFactoryCpuWorker {
size: chunkSize,
isEmpty: chunkIsEmpty,
data: chunkData,
dataOrdering: 'zyx',
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/terrain/voxelmap/i-voxelmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type VoxelsChunkSize = {
readonly y: number;
};

type VoxelsChunkOrdering = 'xyz' | 'xzy' | 'yxz' | 'yzx' | 'zxy' | 'zyx';

/** Compact object storing a portion of the map data */
interface ILocalMapData {
/** Compact array storing the voxel data.
Expand All @@ -29,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 Expand Up @@ -66,4 +66,4 @@ interface IVoxelMap {

const voxelmapDataPacking = new VoxelmapDataPacking();

export { type ILocalMapData, type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize, voxelmapDataPacking };
export { voxelmapDataPacking, type ILocalMapData, type IVoxelMap, type IVoxelMaterial, type VoxelsChunkOrdering, type VoxelsChunkSize };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as THREE from '../../../../../libs/three-usage';
import { PromisesQueue } from '../../../../../helpers/async/promises-queue';
import { type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsChunkOrdering, type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsRenderable } from '../../../voxelsRenderable/voxels-renderable';
import { VoxelsRenderableFactoryCpuWorker } from '../../../voxelsRenderable/voxelsRenderableFactory/merged/cpu/voxels-renderable-factory-cpu-worker';
import { type CheckerboardType } from '../../../voxelsRenderable/voxelsRenderableFactory/voxels-renderable-factory-base';
Expand All @@ -12,6 +12,7 @@ type Parameters = {
readonly workersPoolSize: number;
readonly checkerboardType?: CheckerboardType;
readonly greedyMeshing?: boolean;
readonly voxelsChunkOrdering: VoxelsChunkOrdering;
};

class PatchFactoryCpuWorker extends PatchFactoryBase {
Expand All @@ -25,6 +26,7 @@ class PatchFactoryCpuWorker extends PatchFactoryBase {
workersPoolSize: params.workersPoolSize,
checkerboardType: params.checkerboardType,
greedyMeshing: params.greedyMeshing,
voxelsChunkOrdering: params.voxelsChunkOrdering,
});
super(voxelsRenderableFactory);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as THREE from '../../../../../libs/three-usage';
import { PromisesQueue } from '../../../../../helpers/async/promises-queue';
import { type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsChunkOrdering, type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsRenderable } from '../../../voxelsRenderable/voxels-renderable';
import { VoxelsRenderableFactoryCpu } from '../../../voxelsRenderable/voxelsRenderableFactory/merged/cpu/voxels-renderable-factory-cpu';
import { type CheckerboardType } from '../../../voxelsRenderable/voxelsRenderableFactory/voxels-renderable-factory-base';
Expand All @@ -11,6 +11,7 @@ type Parameters = {
readonly patchSize: VoxelsChunkSize;
readonly checkerboardType?: CheckerboardType;
readonly greedyMeshing?: boolean;
readonly voxelsChunkOrdering: VoxelsChunkOrdering;
};

class PatchFactoryCpu extends PatchFactoryBase {
Expand All @@ -22,6 +23,7 @@ class PatchFactoryCpu extends PatchFactoryBase {
maxVoxelsChunkSize: params.patchSize,
checkerboardType: params.checkerboardType,
greedyMeshing: params.greedyMeshing,
voxelsChunkOrdering: params.voxelsChunkOrdering,
});
super(voxelsRenderableFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from '../../../../../libs/three-usage';
import { AsyncTask } from '../../../../../helpers/async/async-task';
import { type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsChunkOrdering, type IVoxelMap, type IVoxelMaterial, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsRenderable } from '../../../voxelsRenderable/voxels-renderable';
import { VoxelsRenderableFactoryGpu } from '../../../voxelsRenderable/voxelsRenderableFactory/merged/gpu/voxels-renderable-factory-gpu';
import {
Expand All @@ -16,16 +16,24 @@ type PatchGenerationJob = {
readonly resolve: (value: GeometryAndMaterial[]) => void;
};

type Parameters = {
readonly voxelMaterialsList: ReadonlyArray<IVoxelMaterial>;
readonly patchSize: VoxelsChunkSize;
readonly checkerboardType?: CheckerboardType;
readonly voxelsChunkOrdering: VoxelsChunkOrdering;
};

class PatchFactoryGpuOptimized extends PatchFactoryBase {
private nextPatchId = 0;

private readonly pendingJobs: PatchGenerationJob[] = [];

public constructor(voxelMaterialsList: ReadonlyArray<IVoxelMaterial>, patchSize: VoxelsChunkSize, checkerboardType?: CheckerboardType) {
public constructor(params: Parameters) {
const voxelsRenderableFactory = new VoxelsRenderableFactoryGpu({
voxelMaterialsList,
voxelsChunkSize: patchSize,
checkerboardType,
voxelMaterialsList: params.voxelMaterialsList,
voxelsChunkSize: params.patchSize,
checkerboardType: params.checkerboardType,
voxelsChunkOrdering: params.voxelsChunkOrdering,
});
super(voxelsRenderableFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import * as THREE from '../../../../../libs/three-usage';
import { PromisesQueue } from '../../../../../helpers/async/promises-queue';
import { type VoxelsChunkSize, type IVoxelMap, type IVoxelMaterial } from '../../../i-voxelmap';
import * as THREE from '../../../../../libs/three-usage';
import { type IVoxelMap, type IVoxelMaterial, type VoxelsChunkOrdering, type VoxelsChunkSize } from '../../../i-voxelmap';
import { type VoxelsRenderable } from '../../../voxelsRenderable/voxels-renderable';
import { VoxelsRenderableFactoryGpu } from '../../../voxelsRenderable/voxelsRenderableFactory/merged/gpu/voxels-renderable-factory-gpu';
import { type CheckerboardType } from '../../../voxelsRenderable/voxelsRenderableFactory/voxels-renderable-factory-base';
import { PatchFactoryBase } from '../patch-factory-base';

type Parameters = {
readonly voxelMaterialsList: ReadonlyArray<IVoxelMaterial>;
readonly patchSize: VoxelsChunkSize;
readonly checkerboardType?: CheckerboardType;
readonly voxelsChunkOrdering: VoxelsChunkOrdering;
};

class PatchFactoryGpuSequential extends PatchFactoryBase {
private readonly throttler = new PromisesQueue(1);

public constructor(voxelMaterialsList: ReadonlyArray<IVoxelMaterial>, patchSize: VoxelsChunkSize, checkerboardType?: CheckerboardType) {
public constructor(params: Parameters) {
const voxelsRenderableFactory = new VoxelsRenderableFactoryGpu({
voxelMaterialsList,
voxelsChunkSize: patchSize,
checkerboardType,
voxelMaterialsList: params.voxelMaterialsList,
voxelsChunkSize: params.patchSize,
checkerboardType: params.checkerboardType,
voxelsChunkOrdering: params.voxelsChunkOrdering,
});
super(voxelsRenderableFactory);
}
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 @@ -2,7 +2,7 @@ import * as THREE from '../../../../libs/three-usage';
import { DisposableMap } from '../../../../helpers/disposable-map';
import { logger } from '../../../../helpers/logger';
import { vec3ToString } from '../../../../helpers/string';
import { type VoxelsChunkSize, type IVoxelMap } from '../../i-voxelmap';
import { type VoxelsChunkSize, type IVoxelMap, type VoxelsChunkOrdering } from '../../i-voxelmap';
import { PatchFactoryCpu } from '../../patch/patch-factory/merged/patch-factory-cpu';
import { PatchFactoryGpuOptimized } from '../../patch/patch-factory/merged/patch-factory-gpu-optimized';
import { PatchFactoryGpuSequential } from '../../patch/patch-factory/merged/patch-factory-gpu-sequential';
Expand All @@ -16,6 +16,7 @@ import { AsyncPatch } from './async-patch';
type VoxelmapViewerAutonomousOptions = {
patchSize?: VoxelsChunkSize;
computingMode?: EPatchComputingMode;
voxelsChunkOrdering?: VoxelsChunkOrdering;
};

enum EPatchComputingMode {
Expand Down Expand Up @@ -48,16 +49,27 @@ class VoxelmapViewerAutonomous extends VoxelmapViewerBase {
}
}

const voxelsChunkOrdering = options?.voxelsChunkOrdering ?? 'zyx';

let patchFactory: PatchFactoryBase;
if (computingMode === EPatchComputingMode.CPU_CACHED) {
patchFactory = new PatchFactoryCpu({
voxelMaterialsList: map.voxelMaterialsList,
patchSize: voxelsChunksSize,
voxelsChunkOrdering,
});
} else if (computingMode === EPatchComputingMode.GPU_SEQUENTIAL) {
patchFactory = new PatchFactoryGpuSequential(map.voxelMaterialsList, voxelsChunksSize);
patchFactory = new PatchFactoryGpuSequential({
voxelMaterialsList: map.voxelMaterialsList,
patchSize: voxelsChunksSize,
voxelsChunkOrdering,
});
} else if (computingMode === EPatchComputingMode.GPU_OPTIMIZED) {
patchFactory = new PatchFactoryGpuOptimized(map.voxelMaterialsList, voxelsChunksSize);
patchFactory = new PatchFactoryGpuOptimized({
voxelMaterialsList: map.voxelMaterialsList,
patchSize: voxelsChunksSize,
voxelsChunkOrdering,
});
} else {
throw new Error(`Unsupported computing mode "${computingMode}".`);
}
Expand Down
22 changes: 16 additions & 6 deletions src/lib/terrain/voxelmap/viewer/simple/voxelmap-viewer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as THREE from '../../../../libs/three-usage';
import { AsyncTask } from '../../../../helpers/async/async-task';
import { PromisesQueue } from '../../../../helpers/async/promises-queue';
import { vec3ToString } from '../../../../helpers/string';
import { type IVoxelMaterial, type VoxelsChunkSize } from '../../i-voxelmap';
import * as THREE from '../../../../libs/three-usage';
import { type VoxelsChunkOrdering, type IVoxelMaterial, type VoxelsChunkSize } from '../../i-voxelmap';
import { PatchFactoryCpu } from '../../patch/patch-factory/merged/patch-factory-cpu';
import { PatchFactoryCpuWorker } from '../../patch/patch-factory/merged/patch-factory-cpu-worker';
import { PatchFactoryGpuSequential } from '../../patch/patch-factory/merged/patch-factory-gpu-sequential';
Expand Down Expand Up @@ -33,9 +33,10 @@ type ComputationOptions =
};

type VoxelmapViewerOptions = {
patchSize?: VoxelsChunkSize;
computationOptions?: ComputationOptions;
checkerboardType?: CheckerboardType;
readonly patchSize?: VoxelsChunkSize;
readonly computationOptions?: ComputationOptions;
readonly checkerboardType?: CheckerboardType;
readonly voxelsChunkOrdering?: VoxelsChunkOrdering;
};

type ComputationStatus = 'success' | 'skipped' | 'aborted';
Expand Down Expand Up @@ -93,12 +94,15 @@ class VoxelmapViewer extends VoxelmapViewerBase {
checkerboardType = options.checkerboardType;
}

const voxelsChunkOrdering = options?.voxelsChunkOrdering ?? 'zyx';

if (this.computationOptions.method === EComputationMethod.CPU_MONOTHREADED) {
this.patchFactory = new PatchFactoryCpu({
voxelMaterialsList,
patchSize,
checkerboardType,
greedyMeshing: this.computationOptions.greedyMeshing ?? true,
voxelsChunkOrdering,
});
this.maxPatchesComputedInParallel = 1;
} else if (this.computationOptions.method === EComputationMethod.CPU_MULTITHREADED) {
Expand All @@ -108,10 +112,16 @@ class VoxelmapViewer extends VoxelmapViewerBase {
workersPoolSize: this.computationOptions.threadsCount,
checkerboardType,
greedyMeshing: this.computationOptions.greedyMeshing ?? true,
voxelsChunkOrdering,
});
this.maxPatchesComputedInParallel = this.computationOptions.threadsCount;
} else {
this.patchFactory = new PatchFactoryGpuSequential(voxelMaterialsList, patchSize, checkerboardType);
this.patchFactory = new PatchFactoryGpuSequential({
voxelMaterialsList,
patchSize,
voxelsChunkOrdering,
checkerboardType,
});
this.maxPatchesComputedInParallel = 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const vertices = {
mmm: new THREE.Vector3(0, 0, 0),
};
type FaceVertex = {
readonly vertex: THREE.Vector3;
readonly shadowingNeighbourVoxels: [THREE.Vector3, THREE.Vector3, THREE.Vector3];
readonly vertex: THREE.Vector3Like;
readonly shadowingNeighbourVoxels: [THREE.Vector3Like, THREE.Vector3Like, THREE.Vector3Like];
readonly edgeNeighbourVoxels: {
readonly x: [THREE.Vector3, THREE.Vector3];
readonly y: [THREE.Vector3, THREE.Vector3];
readonly x: [THREE.Vector3Like, THREE.Vector3Like];
readonly y: [THREE.Vector3Like, THREE.Vector3Like];
};
};

Expand All @@ -33,10 +33,10 @@ const normalsById = Object.values(normals);

type Normal = {
readonly id: number;
readonly vec: THREE.Vector3;
readonly vec: THREE.Vector3Like;
};

function buildNormal(vec: THREE.Vector3): Normal {
function buildNormal(vec: THREE.Vector3Like): Normal {
for (let i = 0; i < normalsById.length; i++) {
if (normalsById[i]!.equals(vec)) {
return {
Expand All @@ -61,7 +61,7 @@ const faceIndices: [number, number, number, number, number, number] = [0, 2, 1,

let iF = 0;

function buildFace(type: FaceType, v00: THREE.Vector3, v01: THREE.Vector3, v10: THREE.Vector3, v11: THREE.Vector3): Face {
function buildFace(type: FaceType, v00: THREE.Vector3Like, v01: THREE.Vector3Like, v10: THREE.Vector3Like, v11: THREE.Vector3Like): Face {
const normal = normals[type];
const uvUp = new THREE.Vector3().subVectors(v01, v00);
const uvRight = new THREE.Vector3().subVectors(v10, v00);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type WorkerDefinition } from '../../../../../../helpers/async/dedicatedWorkers/dedicated-worker';
import { DedicatedWorkersPool } from '../../../../../../helpers/async/dedicatedWorkers/dedicated-workers-pool';
import { type IVoxelMaterial, type VoxelsChunkSize } from '../../../../i-voxelmap';
import { type VoxelsChunkOrdering, type IVoxelMaterial, type VoxelsChunkSize } from '../../../../i-voxelmap';
import { type CheckerboardType, type VoxelsChunkData } from '../../voxels-renderable-factory-base';

import { VoxelsRenderableFactoryCpu } from './voxels-renderable-factory-cpu';
Expand All @@ -11,6 +11,7 @@ type Parameters = {
readonly workersPoolSize: number;
readonly checkerboardType?: CheckerboardType | undefined;
readonly greedyMeshing?: boolean | undefined;
readonly voxelsChunkOrdering: VoxelsChunkOrdering;
};

class VoxelsRenderableFactoryCpuWorker extends VoxelsRenderableFactoryCpu {
Expand Down
Loading