Update underwater fog calculations and add local point function

This commit is contained in:
2026-04-07 07:11:46 +02:00
parent 4c0b429964
commit be7ccc93e8

View File

@@ -9,13 +9,13 @@ export interface UnderwaterFogState {
}
const MIN_UNDERWATER_FOG_DENSITY = 0.018;
const MAX_UNDERWATER_FOG_DENSITY = 0.055;
const MAX_UNDERWATER_FOG_DENSITY = 0.12;
function clampNumber(value: number, min: number, max: number) {
return Math.max(min, Math.min(max, value));
}
function isPointInsideWaterVolume(point: { x: number; y: number; z: number }, volume: RuntimeWaterVolume) {
function getWaterVolumeLocalPoint(point: { x: number; y: number; z: number }, volume: RuntimeWaterVolume) {
const offset = new Vector3(point.x - volume.center.x, point.y - volume.center.y, point.z - volume.center.z);
const inverseRotation = new Quaternion()
.setFromEuler(
@@ -30,6 +30,12 @@ function isPointInsideWaterVolume(point: { x: number; y: number; z: number }, vo
offset.applyQuaternion(inverseRotation);
return offset;
}
function isPointInsideWaterVolume(point: { x: number; y: number; z: number }, volume: RuntimeWaterVolume) {
const offset = getWaterVolumeLocalPoint(point, volume);
return (
Math.abs(offset.x) <= volume.size.x * 0.5 &&
Math.abs(offset.y) <= volume.size.y * 0.5 &&
@@ -37,9 +43,13 @@ function isPointInsideWaterVolume(point: { x: number; y: number; z: number }, vo
);
}
function resolveUnderwaterFogDensity(volume: RuntimeWaterVolume) {
function resolveUnderwaterFogDensity(volume: RuntimeWaterVolume, point: { x: number; y: number; z: number }) {
const localPoint = getWaterVolumeLocalPoint(point, volume);
const halfHeight = Math.max(volume.size.y * 0.5, 0.0001);
const submersionDepth = clampNumber((halfHeight - localPoint.y) / (halfHeight * 2), 0, 1);
return clampNumber(
MIN_UNDERWATER_FOG_DENSITY + volume.surfaceOpacity * 0.016 + Math.max(volume.waveStrength, 0) * 0.01,
0.045 + volume.surfaceOpacity * 0.035 + Math.max(volume.waveStrength, 0) * 0.015 + submersionDepth * 0.03,
MIN_UNDERWATER_FOG_DENSITY,
MAX_UNDERWATER_FOG_DENSITY
);
@@ -61,6 +71,6 @@ export function resolveUnderwaterFogState(
return {
colorHex: containingVolume.colorHex,
density: resolveUnderwaterFogDensity(containingVolume)
density: resolveUnderwaterFogDensity(containingVolume, telemetry.eyePosition)
};
}