3D hero scene
Full-screen three.js scene of procedurally built rocks, crystals and a planet horizon, toon shaded in three flat tones with pixel-constant outlines. Objects drift and turn, follow the mouse with depth parallax and sink on scroll. The headline arrives word by word over the scene, with the pill button under it. Colours come from the tokens. Pauses when off screen, renders one still frame under reduced motion, and keeps an SVG drawing of the scene when WebGL or the library is unavailable.
Part of the style
Toon Orbit
7 parts that belong together, with their tokens, type and rules.
https://kitbash.me/components/toon-orbit/hero/figma.html?palette=b9eba0fe758ebace
Use the Figma MCP server (https://mcp.figma.com/mcp). Download https://kitbash.me/components/toon-orbit/hero/figma.html?palette=b9eba0fe758ebace and pass that whole HTML document to the html_to_figma tool, so the part "3D hero scene" lands as editable layers in my open Figma file. Fonts come from Google Fonts: if a face is missing in Figma, pick the closest one.
Code
<style>
/* Goes after the pack's base.css */
:root {
--field: #f5f1f2;
--hairline: rgba(19, 15, 29, 0.16);
--glow: rgba(19, 15, 29, 0.32);
--lilac: #130f1d;
--lilac-dim: rgba(19, 15, 29, 0.62);
--paper: #07040f;
--plum-ink: #f5f1f2;
--lime: #8a5855;
--lime-shade: #975f5b;
--lime-deep: #76433f;
--signal: #815450;
--pink: #815cc5;
--orchid: #6d5b95;
--void: #837b99;
--rock: #8777ac;
--stone: #7c59bc;
--line: #8f7fb5;
--orchid-shade: #7853b9;
--pink-shade: #7746c2;
--field-glow: #f8f5ff;
--field-deep: #f1ecfe;
--pink-deep: #966be4;
--orchid-deep: #9971e2;
--rock-shade: #8579a4;
--rock-deep: #837a9c;
--stone-shade: #9573d8;
--stone-deep: #8f73c8;
--glass: rgba(131, 123, 153, 0.5);
}
</style>
<section class="hero">
<div class="hero__sky" aria-hidden="true">
<svg class="hero__still" viewBox="0 0 1440 900" preserveAspectRatio="xMidYMid slice">
<defs>
<symbol id="hero-rock" viewBox="0 0 160 150">
<polygon class="hero__face-a" points="30,26 70,8 118,14 104,52 52,60"/>
<polygon class="hero__face-b" points="8,70 30,26 52,60 44,104 14,108"/>
<polygon class="hero__face-b" points="52,60 104,52 112,104 44,104"/>
<polygon class="hero__face-c" points="118,14 150,44 154,92 112,104 104,52"/>
<polygon class="hero__face-c" points="14,108 44,104 40,138"/>
<polygon class="hero__face-c" points="44,104 112,104 130,126 92,144 40,138"/>
<polygon class="hero__face-c" points="112,104 154,92 130,126"/>
<polygon class="hero__rim" points="8,70 30,26 70,8 118,14 150,44 154,92 130,126 92,144 40,138 14,108"/>
</symbol>
<symbol id="hero-crystal" viewBox="0 0 100 200">
<polygon class="hero__face-a" points="50,2 15,45 45,55"/>
<polygon class="hero__face-b" points="50,2 45,55 85,40"/>
<polygon class="hero__face-a" points="15,45 45,55 45,160 15,150"/>
<polygon class="hero__face-b" points="45,55 85,40 85,145 45,160"/>
<polygon class="hero__face-b" points="15,150 45,160 50,198"/>
<polygon class="hero__face-c" points="45,160 85,145 50,198"/>
<polygon class="hero__rim" points="50,2 85,40 85,145 50,198 15,150 15,45"/>
</symbol>
<symbol id="hero-gem" viewBox="0 0 120 120">
<polygon class="hero__face-a" points="60,30 88.5,50.7 77.6,84.3 42.4,84.3 31.5,50.7"/>
<polygon class="hero__face-a" points="31.5,50.7 6.7,42.7 27.1,14.7 60,4 60,30"/>
<polygon class="hero__face-b" points="60,30 60,4 92.9,14.7 113.3,42.7 88.5,50.7"/>
<polygon class="hero__face-b" points="42.4,84.3 27.1,105.3 6.7,77.3 6.7,42.7 31.5,50.7"/>
<polygon class="hero__face-c" points="88.5,50.7 113.3,42.7 113.3,77.3 92.9,105.3 77.6,84.3"/>
<polygon class="hero__face-c" points="77.6,84.3 92.9,105.3 60,116 27.1,105.3 42.4,84.3"/>
<polygon class="hero__rim" points="60,4 92.9,14.7 113.3,42.7 113.3,77.3 92.9,105.3 60,116 27.1,105.3 6.7,77.3 6.7,42.7 27.1,14.7"/>
</symbol>
</defs>
<g class="hero__paint-stone">
<circle class="hero__face-c hero__rim-stroke" cx="250" cy="1640" r="1010"/>
<path class="hero__face-b" d="M-60 760 Q 240 600 620 800 L 700 900 L -60 900 Z"/>
</g>
<use href="#hero-rock" class="hero__paint-orchid" x="690" y="130" width="420" height="394"/>
<use href="#hero-rock" class="hero__paint-rock" x="800" y="-60" width="230" height="216" transform="rotate(18 915 48)"/>
<use href="#hero-rock" class="hero__paint-rock" x="1250" y="-70" width="300" height="281" transform="rotate(-24 1400 70)"/>
<use href="#hero-rock" class="hero__paint-rock" x="1090" y="560" width="250" height="234" transform="rotate(40 1215 677)"/>
<use href="#hero-rock" class="hero__paint-rock" x="330" y="690" width="270" height="253" transform="rotate(-12 465 816)"/>
<use href="#hero-rock" class="hero__paint-stone" x="-40" y="250" width="200" height="188" transform="rotate(30 60 344)"/>
<use href="#hero-rock" class="hero__paint-stone" x="500" y="150" width="130" height="122" transform="rotate(-20 565 211)"/>
<use href="#hero-rock" class="hero__paint-stone" x="1340" y="390" width="60" height="56"/>
<use href="#hero-crystal" class="hero__paint-pink" x="410" y="210" width="40" height="80" transform="rotate(24 430 250)"/>
<use href="#hero-crystal" class="hero__paint-orchid" x="380" y="520" width="38" height="76" transform="rotate(-62 399 558)"/>
<use href="#hero-crystal" class="hero__paint-pink" x="1330" y="530" width="40" height="92" transform="rotate(8 1350 576)"/>
<use href="#hero-crystal" class="hero__paint-lime" x="1090" y="170" width="38" height="70" transform="rotate(24 1109 205)"/>
<use href="#hero-gem" class="hero__paint-lime" x="60" y="740" width="150" height="150"/>
</svg>
<canvas class="hero__canvas"></canvas>
</div>
<div class="hero__copy">
<h1 class="hero__title">Rare Rocks. Deep Time. Delivered Monthly.</h1>
<a class="hero__cta" href="#">
<span class="hero__cta-label">Claim Your First Stone</span>
<span class="hero__cta-tile" aria-hidden="true">
<svg class="hero__cta-arrow" viewBox="0 0 24 24"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
<svg class="hero__cta-arrow hero__cta-arrow--next" viewBox="0 0 24 24"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
</span>
</a>
</div>
</section>
<style>
.hero {
position: relative;
display: grid;
place-items: center;
min-height: max(100svh, 620px);
padding: calc(var(--space-7) + var(--space-4)) var(--space-4) var(--space-7);
overflow: hidden;
isolation: isolate;
}
.hero__sky {
position: absolute;
inset: 0;
z-index: -1;
background:
radial-gradient(ellipse 46% 52% at 62% 40%, var(--field-glow), transparent 72%),
radial-gradient(ellipse 70% 80% at -8% 38%, var(--field-deep), transparent 62%),
radial-gradient(ellipse 90% 46% at 50% 112%, var(--field-deep), transparent 74%),
var(--field);
}
/* The scene dissolves into the field at the bottom, so the next section starts clean. */
.hero__sky::after {
content: "";
position: absolute;
inset: 93% 0 0;
background: linear-gradient(transparent, var(--field));
}
.hero__still,
.hero__canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
transition: opacity var(--time-settle) var(--ease-cubic);
}
/* The drawn still is what you get without WebGL. While the live scene is on its way it stays out of sight. */
.hero__canvas { opacity: 0; }
.hero--live .hero__canvas { opacity: 1; }
.hero--loading .hero__still { opacity: 0; transition: none; }
.hero--live .hero__still { opacity: 0; }
.hero__paint-rock { --hero-a: var(--rock); --hero-b: var(--rock-shade); --hero-c: var(--rock-deep); }
.hero__paint-stone { --hero-a: var(--stone); --hero-b: var(--stone-shade); --hero-c: var(--stone-deep); }
.hero__paint-orchid { --hero-a: var(--orchid); --hero-b: var(--orchid-shade); --hero-c: var(--orchid-deep); }
.hero__paint-pink { --hero-a: var(--pink); --hero-b: var(--pink-shade); --hero-c: var(--pink-deep); }
.hero__paint-lime { --hero-a: var(--lime); --hero-b: var(--lime-shade); --hero-c: var(--lime-deep); }
.hero__face-a { fill: var(--hero-a); }
.hero__face-b { fill: var(--hero-b); }
.hero__face-c { fill: var(--hero-c); }
.hero__face-a, .hero__face-b, .hero__face-c { stroke: var(--hero-c); stroke-width: 1; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.hero__rim { fill: none; stroke: var(--line); stroke-width: 2; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.hero__rim-stroke { stroke: var(--line); stroke-width: 2; }
.hero__copy {
display: grid;
justify-items: center;
gap: var(--space-5);
max-width: var(--page);
text-align: center;
}
.hero__title {
margin: 0;
max-width: 15em;
font-size: var(--text-display);
font-weight: 500;
line-height: var(--leading-display);
letter-spacing: var(--tracking-display);
text-wrap: balance;
text-shadow: 0 0 0.5em var(--glow);
}
.hero__word {
display: inline-block;
transition:
opacity var(--time-settle) var(--ease-cubic),
filter var(--time-settle) var(--ease-cubic),
transform var(--time-settle) var(--ease-expo);
transition-delay: calc(var(--hero-order) * var(--stagger-word) + var(--time-quick));
}
.hero--armed .hero__word {
opacity: 0;
filter: blur(14px);
transform: translateY(0.25em);
}
.hero__cta {
display: inline-flex;
align-items: center;
gap: var(--space-4);
min-height: calc(var(--tap) + var(--space-3));
padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
border-radius: var(--radius-l);
background: var(--void);
font-weight: 500;
text-decoration: none;
transition:
background-color var(--time-quick) var(--ease-cubic),
opacity var(--time-reveal) var(--ease-cubic) calc(var(--time-reveal) * 1.5),
transform var(--time-settle) var(--ease-expo) calc(var(--time-reveal) * 1.5);
}
.hero--armed .hero__cta {
opacity: 0;
transform: translateY(var(--space-3));
}
/* The tint is a band of light that keeps passing through the label. */
.hero__cta-label {
--hero-label-ink: var(--lilac);
background: linear-gradient(100deg, var(--hero-label-ink) 0 38%, var(--orchid-shade) 50%, var(--hero-label-ink) 62% 100%) 0 0 / 300% 100%;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: hero-shimmer var(--time-shimmer) linear infinite;
}
.hero__cta-tile {
position: relative;
display: grid;
place-items: center;
width: var(--tap);
height: var(--tap);
overflow: hidden;
border-radius: var(--radius-m);
background: var(--paper);
color: var(--void);
transition: background-color var(--time-quick) var(--ease-cubic), color var(--time-quick) var(--ease-cubic);
}
.hero__cta-arrow {
grid-area: 1 / 1;
width: 22px;
height: 22px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
transition: transform var(--time-move) var(--ease-expo);
}
.hero__cta-arrow--next { transform: translateX(calc(-1 * var(--tap))); }
.hero__cta:hover,
.hero__cta:focus-visible { background: var(--paper); }
.hero__cta:hover .hero__cta-label,
.hero__cta:focus-visible .hero__cta-label { --hero-label-ink: var(--void); }
.hero__cta:hover .hero__cta-tile,
.hero__cta:focus-visible .hero__cta-tile { background: var(--void); color: var(--paper); }
.hero__cta:hover .hero__cta-arrow,
.hero__cta:focus-visible .hero__cta-arrow { transform: translateX(var(--tap)); }
.hero__cta:hover .hero__cta-arrow--next,
.hero__cta:focus-visible .hero__cta-arrow--next { transform: none; }
@keyframes hero-shimmer {
from { background-position: 100% 0; }
to { background-position: -50% 0; }
}
@media (max-width: 720px) {
.hero { align-items: end; padding-bottom: calc(var(--space-7) + var(--space-5)); }
.hero__sky {
background:
radial-gradient(ellipse 80% 40% at 50% 30%, var(--field-glow), transparent 72%),
radial-gradient(ellipse 110% 40% at 50% 110%, var(--field-deep), transparent 74%),
var(--field);
}
}
@media (prefers-reduced-motion: reduce) {
.hero__word, .hero__cta, .hero__cta-arrow, .hero__still, .hero__canvas { transition: none; }
.hero__cta-label { animation: none; }
}
</style>
<script>
(() => {
const THREE_URL = 'https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.min.js';
const CAMERA_DISTANCE = 10;
const FIELD_OF_VIEW = 35;
const MAX_PIXEL_RATIO = 2;
const OUTLINE_PIXELS = 1.6;
const ARRIVE_SECONDS = 1.6;
const ARRIVE_STAGGER_SECONDS = 0.07;
const POINTER_REACH = { x: 0.7, y: 0.45 };
const POINTER_EASE = 0.045;
const SCROLL_LIFT = 2.2;
const TALL_ASPECT = 0.8;
// Light sits up, left and in front of the camera, like a drawing lit from the top left corner.
const LIGHT_DIRECTION = [-0.55, 0.72, 0.42];
const PAINTS = {
rock: ['--rock', '--rock-shade', '--rock-deep'],
stone: ['--stone', '--stone-shade', '--stone-deep'],
orchid: ['--orchid', '--orchid-shade', '--orchid-deep'],
pink: ['--pink', '--pink-shade', '--pink-deep'],
lime: ['--lime', '--lime-shade', '--lime-deep'],
horizon: ['--stone', '--stone-shade', '--stone-deep'],
};
// at / tall: place in the frame, -1 is the left or bottom edge, 1 the right or top edge (tall is for portrait).
// depth: distance towards the camera. size: share of the frame's short side. haze: how far it sinks into the field.
const CAST = [
{ shape: 'boulder', paint: 'orchid', seed: 11, at: [0.3, 0.3], tall: [0.05, 0.42], depth: 0, size: 0.46, tallSize: 0.56, spin: 0.05, haze: 0 },
{ shape: 'rock', paint: 'rock', seed: 3, at: [0.27, 0.92], tall: [0.75, 0.93], depth: 1.2, size: 0.2, spin: 0.07, haze: 0 },
{ shape: 'rock', paint: 'rock', seed: 8, at: [0.97, 0.9], tall: [-0.9, 0.72], depth: 2.2, size: 0.27, spin: 0.04, haze: 0 },
{ shape: 'rock', paint: 'rock', seed: 21, at: [0.7, -0.5], tall: [0.9, -0.42], depth: 0.6, size: 0.22, spin: 0.05, haze: 0.06 },
{ shape: 'rock', paint: 'rock', seed: 14, at: [-0.4, -0.74], tall: [-0.55, -0.86], depth: 2.6, size: 0.24, spin: 0.03, haze: 0 },
{ shape: 'rock', paint: 'stone', seed: 5, at: [-0.95, 0.22], tall: [-1, 0.12], depth: 1.4, size: 0.2, spin: 0.05, haze: 0.1 },
{ shape: 'rock', paint: 'stone', seed: 17, at: [-0.24, 0.56], tall: [-0.6, 0.84], depth: -1.5, size: 0.14, spin: 0.08, haze: 0.22 },
{ shape: 'pebble', paint: 'stone', seed: 2, at: [-0.72, 0.74], tall: [0.2, 0.9], depth: -2, size: 0.045, spin: 0.2, haze: 0.3 },
{ shape: 'pebble', paint: 'stone', seed: 9, at: [0.9, 0.06], tall: [0.84, 0.16], depth: -0.5, size: 0.06, spin: 0.16, haze: 0.12 },
{ shape: 'pebble', paint: 'stone', seed: 6, at: [0.3, -0.66], tall: [0.5, -0.9], depth: 1, size: 0.055, spin: 0.14, haze: 0.1 },
{ shape: 'pebble', paint: 'stone', seed: 4, at: [-0.14, 0.7], tall: [-0.2, 0.96], depth: -2.4, size: 0.035, spin: 0.22, haze: 0.34 },
{ shape: 'crystal', paint: 'pink', seed: 1, at: [-0.42, 0.44], tall: [0.62, 0.72], depth: 0.4, size: 0.07, spin: 0.22, haze: 0 },
{ shape: 'bar', paint: 'pink', seed: 2, at: [-0.06, 0.36], tall: [-0.84, 0.36], depth: -0.6, size: 0.075, spin: 0.26, haze: 0.05 },
{ shape: 'crystal', paint: 'orchid', seed: 3, at: [-0.44, -0.26], tall: [-0.78, -0.56], depth: 1.6, size: 0.07, spin: 0.18, haze: 0 },
{ shape: 'bar', paint: 'orchid', seed: 4, at: [0.88, -0.3], tall: [0.8, -0.7], depth: 0.2, size: 0.09, spin: 0.2, haze: 0 },
{ shape: 'shard', paint: 'rock', seed: 5, at: [0.74, -0.4], tall: [0.3, -0.72], depth: 2.4, size: 0.05, spin: 0.3, haze: 0 },
{ shape: 'cube', paint: 'lime', seed: 6, at: [0.56, 0.56], tall: [0.8, 0.52], depth: 0.8, size: 0.05, spin: 0.3, haze: 0 },
{ shape: 'gem', paint: 'lime', seed: 7, at: [-0.86, -0.8], tall: [0.72, -0.94], depth: 3.2, size: 0.15, tallSize: 0.2, spin: 0.09, haze: 0 },
];
const HORIZON = { paint: 'horizon', at: [-0.8, -1], tall: [-0.7, -1], radius: 1.25, tallRadius: 1.5, rise: 0.36, tallRise: 0.24, depth: -1 };
const FILL_VERTEX = `
varying vec3 vViewPosition;
varying vec3 vRoundNormal;
varying vec3 vLocal;
void main() {
vec4 viewPosition = modelViewMatrix * vec4(position, 1.0);
vViewPosition = viewPosition.xyz;
vRoundNormal = normalMatrix * normalize(position);
vLocal = position;
gl_Position = projectionMatrix * viewPosition;
}`;
// Three flat tones picked by how a face meets the light. No gradients: that is the whole look.
const FILL_FRAGMENT = `
uniform vec3 uLit;
uniform vec3 uShade;
uniform vec3 uDeep;
uniform vec3 uHaze;
uniform vec3 uLight;
uniform float uHazeAmount;
uniform float uRound;
uniform float uTerrain;
varying vec3 vViewPosition;
varying vec3 vRoundNormal;
varying vec3 vLocal;
void main() {
vec3 facet = normalize(cross(dFdx(vViewPosition), dFdy(vViewPosition)));
vec3 normal = normalize(mix(facet, normalize(vRoundNormal), uRound));
float facing = dot(normal, uLight);
vec3 color = facing > 0.5 ? uLit : (facing > -0.08 ? uShade : uDeep);
if (uTerrain > 0.5) {
vec3 warped = vLocal * 17.0 + 1.6 * sin(vLocal.yzx * 9.0);
float land = sin(warped.x) * sin(warped.y + 1.9) + 0.6 * sin(warped.z * 1.7 + warped.x * 0.6);
float edgeOn = 1.0 - dot(normalize(vRoundNormal), normalize(-vViewPosition));
color = edgeOn > 0.9 ? uLit : (land > 0.42 ? uShade : uDeep);
}
gl_FragColor = vec4(mix(color, uHaze, uHazeAmount), 1.0);
}`;
// The outline is the object's back faces pushed outwards by a fixed number of pixels.
const OUTLINE_VERTEX = `
uniform vec2 uResolution;
uniform float uWidth;
void main() {
vec4 clip = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vec4 outward = projectionMatrix * modelViewMatrix * vec4(position * 1.02, 1.0);
vec2 direction = (outward.xy / outward.w - clip.xy / clip.w) * uResolution;
float reach = length(direction);
if (reach > 0.0001) clip.xy += direction / reach * uWidth * 2.0 / uResolution * clip.w;
gl_Position = clip;
}`;
const OUTLINE_FRAGMENT = `
uniform vec3 uColor;
void main() { gl_FragColor = vec4(uColor, 1.0); }`;
let threeModule;
const loadThree = () => (threeModule ??= import(THREE_URL));
const seededRandom = (seed) => () => {
seed = (seed + 0x6d2b79f5) | 0;
let mixed = Math.imul(seed ^ (seed >>> 15), 1 | seed);
mixed = (mixed + Math.imul(mixed ^ (mixed >>> 7), 61 | mixed)) ^ mixed;
return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
};
const easeOutExpo = (progress) => (progress >= 1 ? 1 : 1 - Math.pow(2, -10 * progress));
const clamp01 = (value) => Math.min(1, Math.max(0, value));
function browserDrawsWebGL() {
try {
return Boolean(document.createElement('canvas').getContext('webgl2'));
} catch {
return false;
}
}
// A lumpy ball with a few flat cuts, the way a stone breaks.
function chiselledBall(THREE, seed, { detail, lumpiness, cuts, cutDepth, stretch }) {
const random = seededRandom(seed);
const geometry = new THREE.IcosahedronGeometry(1, detail);
const positions = geometry.attributes.position;
const proportions = new THREE.Vector3(1 + random() * stretch, 1 - random() * stretch * 0.6, 1 - random() * stretch * 0.4);
const phase = [random() * 6, random() * 6, random() * 6];
const planes = Array.from({ length: cuts }, () => ({
normal: new THREE.Vector3(random() - 0.5, random() - 0.5, random() - 0.5).normalize(),
distance: 1 - cutDepth * (0.5 + random() * 0.5),
}));
const point = new THREE.Vector3();
for (let index = 0; index < positions.count; index += 1) {
point.fromBufferAttribute(positions, index);
const lump = Math.sin(point.x * 2.6 + phase[0]) * Math.sin(point.y * 3.1 + phase[1]) * Math.sin(point.z * 2.3 + phase[2]);
point.multiplyScalar(1 + lump * lumpiness);
for (const plane of planes) {
const over = point.dot(plane.normal) - plane.distance;
if (over > 0) point.addScaledVector(plane.normal, -over);
}
point.multiply(proportions);
positions.setXYZ(index, point.x, point.y, point.z);
}
return geometry;
}
function pointedPrism(THREE, sides, radius, halfLength, tip) {
const profile = [[0, -halfLength], [radius, -halfLength + tip], [radius, halfLength - tip], [0, halfLength]];
return new THREE.LatheGeometry(profile.map(([x, y]) => new THREE.Vector2(x, y)), sides);
}
const SHAPES = {
boulder: (THREE, seed) => ({ geometry: chiselledBall(THREE, seed, { detail: 2, lumpiness: 0.05, cuts: 16, cutDepth: 0.42, stretch: 0.12 }), crease: 16 }),
rock: (THREE, seed) => ({ geometry: chiselledBall(THREE, seed, { detail: 1, lumpiness: 0.28, cuts: 5, cutDepth: 0.5, stretch: 0.45 }), crease: 28 }),
pebble: (THREE, seed) => ({ geometry: chiselledBall(THREE, seed, { detail: 0, lumpiness: 0.2, cuts: 2, cutDepth: 0.4, stretch: 0.4 }), crease: 20 }),
crystal: (THREE) => ({ geometry: pointedPrism(THREE, 6, 0.42, 1, 0.45), crease: 10 }),
shard: (THREE) => ({ geometry: pointedPrism(THREE, 4, 0.6, 1, 0.9), crease: 10 }),
bar: (THREE) => ({ geometry: new THREE.BoxGeometry(0.38, 1.9, 0.38), crease: 10 }),
cube: (THREE) => ({ geometry: new THREE.BoxGeometry(1.2, 1.2, 1.2), crease: 10 }),
gem: (THREE) => ({ geometry: new THREE.DodecahedronGeometry(1, 0), crease: 10 }),
};
function buildStage(THREE, root, canvas) {
const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true });
renderer.setClearColor(0x000000, 0);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(FIELD_OF_VIEW, 1, 0.1, 60);
const resolution = new THREE.Vector2(1, 1);
const light = new THREE.Vector3(...LIGHT_DIRECTION).normalize();
const tokens = getComputedStyle(root);
const screenColor = (token) => new THREE.Color(tokens.getPropertyValue(token).trim()).convertLinearToSRGB();
const haze = screenColor('--field');
const outlineMaterial = new THREE.ShaderMaterial({
vertexShader: OUTLINE_VERTEX,
fragmentShader: OUTLINE_FRAGMENT,
side: THREE.BackSide,
uniforms: { uColor: { value: screenColor('--line') }, uResolution: { value: resolution }, uWidth: { value: OUTLINE_PIXELS } },
});
function fillMaterial(paint, hazeAmount, options = {}) {
const [lit, shade, deep] = PAINTS[paint].map(screenColor);
return new THREE.ShaderMaterial({
vertexShader: FILL_VERTEX,
fragmentShader: FILL_FRAGMENT,
polygonOffset: true,
polygonOffsetFactor: 1,
polygonOffsetUnits: 1,
uniforms: {
uLit: { value: lit },
uShade: { value: shade },
uDeep: { value: deep },
uHaze: { value: haze },
uLight: { value: light },
uHazeAmount: { value: hazeAmount },
uRound: { value: options.round ? 1 : 0 },
uTerrain: { value: options.terrain ? 1 : 0 },
},
});
}
function drawnObject(geometry, paint, hazeAmount, crease, options) {
const object = new THREE.Group();
object.add(new THREE.Mesh(geometry, fillMaterial(paint, hazeAmount, options)));
object.add(new THREE.Mesh(geometry, outlineMaterial));
if (crease) {
const creaseColor = new THREE.Color(tokens.getPropertyValue(PAINTS[paint][2]).trim()).lerp(new THREE.Color(tokens.getPropertyValue('--line').trim()), 0.45);
object.add(new THREE.LineSegments(new THREE.EdgesGeometry(geometry, crease), new THREE.LineBasicMaterial({ color: creaseColor })));
}
return object;
}
const actors = CAST.map((role, index) => {
const random = seededRandom(role.seed * 97 + index);
const { geometry, crease } = SHAPES[role.shape](THREE, role.seed);
const object = drawnObject(geometry, role.paint, role.haze, crease);
object.rotation.set(random() * 6, random() * 6, random() * 6);
scene.add(object);
return {
role,
object,
home: new THREE.Vector3(),
scale: 1,
axis: new THREE.Vector3(random() - 0.5, random() - 0.5, random() - 0.5).normalize(),
bobPhase: random() * 6,
bobSpeed: 0.25 + random() * 0.3,
arriveAt: index * ARRIVE_STAGGER_SECONDS,
};
});
const horizon = drawnObject(new THREE.IcosahedronGeometry(1, 24), HORIZON.paint, 0, 0, { round: true, terrain: true });
scene.add(horizon);
const halfFrameAt = (depth) => {
const halfHeight = Math.tan((FIELD_OF_VIEW * Math.PI) / 360) * (CAMERA_DISTANCE - depth);
return { halfWidth: halfHeight * camera.aspect, halfHeight };
};
function layOut() {
const width = root.clientWidth;
const height = root.clientHeight;
const pixelRatio = Math.min(window.devicePixelRatio || 1, MAX_PIXEL_RATIO);
renderer.setPixelRatio(pixelRatio);
renderer.setSize(width, height, false);
resolution.set(width * pixelRatio, height * pixelRatio);
outlineMaterial.uniforms.uWidth.value = OUTLINE_PIXELS * pixelRatio;
camera.aspect = width / height;
camera.updateProjectionMatrix();
const tall = camera.aspect < TALL_ASPECT;
const front = halfFrameAt(0);
const shortSide = Math.min(front.halfWidth, front.halfHeight) * 2;
for (const actor of actors) {
const [x, y] = tall ? actor.role.tall : actor.role.at;
const frame = halfFrameAt(actor.role.depth);
actor.home.set(x * frame.halfWidth, y * frame.halfHeight, actor.role.depth);
actor.scale = (((tall && actor.role.tallSize) || actor.role.size) * shortSide) / 2;
}
const [horizonX, horizonY] = tall ? HORIZON.tall : HORIZON.at;
const horizonFrame = halfFrameAt(HORIZON.depth);
const radius = (tall ? HORIZON.tallRadius : HORIZON.radius) * Math.max(horizonFrame.halfWidth, horizonFrame.halfHeight);
const rise = (tall ? HORIZON.tallRise : HORIZON.rise) * horizonFrame.halfHeight * 2;
horizon.scale.setScalar(radius);
horizon.position.set(horizonX * horizonFrame.halfWidth, horizonY * horizonFrame.halfHeight - radius + rise, HORIZON.depth);
horizon.rotation.set(0.5, 0, 0.35);
}
const pointer = { x: 0, y: 0, easedX: 0, easedY: 0 };
function pose(seconds, still) {
pointer.easedX += (pointer.x - pointer.easedX) * (still ? 1 : POINTER_EASE);
pointer.easedY += (pointer.y - pointer.easedY) * (still ? 1 : POINTER_EASE);
const scrolled = clamp01(-root.getBoundingClientRect().top / Math.max(1, root.clientHeight));
camera.position.set(pointer.easedX * POINTER_REACH.x, pointer.easedY * POINTER_REACH.y - scrolled * SCROLL_LIFT, CAMERA_DISTANCE);
camera.lookAt(0, -scrolled * SCROLL_LIFT, 0);
for (const actor of actors) {
const arrived = still ? 1 : easeOutExpo(clamp01((seconds - actor.arriveAt) / ARRIVE_SECONDS));
const bob = still ? 0 : Math.sin(seconds * actor.bobSpeed + actor.bobPhase) * actor.scale * 0.09;
actor.object.position.copy(actor.home).multiplyScalar(1.35 - 0.35 * arrived);
actor.object.position.y += bob;
actor.object.position.z = actor.home.z;
actor.object.scale.setScalar(Math.max(0.0001, actor.scale * arrived));
if (!still) actor.object.rotateOnAxis(actor.axis, actor.role.spin * 0.016);
}
if (!still) horizon.rotation.y = seconds * 0.01;
}
return {
layOut,
pointTo(x, y) { pointer.x = x; pointer.y = y; },
draw(seconds, still) { pose(seconds, still); renderer.render(scene, camera); },
};
}
async function bringSceneToLife(root) {
let stage;
try {
stage = buildStage(await loadThree(), root, root.querySelector('.hero__canvas'));
} catch {
root.classList.remove('hero--loading'); // the drawn still comes back and stays
return;
}
const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
let onScreen = true;
let frame = 0;
let startedAt = 0;
const tick = (now) => {
frame = 0;
if (!startedAt) startedAt = now;
stage.draw((now - startedAt) / 1000, still);
root.classList.replace('hero--loading', 'hero--live');
if (!still && onScreen && !document.hidden) frame = requestAnimationFrame(tick);
};
const wake = () => { if (!frame) frame = requestAnimationFrame(tick); };
new ResizeObserver(() => { stage.layOut(); wake(); }).observe(root);
new IntersectionObserver(([entry]) => { onScreen = entry.isIntersecting; if (onScreen) wake(); }).observe(root);
document.addEventListener('visibilitychange', wake);
root.addEventListener('pointermove', (event) => {
if (event.pointerType !== 'mouse') return;
const box = root.getBoundingClientRect();
stage.pointTo(((event.clientX - box.left) / box.width) * 2 - 1, -(((event.clientY - box.top) / box.height) * 2 - 1));
});
root.addEventListener('pointerleave', () => stage.pointTo(0, 0));
stage.layOut();
wake();
}
function splitTitleIntoWords(root) {
const title = root.querySelector('.hero__title');
const words = title.textContent.trim().split(/\s+/);
title.setAttribute('aria-label', words.join(' '));
title.replaceChildren(...words.flatMap((word, order) => {
const span = document.createElement('span');
span.className = 'hero__word';
span.style.setProperty('--hero-order', order);
span.setAttribute('aria-hidden', 'true');
span.textContent = word;
return [span, ' '];
}));
}
function revealCopy(root) {
splitTitleIntoWords(root);
root.classList.add('hero--armed');
requestAnimationFrame(() => requestAnimationFrame(() => root.classList.remove('hero--armed')));
}
function whenNearScreen(root, action) {
const watcher = new IntersectionObserver((entries) => {
if (!entries.some((entry) => entry.isIntersecting)) return;
watcher.disconnect();
action(root);
}, { rootMargin: '300px' });
watcher.observe(root);
}
const canDrawLive = browserDrawsWebGL();
document.querySelectorAll('.hero').forEach((root) => {
revealCopy(root);
if (!canDrawLive) return;
root.classList.add('hero--loading');
whenNearScreen(root, bringSceneToLife);
});
})();
</script>