
Hey team,
I built Banyan, a procedural banyan tree in a single HTML file. Every tree grows from a 32-bit seed and nothing is downloaded.
Live: Banyan — a procedural tree study
There is a banyan in front of my house and I always wanted one that comes out different for every seed. I started the three.js version in July this year. ez-tree was an influence along the way. I measured a benchmark against it at the end of July: Banyan led on silhouette, wood and foliage whereas ez-tree led on materials and presentation.
Everything in the scene is generated from code: the trunk, aerial prop roots, bark, leaves, ground, grass, litter and light. The file is about 748 KB with three.js r165 bundled in, and it makes no network requests. There is no texture or image anywhere in it.
Some of the three.js details since this is the place for them:
- Leaves are InstancedMesh blades. Real geometry, no alpha test. That is why 4x MSAA is their only anti-aliasing, and MSAA is the biggest remaining cost in the frame.
- Bark detail comes from noise fields baked once on the GPU when the tree is built, not evaluated per pixel every frame.
- The hardest problem was phones. I moved five noise fields from the pixel shader to the vertex shader. That helped on my laptop but made things worse on my iPhone 13, because the trunk has about 125,000 vertices and covers fewer pixels than that at the opening view. Baking the fields at build time was the answer.
- First visit used to be 6.6 seconds of shader compilation. It is 3.3 seconds cold and 1.0 warm now. At boot the page compiles one fixed program and reads its link status to learn whether chrome’s program cache is warm. Warm means a plain synchronous compile. Cold means every link is issued at once and one link status is read per animation frame so the page stays responsive. compileAsync did not help, it reads a warm cache but never fills one.
- On a Radeon 740M at 1080x896 the frame went from 21 ms of GPU time to 12.7 ms. Four fragment programs were 18.5 of those 21 ms: leaves 6.6, bark 5.2, the shadow filter 3.9, soil 2.8. MSAA was another 7.6.
- On an iPhone 13 it holds about 43 fps while dragging.
At the balanced tier, a seed always gives the same geometry. Pixels can differ, because the bark is baked on your GPU and auto mode may pick a lighter tier on a small screen. I picked ten seeds out of 2400 candidates and recorded their digests, and the README has a console snippet that checks seed 863 against them. The digests are a 32-bit FNV-1a sample over the vertices plus six per-system digests. They catch drift between builds; they are not a proof of identity. ‘npm run check’ rebuilds the file and compares it byte for byte with the committed one, and CI runs the same check on Linux and Windows.
It is banyan-inspired, not botanical: one trunk, age fixed at 100 years, at most 12 prop roots, and wind as vertex sway.
If you have stood under a real banyan, I would like to know how far off the geometry, shape, foliage are and where the frame should go next.
-Satya
