Skip to content

UnderwaterFogEffectDesc

The UnderwaterFogEffectDesc class is a Descriptor that fogs the view with the water between the camera and what it sees. It reads the depth of each pixel, integrates exponential fog along the ray below the water surface, adds the sunlight the water scatters toward the camera, and leaves everything above the surface untouched. Strengths are given per water column, from the map surface to the water surface, so a 2 m flood and a 30 m sea are described with the same numbers. The pass is off while the camera is above the water.

Type: boolean | undefined

Description: Controls the visibility of the effect. The pass also stays off while the camera is above the water surface, whatever visible is.

Default: true

Type: number | ((pos: LatLng) => number | undefined)

Description: Where the water surface is, as a height above the ellipsoid in meters. A number gives a level surface; a function answers for a position and returns undefined where there is no water. It is evaluated under the camera every frame. Required.

Example:

{
underwaterFog: {
waterLevel: 58,
}
}
{
underwaterFog: {
waterLevel: (pos) => floodLevelAt(pos),
}
}

Type: Vector3 | undefined

Description: Optical depth of the whole water column crossed vertically, per channel (x red, y green, z blue). Light is dimmed by exp(-strength) through the column, so 1.5 lets about a fifth through. More in red than in blue sinks the view into blue-green with distance, like clear sea water; more in blue than in red turns it muddy.

Default: new Vector3(1.5, 1.5, 1.5)

Example:

{
underwaterFog: {
waterStrength: new Vector3(2, 1.25, 0.9),
}
}

Type: number | undefined

Description: How fast the water thins toward the surface, 0 or more. The density at the surface is exp(-waterFalloff) of the density at the map surface; 0 keeps it uniform. The column’s total strength stays waterStrength.

Default: 0

Example:

{
underwaterFog: {
waterFalloff: 0.5,
}
}

Type: number | undefined

Description: Sunlight scattered by the water toward the camera, relative to the sky light; 0 disables it. The sun direction follows view.atmosphere, and both lights follow the sun’s elevation: under water at night nothing scatters, and the water is dark.

Default: 1

Example:

{
underwaterFog: {
sunIntensity: 2,
}
}

Type: number | undefined

Description: Henyey-Greenstein asymmetry of the water’s scattering, between -1 and 1 (exclusive). Water forward-scatters, so 0.7 to 0.9 puts a bright halo around the sun; 0 scatters evenly.

Default: 0.8

Example:

{
underwaterFog: {
phaseG: 0.6,
}
}

Type: number | undefined

Description: Share of the water column, from the map surface up, that the bottom layer fills, within [0, 1].

Default: 0.04

Example:

{
underwaterFog: {
bottomRatio: 0.1,
}
}

Type: number | undefined

Description: Optical depth of the bottom layer crossed vertically. 0 leaves the map as drawn; raise it for a layer of silt over the ground.

Default: 0

Example:

{
underwaterFog: {
bottomStrength: 1,
}
}

Type: Color | undefined

Description: Colour the water scatters in. It is light before tone mapping: with view.toneMappingExposure at 10, a colour divided by 10 shows as given.

Default: new Color().setStyle("#2d7a86")

Example:

{
underwaterFog: {
color: new Color().setStyle("#5a3f1c"),
}
}

Type: Color | undefined

Description: Colour the bottom layer scatters in. It is lit like color: light before tone mapping, dimmed by the water and the bottom layer above it.

Default: color

Example:

{
underwaterFog: {
bottomColor: new Color().setStyle("#241808"),
}
}
  • One full-screen pass with a fixed 8-step integration per pixel: its cost does not depend on the scene or the options.
  • The pass is disabled, at no cost, while the camera is above the water.
  • The water level under the camera is resolved once per frame, so a waterLevel function should be cheap.
