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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(foliage): a mesh scattered with no material keeps its own colours - a field with no material painted every plant the stock green of grass from root to tip, a mesh's own vertex colours with it, so a grey rock came out a green one and a dark spruce a pale one; a mesh that has colours is painted in them, each plant by its shade, and one with none (or white throughout) is painted as before
yhh484af2df2ab3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 77ae102ee..ecd24b3b5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -33,6 +33,7 @@ published separately; it ships inside the editor.### Fixed+- **A mesh scattered with no material keeps its own colours.** A `Foliage` field with no material is painted, since nothing reads the wind's weights then, and it painted every plant the stock green of grass from root to tip, whatever the mesh's own vertex colours said: a grey rock scattered along a shore came out a smooth green one, and a dark spruce a pale one. A mesh that has colours of its own is now painted in them, each plant a little lighter or darker by its shade; a mesh with no colour channel, or white throughout, is painted as before.- **A playable samples its textures as their import settings say.** A single-file playable was handed no manifest, which is the only way a texture's `.meta` reaches a package, so its wrap mode, its filter, whether it is colour and its 9-slice border were all lost and every texture took the defaults. A ground or a wall whose picture repeats then drew one copy of it and smeared its last row of pixels over everything past the first tile: cliffs came out one flat colour with level stripes along the waterline, while the same scene was right in the editor and in a web export. The settings of the textures a playable inlines now ride in the page and reach the runtime the way they reach every other package.- **Drawing a frame costs the CPU a third of what it did.** Issuing a frame's draws went through four costs the engine did not need. Every draw staged a light-probe block of its own, up to 16 KB of zeroes filled, copied and uploaded, though no probe lit it; a run no probe lights now reads the zero block. Every draw bound every texture unit its slots named, where the program it drew with read none of some (a shadow caster, most of all); a draw now binds only the units its program samples, read from the program once. The face to cull was set at every change of pipeline though it nearly never changed. And every vertex array bound asked the browser which index buffer it held, through a WebGL emulation (`FULL_ES3`) nothing in the engine used. Pipelines and other device objects are also found by index rather than searched for. On the Bistro scene at 640x360, where the CPU and not the GPU sets the pace, the renderer's CPU time a frame went from 11 to 14 ms down to 3.3 ms, the frame from 13.4 to 6.7 ms, and the WebGL calls a frame from 17,926 to 7,139; 52 pixel gates on WebGL2 and 23 on WebGPU draw what they did. `node tools/render-submit-cost.mjs` measures it: a synthetic grid in three spreads of material, or an example such as `--scene bistro`, with `--glcalls` and `--profile`.- **A cloud's shadow dims the sun, not the sky.** Sky Atmosphere's **Cloud Shadows** and a custom sky's `groundShadow` multiplied every surface in the frame by the shadow, after it was lit: ambient light, reflections, other lights and emission were dimmed with the sun, and an unlit or emissive object went dark under a cloud. Measured against a reference island lit the same way, its ground came out 25 to 35 levels darker than the reference, and lit by the sky alone it was black. Now the sky draws a cloud shadow map before the scene, into one tile of the sun's shadow atlas (16 km round the camera, `u_cloudShadowExtentKm`), and the first directional light is dimmed by it where each surface is shaded, as Unreal's atmosphere sun reads its clouds' shadow map; nothing else is. The cloud shadow criteria now light their floor by a sun and add a floor nothing lights, which must keep its colour under the clouds; they and 56 shadow and sun criteria pass on WebGL2 and WebGPU. With the atlas full (High quality's four cascades fill it) the sun gives up its farthest cascade to the map. The map holds the light left at the camera's own height, exact below the cloud layer and an estimate inside it.diff --git a/sdk/src/foliage/builder.ts b/sdk/src/foliage/builder.tsindex 5202f82f1..b2c8325b2 100644--- a/sdk/src/foliage/builder.ts+++ b/sdk/src/foliage/builder.ts@@ -231,6 +231,25 @@ function reader(mesh: MeshData, semantic: number, components: number): ((v: numb};}+/** Reads a source mesh's own vertex colour, whichever way it is stored, or null where it has none worth keeping (no channel, or white throughout). */+function ownColours(mesh: MeshData): ((v: number, out: number[]) => void) | null {+ const channel = mesh.channels.find((c) => c.semantic === MeshChannel.Color);+ if (!channel || channel.components < 3) return null;+ if (channel.type !== MeshChannelType.Float32 && channel.type !== MeshChannelType.UNorm8) return null;+ const view = new DataView(mesh.vertices.buffer, mesh.vertices.byteOffset, mesh.vertices.byteLength);+ const float = channel.type === MeshChannelType.Float32;+ const read = (v: number, out: number[]): void => {+ const at = v * mesh.vertexStride + channel.offset;+ for (let k = 0; k < 3; k++) out[k] = float ? view.getFloat32(at + k * 4, true) : view.getUint8(at + k) / 255;+ };+ const c = [1, 1, 1];+ for (let v = 0; v < mesh.vertexCount; v++) {+ read(v, c);+ if (c[0]! < 0.995 || c[1]! < 0.995 || c[2]! < 0.995) return read;+ }+ return null;+}+/*** Copies of a mesh, one per plant, as one mesh: placed, turned about the vertical and scaled, with the wind's weights painted by how* high each vertex stands on its own copy (a tree's trunk hardly moves, its crown does). With `seats` the mesh has a second uv: for a@@ -246,8 +265,11 @@ export function buildInstancedMesh(source: MeshData, plants: readonly Plant[], pconst readPosition = reader(source, MeshChannel.Position, 3);const readNormal = reader(source, MeshChannel.Normal, 3);const readUv = reader(source, MeshChannel.TexCoord0, 2);+ // A field with no material is painted, and a mesh that has colours of its own is painted in those: a grey rock or a dark spruce+ // came out the stock green of grass from root to tip, whatever its model said. One with no colours, or all white, has none to keep.+ const readOwn = paint ? ownColours(source) : null;const minY = source.aabbMin[1], span = Math.max(1e-6, source.aabbMax[1] - source.aabbMin[1]);- const p3 = [0, 0, 0], n3 = [0, 1, 0], uv2 = [0, 0];+ const p3 = [0, 0, 0], n3 = [0, 1, 0], uv2 = [0, 0], own = [1, 1, 1];for (const plant of plants) {const base = b.pos.length / 3;const cos = Math.cos(plant.yaw), sin = Math.sin(plant.yaw);@@ -271,7 +293,10 @@ export function buildInstancedMesh(source: MeshData, plants: readonly Plant[], py = top + dy * c - z * sn; z = dy * sn + z * c;n3[1] = ny * c - nz * sn; n3[2] = ny * sn + nz * c;}- const color = paint ? paintColor(paint, t, plant.shade) : [t, plant.phase, plant.shade * (0.6 + 0.4 * t)] as const;+ let color: readonly [number, number, number];+ if (!paint) color = [t, plant.phase, plant.shade * (0.6 + 0.4 * t)];+ else if (readOwn) { readOwn(v, own); color = [own[0]! * plant.shade, own[1]! * plant.shade, own[2]! * plant.shade]; }+ else color = paintColor(paint, t, plant.shade);q[0] = x * cos + z * sin; q[1] = y; q[2] = -x * sin + z * cos;m[0] = n3[0]! * cos + n3[2]! * sin; m[1] = n3[1]!; m[2] = -n3[0]! * sin + n3[2]! * cos;if (lean) { lean(q); lean(m); }diff --git a/sdk/tests/foliage-builder.test.ts b/sdk/tests/foliage-builder.test.tsindex 24606c1b2..4b285f347 100644--- a/sdk/tests/foliage-builder.test.ts+++ b/sdk/tests/foliage-builder.test.ts@@ -107,6 +107,32 @@ describe('instanced meshes and billboards', () => {expect(colorOf(mesh, 2)[0]).toBe(255);});+ // A field with no material is painted. A mesh with colours of its own was painted the stock green of grass all the same, from+ // root to tip: a grey rock scattered by a field came out a green one, and a dark spruce a pale one.+ it('paints a mesh with no material in the mesh\'s own colours, each plant by its shade', () => {+ const grey = buildGrassMesh([{ x: 0, y: 0, z: 0, yaw: 0, scale: 1, phase: 0, shade: 1 }],+ { height: 10, width: 2, segments: 1, thin: 1, paint: { base: [0.4, 0.4, 0.4], tip: [0.4, 0.4, 0.4] } });+ const stock = { base: [0.14, 0.3, 0.08], tip: [0.55, 0.72, 0.25] } as const;+ const was = colorOf(grey, 2);+ const mesh = buildInstancedMesh(grey, [{ x: 0, y: 0, z: 0, yaw: 0, scale: 1, phase: 0, shade: 0.5 }], { base: [...stock.base], tip: [...stock.tip] });+ const now = colorOf(mesh, 2);+ // Its own grey, half as light for a plant of half the shade: not green.+ expect(now[0]).toBe(now[1]);+ expect(now[1]).toBe(now[2]);+ expect(Math.abs(now[0]! - was[0]! * 0.5)).toBeLessThanOrEqual(1);+ // And a mesh that has no colours to keep (white throughout) is still painted as a plant is.+ const white = buildGrassMesh([{ x: 0, y: 0, z: 0, yaw: 0, scale: 1, phase: 0, shade: 1 }],+ { height: 10, width: 2, segments: 1, thin: 1, paint: { base: [1, 1, 1], tip: [1, 1, 1] } });+ for (let v = 0; v < white.vertexCount; v++) {+ const view = new DataView(white.vertices.buffer, white.vertices.byteOffset, white.vertices.byteLength);+ const at = v * white.vertexStride + white.channels.find((c) => c.semantic === 1)!.offset;+ for (let k = 0; k < 3; k++) view.setUint8(at + k, 255);+ }+ const painted = colorOf(buildInstancedMesh(white, [{ x: 0, y: 0, z: 0, yaw: 0, scale: 1, phase: 0, shade: 1 }], { base: [...stock.base], tip: [...stock.tip] }), 2);+ expect(painted[1]).toBeGreaterThan(painted[0]!);+ expect(painted[1]).toBeGreaterThan(painted[2]!);+ });+it('makes two crossed cards a plant showing the rectangle it is given', () => {const mesh = buildBillboardMesh([{ x: 0, y: 0, z: 0, yaw: 0, scale: 1, phase: 0, shade: 1 }], 4, 8, { u0: 0.25, v0: 0, u1: 0.75, v1: 0.5 });expect(mesh.vertexCount).toBe(8);diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex c531f9448..38e0ba401 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4770,4 +4770,6 @@ export const NOTED = {{"note": "A playable samples its textures as their import settings say."},"perf(render): drawing a frame costs the CPU a third of what it did - a draw no probe lights reads the zero block instead of staging its own, a draw binds only the texture units its program samples, the cull face is cached, vertex arrays are bound without asking the browser for their index buffer (no FULL_ES3), and device objects are found by index":{"note": "Drawing a frame costs the CPU a third of what it did."},+ "fix(foliage): a mesh scattered with no material keeps its own colours - a field with no material painted every plant the stock green of grass from root to tip, a mesh's own vertex colours with it, so a grey rock came out a green one and a dark spruce a pale one; a mesh that has colours is painted in them, each plant by its shade, and one with none (or white throughout) is painted as before":+ {"note": "A mesh scattered with no material keeps its own colours."},};