3D engine that AI agents drive
Roll each clump's standing card so far tree levels look the same from every side (part of #432)
Nguyen Xuan Anbdcbff2642dc
diff --git a/changes/432-clump-roll.md b/changes/432-clump-roll.mdnew file mode 100644index 00000000..3daaff2f--- /dev/null+++ b/changes/432-clump-roll.md@@ -0,0 +1 @@+- Far tree levels look as bright from every side. Every standing clump card of a crown used to face one world direction, so level 2 of a conifer drew at 0.80 of level 0 from one side and 0.27 from the other two. Each clump now rolls its standing card by the golden angle from the last (issue #432).diff --git a/src/vegetation.js b/src/vegetation.jsindex 9e278951..4d00b924 100644--- a/src/vegetation.js+++ b/src/vegetation.js@@ -27,6 +27,7 @@ export { SPECIES };const BARK_TILE_SIZE = 0.6;const SCANNED_BARK_TILE_SIZE = 0.8;const LEAF_MODES = ['cards', 'mesh'];+const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));const GRASS_LEVEL_DISTANCES = [24];const LEAF_COLOR_BASE = [0.85, 0.95, 0.8];const LEAF_COLOR_TIP = [1.1, 1.05, 0.9];@@ -324,8 +325,14 @@ function addCrownClumps(builder, skeleton, spec, mode, random, widthScale = 1) {if (!cells.has(key)) cells.set(key, []);cells.get(key).push(site);}+ // Two cards of a clump lie about flat and the third stands along the vertical. Each clump rolls its standing card by the+ // golden angle from the last, so the standing cards of a crown face every way evenly and the crown looks as bright from every+ // side (issue #432). Unrolled, they all faced one world direction, and the far levels were three times darker from one side.+ let clumpIndex = 0;for (const sites of cells.values()) {const center = scaleVector(sites.reduce((sum, site) => addVectors(sum, site.position), [0, 0, 0]), 1 / sites.length);+ const yaw = clumpIndex * GOLDEN_ANGLE;+ clumpIndex++;const shading = crownShading(skeleton, center);const radius = cellSize * (0.55 + 0.1 * Math.min(sites.length, 5)) * widthScale * (spec.clumpScale ?? 1);if (mode === 'mesh') {@@ -336,7 +343,7 @@ function addCrownClumps(builder, skeleton, spec, mode, random, widthScale = 1) {const direction = card === 2 ? WORLD_UP : [Math.cos(card * 1.9 + center[0]), 0.15, Math.sin(card * 1.9 + center[2])];addLeafSurface(builder, {origin: addScaled(center, normalized(direction), -radius),- frame: createLeafFrame(normalized(direction), 0),+ frame: createLeafFrame(normalized(direction), card === 2 ? yaw : 0),length: radius * 2,width: radius * 2,rows: [[0, 1], [1, 1]],diff --git a/tests/tree-sky-levels.test.js b/tests/tree-sky-levels.test.jsindex 024dd5ba..bebb2904 100644--- a/tests/tree-sky-levels.test.js+++ b/tests/tree-sky-levels.test.js@@ -35,3 +35,31 @@ test('level 0 keeps the sky visibility it measures from its own leaves', () => {const second = createTree('broadleaf', 5);assert.deepEqual(foliageGeometry(first.levels[0]).skyVisibility, foliageGeometry(second.levels[0]).skyVisibility);});++test('the standing clump cards of a far level face every way, so the crown is as bright from every side', () => {+ // Before #432 every standing card of a crown faced one world direction, and level 2 drew three times darker from one side.+ // The facing comes from the triangles themselves, since the shading normals may be blended toward the crown.+ for (const species of ['broadleaf', 'birch', 'conifer', 'palm', 'bush']) {+ const { positions, indices } = foliageGeometry(createTree(species, 3).levels[2]);+ let sumX = 0;+ let sumZ = 0;+ let count = 0;+ for (let corner = 0; corner < indices.length; corner += 3) {+ const point = index => [positions[index * 3], positions[index * 3 + 1], positions[index * 3 + 2]];+ const [a, b, c] = [point(indices[corner]), point(indices[corner + 1]), point(indices[corner + 2])];+ const u = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];+ const v = [c[0] - a[0], c[1] - a[1], c[2] - a[2]];+ const normal = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];+ const length = Math.hypot(...normal);+ if (length === 0 || Math.abs(normal[1] / length) > 0.5) continue;+ const horizontal = Math.hypot(normal[0], normal[2]);+ sumX += normal[0] / horizontal;+ sumZ += normal[2] / horizontal;+ count++;+ }+ // The mean facing of the standing triangles: 1 when all face one way (main before this change), near 0 when they spread evenly.+ const resultant = Math.hypot(sumX, sumZ) / count;+ assert.ok(count > 0 && resultant < 0.3, `${species} standing cards lean one way (${resultant.toFixed(2)} of ${count})`);+ }+});+