import ThreeView, { Color } from "@navaramap/three";
import { UnderwaterFogEffectDesc } from "@navaramap/three-default-descs";
import {
DefaultPlugin,
type DefaultDescriptions,
} from "@navaramap/three-default-plugin";
import { Vector3 } from "three";
const view = new ThreeView<DefaultDescriptions>();
view.addPlugin(new DefaultPlugin());
await view.init();
const STREET = 56; // height above the ellipsoid, in meters
const level = STREET + 2;
// The flooded blocks
const blocks = view.addSource({
type: "geojson",
data: {
type: "Feature",
properties: null,
geometry: {
type: "Polygon",
coordinates: [
[
[139.699, 35.661],
[139.699, 35.658],
[139.702, 35.658],
[139.702, 35.661],
[139.699, 35.661],
],
],
},
},
});
// The water surface: a thin slab of water whose top is the level
view.addLayer({
type: "vector",
source: blocks,
polygon: {
color: new Color().setStyle("#5a3f1c"),
height: level - 0.2,
extrudedHeight: 0.2,
clampToGround: false,
water: true,
},
});
// Muddy water: silt takes blue first, so the view warms with distance
view.addEffect<UnderwaterFogEffectDesc>({
underwaterFog: {
waterLevel: level,
waterStrength: new Vector3(0.7, 0.8, 1),
waterFalloff: 0.5,
color: new Color().setStyle("#5a3f1c"),
},
});
import ThreeView from "@navaramap/three";
import { UnderwaterFogEffectDesc } from "@navaramap/three-default-descs";
import {
DefaultPlugin,
type DefaultDescriptions,
} from "@navaramap/three-default-plugin";
const view = new ThreeView<DefaultDescriptions>();
view.addPlugin(new DefaultPlugin());
await view.init();
const fog = view.addEffect<UnderwaterFogEffectDesc>({
underwaterFog: { waterLevel: 58 },
});
// The camera enters the fog once the level passes its height
function setWaterLevel(level: number) {
fog.update({ underwaterFog: { waterLevel: level } });
}
import ThreeView, { type LatLng } from "@navaramap/three";
import { UnderwaterFogEffectDesc } from "@navaramap/three-default-descs";
import {
DefaultPlugin,
type DefaultDescriptions,
} from "@navaramap/three-default-plugin";
const view = new ThreeView<DefaultDescriptions>();
view.addPlugin(new DefaultPlugin());
await view.init();
// The flooded area and its surface height above the ellipsoid
const FLOOD = { west: 139.699, east: 139.702, south: 35.658, north: 35.661 };
const FLOOD_LEVEL = 58;
const floodLevelAt = ({ lng, lat }: LatLng) =>
lng >= FLOOD.west && lng <= FLOOD.east && lat >= FLOOD.south && lat <= FLOOD.north
? FLOOD_LEVEL
: undefined;
// Asked under the camera every frame; undefined where there is no water
view.addEffect<UnderwaterFogEffectDesc>({
underwaterFog: {
waterLevel: floodLevelAt,
},
});
import ThreeView, { Color } from "@navaramap/three";
import { UnderwaterFogEffectDesc } from "@navaramap/three-default-descs";
import {
DefaultPlugin,
type DefaultDescriptions,
} from "@navaramap/three-default-plugin";
import { Vector3 } from "three";
const view = new ThreeView<DefaultDescriptions>();
view.addPlugin(new DefaultPlugin());
await view.init();
// Sea level as a height above the ellipsoid: the geoid height here
const SEA_LEVEL = 36.7;
// Red goes first, so the view sinks into blue-green
view.addEffect<UnderwaterFogEffectDesc>({
underwaterFog: {
waterLevel: SEA_LEVEL,
waterStrength: new Vector3(2, 1.25, 0.9),
waterFalloff: 0.5,
color: new Color().setStyle("#2d7a86"),
},
});
  • The effect does not draw the water surface. Add a water polygon (water: true) whose top is the level, so the surface is seen from below.
  • One water level, the one under the camera, serves the whole frame. Where the level varies across the view, the fog ends at a plane through that level; where the camera stands on dry ground beside the water, there is no fog.
  • The camera leaves the water 0.2 m above the surface, so a camera resting on the surface does not flicker the pass.
  • The map surface under the camera comes from the terrain. Where the level is at or under the terrain, there is no water. Without a terrain layer, or before it loads, the column is taken as 10 m.
  • The pass runs before the transparent pass, so transparent meshes are drawn over the fog.
  • SSR and FogLight, and SSAO and LensFlare without a Clouds effect, are also placed right before the transparent pass, so they run in the order they are added. Add this effect after them, so the water fogs what they draw.