Skip to content

3D engine that AI agents drive

Refuse inherited texture names and bad impostor frame sizes (#2557)

Nguyen Xuan An34be2bf18cdb
diff --git a/changes/2557.md b/changes/2557.md
new file mode 100644
index 00000000..07ed39ac
--- /dev/null
+++ b/changes/2557.md
@@ -0,0 +1 @@
+- Leaf and bark texture names such as `constructor` now fail as unknown kinds, and `bakeImpostorAtlas` refuses a `frameSize` outside 8 to 512 instead of returning an empty or unbounded atlas (#2557, #2558).
diff --git a/docs/vegetation.md b/docs/vegetation.md
index 72c1b653..0caffa70 100644
--- a/docs/vegetation.md
+++ b/docs/vegetation.md
@@ -135,7 +135,7 @@ const view = selectLevels(forest.instances, camera.eye, { impostors: { from: 3 }
`pixelError: { pixels, height, fov }` sets the switch distances from an error in screen pixels instead of metres. The default distances (38, 95 and 150 m) hold for 4 pixels on a 1080 pixel high view with a 60 degree vertical field of view; `pixelErrorScale` returns the factor for any other view, and the factor grows with the instance's scale. It works with `fade`, with `impostors` and with the plain selection.
-`impostors` (`true` or `{ from, frameSize, level }`) draws every tree from level `from` on (default 3, beyond 150 m) as one card instead of its mesh. `from: 2` starts at 95 m. Bushes and palms take it too; grass and flowers do not. The result gains `impostors` (cards drawn) and `fading` (instances in a cross-fade).
+`impostors` (`true` or `{ from, frameSize, level }`; `frameSize` is a whole number from 8 to 512, default 96) draws every tree from level `from` on (default 3, beyond 150 m) as one card instead of its mesh. `from: 2` starts at 95 m. Bushes and palms take it too; grass and flowers do not. The result gains `impostors` (cards drawn) and `fading` (instances in a cross-fade).
An impostor is baked per tree variant on first use, or by `prepareImpostors(variants)`. `bakeImpostorAtlas` draws the tree from 8 by 8 directions of a hemi-octahedral grid (the upper hemisphere, so a view from below the horizon shows the horizon view) into a colour atlas (sRGB, alpha is the leaf mask) and a normal atlas (linear, the tangent space of that view, green up), each 96 px a frame, on the CPU in plain JavaScript: it runs in Node and the browser alike and the pixels repeat exactly. It bakes level 3 of the tree, the mesh the card replaces, so the card keeps the same silhouette (`level` picks another). Instance tint multiplies the colour like it does on the meshes.
diff --git a/src/vegetation-impostors.js b/src/vegetation-impostors.js
index 8f770df7..7f1ef029 100644
--- a/src/vegetation-impostors.js
+++ b/src/vegetation-impostors.js
@@ -7,12 +7,15 @@
// dither (`alphaMode: 'dither'`), chosen from the distance alone, so a seek to any camera repeats the same picture.
import { IMPOSTOR_FRAMES } from './impostor-shader.js';
+import { requireFiniteInRange } from './modeling-limits.js';
export const FADE_STEPS = 8;
export const IMPOSTOR_CARD_TRIANGLES = 2;
const DEFAULT_FRAME_SIZE = 96;
const DEFAULT_BAKE_LEVEL = 3;
const FRAME_MARGIN_PIXELS = 2;
+const MINIMUM_FRAME_SIZE = 8;
+const MAXIMUM_FRAME_SIZE = 512;
const EMPTY_BAND = 1e-6;
const impostorCache = new WeakMap();
@@ -236,6 +239,7 @@ function bleedColors(colorPixels, size, frameColumn, frameRow, atlasWidth) {
* Returns the pixels of the colour and normal atlas and the card: `radius` and `centerHeight` in the tree's meters.
*/
export function bakeImpostorAtlas(variant, { frameSize = DEFAULT_FRAME_SIZE, level = DEFAULT_BAKE_LEVEL } = {}) {
+ requireFiniteInRange('bakeImpostorAtlas', 'frameSize', frameSize, MINIMUM_FRAME_SIZE, MAXIMUM_FRAME_SIZE, true);
const minimum = variant.bounds.minimum;
const maximum = variant.bounds.maximum;
const center = [(minimum[0] + maximum[0]) / 2, (minimum[1] + maximum[1]) / 2, (minimum[2] + maximum[2]) / 2];
diff --git a/src/vegetation-textures.js b/src/vegetation-textures.js
index eb7223e9..85315f37 100644
--- a/src/vegetation-textures.js
+++ b/src/vegetation-textures.js
@@ -140,7 +140,7 @@ const LEAF_PAINTERS = {
/** RGBA leaf texture: the stem base sits at the bottom edge, transparent elsewhere. */
export function createLeafPixels(kind, color, seed = 1) {
- const painter = LEAF_PAINTERS[kind];
+ const painter = Object.hasOwn(LEAF_PAINTERS, kind) ? LEAF_PAINTERS[kind] : null;
if (!painter) throw new RangeError(`Unknown leaf texture kind: ${kind}`);
const pixels = createPixels(color);
painter(pixels, color, createRandom(seed));
@@ -155,7 +155,7 @@ const BARK_STYLES = {
/** Opaque, tileable bark luminance texture with vertical furrows (rough) or dark lenticels (birch). */
export function createBarkPixels(kind, seed = 1) {
- const style = BARK_STYLES[kind];
+ const style = Object.hasOwn(BARK_STYLES, kind) ? BARK_STYLES[kind] : null;
if (!style) throw new RangeError(`Unknown bark texture kind: ${kind}`);
const pixels = createPixels(style.base);
for (let y = 0; y < TEXTURE_SIZE; y++) {
diff --git a/tests/vegetation-name-lookups.test.js b/tests/vegetation-name-lookups.test.js
new file mode 100644
index 00000000..ac41e0c1
--- /dev/null
+++ b/tests/vegetation-name-lookups.test.js
@@ -0,0 +1,25 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { createBarkPixels, createLeafPixels } from '../src/vegetation-textures.js';
+import { bakeImpostorAtlas } from '../src/vegetation-impostors.js';
+import { createTree } from '../src/vegetation.js';
+
+test('leaf texture kinds that only exist on Object.prototype are refused by name', () => {
+ for (const kind of ['constructor', 'toString', '__proto__', 'hasOwnProperty']) {
+ assert.throws(() => createLeafPixels(kind, [64, 110, 36]), /Unknown leaf texture kind/, kind);
+ }
+});
+
+test('bark texture kinds that only exist on Object.prototype are refused by name', () => {
+ for (const kind of ['constructor', 'toString', '__proto__', 'valueOf']) {
+ assert.throws(() => createBarkPixels(kind), /Unknown bark texture kind/, kind);
+ }
+});
+
+test('an impostor bake refuses a frame size that gives an empty, infinite or huge atlas', () => {
+ const variant = createTree('bush', 1);
+ for (const frameSize of [0, -8, NaN, Infinity, 4, 1000000, 32.5, '32']) {
+ assert.throws(() => bakeImpostorAtlas(variant, { frameSize }), /bakeImpostorAtlas: frameSize/, String(frameSize));
+ }
+ assert.equal(bakeImpostorAtlas(variant, { frameSize: 16 }).width, 128);
+});