Skip to main content
Live WebGL2 demo Open full screen

Idea:

I saw Lincoln Margison’s post on X about using Star Polygon Rays to trim empty pixels around octahedral tree impostors. Plain 2-triangle impostor cards waste a huge amount of fragment work on transparent sky around the canopy (70% to 86% of the card on trees). Wrapping the silhouette in a 16-triangle star fan cuts that overdraw down.

I wanted to see if we could cut the same empty area with far fewer triangles. That turned into Tangram 6+1: instead of 16 radial rays, each view folds up to 3 stacked horizontal trapezoids around the tree (2 to 6 active triangles), and switches to a single 1-triangle cutout when a tree gets small on screen.

Spin the tree above, toggle Wire to see the triangles, or switch between Broadleaf, Conifer, and Windswept. To time your GPU, open 50k Forest. On a phone, scroll past the scene to run the benchmark and see the results.

Why 3 trapezoids instead of 16 rays:

Drag the divider or switch tabs to compare the plain 2-triangle card and 16-triangle star against Tangram 6.

A 16-ray star fan always starts from one center point, so every ray has to cover both neighboring wedges without clipping twigs. That leaves pockets of empty space around asymmetric canopies and narrow trunks, while still submitting 16 triangles per tree.

Trees naturally divide along height: trunk at the bottom, wider lower boughs, and a tapering crown. Up to three stacked trapezoids follow those sides. The vertex shader collapses unused bands. A pointed band can also collapse one triangle. Windswept row 86 has five triangles with the seam pad alone. MSAA padding can open the tip back up.

Broadleaf, Conifer, and Windswept tree silhouettes wrapped in Tangram 6 cutouts Broadleaf, Conifer, and Windswept trees wrapped in 1 to 3 stacked trapezoids.

77-second walkthrough from empty card overdraw to the 50,000-tree forest timings.

The 1-triangle far LOD:

Once a tree shrinks below 18 px tall on screen, we switch to Far1, a conservative enclosing triangle baked into the same lookup row. The baker expands candidate triangles before rounding and keeps the smallest tested fit that still covers the hull. A smaller fit may exist on the byte grid.

Distant tree enclosed by a single Far1 cutout triangle Far1 packs the whole distant silhouette into one tight enclosing triangle.

The Far1 mesh uses 4 corners with indices [0, 1, 2, 0, 2, 3]. On valid LUT rows, corners 2 and 3 both decode to vertex C ([A, B, C, C]), so only 1 triangle draws (or you can set the draw range to 3 indices when every far instance is valid). If a row is invalid or too close, the same 4 corners open back up into the full 2-triangle card.

How to build it in your engine:

Get the reference source on GitHub. It includes the runnable WebGL2 demo, baked rows and certificates, and the offline baker. Clone it and run these from the folder with package.json, using Node.js 22 or newer and npm.

npm ci
npm run build
npm run bake:cutouts -- broadleaf

Open dist/index.html to try the reference. The one-species bake checks containment on every row and runs the holdout before printing the path to baked-broadleaf.ts. Copy its broadleaf entry into src/core/baked-cutouts.ts and rebuild to use the new data.

Replace broadleaf in createProceduralTree (src/core/trees.ts) with a centered, non-indexed triangle mesh. Match its scale, vertex normals and colors. Clear the old broadleaf overrides in scripts/cutout-reference-depth.ts to [].

For textured leaves, sample the leaf alpha and discard transparent fragments at the material’s cutoff before writing either atlas. The demo’s bake shader treats mesh fragments as opaque. You can also supply both atlases from an engine that already applies that cutoff. If the CPU certificate fails, use the rendered atlases. In bakeSpecies (scripts/cutout-baker.ts), load the saved albedoBytes and normalDepthBytes from bakeOctahedralAtlases (src/core/octahedral.ts) in place of cpuBakeAtlases(species), then rebake. Use those atlases at runtime.

1. Atlas setup:

Bake an 8x8 hemi-octahedral grid (64 views, 7x7 = 49 cells, 98 barycentric view triangles) into two 2048x2048 RGBA8 textures (256 px per tile, 4 px inner gutter, 4-ring edge-bleed dilation, linear filter, no mipmaps).

