Back to the cocoons

Cocoons Explained

Nine silk cocoons are let down on threads into the dark. One by one the light comes up inside them, from cream white to a lantern, until each web tears rope by rope and the cocoon falls. Every one of their fibres is drawn, and lit by small networks that learnt how light moves through silk from path-traced pictures. This page describes how that 26 second loop is made, lit and drawn, and what a frame costs.

Cocoons hanging in the dark, one glowing amber under its web
Loop
26 s, closes on itself
Cocoons
9, one learnt cocoon turned nine ways
Fibres
87,260 a cocoon, 1.38 M points
Learnt from
1,400 Cycles views
Networks
64 wide, 72 KB of weights
Download
5.9 MB of silk, 80 KB of code
Frame
12.3 ms on an M1 Max

What runs when

The expensive physics, light scattering many times between hairs and through a translucent shell, happens once, offline, in a path tracer. Small networks learn what came of it. The page only asks them, live, for the light at each place on the silk, and does everything else around that.

WhenWhatWhere
Once, offlineThe cocoon is made: shell, relief and 87,260 fibres of four kindsbake/hero.py
Once, offline1,400 views path traced in Cycles, 576 × 576 at 48 samples, every lamp and the light inside in a light group of its own (two Blender workers on an RTX 5080, about 3 s a view)bake/render_hero.py
Once, offlineThe networks, the textures they read and a few numbers of each fibre are learnt from those pictures, overnight on the same cardbake/train_hero.py, export_hero.py
Once, at page loadThe silk is unpacked; the shell’s relief is made on the GPU from the same noise; four tearings of a web are worked out in workershero/load.ts, relief.wgsl, tear.worker.ts
Every frameWhere each cocoon is; webs and threads; the networks at the fibres and on the shells that need it; fibres drawn as ribbons over the shells; lens, glow, film curvesrc/glow/

It is raw WebGPU inside a small sketch runner; three.js supplies the camera matrices and orbit controls only.

One cocoon

The shell is a superellipsoid (half-lengths 1.02 and 0.625, exponent 2.35) carrying a felted relief. Over it lie four kinds of fibre, each a curve through control points: the page draws the curve Cycles draws, a Catmull-Rom spline through them.

KindFibresPoints eachWhat it is
Wound30,00024Silk laid on the shell as it was spun
Fuzz12,00026Loose loops standing off it
Down45,0007Short fine hairs, a haze over everything
Floss260112Long strays
All87,2601.38 Mpoints to light, every time it is lit
The bare felted shell
With the wound silk
With the fuzz
With the down
Everything
The same cocoon in daylight, kind by kind. The shell on its own is only felt; most of what reads as silk is the fibres over it.

The webs and threads it hangs by are made in the page, not learnt with it, and lit by a separate small network that only ever saw single strands in the dark.

Light, learnt

Light adds up. The light leaving a place on the silk towards the eye is a sum over the lamps, and each lamp’s share can be split into a part that belongs to the place and the eye, and a part that belongs to the lamp. That split is precomputed radiance transfer: the transfer of each place is worked out ahead, and lighting it is a dot product.

L(place, eye)  =  Σ over lamps   colour(lamp) · W(place, eye) · B(direction of lamp)

W: 2 × 24 numbers (light that stays white, light the silk has warmed)    B: 24 numbers
plus 3 numbers for the light inside, which has no direction to learn

Classic precomputed radiance transfer stores the transfer in spherical harmonics. Here both halves are small networks: W, three layers of 64, and B, which turns a direction into its 24 numbers. Together they form a relightable surface light field of this one cocoon. W is told, at each place:

  • Where it is, by two textures learnt over the cocoon: 192 × 96 of 16 numbers for how light reaches it at all, 1024 × 512 of 8 for what is smaller.
  • Which fibre it is, by a few numbers of the fibre’s own at knots along it, learnt too. Neighbouring hairs can then differ, as they do.
  • Which way things face: the eye, the fibre’s direction, the relief’s normal and depth. These are given, not learnt, which is why another relief can be put under the same networks.
  • Where along the fibre, and how far it stands off the shell.

The weights are half floats written into the shader’s text as literals, not read from a buffer. That alone made the networks about 40 times faster. Against Cycles at held-out views the page comes to 21.2 dB on this cocoon.

Any number of lamps

Because the light is a sum, the page never asks the networks about a lamp. Once a frame, for each lamp, it works out B and its colour, multiplies them into the last layer of W, and adds the layers up. W then answers with the colour itself. Three lamps cost what one does, and so do a hundred, as on the sunrise page, where every patch of the sky is a lamp.

Four pictures of the hero cocoon from the page, each lit by one thing alone: the key lamp, the back lamp, the rim lamp, the light inside. Any mix of them is their weighted sum, which is what the page computes before its film curve. Here the finished pictures are mixed (in linear light), so strong mixes come out a little brighter than the page would draw them.

Shaded where it lives

