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

Fix/avoid lod gaps #30

Merged
merged 2 commits into from
Apr 8, 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
4 changes: 4 additions & 0 deletions src/lib/terrain/async-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class AsyncPatch {
return this.data.state === 'ready' && this.visible && !!this.data.patch;
}

public get isReady(): boolean {
return this.data.state === 'ready';
}

public async ready(): Promise<void> {
if (this.data.state === 'pending') {
await this.data.promise;
Expand Down
40 changes: 33 additions & 7 deletions src/lib/terrain/terrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Terrain {
strength: 0.4,
spread: 0.85,
},
map: {
minAltitude: -1,
maxAltitude: 256,
},
},
lod: {
enabled: true,
Expand Down Expand Up @@ -144,7 +148,7 @@ class Terrain {
patch.visible = false;
}

const visibilitySphere = new THREE.Sphere(position, radius);
const visibilitySphere = new THREE.Sphere(new THREE.Vector3(position.x, 0, position.z), radius);

type WantedPatch = {
readonly patchId: THREE.Vector3;
Expand All @@ -155,12 +159,15 @@ class Terrain {

const patchId = new THREE.Vector3();
for (patchId.x = patchIdFrom.x; patchId.x < patchIdTo.x; patchId.x++) {
for (patchId.y = patchIdFrom.y; patchId.y < patchIdTo.y; patchId.y++) {
for (patchId.z = patchIdFrom.z; patchId.z < patchIdTo.z; patchId.z++) {
const patchStart = new THREE.Vector3().multiplyVectors(patchId, this.patchSize);
for (patchId.z = patchIdFrom.z; patchId.z < patchIdTo.z; patchId.z++) {
patchId.y = 0;

const boundingBox = new THREE.Box3(patchStart, patchStart.clone().add(this.patchSize));
if (visibilitySphere.intersectsBox(boundingBox)) {
let patchStart = new THREE.Vector3().multiplyVectors(patchId, this.patchSize);

const boundingBox = new THREE.Box3(patchStart, patchStart.clone().add(this.patchSize));
if (visibilitySphere.intersectsBox(boundingBox)) {
for (patchId.y = this.minPatchIdY; patchId.y < this.maxPatchIdY; patchId.y++) {
patchStart = new THREE.Vector3().multiplyVectors(patchId, this.patchSize);
wantedPatchesList.push({
patchId: patchId.clone(),
patchStart,
Expand Down Expand Up @@ -222,7 +229,18 @@ class Terrain {
if (this.heightmapViewerNeedsUpdate) {
this.heightmapViewer.resetSubdivisions();
for (const patch of Object.values(this.patches)) {
if (patch.hasVisibleMesh()) {
let wholeColumnIsDisplayed = true;

for (let iY = this.minPatchIdY; iY < this.maxPatchIdY; iY++) {
const id = new PatchId({ x: patch.id.x, y: iY, z: patch.id.z });
const columnNeighbour = this.patches[id.asString];
if (!columnNeighbour || !columnNeighbour.isReady || !columnNeighbour.visible) {
wholeColumnIsDisplayed = false;
break;
}
}

if (wholeColumnIsDisplayed) {
this.heightmapViewer.hidePatch(patch.id.x, patch.id.z);
}
}
Expand Down Expand Up @@ -286,6 +304,14 @@ class Terrain {
this.garbageCollectPatches();
}

private get minPatchIdY(): number {
return Math.floor(this.parameters.voxels.map.minAltitude / this.patchFactory.maxPatchSize.y);
}

private get maxPatchIdY(): number {
return Math.ceil(this.parameters.voxels.map.maxAltitude / this.patchFactory.maxPatchSize.y);
}

private garbageCollectPatches(): void {
const patches = Object.entries(this.patches);
const invisiblePatches = patches.filter(([, patch]) => !patch.visible);
Expand Down