Skip to content

3D engine that AI agents drive

Check tree height, grass radius and vegetation seeds at the boundary (#2408)

Nguyen Xuan An6cb361c0e892
diff --git a/changes/2408.md b/changes/2408.md
new file mode 100644
index 00000000..c0b493b5
--- /dev/null
+++ b/changes/2408.md
@@ -0,0 +1 @@
+- `createTree` refuses a height outside 0.01 to 1000 m, `createGrassClump` refuses a bad blade radius, and `scatterVegetation`, `createGrassClump` and `createFlowerPatch` refuse a seed that is not a finite number, instead of building NaN geometry, running out of memory or quietly using seed 0 (#2408, #2409, #2410)
diff --git a/changes/2408.size.md b/changes/2408.size.md
new file mode 100644
index 00000000..9832e674
--- /dev/null
+++ b/changes/2408.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: 99 bytes gzip for refusing a tree height of NaN, 0 or over 1000 m (an out-of-memory crash), a NaN grass radius and a non-finite seed that silently became seed 0
diff --git a/docs/vegetation.md b/docs/vegetation.md
index 72c1b653..e3122ab8 100644
--- a/docs/vegetation.md
+++ b/docs/vegetation.md
@@ -23,7 +23,7 @@ const renderer = new CrucibleRenderer(canvas, [terrain, ...view.parts], options)
## Trees
-`createTree(species | { species, height, leaves }, seed)` grows one tree. Species: `broadleaf`, `birch`, `conifer`, `palm`, `bush`.
+`createTree(species | { species, height, leaves }, seed)` grows one tree. Species: `broadleaf`, `birch`, `conifer`, `palm`, `bush`. `height` runs from 0.01 to 1000 m; a seed of any function here must be a finite number.
The same species and seed always give the same tree.
The skeleton is a parametric recursive branching model: each generation has a length, wander, tropism (up or droop), taper and child spread.
diff --git a/src/vegetation-options.js b/src/vegetation-options.js
index 01ce0d92..2f8956e3 100644
--- a/src/vegetation-options.js
+++ b/src/vegetation-options.js
@@ -1,7 +1,7 @@
// Option checks that stop a hang, an out-of-memory death or an opaque TypeError inside the vegetation builders.
function need(value, label, minimum, maximum, whole) {
- if (!(value >= minimum && value <= maximum) || (whole && !Number.isInteger(value))) {
+ if (typeof value !== 'number' || !(value >= minimum && value <= maximum) || (whole && !Number.isInteger(value))) {
throw new RangeError(`${label} must be ${whole ? 'a whole number ' : ''}from ${minimum} to ${maximum}; got ${value}.`);
}
}
@@ -25,3 +25,17 @@ export function checkClumpOptions(label, count, maximumCount, height) {
need(count, `${label} count`, 1, maximumCount, true);
need(height, `${label} height`, 0.001, 100);
}
+
+export function checkTreeHeight(height) {
+ need(height, 'Tree height', 0.01, 1000);
+}
+
+export function checkGrassRadius(radius) {
+ need(radius, 'Grass blade radius', 0, 100);
+}
+
+export function checkSeed(functionName, seed) {
+ if (typeof seed !== 'number' || !Number.isFinite(seed)) {
+ throw new TypeError(`${functionName} seed must be a finite number; got ${seed}.`);
+ }
+}
diff --git a/src/vegetation.js b/src/vegetation.js
index 9a32862b..160eb753 100644
--- a/src/vegetation.js
+++ b/src/vegetation.js
@@ -18,7 +18,7 @@ import { coverageMean, createCrownSkyField, matchMeanVisibility, scaleVisibility
import { TREE_LEVEL_GAINS } from './tree-level-gains.js';
import { LEVEL_SETTINGS, SPECIES, TREE_LEVEL_DISTANCES } from './vegetation-species.js';
import { barkTextureDescriptor, leafTextureDescriptor } from './vegetation-textures.js';
-import { checkClumpOptions, checkScatterOptions } from './vegetation-options.js';
+import { checkClumpOptions, checkGrassRadius, checkScatterOptions, checkSeed, checkTreeHeight } from './vegetation-options.js';
import { pixelErrorScale, selectBlendedLevels } from './vegetation-impostors.js';
export { prepareImpostors, getImpostor, bakeImpostorAtlas, blendLevels, pixelErrorScale } from './vegetation-impostors.js';
@@ -504,6 +504,7 @@ export function createTree(speciesOrOptions = 'broadleaf', seed = 1) {
if (!Number.isFinite(seed)) throw new TypeError('Tree seed must be a finite number.');
const random = createRandom(seed * 7919 + 17);
const height = options.height ?? randomInRange(random, preset.height);
+ checkTreeHeight(height);
const skeleton = growSkeleton(preset, height, random);
const scannedBark = resolveBarkTextures(options.barkTextures, options.species);
const bark = { scanned: Boolean(scannedBark), tileSize: options.barkTileSize ?? (scannedBark ? SCANNED_BARK_TILE_SIZE : BARK_TILE_SIZE) };
@@ -593,6 +594,8 @@ function buildGrassCards(random, height, radius) {
export function createGrassClump({ blades = 16, height = 0.45, radius = 0.13, style = 'blades', color = [0.23, 0.39, 0.07], seed = 1 } = {}) {
if (!['blades', 'cards'].includes(style)) throw new RangeError('Grass style must be blades or cards.');
checkClumpOptions('Grass blade', blades, 2048, height);
+ checkGrassRadius(radius);
+ checkSeed('createGrassClump', seed);
const random = createRandom(seed * 6271 + 5);
const material = grassMaterial(style, color);
const geometries = style === 'cards'
@@ -631,6 +634,7 @@ function buildFlowerPatch(random, flowers, height, petalColor) {
/** A patch of small flowers on thin stems; color lives in vertex colors, so one material serves every hue. */
export function createFlowerPatch({ flowers = 5, height = 0.4, color = [1, 0.85, 0.3], seed = 1 } = {}) {
checkClumpOptions('Flower', flowers, 256, height);
+ checkSeed('createFlowerPatch', seed);
const random = createRandom(seed * 3571 + 11);
const geometry = buildFlowerPatch(random, flowers, height, color);
const material = { color: [1, 1, 1], roughness: 0.8, metallic: 0, emission: 0 };
@@ -745,6 +749,7 @@ export function scatterVegetation({
throw new TypeError('Scatter area needs finite minX < maxX and minZ < maxZ.');
}
checkScatterOptions({ area, density, spacing, variants, scale, mix });
+ checkSeed('scatterVegetation', seed);
const random = createRandom(seed);
const speciesNames = Object.keys(mix).filter(name => mix[name] > 0);
const accept = ([x, z]) => slopeDegrees(terrainHeight, x, z) <= slopeLimit && !exclusion(x, z);
diff --git a/tests/vegetation-options.test.js b/tests/vegetation-options.test.js
index fa6c1e65..aab30026 100644
--- a/tests/vegetation-options.test.js
+++ b/tests/vegetation-options.test.js
@@ -61,3 +61,28 @@ test('scatter scale maximum must be finite and at least the minimum', () => {
test('scatter mix refuses an infinite weight and names the species', () => {
assert.throws(scatter({ mix: { grass: Infinity, bush: 1 } }), /Scatter mix weight for grass must be finite/);
});
+
+test('tree height must be from 0.01 to 1000 meters', async () => {
+ const { createTree } = await import('../src/vegetation.js');
+ for (const height of [NaN, 0, -3, Infinity, 1e6, 'tall']) {
+ assert.throws(() => createTree({ species: 'broadleaf', height }), /Tree height must be from 0.01 to 1000/, `height ${height}`);
+ }
+ assert.ok(createTree({ species: 'bush', height: 1000 }).bounds.maximum.every(Number.isFinite));
+ assert.ok(createTree('broadleaf', 3).bounds.maximum.every(Number.isFinite));
+});
+
+test('grass blade radius must be finite and from 0 to 100', () => {
+ for (const radius of [NaN, -1, Infinity, '0.1']) {
+ assert.throws(() => createGrassClump({ radius }), /Grass blade radius must be from 0 to 100/, `radius ${radius}`);
+ }
+ assert.ok(createGrassClump({ radius: 0 }).bounds.maximum.every(Number.isFinite));
+});
+
+test('scatter, grass and flower seeds must be finite numbers', () => {
+ for (const seed of [NaN, Infinity, 'x']) {
+ assert.throws(scatter({ seed }), /scatterVegetation seed must be a finite number/, `scatter ${seed}`);
+ assert.throws(() => createGrassClump({ seed }), /createGrassClump seed must be a finite number/, `grass ${seed}`);
+ assert.throws(() => createFlowerPatch({ seed }), /createFlowerPatch seed must be a finite number/, `flower ${seed}`);
+ }
+ assert.equal(scatter({ seed: -4.5 })().instances.length > 0, true);
+});