Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(foliage): grass on a hill is not thinned under your feet - a cell stood at the Foliage entity's height while its plants stood on the ground, and how far a cell is was measured from the cell, so on ground 16 m up the grass at the eye was thinned as grass 19 m off and on a mountain all of it was; a cell stands on the ground under its centre

yhh6d07b7229c8a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 80f5038b8..b0404027e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,7 @@ published separately; it ships inside the editor.
### Fixed
+- **Grass on a hill is not thinned under your feet.** Plants thin with distance, and the distance was measured from their cell, which stood at the `Foliage` entity's own height while the plants stood on the ground. On a meadow 16 m up, with the eye 3 m over it, the grass underfoot was thinned as grass 19 m away: a third of the blades, each grown several times as wide, bare ground between them; on a mountain every blade was at its thinnest at any distance. A cell now stands on the ground under its centre and its plants about that height, so the distance is the one to where they are.
- **A view of many materials no longer loses the WebGPU device.** The WebGPU backend kept 256 bind groups and emptied the whole cache when it filled, on the assumption that a steady scene never fills it. A view that binds more (three imported towns at once: hundreds of materials, each in several passes) made every group again every frame, and on Windows the device was lost with `CreateDescriptorHeap failed with E_OUTOFMEMORY` after 700 frames with three towns in view and 1190 with two; one town was under the limit and ran. The cache holds 4096, is found by hash instead of a walk of every entry, and when full lets go only of the groups neither this frame nor the last bound.
- **A waterfall's foot is torn to veils, and light goes through it.** A `Waterfall`'s sheets are blended one over another, so three of them added up to a white board in the lower half of the fall. The stock shader now tears more of the foot away (`u_veil` 0.75, by the foot itself, `u_tearBy` 1): beside the fall it was made from, the foot is 10 levels of 255 over and the middle 14, where the foot was 55 over. And a sheet was lit as an opaque wall facing one way; `u_translucency` (1 by default) lets the light through, as the Model shader's does. `u_veil` 0.47, `u_tearBy` 0.85 and `u_translucency` 0 are the sheet as it was.
- **A waterfall falls in strands, not in speckle.** The stock Waterfall shader's pattern was sixteen times too fine each way: sixty cells across half a sheet and fifteen to a second of the fall, so a sheet read as a solid white board peppered with dark flecks. About four strands now lie across half a sheet (`u_strands` 3.75) and one lasts about a second of the fall (`u_run` 0.97, now the count itself and no longer a share of 28), and a little more of the foot is torn away (`u_veil` 0.47). `u_strands` 60, `u_run` 15.4 and `u_veil` 0.42 are the old sheet.
diff --git a/sdk/src/foliage/foliagePlugin.ts b/sdk/src/foliage/foliagePlugin.ts
index b1cf94a29..64b4d3140 100644
--- a/sdk/src/foliage/foliagePlugin.ts
+++ b/sdk/src/foliage/foliagePlugin.ts
@@ -520,6 +520,10 @@ export class FoliagePlugin implements Plugin {
const sx = t.worldScale.x || 1, sz = t.worldScale.z || 1;
const worldX = t.worldPosition.x + localX * sx, worldZ = t.worldPosition.z + localZ * sz;
const hasTerrain = !!terrains && terrains.built_.size > 0;
+ // The cell stands on the ground under its centre, not at the entity's height: how far a cell is (what thins its plants) is
+ // measured from where they are. On a hill a cell at the entity's height was a hill's height away with the eye in the grass.
+ const groundY = (hasTerrain ? terrains!.heightAt(worldX, worldZ) : null) ?? t.worldPosition.y;
+ const lift = (groundY - t.worldPosition.y) / (t.worldScale.y || 1);
// Plants thin, each by its own number, to what the next far version keeps before it takes over, and to none at the draw distance.
if (built.instance) {
const thins = !built.source || !data.farCards;
@@ -537,7 +541,7 @@ export class FoliagePlugin implements Plugin {
heightMin: data.heightMin, heightMax: data.heightMax,
waterDepth: data.waterDepth, zonePadding: data.zonePadding ?? 0,
tags: ['foliage', data.mesh ? 'tree' : 'grass', ...(data.zoneTags ?? '').split(',').map((s) => s.trim()).filter(Boolean)],
- baseY: t.worldPosition.y,
+ baseY: groundY,
paint: data.material ? null : { base: [0.14, 0.3, 0.08], tip: [0.55, 0.72, 0.25] },
far: { levels: data.farLevels, keep: data.farKeep, grow: data.farGrow, cards: data.farCards, only, up: data.farGrowUp },
look: built.source
@@ -563,15 +567,15 @@ export class FoliagePlugin implements Plugin {
// Put in the world on a later frame, within its budget; not at all if the field has moved on while it was being made.
field.landed_.push(() => {
if (signal?.aborted || built.generation !== generation || !app.world.valid(parent)) { resolve(null); return; }
- try { resolve(this.land(app, built, cx, cz, localX, localZ, meshes ?? here(), only !== null)); } catch (e) { reject(e); }
+ try { resolve(this.land(app, built, cx, cz, localX, lift, localZ, meshes ?? here(), only !== null)); } catch (e) { reject(e); }
});
}));
}
- return this.land(app, built, cx, cz, localX, localZ, here(), only !== null);
+ return this.land(app, built, cx, cz, localX, lift, localZ, here(), only !== null);
}
/** Puts a cell's geometry in the world: its meshes on the GPU and the entity that draws them (its parent is set on publishing). */
- private land(app: App, built: BuiltFoliage, cx: number, cz: number, localX: number, localZ: number, meshes: MeshData[], banded = false): GrownCell {
+ private land(app: App, built: BuiltFoliage, cx: number, cz: number, localX: number, lift: number, localZ: number, meshes: MeshData[], banded = false): GrownCell {
if (meshes.length === 0) return { entity: null, handles: [] };
const world = app.world, data = built.data;
const assets = app.getResource(Assets);
@@ -587,7 +591,7 @@ export class FoliagePlugin implements Plugin {
}
const cell = world.spawn(`FoliageCell ${cx},${cz}`);
- world.insert(cell, Transform, { position: { x: localX, y: 0, z: localZ } });
+ world.insert(cell, Transform, { position: { x: localX, y: lift, z: localZ } });
world.insert(cell, MeshRenderer, {
mesh: handles[0]!, material: built.instance || data.material, lit: true, opaque: true, cullBackfaces: false,
castShadows: data.castShadows !== false,
diff --git a/sdk/tests/foliage-zones.test.ts b/sdk/tests/foliage-zones.test.ts
index 02933fa57..586430765 100644
--- a/sdk/tests/foliage-zones.test.ts
+++ b/sdk/tests/foliage-zones.test.ts
@@ -11,6 +11,8 @@ import { Foliage, FoliagePlugin } from '../src/foliage/foliagePlugin';
import { WorldGens, WorldGenRes } from '../src/worldgen/worldgenPlugin';
import { WorldStreamingSource } from '../src/residency/components';
import { createMockModule } from './mocks/wasm';
+import { TerrainsRes } from '../src/terrain/terrainPlugin';
+import type { MeshData } from '../src/asset/meshFormat';
const tick = async (app: App) => { for (let i = 0; i < 16; i++) { app.tick(1 / 60); await new Promise((r) => setTimeout(r, 0)); } };
const boot = () => {
@@ -57,3 +59,23 @@ describe('grass reservations and shadows', () => {
zones.set({ ...building, excludes: ['*'] }); await tick(app); expect(cells(app)).toHaveLength(0);
});
});
+
+describe('a cell of a Foliage area over a ground', () => {
+ it('stands on the ground under its centre, with its plants about its own height: how far it is is measured from where they are', async () => {
+ const { app } = boot();
+ const made: MeshData[] = [];
+ app.insertResource(Assets, { createMesh: (m: MeshData) => { made.push(m); return made.length; }, releaseBuiltMesh: () => {} } as any);
+ // A hillside 200 up that rises a little to the east.
+ const heightAt = (x: number, _z: number): number => 200 + x * 0.1;
+ app.insertResource(TerrainsRes, { built_: new Map([[1, 1]]), heightAt, normalAt: () => ({ x: 0, y: 1, z: 0 }), loading: false, revision_: 1 } as any);
+ await tick(app);
+ const grown = cells(app);
+ expect(grown.length).toBeGreaterThan(0);
+ for (const e of grown) {
+ const at = app.world.get(e, Transform).position;
+ expect(at.y).toBeCloseTo(heightAt(at.x, at.z), 3);
+ }
+ // No plant stands a hill's height over its cell: with the cell at the entity's height the lowest was 198 up.
+ for (const mesh of made) expect(Math.abs(mesh.aabbMin[1])).toBeLessThan(3);
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index d1dcd680b..13e584771 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4594,6 +4594,8 @@ export const NOTED = {
{"note": "A view of many materials no longer loses the WebGPU device."},
"feat(terrain): a layer with a grain is read without repeat and without hexagons - near the eye every hex cell took its layer turned any way round, so sand in ripples or rock in beds was lit cell by cell and showed the cells; u_antiTileTurn is how far a cell may be turned, and 0 shifts it only":
{"note": "A layer with a grain is read without repeat and without hexagons."},
+ "fix(foliage): grass on a hill is not thinned under your feet - a cell stood at the Foliage entity's height while its plants stood on the ground, and how far a cell is was measured from the cell, so on ground 16 m up the grass at the eye was thinned as grass 19 m off and on a mountain all of it was; a cell stands on the ground under its centre":
+ {"note": "Grass on a hill is not thinned under your feet."},
"fix(terrain): a detail the pages cannot hold no longer cracks the ground - a tree that wanted more nodes than the atlas has pages was drawn in part by coarser pages standing in, which do not meet their finer neighbours' edges, and the pages were fought over every frame; the ground is now drawn at the share of its detail's range that fits, and says so once":
{"note": "A detail the pages cannot hold no longer cracks the ground."},
"fix(foliage): a far plant grows across the ground, not taller - a mesh plant kept in a far version was grown the same every way to cover the ground of those left out, so a flower that stood for ten stood ten times as high and a far meadow was a field of poles; it now grows across the ground only, and Foliage.farGrowUp gives its height a share of the growth back":