Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Give every tree level the sky visibility measured on level 0 (part of #394)

Nguyen Xuan An1304de34d580
diff --git a/changes/394.md b/changes/394.md
new file mode 100644
index 00000000..e20a68bd
--- /dev/null
+++ b/changes/394.md
@@ -0,0 +1 @@
+- Lower tree levels no longer draw near black under sky light: levels 1 to 3 read the sky visibility field measured on level 0 and keep its mean, so every level sees about as much sky as level 0 (it was 0.04 to 0.06 against 0.08 to 0.12). Part of #394.
diff --git a/changes/394.size.md b/changes/394.size.md
new file mode 100644
index 00000000..9f462de6
--- /dev/null
+++ b/changes/394.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: the lazy vegetation code now shares one sky field between tree levels and matches their mean (94 B), which keeps the lower levels from drawing near black.
diff --git a/src/tree-sky.js b/src/tree-sky.js
index 65232735..871c4b87 100644
--- a/src/tree-sky.js
+++ b/src/tree-sky.js
@@ -36,20 +36,22 @@ function boundsOfPositions(positions) {
}
/**
- * Returns one sky visibility in 0 to 1 for every vertex of a foliage geometry. `vertexWeight` scales how much leaf each
- * vertex stands for: a level that keeps fewer leaves passes a larger value for each one.
+ * Measures the leaf density of one foliage geometry and returns `{ visibilityOf(positions) }`, which gives one sky visibility
+ * in 0 to 1 for every vertex of any geometry of the same crown. A thinner level of detail asks the field of the fullest
+ * level, so every level reads the same value at the same place: working it out again from the few vertices of a thin level
+ * put them in cells that looked solid and left the far crown near black.
*/
-export function computeCrownSkyVisibility(positions, vertexWeight = 1) {
- const vertexCount = positions.length / 3;
+export function createCrownSkyField(positions) {
const { minimum, maximum } = boundsOfPositions(positions);
const longest = Math.max(maximum[0] - minimum[0], maximum[1] - minimum[1], maximum[2] - minimum[2], 1e-6);
const cell = longest / GRID_CELLS_ALONG_LONGEST_SIDE;
const size = [0, 1, 2].map(axis => Math.floor((maximum[axis] - minimum[axis]) / cell) + 1);
const density = new Float32Array(size[0] * size[1] * size[2]);
const cellOf = (value, axis) => Math.floor((value - minimum[axis]) / cell);
+ const clampedCellOf = (value, axis) => Math.min(size[axis] - 1, Math.max(0, cellOf(value, axis)));
for (let offset = 0; offset < positions.length; offset += 3) {
const index = (cellOf(positions[offset + 2], 2) * size[1] + cellOf(positions[offset + 1], 1)) * size[0] + cellOf(positions[offset], 0);
- density[index] += OPACITY_PER_VERTEX * vertexWeight;
+ density[index] += OPACITY_PER_VERTEX;
}
const step = cell / STEPS_PER_CELL;
// Visibility is worked out once for each occupied cell, from its centre, and shared by the vertices inside it.
@@ -72,14 +74,34 @@ export function computeCrownSkyVisibility(positions, vertexWeight = 1) {
return Math.max(MINIMUM_VISIBILITY, open / UP_DIRECTIONS.length);
}
- const visibility = new Float32Array(vertexCount);
- for (let vertex = 0; vertex < vertexCount; vertex++) {
- const column = cellOf(positions[vertex * 3], 0);
- const row = cellOf(positions[vertex * 3 + 1], 1);
- const layer = cellOf(positions[vertex * 3 + 2], 2);
- const index = (layer * size[1] + row) * size[0] + column;
- if (Number.isNaN(cellVisibility[index])) cellVisibility[index] = visibilityOfCell(column, row, layer);
- visibility[vertex] = cellVisibility[index];
+ function visibilityOf(targetPositions) {
+ const vertexCount = targetPositions.length / 3;
+ const visibility = new Float32Array(vertexCount);
+ for (let vertex = 0; vertex < vertexCount; vertex++) {
+ const column = clampedCellOf(targetPositions[vertex * 3], 0);
+ const row = clampedCellOf(targetPositions[vertex * 3 + 1], 1);
+ const layer = clampedCellOf(targetPositions[vertex * 3 + 2], 2);
+ const index = (layer * size[1] + row) * size[0] + column;
+ if (Number.isNaN(cellVisibility[index])) cellVisibility[index] = visibilityOfCell(column, row, layer);
+ visibility[vertex] = cellVisibility[index];
+ }
+ return visibility;
}
- return visibility;
+
+ return { visibilityOf };
+}
+
+function meanOf(values) {
+ let sum = 0;
+ for (const value of values) sum += value;
+ return sum / values.length;
+}
+
+/**
+ * Scales `visibility` so its mean is `targetMean`, keeping the floor and 1 as the limits. The larger leaves of a thinner level
+ * sit further out in the crown than the leaves of level 0, so a field lookup alone leaves them seeing more sky on average.
+ */
+export function matchMeanVisibility(visibility, targetMean) {
+ const scale = targetMean / meanOf(visibility);
+ return visibility.map(value => Math.min(1, Math.max(MINIMUM_VISIBILITY, value * scale)));
}
diff --git a/src/vegetation.js b/src/vegetation.js
index 93124bdd..17db3b53 100644
--- a/src/vegetation.js
+++ b/src/vegetation.js
@@ -14,7 +14,7 @@ import {
createMeshBuilder, createRandom, crossProduct, lerp, lerpVectors, normalized, randomInRange, scaleVector,
subtractVectors, valueNoise, vectorLength,
} from './vegetation-geometry.js';
-import { computeCrownSkyVisibility } from './tree-sky.js';
+import { createCrownSkyField, matchMeanVisibility } from './tree-sky.js';
import { LEVEL_SETTINGS, SPECIES, TREE_LEVEL_DISTANCES } from './vegetation-species.js';
import { barkTextureDescriptor, leafTextureDescriptor } from './vegetation-textures.js';
import { pixelErrorScale, selectBlendedLevels } from './vegetation-impostors.js';
@@ -414,22 +414,31 @@ function createMaterials(preset, mode, scannedBark, scannedLeaves) {
};
}
-// Every level describes the same crown, so each vertex of a thinner level stands for more leaf than one of level 0.
+// Every level describes the same crown, so every level reads one sky visibility field, measured on the leaves of level 0.
// `skyVisibility` is worked out the first time something reads it (the renderer, when it uploads the geometry), so a
// tree that is only measured or exported costs nothing extra, and a forest of distinct trees pays for the ones drawn.
+function meanOf(values) {
+ let sum = 0;
+ for (const value of values) sum += value;
+ return sum / values.length;
+}
+
function bakeCrownSkyVisibility(levels) {
const foliageOf = level => level.parts.find(part => part.role === 'foliage').geometry;
- const fullVertexCount = foliageOf(levels[0]).positions.length / 3;
- for (const level of levels) {
+ let field = null;
+ for (const [levelIndex, level] of levels.entries()) {
const geometry = foliageOf(level);
- const vertexCount = geometry.positions.length / 3;
- if (vertexCount === 0) continue;
+ if (geometry.positions.length === 0) continue;
let skyVisibility = null;
Object.defineProperty(geometry, 'skyVisibility', {
enumerable: true,
configurable: true,
get() {
- skyVisibility ??= computeCrownSkyVisibility(geometry.positions, fullVertexCount / vertexCount);
+ field ??= createCrownSkyField(foliageOf(levels[0]).positions);
+ if (!skyVisibility) {
+ skyVisibility = field.visibilityOf(geometry.positions);
+ if (levelIndex > 0) skyVisibility = matchMeanVisibility(skyVisibility, meanOf(foliageOf(levels[0]).skyVisibility));
+ }
return skyVisibility;
},
});
diff --git a/tests/tree-sky-levels.test.js b/tests/tree-sky-levels.test.js
new file mode 100644
index 00000000..024dd5ba
--- /dev/null
+++ b/tests/tree-sky-levels.test.js
@@ -0,0 +1,37 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { createTree } from '../src/vegetation.js';
+
+// Issue #394: a lower level of detail worked out its own sky visibility from its few, large vertices, which put the
+// far levels at the floor (0.04 to 0.06) where level 0 sees 0.08 to 0.12. Under sky light they drew near black.
+
+function meanOf(values) {
+ let sum = 0;
+ for (const value of values) sum += value;
+ return sum / values.length;
+}
+
+function foliageGeometry(level) {
+ return level.parts.find(part => part.role === 'foliage').geometry;
+}
+
+for (const species of ['broadleaf', 'birch', 'conifer', 'palm', 'bush']) {
+ test(`every level of a ${species} sees about as much sky as level 0`, () => {
+ const tree = createTree(species, 3);
+ const levelZeroMean = meanOf(foliageGeometry(tree.levels[0]).skyVisibility);
+ // The floor of the visibility keeps a very dense crown from matching exactly, so a small absolute margin applies too.
+ const allowedDifference = Math.max(0.1 * levelZeroMean, 0.02);
+ tree.levels.forEach((level, index) => {
+ const geometry = foliageGeometry(level);
+ assert.equal(geometry.skyVisibility.length, geometry.positions.length / 3);
+ const difference = Math.abs(meanOf(geometry.skyVisibility) - levelZeroMean);
+ assert.ok(difference <= allowedDifference, `${species} level ${index} differs from level 0 by ${difference.toFixed(3)}`);
+ });
+ });
+}
+
+test('level 0 keeps the sky visibility it measures from its own leaves', () => {
+ const first = createTree('broadleaf', 5);
+ const second = createTree('broadleaf', 5);
+ assert.deepEqual(foliageGeometry(first.levels[0]).skyVisibility, foliageGeometry(second.levels[0]).skyVisibility);
+});