webdevpuneet.com

Three.js Scroll Coral Reef Grow — Reef Restoration Story

Three.js Scroll Coral Reef Grow · Scroll · Plain HTML, CSS & JS · Live preview

About this UI Snippet

Three.js Scroll Coral Reef Grow — A Reef That Comes Back to Life as You Scroll

Screenshot of the Three.js Scroll Coral Reef Grow snippet rendered live

This snippet tells a small restoration story underwater. It opens on a sandy seabed 12 metres down with rocks, drifting marine snow, light rays from the surface and bright caustic patterns rippling across the sand. All that is left of the reef is bleached white stumps. As you scroll, the coral grows back and its colour returns colony by colony: bushy staghorn coral, flat sea fans, clusters of tube anemones and round brain corals. Halfway through, the fish start coming back in four schools, bubbles rise from the reef, and the camera circles down into it. A HUD tracks reef health and the number of fish, and four chapter captions narrate the recovery. Scroll back up and it all fades again.

One scroll value, many timelines

A GSAP tween on a pinned ScrollTrigger (scrub: 1, 450% of scroll) takes form.t from 0 to 1. Everything else is worked out from that number every frame: each coral segment has its own start and end time, each colony has a time when its colour comes back, each fish has a time when it appears, and the camera path is a function of it. Separately, a running time value drives the things that should keep moving when you stop scrolling: the swaying coral, the swimming fish, the bubbles and the caustics.

Coral built from branches

Staghorn and sea fans are generated by a small recursive branch() function. Each call adds one segment, then spawns two or three children from its tip with a shorter length, a thinner radius and a turned direction, down to four levels. Sea fans only turn around the fan's own normal, so all their branches stay in one flat plane. Tube anemones are single segments in tight clusters. Every segment remembers a start and end time, and a child starts when its parent is three quarters grown, so the coral visibly spreads outward generation by generation. Staghorn trunks start at 40% length, which is what makes the bleached stumps.

About 1,700 pieces, four draw calls

All branch segments share one tapered cylinder in a THREE.InstancedMesh, the rounded tips share one sphere, the brain corals share one bumpy icosahedron (vertices pushed out by a product of sines), and the fish share one hand-built low-poly fish geometry. Each frame the script writes a matrix per instance: position at the middle of the segment, rotation from setFromUnitVectors, and scale for radius and current length. Instances that have not started yet get a zero-scale matrix.

Swaying that keeps branches joined

The water movement is a function of position only: sway(p) returns a small sideways offset that grows with height and changes over time. Both ends of every segment are pushed by it, and because a child segment starts exactly where its parent ends, connected branches bend together instead of coming apart. Sea fans sway a bit more and anemones a lot more, set by a per-colony amplitude.

Colour coming back

Each colony has a living colour, and its instances are blended from a bleached off-white to that colour as the scroll passes the colony's start time, with tips a little lighter. Instance colours are only rewritten when the scroll value has actually changed, which keeps the per-frame cost down.

Water you can feel

The caustics are a Voronoi texture made on a canvas: pixels where the distance to the nearest and second-nearest point is almost the same light up, which gives the familiar net of light lines. The points wrap around, so it tiles, and it is used as the seabed's emissive map and slowly scrolled. The light rays are additive planes with a soft canvas gradient that always turn to face the camera. Fog matches the water colour so the reef fades into blue in the distance.

Fish schools

Each school circles the reef at its own radius, height and speed. Fish keep a random offset inside the school that rotates with it, so the formation turns as one, plus a small wobble. Their heading comes from the circle's tangent. The schools appear one after another from 50% to 80% of the scroll, a fish at a time. The Three.js Scroll Jellyfish Drift and Three.js Scroll Galaxy Formation snippets use the same scroll-to-world approach.

What's included

Features

Restoration story: bleached stumps regrow and regain colour as you scroll, then fish return
Four kinds of coral: recursive staghorn, flat sea fans, swaying tube anemones and bumpy brain corals
About 1,700 coral pieces and fish drawn with four InstancedMesh draw calls
Position-based sway that keeps connected branches joined while they move
Four fish schools that circle the reef, each arriving at its own point in the scroll
Caustic light from a seamless Voronoi canvas texture, light rays from the surface, bubbles and marine snow
Reef health and fish-count HUD, progress bar, four chapter captions and an intro card
Seeded layout, mouse look, off-screen pause and reduced-motion support

Step by step

How to Use

  1. 1
    Load the three CDN scriptsAdd three.min.js, gsap.min.js and ScrollTrigger.min.js from the CDN panel, in that order, before the snippet JS.
  2. 2
    Scroll through the reefThe stage pins. Bleached stumps grow back and colour returns, sea fans and anemones spread, then fish schools arrive while the camera circles down.
  3. 3
    Stop scrollingThe coral keeps swaying, the fish keep swimming and the light keeps rippling. Scroll back up to watch the reef fade again.
  4. 4
    Change the reefEdit the colony loops: the number of staghorn, fan and anemone colonies, their colour lists, start times and branch depth.
  5. 5
    Change the fishEdit the SCHOOLS array: colour, number of fish, circle radius, height, speed, size and the scroll point (th) where each school arrives.
  6. 6
    Tune the waterChange the fog colour and distances, the caustics emissiveIntensity, or the number of light rays and bubbles.

Real-world uses

Common Use Cases

