Skip to content

About

☀️ Use environment map as source of light and shadows in your Three.js scenes 🔦

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Repository files navigation

three-environment-shadows

Use environment as a source of light and shadows in your Three.js scene. Extracts light sources from environment map automatically - no need for any more of the manually set PointLights and SunLights 🤲

Example

// NOTE WebGLRenderer

import * as Three from 'three';
import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
import { EnvironmentShadows } from 'three-environment-shadows';

const hdri = await new UltraHDRLoader().loadAsync('./environment.jpg');
hdri.mapping = Three.EquirectangularReflectionMapping;

scene.background = hdri;
scene.environment = hdri;

mesh.castShadow = true;
mesh.receiveShadow = true;

const shadows = new EnvironmentShadows(renderer, scene, {
  bounds: new Three.Sphere(new Three.Vector3(0, 1, 0), 6),
});
await shadows.ready;

renderer.setAnimationLoop(() => {
  shadows.update(camera);
  renderer.render(scene, camera);
});
// NOTE WebGPURenderer

import * as Three from 'three/webgpu';
import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
import { EnvironmentShadows } from 'three-environment-shadows/webgpu';

await renderer.init();

const hdri = await new UltraHDRLoader().loadAsync('./environment.jpg');
hdri.mapping = Three.EquirectangularReflectionMapping;

scene.background = hdri;
scene.environment = hdri;

mesh.material = new Three.MeshStandardNodeMaterial();
mesh.castShadow = true;
mesh.receiveShadow = true;

const shadows = new EnvironmentShadows(renderer, scene, {
  bounds: new Three.Sphere(new Three.Vector3(0, 1, 0), 6),
});
await shadows.ready;

renderer.setAnimationLoop(() => {
  shadows.update(camera);
  renderer.render(scene, camera);
});

Support

  • Three.WebGLRenderer (GLSL) r162+
  • Three.WebGPURenderer (TSL) r172+

Props

Option Default
samplesPerFrame 24 Shadow maps rendered per frame while a bake runs
samplesPerBake 512 Samples per bake, more is smoother and slower to update
fadeTime 0.3 Crossfade time (seconds) from the old to the new lightmap
mapSize 512 Shadow map size of each sample
atlasSize 1024 Lightmap atlas size (texels)
shadowNoise 8 Random offset of the shadow lookups (shadow map texels), softens stair-stepped edges
directionJitter 2 Random tilt of the sample directions (degrees), blends the shadows of neighboring samples
specularOcclusion true The baked visibility also darkens environment reflections
shadowBalance 0.5 Shadow strength (0 to 1), higher ignores more of the weak occlusion
autoUpdate false Moved meshes, a new environment or a new layout start a new bake. When false, set needsUpdate = true to start a bake
frustumAtlas true Only meshes in the camera frustum get a place in the atlas
frustumMargin 20 Growth of the bounding spheres for the frustum test (percent)
visibleAtlas false Charts seen by the camera (ray test) get more atlas area
visibilityResolution 384 Width of the ray grid of the visibility test
visibleScale 2 Atlas area of the visible charts, times the area of the hidden ones
bounds mesh bounds Sphere, the area that receives shadows
meshes scene meshes Meshes to use, default: all meshes that cast or receive shadows
exclude none Objects whose meshes (children included) do not cast shadows, they still receive. shadows.exclude is a Set, changes apply to the next bake

Credits

About

☀️ Use environment map as source of light and shadows in your Three.js scenes 🔦

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages