NatureGL Waterv1.1.0

Guides

Sky and lighting

The water needs a sky to reflect and a light to glitter under. Out of the box it brings its own, a time-of-day sky with clouds, stars and a moon. Your lights can follow it with one call per frame.

time from 16:24 to 20:48 on the sunset preset. The sun colour, the glitter path, the sky reflection and the bound lights all follow; after dark the moon becomes the key light.

#The built-in sky

WaterSystem.create() adds a sky dome to your scene. It follows the camera and scales itself to 0.9 × camera.far. It's the analytic sky from the original page: a sun arc from time, zenith and horizon colours, an fbm cloud deck with self-shadowing and silver lining, stars and a moon.

ParamDefaultEffect
time14.5Hour of day, 0–24. Eases the short way round the clock
clouds0.32Cloud cover 0–1. Also dims the sun
cloudDarkness0Storm darkening 0–1
overcast0Sky desaturation 0–1
fogDensity0.00032Exponential-squared aerial perspective, per metre
exposure1Exposure of the final composite
js
water.setParams({ time: 18.2 });                       // eases toward sunset
water.setParams({ clouds: 0.9, cloudDarkness: 0.6 });  // a darkening sky
water.stats.sunElevation;                              // degrees

These params only drive the built-in sky. With an external sky, time, clouds, cloudDarkness and overcast do nothing, and fogDensity still fogs the water.

dusk: blue hour, time: 18.45
moonlit: the moon is the key light, time: 23.4

#Lights that follow the sky

js
const sun = new THREE.DirectionalLight();
const hemi = new THREE.HemisphereLight();
scene.add(sun, sun.target, hemi);

renderer.setAnimationLoop(() => {
  water.update(dt);
  water.bindLights(sun, hemi, { distance: 250, intensity: 1.3 });
  water.render();
});

bindLights(directional?, hemisphere?, opts?) copies the water's current key light: the sun by day, the moon once the sun is down, from the built-in sky or an external one.

  • The directional light is placed opts.distance (250) metres from its target along the light direction, so move the target to where your shadows should be. Its colour times opts.intensity (1.3) equals the key light.
  • The hemisphere light takes the zenith and horizon colours for the sky, and the sky's ground colour.

#Scene fog

With manageFog: true (the default) the water keeps scene.fog as a THREE.FogExp2, coloured by the sky's horizon, with the fogDensity param as its density. Your objects then fade into the same haze as the sea. Pass manageFog: false to manage scene.fog yourself.

js
const water = await WaterSystem.create({ renderer, scene, camera, manageFog: false });

#Use a different sky

setSky() replaces the built-in sky with any object that has a sun, and optionally an environment map, cloud shadows, a fog colour, ambient light and a moon. The built-in dome is removed from the scene, and setSky(null) puts it back.

js
water.setSky(skySystem);   // naturegl-sky's SkySystem fits as is
water.setSky(null);        // back to the built-in sky

The full contract, the options and the NatureGL Sky setup are on With other packs.

#BuiltinSky on its own

BuiltinSky is exported, and water.builtinSky is always there, even while an external sky is active.

Member
sun{ direction, color, intensity }: the key light (the moon at night)
sunDirectionThe true sun, which may be below the horizon
moon.direction, fogColor, ambient.sky, ambient.ground
day0 at night … 1 with the sun well up
meshThe dome
apply(params)Recompute from time, clouds, cloudDarkness, overcast, fogDensity
follow(camera), bindLights(directional, hemisphere, opts), dispose()