Add fog volume mesh configuration in runtime-host.ts

This commit is contained in:
2026-04-07 11:18:51 +02:00
parent cf48cabb11
commit edea9a6976

View File

@@ -18,6 +18,7 @@ import {
PointLight, PointLight,
Quaternion, Quaternion,
Scene, Scene,
ShaderMaterial,
Vector3, Vector3,
SpotLight, SpotLight,
WebGLRenderTarget, WebGLRenderTarget,
@@ -101,6 +102,7 @@ export class RuntimeHost {
private readonly cameraForward = new Vector3(); private readonly cameraForward = new Vector3();
private readonly volumeOffset = new Vector3(); private readonly volumeOffset = new Vector3();
private readonly volumeInverseRotation = new Quaternion(); private readonly volumeInverseRotation = new Quaternion();
private readonly fogLocalCameraPosition = new Vector3();
private readonly domElement: HTMLCanvasElement; private readonly domElement: HTMLCanvasElement;
private readonly ambientLight = new AmbientLight(); private readonly ambientLight = new AmbientLight();
private readonly sunLight = new DirectionalLight(); private readonly sunLight = new DirectionalLight();
@@ -576,6 +578,7 @@ export class RuntimeHost {
(brush.rotationDegrees.y * Math.PI) / 180, (brush.rotationDegrees.y * Math.PI) / 180,
(brush.rotationDegrees.z * Math.PI) / 180 (brush.rotationDegrees.z * Math.PI) / 180
); );
this.configureFogVolumeMesh(mesh, materials);
this.brushGroup.add(mesh); this.brushGroup.add(mesh);
this.brushMeshes.set(brush.id, mesh); this.brushMeshes.set(brush.id, mesh);
} }
@@ -583,6 +586,25 @@ export class RuntimeHost {
this.applyShadowState(); this.applyShadowState();
} }
private configureFogVolumeMesh(mesh: Mesh<BufferGeometry, Material[]>, materials: Material[]) {
const fogMaterials = materials.filter(
(material): material is ShaderMaterial => material instanceof ShaderMaterial && material.uniforms["localCameraPosition"] !== undefined
);
if (fogMaterials.length === 0) {
mesh.onBeforeRender = null;
return;
}
mesh.onBeforeRender = (_renderer, _scene, camera) => {
const localCameraPosition = mesh.worldToLocal(this.fogLocalCameraPosition.copy(camera.position));
for (const material of fogMaterials) {
(material.uniforms["localCameraPosition"] as { value: Vector3 }).value.copy(localCameraPosition);
}
};
}
private rebuildModelInstances(modelInstances: RuntimeSceneDefinition["modelInstances"]) { private rebuildModelInstances(modelInstances: RuntimeSceneDefinition["modelInstances"]) {
this.clearModelInstances(); this.clearModelInstances();