Ocean and climate charities
Show what restoration means: scroll a dead reef back to life as the donation or impact story unfolds.
Marine biology and science lessons
Introduce coral types, bleaching and recovery with a scene students can control themselves.
Dive centres and travel brands
A calm, colourful hero for a diving school, resort or underwater photography site.
Sustainability reports and annual reviews
Use reef health as a visual metaphor for progress, with your own figures in the captions.
Learning procedural 3D with Three.js
Recursive branching, instancing, per-instance colour, canvas textures and simple schooling in one scene.

Got questions?

Frequently Asked Questions

A recursive branch() function adds a segment, then spawns two or three children from its tip with a shorter length, a thinner radius and a turned direction, four levels deep. Sea fans only turn around their own normal so they stay flat. Each segment stores a start and end time, and children start when their parent is three quarters grown, which makes the coral spread outward as you scroll.

All branch segments are instances of one cylinder, all tips of one sphere, all brain corals of one bumpy sphere and all fish of one small fish geometry, so the reef is four draw calls. Each frame the script only writes a transform matrix per instance, and instance colours are rewritten only when the scroll position changes.

The sway offset is a function of position (and time) only. Both ends of every segment are moved by it, and a child segment starts at exactly the point where its parent ends, so the two ends get the same offset and the joint never opens up.

A 256 px canvas is filled with a Voronoi pattern: for each pixel the distances to the nearest and second-nearest of 24 random points are compared, and pixels where they are almost equal are bright. The points wrap around the edges, so the texture tiles. It is used as the seabed's emissive map and its offset drifts over time.

Yes. Click JSX for a React component, Vue for a Vue 3 SFC or Angular for a standalone component. Build the scene in a mount effect against a canvas ref, and on unmount kill the ScrollTrigger, cancel the animation frame, disconnect the IntersectionObserver and dispose of the geometries, materials, textures and renderer.

Copy & paste

Source Code

<section class="crf-stage" id="crfStage">
  <canvas id="crfCanvas" aria-hidden="true"></canvas>
  <div class="crf-vignette"></div>

  <div class="crf-intro" id="crfIntro">
    <p class="crf-kicker">Reef restoration · 12 m deep</p>
    <h2>Bring the reef back</h2>
    <p class="crf-hint"><span></span>Scroll to grow the coral</p>
  </div>

  <div class="crf-chapters">
    <p class="crf-ch"><b>01</b>Bare rock<span>Bleached stumps on an empty seabed</span></p>
    <p class="crf-ch"><b>02</b>First polyps<span>Colour creeps back into the coral</span></p>
    <p class="crf-ch"><b>03</b>Branching out<span>Staghorn, sea fans and anemones spread</span></p>
    <p class="crf-ch"><b>04</b>Life returns<span>The fish move back in</span></p>
  </div>

  <div class="crf-hud">
    <div><small>Reef health</small><strong id="crfPct">0</strong><em>%</em></div>
    <div><small>Fish</small><strong id="crfFish">0</strong></div>
    <div class="crf-bar"><i id="crfBar"></i></div>
  </div>
</section>
<section class="crf-after">
  <h2>A living reef.</h2>
  <p>Scroll back up to watch it fade again: the fish leave, the colour drains and the coral shrinks back to bleached stumps.</p>
</section>

Extend with AI

Take This Snippet Further With AI

You do not have to work out procedural coral and instancing from scratch. Paste this snippet's HTML, CSS and JS into an AI assistant like Claude and ask it how the recursive branching works, why the sway keeps branches joined, or how the Voronoi caustics texture is made. It can also help you take it further: add a sea turtle that glides through once the reef is healthy, let fish scatter away from the mouse, add a diver silhouette with a bubble trail, or show real reef facts in the chapter captions.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a scroll-driven "coral reef restoration" scene in plain HTML, CSS and JavaScript with Three.js r128, GSAP and ScrollTrigger from a CDN.

Requirements:
- A pinned full-screen stage (scrub 1, end +=450%) where a tween takes one value t from 0 to 1. A requestAnimationFrame loop also keeps a running time for ambient motion. Pause rendering off screen with IntersectionObserver; with prefers-reduced-motion freeze the ambient time and use scrub: true.
- Underwater look: a CSS gradient behind a transparent canvas, Fog in the water colour, hemisphere and directional light, a sandy seabed with gentle dunes, jittered rock dodecahedrons, a seamless Voronoi caustics canvas texture used as the seabed emissive map and scrolled over time, additive light-ray planes that face the camera, rising bubbles and drifting marine snow.
- Coral from a recursive branch() function (segments with start and end times; a child starts when its parent is 75% grown): bushy staghorn colonies that begin as bleached stumps at 40% length, flat sea fans that only turn around their own normal, and clusters of single tube anemones. Draw all segments with one tapered-cylinder InstancedMesh and leaf tips with one sphere InstancedMesh, plus instanced bumpy brain corals that grow from 35% scale.
- Sway: a function of position and time that offsets both ends of every segment, scaled by height and a per-colony amplitude, so connected branches bend together.
- Colour: blend every colony from bleached off-white to its living colour as t passes the colony start time; only rewrite instance colours when t changes.
- Four fish schools (one InstancedMesh of a hand-built low-poly fish) circling the reef at their own radius, height and speed, with offsets that rotate with the school, appearing one fish at a time from about 50% to 80% of the scroll.
- Camera circles down from far and high to close and low as t grows, with a small mouse-look offset.
- Overlay: an intro card that fades on scroll, four chapter captions at set t ranges (Bare rock, First polyps, Branching out, Life returns), and a HUD with reef health %, visible fish count and a progress bar.
- Use a seeded random so the reef is the same on every load. Scrolling back up must reverse everything.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.