The first texture holds Albedo in RGB and binary Alpha in A (0.5 cutoff, or 0.01 under blended alpha-to-coverage). The second holds World Normal in RGB (n * 0.5 + 0.5) and signed depth along tile forward in A (0.5 + 0.5 * dot(worldPos, forward) / cardHalfExtent), with parallax UV shift clamped to ±0.18 in tile space. Use orthographic half-extent radius * (256 / 248) * 1.03 so geometry stays inside the 4 px gutter.

2. Offline bake pipeline:

  1. Bake the 64-view atlasRender Albedo+Alpha and World Normal+Depth with a 4 px gutter per 256 px tile and 4 rings of edge bleed.
  2. Build an atlas certificateSummarize each 4x4 px block as a 16-bit dilated alpha mask plus [minDepth-1, maxDepth+1] so runtime can fall back to the card if the atlas changes.
  3. Trace conservative support per LUT rowWalk a 256x256 card quadtree with interval bounds across every pose, barycentric blend weight, depth parallax shift, and distance tier to collect horizontal [left, right] spans.
  4. Fit 1 to 3 trapezoids + Far1 triangleSearch 1 to 3 bands and round the hull edges outward. If a rounded edge reaches a byte limit, use a conservative bounding box for that band. Enclose the convex hull in Far1 and check coverage after quantization.
  5. Holdout checkVerify exact containment on all 896 rows, then raster-test 1,018 poses x 4 sampling modes (4,072 cases per species) for 0 clipped pixels.

3. 896-row LUT and 24 B/row packing:

Why 896 rows instead of 98? At closer distances, perspective ray shift and 3-frame blending move the silhouette inside a single view triangle. Subdividing the 7x7 atlas cells 2x (14x14x2 = 392 sub-triangles) and splitting two distance tiers keeps the cutouts tight without clipping:

RowsRegionDistance tier
0-391Upper hemisphere (14x14x2)Far (>= 6R, also shadow pass)
392-783Upper hemisphere (+392)Near (2.35R to < 6R)
784-895112 below-horizon rows (784 + 56*near + 14*edge + seg)sin(elev) >= -0.205 (about -11.8°)

Below 2.35R, below the elevation limit, or on an invalid row, the shader falls back to the full 2-triangle card. On certified cuts at overhead view triangles 48 and 49, it uses the hemi-octahedral basis to avoid twisting at the zenith.

To sanity-check your LUT indexing, test the +Z horizon direction (0, 0, 1): it must hit row 390 at 8R and row 782 at 4R.

The reference repo uses 60 bytes per row (15 texels), including the Strips and Star comparisons. A Tangram and Far1 port can upload 6 RGBA8UI texels per row (24 bytes, about 21 KB per species).

Follow bakeCutoutsAndVerify in src/core/cutouts.ts. Decode the full 896 * 60-byte buffer and validate the atlas certificate. Use the card if the data or certificate cannot be read. A row is valid when every contributing tile passes (skip zero-weight tiles only on horizon rows). For each row, use the row * 60 source offset, clear byte 3 if invalid, set byte 22 to 1 if valid and 0 otherwise, then copy the first 24 bytes to row * 24. Use nearest filtering and no mipmaps.

Single-row 24-byte RGBA8UI LUT layout for Tangram 6 and Far1 Six RGBA8UI texels per row: 16 bytes for Tangram 6 and 8 bytes for Far1.

TexelChannelsRangeMeaning
0(y0, y1, y2, y3)[-0.25, 1.25]Split heights; y3 == 0 marks invalid row
1-3(l0, r0, l1, r1)[-0.25, 1.25]Bottom and top X bounds for bands 0-2
4(ax, ay, bx, by)[-1.25, 2.25]Far1 triangle vertices A and B
5(cx, cy, valid, 0)[-1.25, 2.25]Far1 vertex C; z == 0 marks invalid row

4. Vertex decode, seam pad, and MSAA guard:

