Banyan, a procedural tree grown from a 32-bit seed in one HTML file

banyan-863-orbit

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

Source: GitHub - satyasairay/banyan: A banyan tree grown from a 32-bit seed. One self-contained HTML file, three.js, zero downloaded assets. · GitHub

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

2 Likes

Baking the noise fields once at build time, after the vertex-shader move backfired on the iPhone, is a lesson I’d have liked earlier. On compileAsync my experience was different, but for a different goal. My black hole shader is about 140 KB of GLSL, and compileAsync with KHR_parallel_shader_compile removed a 2.4 to 2.7 s main-thread freeze on arrival; Lighthouse mobile blocking time went from 7.95 s to about 2 s. It doesn’t make a cold compile shorter, it just stops it blocking. One trap: compile with the same render target bound that you’ll draw into, or the first real frame builds a different program. Did your link-status probe ever misread a warm cache?

1 Like

Thanks Jonas. Your compileAsync result matches mine for the goal you mentioned.

On a cold profile it took my first frame from 6.6s to 3.9s and the page stayed responsive. The trap was the return visit. On my build the parallel path read a warm Chrome program cache but never filled one, so every later visit stayed at 2 to 4s. A synchronous link status read was the thing that filled it, and the next visits took 140 to 220ms. That is why I kept the sync path for returning visitors and the probe decides which visitor you are.

Yes, the probe misread once in the safe direction. It links a six octave value noise fragment, and reads LINK_STATUS. On the desktop D3D compiler that takes about 12ms cold and 1ms warm, so the threshold is 8ms. On my iPhone 13 under Metal it reads 13 and 9ms on a build the phone had loaded several times that day, called the visit cold, and took the sliced path, which at one status read per animation frame cost about 0.4 to 0.5s of pure sequencing. I changed the slice to a 6ms time budget per frame instead of one read per frame and on afterward runs the same phone read 1 to 2ms and went synchronous. It has never read warm on a cold cache which is the direction that would hurt.

On your render target trap: my probe binds no target, the scene programs are linked by renderer.compile(scene, camera) and the first frame draws to the default framebuffer with 4x MSAA. One program still compiles inside the first frame 16 after compile against 17 in the old synchronous frame so your point may apply there. Did the variation you saw come from the target itself or from state like MSAA or sRGB. Also which Chrome and GPU for the 7.95 to 2s Lighthouse number can I know?

1 Like