The networks are not run per pixel. A compute pass runs them at the fibres’ control points and writes each point’s light, two half floats a channel, into a buffer. Drawing then only reads it. This is texture-space, or decoupled, shading: the cost follows the silk, not the screen.

  • Fibres are drawn as ribbons turned to the eye along their curves, never thinner than a pixel: a thinner one is drawn a pixel wide and that much fainter. 4 × MSAA, in HDR.
  • Shells are lit on a grid over each, 1024 × 512 at most, once and not for every pixel they cover; most of a shell lies under silk anyway. At 2048 the grid cost 3.3 ms more and looked the same to 61 dB close up: it was finer than the 1024-wide texture the network reads.
  • The relief is not downloaded. The page makes it on the GPU from the same noise the bake used.
  • Shadows of one cocoon on another are not in what was learnt, because each cocoon was learnt alone. The page casts them from soft ellipsoids standing in for the cocoons, the six nearest, worked out at a few places along each fibre.

The light inside

In Cycles the light inside was a light group of its own, so the networks learnt how silk answers light from within as three more numbers. The page decides how much of it there is: none while the cocoon hangs cream white, coming up over five seconds to all aglow, guttering as the strands of its web snap, and going out as it falls.

  • The pupa was not in the bake. Its shape is a distance field in the page (blunt head, wing cases, rings), and a grid over each cocoon works out its shadow in the light from behind and from within, and where it shows through the silk. So it stirs, and the lamp inside moves about.
  • A lantern needs a dark room. Light added inside a lit cocoon only made it a warmer cream. What made it read as a lantern was lighting it less from outside as it glows, by up to 65%, as a room darkens when a lamp is lit in it. That is a choice for the picture, not physics.
  • The two lights are kept apart: the light from outside and the light from inside are stored separately for every point and shell, and mixed only as they are drawn. The camera stands still, so a cocoon that hangs still need not be lit again while its glow flickers.

Torn and falling

Each cocoon hangs by one thread from a web of ropes, and is torn out of it by its own weight. The cocoon is a rigid body; each strand a spring that only pulls, from where it leaves the cocoon’s surface, with its own strength, tiring as it is strained. A break loads the others of its rope more. When and how it tilts, swings and falls comes out of that, a fibre bundle model. Four tearings are worked out in workers as the page loads, and played back: nothing of it is downloaded.

6 s · let down, cream white
10.5 s · the light comes up
15 s · its web gives, it tilts
18.5 s · it falls, going out
19.3 s · what is left is drawn up

Every cocoon is a function of its own age, the loop time plus its phase. It is let down into its place, hangs, glows, tears and falls; the remains of its web are drawn up out of the picture, and the next is let down. At the ends of its life all of it is out of sight, so the 26 seconds close on themselves without blending anything.

The lens

Depth of field is part of the picture: three cocoons are sharp, two big ones before them are soft shapes at the edges, four behind are softer still. A pixel’s blur is that of the silk in it, not of the black behind, and near and far blur are kept apart by their sign, or one over the other would come out sharp.

The blur is gathered from a half-size copy. That copy is already as soft as a pixel or two of blur, so blurs of up to about three pixels are gathered at full size instead, and the half-size gather takes over beyond. One weakness is left: blurs of three to eight pixels come out blotchy, because the gather’s taps are spread for the widest blur and a small one gets few of them. So every cocoon is either inside the sharp zone or well out of it.

A cocoon half in focus: blotchy with the lens, crisp without it
Before the cocoons were moved: half in the sharp zone and half out, the small blurs went smeary down its middle (left, lens on; right, lens off).

Spent where it shows

Nine cocoons are 12.4 million points of network, and without any of what follows a frame takes 70 ms. What is spent now follows what can be seen.

  • Fewer hairs where they don’t show. As many are drawn as come out about a pixel wide, each drawn the wider: the same silk, fewer strands. Where the lens blurs a cocoon there are fewer still. The down, a haze, is drawn widest.
  • Lit again only when needed. A cocoon is lit anew when it has turned by more than 0.03 radians, every 6th frame while the lamp and pupa inside it move, and every 12th while it hangs cream white, staggered so that each frame has about as much. On average fewer than two of the nine are lit in a frame.
  • Light along a fibre changes slowly. It is worked out at every second point a fibre is drawn through (every third where blurred) and mixed between. The same to 42 dB.
  • Coarser where blurred: the shells’ grid follows the size and blur on screen, the shells are drawn through every other line of their mesh, and the ribbons are as long as twelve pixels.
The same cocoon lit at every point and at every second point
Light worked out at every point of every fibre (left) and at every second, mixed between (right).

One frame, timed

Measured on an M1 Max, 2268 × 1473 pixels (a 1512 × 982 window at 1.5 pixels to a point), the whole loop sampled. Each part is the frame with it minus the frame without it, so they add up to a little more than the frame, which takes 12.3 ms.