The excerpt shows the band decode and 1/1024 seam overlap. For the full vertex and fragment shaders, start at createImpostorMaterial in src/engine/shaders.ts. Use selectCutoutRow in src/core/octahedral.ts for the CPU row lookup and the vertex shader’s main for its GPU counterpart. Route out-of-range poses to fallbackUV before fetching the LUT. That function opens one full-card quad and collapses the other bands.

// Tangram 6: 3 quads (12 verts), aPatchCorner=(band,c)
vec4 lut(int col, int row) {
  return vec4(texelFetch(uCutoutLut, ivec2(col, row), 0))
    * (1.0 / 255.0);
}
vec2 decodeTangram(int row, float guard, out bool ok) {
  int band = int(aPatchCorner.x);
  int c = int(aPatchCorner.y);
  vec4 y = lut(0, row);
  ok = y.w > 0.0;
  if (!ok) return fallbackUV();
  y = y * 1.5 - 0.25;
  vec4 e = lut(band + 1, row) * 1.5 - 0.25;
  float h = y[band + 1] - y[band];
  if (h <= 0.0 || (e.x == e.y && e.z == e.w))
    return vec2(0.0); // hardware-collapsed quad
  bool top = c >= 2;
  vec2 edge = top ? e.zw : e.xy;
  vec2 opp  = top ? e.xy : e.zw;
  float pad = max(1.0 / 1024.0, guard);
  vec2 slope = (edge - opp) / h;
  edge += slope * pad;
  edge += vec2(-1.0, 1.0) * guard
    * sqrt(1.0 + slope * slope);
  float mid = 0.5 * (edge.x + edge.y);
  float x = (c == 1 || c == 2)
    ? max(edge.y, mid) : min(edge.x, mid);
  return vec2(x, y[band + int(top)]
    + (top ? pad : -pad));
}

With MSAA off, use guard = 0.0. With 4x MSAA and alpha-to-coverage, createImpostorMaterial sets uSampleGuard = 0.405 / min(w * Pxx, h * Pyy) from the viewport and projection. The vertex shader scales it by farthest clip W, inverse world-space card half-extent, and plane tilt. Draw Tangram 6 at 18 px of screen height and above, and Far1 below it. The 6R and 2.35R thresholds select LUT rows by distance.

Where it wins and where it doesn’t:

Averaged across all 392 far-tier upper-hemisphere LUT rows (before runtime MSAA padding), Tangram 6 trims more empty card area than Star 16 on all three test species, with 0 px clipped across 4,072 tested cases (1,018 poses x 4 sampling modes) per species:

SpeciesOpaque tree fillTangram 6 areaStar 16 area
Broadleaf28.5%54.0%60.3%
Conifer20.0%43.1%51.5%
Windswept13.8%34.7%42.4%

The Conifer at 85° is one example where Star 16 covers less area. Looking almost straight down, the silhouette turns round and the radial rays fit it more closely (39.6% for Star 16, 44.7% for Tangram 6).

Octahedral atlas tiles and Conifer elevation area curve showing the 85 degree crossover Conifer shaded area across 0 to 85 degrees, including the 85 degree crossover where Star 16 has smaller area.

Tangram 6+1 drew the forest faster in these WebGL2 runs on an AMD Radeon integrated GPU with 4x MSAA. The baseline was unstretched Star 16 with no Far1. The GPU timer covered the whole forest, including the near and far draws.

50,000-tree forest showing Tangram 6 mid-range and Far1 distant LOD tinting 50,000-tree forest split at 18 px screen height between Tangram 6 and Far1.

Median GPU time to render the forest, in milliseconds.

50k-tree sceneCard 2Star 16Tangram 6+1vs Star 16
Broadleaf, camera 8°103.11120.2398.13-18%
Conifer, camera 85°25.9225.6920.75-19%
Windswept, camera 85°22.2823.1017.04-26%

The forest benchmark also reports Star 16+1, which uses the same Far1 split as Tangram 6+1. Run it to see both comparisons on your GPU.

Credits:

  • Star Polygon Rays cutout idea: Lincoln Margison
  • Horizontal strip cutout method: Kvyverse (“Octahedral Tree Impostors”)
  • Octahedral impostors: Ryan Brucks (2018)