Three.js Dense Forest Environment (Instanced Trees & Wind)
A dense procedural forest in Three.js, with thousands of instanced conifers and broadleaf trees placed by jittered grid and density noise, around 20,000 undergrowth cards, wind sway injected into standard materials, soft shadows, mist and fireflies at dusk.
Key topics: procedural forest, instanced trees, three.js vegetation, wind shader, onbeforecompile, foliage
Game Over
What this code does
- Placement: a jittered grid thinned by low-frequency noise makes natural clearings, with conifers in dense patches and broadleaf trees elsewhere.
- Instancing: each part (trunks, three cone tiers, two blob canopies) is one InstancedMesh, so the forest takes about ten draw calls.
- Wind: onBeforeCompile adds a height-weighted sine sway to MeshStandardMaterial, phased by each instance's world position.
- Undergrowth: crossed planes with the same wind patch, scattered over the terrain.
- Atmosphere: lighting presets change sun, sky, exponential fog density and hemisphere fill; fireflies drift at dusk.
- Controls: light, density (repopulates), wind strength and fireflies.
Related Demos
Continue Learning
JavaScript Implementation
// Instanced forest with wind: patch the standard material's vertex shader
material.onBeforeCompile = (shader) => {
shader.uniforms.uTime = uTime
shader.vertexShader = 'uniform float uTime;\n' + shader.vertexShader.replace('#include <begin_vertex>', `#include <begin_vertex>
vec4 root = instanceMatrix * vec4(0.0, 0.0, 0.0, 1.0); // per-tree phase
transformed.x += sin(uTime * 1.6 + root.x * 0.15) * position.y * 0.04;`)
}
const crowns = new THREE.InstancedMesh(coneGeometry, material, trees.length)
trees.forEach((p, i) => crowns.setMatrixAt(i, new THREE.Matrix4().compose(p, randomYaw(), randomScale())))