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

Refactor/various refactorings #68

Merged
merged 32 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
021441e
feat: optimize IHeightmap.sampleHeightmap() API
piellardj Feb 4, 2025
94205b5
refactor: renaming
piellardj Feb 4, 2025
f7e29f7
refactor: extract class MaterialsStore
piellardj Feb 4, 2025
ebb71c9
fix: shader part
piellardj Feb 4, 2025
a4cc78d
feat: optimize IHeightmap.sampleHeightmap()
piellardj Feb 4, 2025
7998d67
refactor: prefer Map to Record (1)
piellardj Feb 8, 2025
e7404b6
refactor: delete unused code
piellardj Feb 8, 2025
7dd8d69
refactor: prefer Map to Record (2)
piellardj Feb 8, 2025
7b410b3
refactor: prefer Map to Record (3)
piellardj Feb 8, 2025
48213a7
refactor: prefer Map to Record (4)
piellardj Feb 8, 2025
cb0fb75
refactor: prefer Map to Record (5)
piellardj Feb 8, 2025
9f77030
refactor: prefer Map to Record (6)
piellardj Feb 8, 2025
83e8057
refactor: prefer Map to Record (7)
piellardj Feb 8, 2025
3875610
refactor: prefer Map to Record (8)
piellardj Feb 8, 2025
dd55ae9
refactor: group altitude parameters
piellardj Feb 8, 2025
c4c3bdc
refactor: use ReadonlyArray when possible
piellardj Feb 8, 2025
9f2a6b2
refactor: prefer Iterable
piellardj Feb 8, 2025
a8e93b7
refactor: name all THREE objects for easier debugging
piellardj Feb 8, 2025
f11c50d
test: fix
piellardj Feb 8, 2025
f50b0ab
refactor: extract interface IVoxelmapViewer
piellardj Feb 8, 2025
157c357
refactor: remove VoxelmapViewerAutonomous
piellardj Feb 9, 2025
355abfd
perf: optimize processing for empty chunks
piellardj Feb 9, 2025
d46d837
refactor: add PatchFactoryBase.maxPatchesComputedInParallel
piellardj Feb 9, 2025
d016719
refactor: start renaming "patch" to "chunk"
piellardj Feb 9, 2025
6bcd662
refactor: remove mention to patch in VoxelsRenderable
piellardj Feb 9, 2025
219ac50
refactor: move file
piellardj Feb 9, 2025
3ce1348
refactor: finish renamings from "patch"->"chunk"
piellardj Feb 9, 2025
f6c137c
refactor: simplify buildChunkRenderable
piellardj Feb 9, 2025
e4a79be
refactor: extract type
piellardj Feb 9, 2025
6fe6607
refactor: move files
piellardj Feb 10, 2025
f36e5ab
Revert "refactor: simplify buildChunkRenderable"
piellardj Feb 10, 2025
a859427
refactor: remove and sort imports
piellardj Feb 10, 2025
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
5 changes: 4 additions & 1 deletion src/lib/effects/billboard/billboard-shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ function createBillboardMaterial(params: Parameters): THREE.Material {
let material: THREE.Material;
if (params.material === 'Phong') {
material = new THREE.MeshPhongMaterial();
material.name = 'billboard-material-phong';
} else if (params.material === 'Basic') {
material = new THREE.MeshBasicMaterial();
material.name = 'billboard-material-basic';
} else {
throw new Error(`Unsupported material "${params.material}".`);
}
Expand Down Expand Up @@ -151,12 +153,13 @@ function createBillboardInstancedBufferGeometry(): THREE.InstancedBufferGeometry
);
bufferGeometry.setAttribute('normal', new THREE.Float32BufferAttribute([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], 3));
bufferGeometry.setAttribute('uv', new THREE.Float32BufferAttribute([0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0], 2));
bufferGeometry.name = 'billboard-instanced-buffergeometry';
return bufferGeometry;
}

export {
createBillboardMaterial,
createBillboardInstancedBufferGeometry,
createBillboardMaterial,
type AttributeDefinition,
type UniformDefinition,
type VaryingDefinition,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/effects/billboard/gpu/gpu-textures-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GpuTexturesState {
private readonly renderTargets: [THREE.WebGLRenderTarget, THREE.WebGLRenderTarget];
private currentRenderTargetId: number = 0;

private readonly pipelines: Record<string, Pipeline> = {};
private readonly pipelines = new Map<string, Pipeline>();

public constructor(params: Parameters) {
this.renderTargets = [
Expand Down Expand Up @@ -120,7 +120,7 @@ ${['vUv', ...params.textureNames.map((_name: string, index: number) => `out_frag
}`;
}

this.pipelines[name] = {
this.pipelines.set(name, {
shader: new THREE.RawShaderMaterial({
glslVersion: '300 es',
blending: THREE.NoBlending,
Expand All @@ -131,12 +131,12 @@ ${['vUv', ...params.textureNames.map((_name: string, index: number) => `out_frag
fragmentShader,
}),
requiresPreviousState: definition.requiresPreviousState,
};
});
}
}

public runPipeline(renderer: THREE.WebGLRenderer, name: string): void {
const pipeline = this.pipelines[name];
const pipeline = this.pipelines.get(name);
if (!pipeline) {
throw new Error(`Unknown pipeline "${name}".`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/effects/billboard/instanced-billboard-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InstancedBillboardBatch {
public readonly maxInstancesCount: number;
private readonly instanceWorldPositionAttribute: THREE.InstancedBufferAttribute;
private readonly instanceLocalTransformAttribute: THREE.InstancedBufferAttribute;
private readonly instanceCustomAttributes: Record<string, CustomAttribute>;
private readonly instanceCustomAttributes = new Map<string, CustomAttribute>();

public constructor(params: Parameters) {
const billboardGeometry = createBillboardInstancedBufferGeometry();
Expand All @@ -50,7 +50,6 @@ class InstancedBillboardBatch {
this.instanceLocalTransformAttribute = new THREE.InstancedBufferAttribute(new Float32Array(instanceLocalTransformBuffer), 4);
billboardGeometry.setAttribute('aInstanceLocalTransform', this.instanceLocalTransformAttribute);

this.instanceCustomAttributes = {};
for (const [name, definition] of Object.entries(params.customAttributes)) {
const size = attributeSizes[definition.type];
if (typeof size === 'undefined') {
Expand All @@ -59,10 +58,11 @@ class InstancedBillboardBatch {

const bufferAttribute = new THREE.InstancedBufferAttribute(new Float32Array(size * params.maxInstancesCount), size);
billboardGeometry.setAttribute(`a_${name}`, bufferAttribute);
this.instanceCustomAttributes[name] = { bufferAttribute, size };
this.instanceCustomAttributes.set(name, { bufferAttribute, size });
}

this.mesh = new THREE.InstancedMesh(billboardGeometry, params.billboardMaterial, params.maxInstancesCount);
this.mesh.name = 'instanced-billboard-mesh';
this.mesh.count = 0;
this.mesh.frustumCulled = false;
this.mesh.receiveShadow = params.receiveShadows;
Expand Down Expand Up @@ -94,7 +94,7 @@ class InstancedBillboardBatch {
}

public setInstanceCustomAttribute(instanceId: number, name: string, value: ReadonlyArray<number>): void {
const customAttribute = this.instanceCustomAttributes[name];
const customAttribute = this.instanceCustomAttributes.get(name);
if (!customAttribute) {
throw new Error(`Unknown attribute "${name}".`);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/effects/billboard/instanced-billboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class InstancedBillboard {

public constructor(params: Parameters) {
this.container = new THREE.Group();
this.container.name = 'instanced-billboard-container';

this.maxInstancesCount = params.maxInstancesCount ?? Infinity;

Expand Down
1 change: 1 addition & 0 deletions src/lib/effects/props/props-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class PropsBatch {
this.groupsDefinitions = new Map();

this.instancedMesh = new THREE.InstancedMesh(params.bufferGeometry, this.material.material, this.maxInstancesCount);
this.instancedMesh.name = 'props-batch';
this.instancedMesh.count = 0;
}

Expand Down
40 changes: 20 additions & 20 deletions src/lib/effects/props/props-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@ import * as THREE from '../../libs/three-usage';
import { PropsHandler, type Parameters as PropshandlerParameters } from './props-handler';

type Parameters = PropshandlerParameters & {
readonly patchSize: THREE.Vector3Like;
readonly chunkSize: THREE.Vector3Like;
};

type PatchId = THREE.Vector3Like;
type ChunkId = THREE.Vector3Like;

function buildPatchIdString(patchId: PatchId): string {
return `${patchId.x}_${patchId.y}_${patchId.z}`;
function buildChunkIdString(chunkId: ChunkId): string {
return `${chunkId.x}_${chunkId.y}_${chunkId.z}`;
}

class PropsViewer extends PropsHandler {
private readonly patchSize: THREE.Vector3Like;
private readonly chunkSize: THREE.Vector3Like;

public constructor(params: Parameters) {
super(params);

this.patchSize = new THREE.Vector3().copy(params.patchSize);
this.chunkSize = new THREE.Vector3().copy(params.chunkSize);
}

public setPatchPropsFromLocalMatrices(patchId: THREE.Vector3Like, localMatricesList: ReadonlyArray<THREE.Matrix4>): void {
const patchWorldOrigin = new THREE.Vector3().multiplyVectors(patchId, this.patchSize);
const patchTransformMatrix = new THREE.Matrix4().makeTranslation(patchWorldOrigin);
public setChunkPropsFromLocalMatrices(chunkId: THREE.Vector3Like, localMatricesList: ReadonlyArray<THREE.Matrix4>): void {
const chunkWorldOrigin = new THREE.Vector3().multiplyVectors(chunkId, this.chunkSize);
const chunkTransformMatrix = new THREE.Matrix4().makeTranslation(chunkWorldOrigin);
const worldMatricesList = localMatricesList.map(localMatrix =>
new THREE.Matrix4().multiplyMatrices(patchTransformMatrix, localMatrix)
new THREE.Matrix4().multiplyMatrices(chunkTransformMatrix, localMatrix)
);
this.setPatchPropsFromWorldMatrices(patchId, worldMatricesList);
this.setChunkPropsFromWorldMatrices(chunkId, worldMatricesList);
}

public setPatchPropsFromWorldMatrices(patchId: THREE.Vector3Like, worldMatricesList: ReadonlyArray<THREE.Matrix4>): void {
const patchIdString = buildPatchIdString(patchId);
this.setGroup(patchIdString, worldMatricesList);
public setChunkPropsFromWorldMatrices(chunkId: THREE.Vector3Like, worldMatricesList: ReadonlyArray<THREE.Matrix4>): void {
const chunkIdString = buildChunkIdString(chunkId);
this.setGroup(chunkIdString, worldMatricesList);
}

public deletePatchProps(patchId: THREE.Vector3Like): void {
const patchIdString = buildPatchIdString(patchId);
this.deleteGroup(patchIdString);
public deleteChunkProps(chunkId: THREE.Vector3Like): void {
const chunkIdString = buildChunkIdString(chunkId);
this.deleteGroup(chunkIdString);
}

public hasPatchProps(patchId: THREE.Vector3Like): boolean {
const patchIdString = buildPatchIdString(patchId);
return this.hasGroup(patchIdString);
public hasChunkProps(chunkId: THREE.Vector3Like): boolean {
const chunkIdString = buildChunkIdString(chunkId);
return this.hasGroup(chunkIdString);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/effects/weather/rain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ return vec4(0.5, 0.5, 1, 1);
this.instancedBillboard.initializePositions(renderer);

this.container = new THREE.Group();
this.container.name = 'rain-container';
this.instancedBillboard.container.position.copy(this.instancedBillboard.positionsRange.clone().multiplyScalar(-0.5));
this.container.add(this.instancedBillboard.container);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/effects/weather/snow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ return vec4(0.9, 0.9, 1, 1);
this.instancedBillboard.initializePositions(renderer);

this.container = new THREE.Group();
this.container.name = 'snow-container';
this.instancedBillboard.container.position.copy(this.instancedBillboard.positionsRange.clone().multiplyScalar(-0.5));
this.container.add(this.instancedBillboard.container);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/effects/weather/weather-particles-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class GpuInstancedBillboard {

public constructor(params: Parameters) {
this.container = new THREE.Group();
this.container.name = 'weather-particles-container';

const textureSize = nextPowerOfTwo(Math.floor(Math.sqrt(params.maxInstancesCount)));
if (params.maxInstancesCount > textureSize * textureSize) {
Expand Down Expand Up @@ -167,6 +168,7 @@ localTransform = mat2(size.x, 0, 0, size.y);`,
{
const billboardGeometry = createBillboardInstancedBufferGeometry();
this.mesh = new THREE.InstancedMesh(billboardGeometry, this.displayPipeline.shader, params.maxInstancesCount);
this.mesh.name = 'weather-particles-mesh';
this.mesh.count = 0;
this.mesh.frustumCulled = false;
this.mesh.receiveShadow = params.rendering.shadows.receive;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/helpers/async/dedicatedWorkers/dedicated-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ type TaskResponseMessage =

type RequestMessage = {
readonly messageId: number;
readonly taskRequestMessagesList: ReadonlyArray<TaskRequestMessage>;
readonly taskRequestMessagesList: Iterable<TaskRequestMessage>;
};
type ResponseMessage = {
readonly messageId: number;
readonly taskResponseMessagesList: ReadonlyArray<TaskResponseMessage>;
readonly taskResponseMessagesList: Iterable<TaskResponseMessage>;
};

type PendingTask = {
Expand Down Expand Up @@ -163,7 +163,7 @@ class DedicatedWorker {
return this.pendingTasks.size;
}

private sendTasksToWorker(tasksList: ReadonlyArray<TaskRequestMessage>, transfer: Transferable[]): void {
private sendTasksToWorker(tasksList: Iterable<TaskRequestMessage>, transfer: Transferable[]): void {
if (!this.worker) {
throw new Error('Worker has been terminated.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/customizable-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CustomizableTexture {
public readonly texture: THREE.Texture;

public needsUpdate: boolean = true;
public get layerNames(): ReadonlyArray<string> {
public get layerNames(): string[] {
return Array.from(this.layers.keys());
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/helpers/disposable-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ interface IDisposable {
type Key = string | number;

class DisposableMap<T extends IDisposable> {
private store: Record<Key, T> = {};
private readonly store = new Map<Key, T>();
private readonly itemsList = new CachedItem<T[]>(() => Object.values(this.store));

public setItem(id: Key, item: T): void {
this.deleteItem(id);
this.store[id] = item;
this.store.set(id, item);
this.itemsList.invalidate();
}

public getItem(id: Key): T | null {
return this.store[id] || null;
return this.store.get(id) || null;
}

public deleteItem(id: Key): boolean {
const item = this.store[id];
const item = this.store.get(id);
if (typeof item !== 'undefined') {
item.dispose();
delete this.store[id];
this.store.delete(id);
this.itemsList.invalidate();
return true;
}
Expand All @@ -43,7 +43,7 @@ class DisposableMap<T extends IDisposable> {
for (const item of this.allItems) {
item.dispose();
}
this.store = {};
this.store.clear();
this.itemsList.invalidate();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/helpers/fullscreen-quad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import * as THREE from '../libs/three-usage';

function createFullscreenQuad(attributeName: string): THREE.Mesh {
const fullscreenQuadGeometry = new THREE.BufferGeometry();
fullscreenQuadGeometry.name = 'fullscreen-quad-geometry';
fullscreenQuadGeometry.setAttribute(attributeName, new THREE.Float32BufferAttribute([0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1], 2));
fullscreenQuadGeometry.setDrawRange(0, 6);
const fullscreenQuad = new THREE.Mesh(fullscreenQuadGeometry);
fullscreenQuad.name = 'fullscreenq-quad-mesh';
fullscreenQuad.frustumCulled = false;
return fullscreenQuad;
}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export { PromisesQueue } from './helpers/async/promises-queue';
export { ELogLevel, setVerbosity } from './helpers/logger';
export { HeightmapViewerCpu, type HeightmapViewerCpuStatistics } from './terrain/heightmap/cpu/heightmap-viewer-cpu';
export { HeightmapViewerGpu, type HeightmapViewerGpuStatistics } from './terrain/heightmap/gpu/heightmap-viewer-gpu';
export type { IHeightmap, IHeightmapCoords, IHeightmapSample } from './terrain/heightmap/i-heightmap';
export type { HeightmapSamples, IHeightmap } from './terrain/heightmap/i-heightmap';
export { type IHeightmapViewer } from './terrain/heightmap/i-heightmap-viewer';
export { MaterialsStore } from './terrain/materials-store';
export { TerrainViewer } from './terrain/terrain-viewer';
export { computeBoard, EBoardSquareType, type Board, type BoardSquare } from './terrain/voxelmap/board/board';
export { BoardRenderableFactory, type BoardRenderable } from './terrain/voxelmap/board/board-renderable-factory';
Expand All @@ -17,18 +18,15 @@ export {
type VoxelsChunkOrdering,
type VoxelsChunkSize,
} from './terrain/voxelmap/i-voxelmap';
export {
VoxelmapViewerAutonomous,
type VoxelmapViewerAutonomousOptions,
} from './terrain/voxelmap/viewer/autonomous/voxelmap-viewer-autonomous';
export type { IVoxelmapViewer } from './terrain/voxelmap/i-voxelmap-viewer';
export {
EComputationMethod,
EComputationResult,
VoxelmapViewer,
type ComputationOptions,
type VoxelmapViewerOptions,
type VoxelsChunkData,
} from './terrain/voxelmap/viewer/simple/voxelmap-viewer';
} from './terrain/voxelmap/viewer/voxelmap-viewer';
export { VoxelmapVisibilityComputer } from './terrain/voxelmap/voxelmap-visibility-computer';
export { EVoxelsDisplayMode } from './terrain/voxelmap/voxelsRenderable/voxels-material';
export { type CheckerboardType } from './terrain/voxelmap/voxelsRenderable/voxelsRenderableFactory/voxels-renderable-factory-base';
Expand Down
Loading