PartWhat it costsms
Wound silkThe most fibres and points3.8
An empty frameLens, glow, film curve: every pixel, whatever is there2.4
FuzzLoose loops standing off the shell1.7
Down45,000 short hairs a cocoon1.5
ShellsTheir grids when lit, their meshes every frame1.5
FlossFew, but 112 points each0.8
Webs and threadsMade anew every frame from the tearing0.8
The frame12.3

How it got here

StepPicturems a frame
Seven cocoons, none of the savings below1600 × 90054
Fewer hairs where small or blurred; blurred ones lit every 3rd frame, the others every other1600 × 90021.0
Light at every second point of a fibre1600 × 90017.5
The same on a 2× screen: 30 frames a second3024 × 196432.8
Nine cocoons, closer; at most 1.5 pixels to a point2268 × 147325.2
Wider hairs on the shell, a coarse mesh for blurred shells2268 × 147320.7
Webs lit with their cocoon; the six nearest cast shadows2268 × 147319.7
Outside and inside light apart; lit again only when turned, or now and then2268 × 147315.8
Widths by kind, every shell through half its mesh, 64 lens taps not 962268 × 1473≈14
Ribbons twelve pixels long, not seven2268 × 147312.3

What did not help: one sample a pixel instead of four (Apple’s GPUs resolve samples in tile memory, and it cost the same), and lighting the blurred ones every 5th frame instead of every 3rd (they were not the cost). A lower pixel ratio would save 1.5 ms more, and was not taken.

The two lessons from that list. Most of the cost was relighting things that had not changed: with a camera that stands still, keeping the two lights apart and asking only when a cocoon turned was worth more than any cut to the networks. And timing itself misled: under a 60 frames a second cap the card lowers its clock as it has less to do, so the passes’ own times hardly fell when work was halved. Every number here is whole frames drawn back to back, with the card busy.

What is downloaded

The main page fetches 5.9 MB of silk and 80 KB of code, compressed. Fibre positions are kept as differences of differences of 16-bit numbers, byte plane by byte plane, which compress well; the learnt texture is an AVIF; the relief, the webs, the threads and the tearing are all made in the page.

WhatAs sentMB
Wound silk, its points720,000 points2.23
The smaller learnt texture1024 × 512 × 8, AVIF1.12
Down, its points315,000 points0.83
Fuzz, its points312,000 points0.72
Each fibre’s own learnt numbers4 bytes at each knot0.64
The larger learnt texture192 × 96 × 160.22
Code: the page, shaders, lens, tearingJavaScript, gzip0.08
The networkshalf floats0.07
Floss, its points29,000 points0.06
All of it5.97

The learnt numbers are the smallest part of it: 72 KB of networks describe how light moves through all of the silk. Most of the download is where the silk is, not how it is lit.

How it is checked

  • Against Cycles. The page can be held at the eye and lamps of held-out views and compared with the path traced pictures (tools/truth.mjs, bake/compare.py): 21.2 dB for this cocoon.
  • Every shortcut against no shortcut. Light at every second point: 42 dB the same. Shells’ grid at 1024 instead of 2048: 61 dB. A moving frame lit by turns against one lit in full: 40.7 dB.
  • The seam. All 780 frames of the film are rendered held at their moment, and the step from the last back to the first (14.3) is within the steps between neighbours (13.2 to 15.6).
  • The quantity, not the picture. One speed-up drew every frame black and still timed faster; another fault made thin strands black whatever was changed in the shading, and was the lens all along. Since then, every change to the speed is rendered and compared, and suspicious pixels are traced by reading the light buffers back.

What is not real

  • The nine are one cocoon, turned nine ways. It was learnt alone, so how cocoons shade one another is not learnt but cast from ellipsoids.
  • The pupa, its stirring and the way it shows through the silk are worked out in the page, and so is how much light is inside.
  • Cocoons are lit less from outside as they glow, which makes them lanterns. Real ones are not.
  • The tearing is played 1.3 times as fast as it was worked out; the cocoons are light and the lens is close.
  • Glints off the strands’ skin are added by hand: the networks blur them away.
  • A few single pixels on glowing silk come out dark or blue. They come from where down lies over a shell, and are not yet found.

Methods used: Sloan, Kautz and Snyder, Precomputed Radiance Transfer for Real-Time Rendering in Dynamic, Low-Frequency Lighting Environments (2002); Rainer, Bousseau, Ritschel and Drettakis, Neural Precomputed Radiance Transfer (2022); Wood et al., Surface Light Fields for 3D Photography (2000); Thies, Zollhöfer and Nießner, Deferred Neural Rendering (2019) for learnt textures; Park et al., DeepSDF (2019) for numbers learnt per thing; Hillesland and Yang, Texel Shading (2016); Chiang, Bitterli, Tappan and Burley, A Practical and Controllable Hair and Fur Model for Production Path Tracing (2016), which Cycles’ Principled Hair follows; Daniels (1945) for bundles of threads that break.