Raymarch Glass Planet: SDF Glass Sphere, Tilted Ring and Orbiting Moon in a Dependency-Free Shader
Goal
Build one full-viewport black hero. Behind a large flush-left headline ("Built from light", one short line that may wrap), a full-bleed <canvas> draws, by raymarching signed distance fields in a WebGL1 fragment shader, a milky glass planet (translucent sphere whose fresnel rim lights up), a thin tilted ring around it and a tiny moon travelling along the ring. No three.js, no models, no images. The scene turns toward the pointer with inertia (fine pointers only), and while the hero scrolls away the canvas drifts down and scales up a little on a GSAP scrub. Below the hero there is a 60vh black spacer so there is room to scroll.
Tech
Vanilla HTML/CSS/JS with ES module imports. gsap + gsap/ScrollTrigger (npm). Raw WebGL1, no other dependency. Font: Inter 500.
Layout / HTML
<div class="rp">
<section class="rp-hero">
<canvas class="rp-canvas" data-rp-canvas aria-hidden="true"></canvas>
<h1 class="rp-title">Built from light</h1>
</section>
<section class="rp-after" aria-hidden="true"></section>
</div>
Styling
:root: --rp-bg:#000, --rp-ink:#fff, --rp-planet:#ff8fa3 (the single accent), --rp-ring:#f2f2f2.
.rp: background--rp-bg, colour--rp-ink, Inter..rp-hero:position:relative; height:100svh; min-height:560px; overflow:hidden..rp-canvas:position:absolute; inset:0; width:100%; height:100%; display:block..rp-title:position:relative; margin:0; padding:14vh clamp(1.25rem,4vw,3rem) 0; font-size:clamp(2.75rem,8.4vw,8rem); font-weight:500; line-height:.92; letter-spacing:-.045em; max-width:9ch..rp-after:height:60vh.
The shader (be exact)
Full-screen triangle ([-1,-1, 3,-1, -1,3]), vertex shader passes it through. Context: getContext("webgl", { premultipliedAlpha:true, alpha:true, antialias:true }), cleared to transparent every frame, dpr = min(devicePixelRatio, 1.5).
Uniforms: uRes, uTime, uPointer (vec2), uCenter (vec2), uScale, uBase, uLight, uRim, uRing (vec3).
- Ray:
uv = (gl_FragCoord.xy - .5*uRes)/uRes.y; uv -= uCenter; uv /= uScale; ro = (0,0,4.2); rd = normalize(vec3(uv,-1.55)). 90 steps,t += d*.9, hit atd < .0012, give up att > 9. No hit →vec4(0). map(p):t = uTime*.12; rotateq = pbyxz *= rot(uPointer.x*.25),yz *= rot(uPointer.y*.12). Spherelength(q)-1. Ring spacea = q; a.xy *= rot(.32); a.yz *= rot(1.32 + .04*sin(t)), ring = torus(1.62, .018). Moon atvec3(cos(t*2.2), 0, sin(t*2.2))*1.62in ring space, radius.05. Material ids 1 sphere, 2 ring, 3 moon.- Normal by central differences,
e = .0015. - Light
L = normalize(-.6,.7,.5);dif,fres = pow(1 - dot(n,-rd), 2.4),spec = pow(dot(reflect(rd,n),L), 40).
Sphere: c = mix(uBase, uLight, .35 + .55*dif); c = mix(c, uRim, fres*.85); c += spec*.35, alpha .42 + .5*pow(1-dot(n,-rd),2) (milky, rim more opaque). Ring and moon: mix(uRing, white, .25 + .6*dif) + spec*.6 + fres*.3, alpha .95.
- Output premultiplied:
vec4(c*a, a).
Colours: read --rp-planet and --rp-ring from computed style (hex or rgb to 0..1). uBase = planet, uLight = mix(planet, white, .7), uRim = mix(planet, white, .92), uRing = ring. Re-read every 30 frames so a live colour change shows up.
Motion (be exact)
const DEFAULTS = { scale: .62, speed: 1, pointer: 1, center: [.42, .12], scrollY: 25, scrollScale: 1.15 };
- Time: own rAF loop;
time += min(dt, .1) * speed. Draw only while an IntersectionObserver says the canvas is visible. A ResizeObserver resizes the drawing buffer. - Placement: desktop
center:[.42,.12], scale:.62; below 760pxcenter:[.25,-.35],scale*1.45. - Pointer inertia: on
pointermove(only if(pointer: fine)), target[(x/innerWidth - .5)*pointer, (y/innerHeight - .5)*pointer]; each frame the smoothed value moves 6% of the way:v += (target - v)*.06. - Scroll:
gsap.to(canvas, { yPercent:25, scale:1.15, ease:"none", scrollTrigger:{ trigger:".rp-hero", start:"top top", end:"bottom top", scrub:true } }). prefers-reduced-motion: reduce: time does not advance, no pointer, no scroll tween (a still frame).
destroy(): stop rAF, disconnect both observers, remove the listener, kill the tween and its ScrollTrigger, clearProps:"all" on the canvas, delete program, shaders and buffer.
Assets / images
None.
Behavior notes
- The canvas is transparent: the black is the section background, so the headline and page colours stay in CSS.
- Everything lives in one fragment shader of ~60 lines; cost scales with canvas pixels, which is why DPR is capped at 1.5.
- Only the prefixed variables are global.
Using this outside its demo page
This component is written as a complete page: that is how the demo is meant to look. If you are dropping it into an existing project, or combining it with other components, these are the things it declares at document level and that you need to move or reconcile first.
- Palette on
:root:--rp-bg,--rp-ink,--rp-planet,--rp-ring. Generic names like--ink,--paperor--mutedare defined by many components in this catalogue with different values, and they will also collide with whatever your own project defines. Move them onto the component's wrapper (.my-section { --ink: … }) or keep the prefix. - **Rules on
*, *::before, *::after,html, body**: the demo owns the whole document, so these set the box model and resets. Dropped into an existing project they restyle the entire page, not just this section. Re-target them at the component's wrapper before using it.