Implement terrain LOD visibility calculation and refine mesh disposal logic

This commit is contained in:
2026-04-29 23:03:02 +02:00
parent 64f12c7607
commit eb7afbedbd

View File

@@ -4097,6 +4097,40 @@ export class RuntimeHost {
this.updateTerrainLodVisibility(); this.updateTerrainLodVisibility();
} }
private updateTerrainLodVisibility() {
const cameraPosition = {
x: this.camera.position.x,
y: this.camera.position.y,
z: this.camera.position.z
};
for (const renderObjects of this.terrainMeshes.values()) {
for (const chunk of renderObjects.chunks) {
const nextLevelIndex = resolveTerrainLodLevelIndex({
levelCount: chunk.levels.length,
chunkDiagonal: chunk.diagonal,
cameraPosition,
chunkWorldCenter: {
x: chunk.worldCenter.x,
y: chunk.worldCenter.y,
z: chunk.worldCenter.z
},
perspective: true
});
if (chunk.activeLevelIndex === nextLevelIndex) {
continue;
}
chunk.activeLevelIndex = nextLevelIndex;
for (let levelIndex = 0; levelIndex < chunk.levels.length; levelIndex += 1) {
chunk.levels[levelIndex]!.visible = levelIndex === nextLevelIndex;
}
}
}
}
private createRuntimeTerrainMaterial(terrain: RuntimeTerrain): Material { private createRuntimeTerrainMaterial(terrain: RuntimeTerrain): Material {
const layerTextures = terrain.layers.map((layer) => const layerTextures = terrain.layers.map((layer) =>
getTerrainLayerTexture( getTerrainLayerTexture(
@@ -4901,10 +4935,16 @@ export class RuntimeHost {
} }
private clearTerrainMeshes() { private clearTerrainMeshes() {
for (const mesh of this.terrainMeshes.values()) { for (const renderObjects of this.terrainMeshes.values()) {
this.terrainGroup.remove(mesh); this.terrainGroup.remove(renderObjects.group);
mesh.geometry.dispose();
mesh.material.dispose(); for (const chunk of renderObjects.chunks) {
for (const mesh of chunk.levels) {
mesh.geometry.dispose();
}
}
renderObjects.material.dispose();
} }
this.terrainMeshes.clear(); this.terrainMeshes.clear();
@@ -5237,6 +5277,7 @@ export class RuntimeHost {
this.updateRuntimeWaterReflections(); this.updateRuntimeWaterReflections();
} }
this.updateTerrainLodVisibility();
this.updateUnderwaterSceneFog(); this.updateUnderwaterSceneFog();
this.syncCelestialShadowState(); this.syncCelestialShadowState();