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
perf(render): the lens flare is worked out at half size and laid over the full-size picture
yhhb451a03071fb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 62420c6bf..784a741a8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -47,6 +47,7 @@ published separately; it ships inside the editor.### Changed+- **Lens flare costs a third of what it did.** The flare looked ninety places in the picture for something bright at every pixel of the full-size frame, to light almost none of them: 0.76 ms of a 1080p frame in the terrain-water example. It is now worked out at half size and laid over the full-size picture (0.22 ms and 0.07 ms), 0.47 ms less. The picture is the same except along the hard edges of whatever throws the flare, where it softens by a pixel or two (0.6 of 255 on average in a scene built to make that worst). A new render scene, `hdr-lens-flare`, is the first to check a flare on both backends.- **The engine's own examples and fixtures no longer commit a generated WebGPU shader twin.** The `wgsl full` section of a `.esshader` is a translation of its GLSL joined to the engine's headers, so every edit to an engine header (the render origin in `FrameConstants`, a lighting function) changed it in dozens of files nobody had touched: 83% of that text is the engine's own lighting code, 2% the author's. It is now derived when the shader is read: by the export (`cookAssets`), by the fixture server the render checks use, and by the editor when it serves a project of the repo. Deriving all 44 shaders takes 2.3 s. `gen-shader-twins --derived` checks that each one still translates and that none commits a twin again; a project of your own keeps its twin in the file as before, because the converters are not in a packaged editor. 18 files lose about 11,000 lines; the 248 WebGPU render scenes draw the same.- **The web module is 144 KB smaller (50 KB gzipped) because it no longer carries a C++ stream library.** `Log`, the shader parser, the font and material parsers, file reads and one JSON status string used `ostringstream`, `istringstream` and `ifstream`, which link libc++'s whole iostream and locale machinery (money, number and time facets, global locale setup at start-up) into every build. They now use `std::string`, a small line reader and stdio, with output kept the same: all 69 shaders parse and assemble to byte-identical text for both targets, a log line prints as before, and the 262 WebGL2 render scenes still draw what they claim. 2,202,592 to 2,058,299 bytes.- **Instanced meshes that share a material now draw together, so a scene makes a tenth of the draw calls.** An opaque draw sorted by shader, then material, then depth, so two meshes sharing a material alternated by depth and no two neighbours were the same mesh: every instanced draw broke the merge (610 of 612 in the terrain-water example). An instanced mesh draw now puts a hash of its geometry in the top bits of the depth field, so one mesh's draws sit together and merge into one instanced call, in the scene, the depth prepass and the shadow pass alike. The example goes from 612 scene, 610 prepass and 385 shadow draws to 57, 55 and 45, from 10,709 GL calls a frame to 1,977, and from about 6.2 ms to 4.1 ms of CPU a frame. Only the order among opaque draws of one material changes, and they are order-independent (they write depth); the 262 WebGL2 render scenes still draw what they claim.diff --git a/fixtures/scenes/hdr-lens-flare.esscene b/fixtures/scenes/hdr-lens-flare.esscenenew file mode 100644index 000000000..9ddc3e991--- /dev/null+++ b/fixtures/scenes/hdr-lens-flare.esscene@@ -0,0 +1,149 @@+{+ "version": 4,+ "name": "HDR Lens Flare",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 0,+ "z": 10+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 300,+ "isActive": true,+ "priority": 0+ }+ }+ ],+ "visible": true+ },+ {+ "id": 1,+ "name": "LitQuad",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -90,+ "y": 40,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 120,+ "y": 120+ },+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "lit": true+ }+ }+ ],+ "visible": true+ },+ {+ "id": 2,+ "name": "HotLight",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -90,+ "y": 40,+ "z": 0+ }+ }+ },+ {+ "type": "Light",+ "data": {+ "type": 0,+ "color": {+ "r": 1,+ "g": 0.9,+ "b": 0.7,+ "a": 1+ },+ "intensity": 6,+ "radius": 400+ }+ }+ ],+ "visible": true+ },+ {+ "id": 3,+ "name": "PostProcess",+ "parent": null,+ "children": [],+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 0,+ "z": 0+ }+ }+ },+ {+ "type": "PostProcessVolume",+ "data": {+ "isGlobal": true,+ "priority": 0,+ "weight": 1,+ "blendDistance": 0,+ "shape": "box",+ "size": {+ "x": 5,+ "y": 5+ },+ "effects": [+ {+ "type": "lensFlare",+ "enabled": true,+ "uniforms": {+ "u_intensity": 1,+ "u_threshold": 1.2,+ "u_ghosts": 5,+ "u_dispersal": 0.35,+ "u_haloWidth": 0.45,+ "u_chromatic": 0.012+ }+ }+ ]+ }+ }+ ],+ "visible": true+ }+ ]+}\ No newline at end of filediff --git a/fixtures/scenes/hdr-lens-flare.textures.json b/fixtures/scenes/hdr-lens-flare.textures.jsonnew file mode 100644index 000000000..0967ef424--- /dev/null+++ b/fixtures/scenes/hdr-lens-flare.textures.json@@ -0,0 +1 @@+{}diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.tsindex dffa3c250..8bf69c6f5 100644--- a/sdk/src/postprocess/effects.ts+++ b/sdk/src/postprocess/effects.ts@@ -777,6 +777,12 @@ registerEffect({type: 'lensFlare',label: 'Lens Flare',factory: () => lensEffects.createLensFlare(),+ // The flare is smooth (ghosts fade by the cube of their distance from the edge, the halo by the fourth power) and dear: ninety taps a pixel to+ // find what is bright. It is worked out at half size and laid over the full-size picture, which is a quarter of the pixels paying.+ multiPass: [+ { name: 'lensflare_flare', factory: () => lensEffects.createLensFlare(), scale: 0.5 },+ { name: 'lensflare_composite', factory: () => lensEffects.createLensFlareComposite() },+ ],uniforms: [{ name: 'u_intensity', label: 'Intensity', min: 0, max: 4, step: 0.01, defaultValue: 0.4, neutralValue: 0 },// Only what is brighter than this (in the picture's own units, HDR included) throws a flare.diff --git a/sdk/src/postprocess/lensEffects.ts b/sdk/src/postprocess/lensEffects.tsindex 6b651685e..447289821 100644--- a/sdk/src/postprocess/lensEffects.ts+++ b/sdk/src/postprocess/lensEffects.ts@@ -231,7 +231,6 @@ vec3 fringed(vec2 uv, vec2 dir, vec2 spread) {}void main() {- vec4 color = texture(u_texture, v_texCoord);vec2 uv = vec2(1.0) - v_texCoord;vec2 toCentre = vec2(0.5) - uv;vec2 ghostStep = toCentre * u_dispersal;@@ -246,7 +245,7 @@ void main() {vec2 haloDir = normalize(toCentre + 1e-5) * u_haloWidth;float haloEdge = 1.0 - clamp(length(vec2(0.5) - (uv + haloDir)) / 0.7071, 0.0, 1.0);flare += fringed(uv + haloDir, haloDir, spread) * pow(haloEdge, 4.0) * 2.0;- fragColor = vec4(color.rgb + flare * u_tint.rgb * u_intensity, color.a);+ fragColor = vec4(flare * u_tint.rgb * u_intensity, 1.0);}#pragma end@@ -264,7 +263,6 @@ fn fringed(uv : vec2f, dir : vec2f, spread : vec2f) -> vec3f {}@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {- let color = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);let uv = vec2f(1.0) - v.v_texCoord;let toCentre = vec2f(0.5) - uv;let ghostStep = toCentre * mc.u_dispersal;@@ -280,7 +278,36 @@ fn fringed(uv : vec2f, dir : vec2f, spread : vec2f) -> vec3f {let haloDir = dir * mc.u_haloWidth;let haloEdge = 1.0 - clamp(length(vec2f(0.5) - (uv + haloDir)) / 0.7071, 0.0, 1.0);flare = flare + fringed(uv + haloDir, haloDir, spread) * pow(haloEdge, 4.0) * 2.0;- return vec4f(color.rgb + flare * mc.u_tint.rgb * mc.u_intensity, color.a);+ return vec4f(flare * mc.u_tint.rgb * mc.u_intensity, 1.0);+}+#pragma end+`;+ return Material.compileShader(source);+ },+ /** The second pass of the lens flare: the flare, worked out at half size, laid over the picture it came from. */+ createLensFlareComposite(): ShaderHandle {+ const source = `#pragma shader "PP LensFlare Composite"+#pragma version 300 es+#pragma domain PostProcess++#pragma fragment+precision highp float;++in vec2 v_texCoord;+uniform sampler2D u_texture;+uniform sampler2D u_sceneTexture;+out vec4 fragColor;++void main() {+ vec4 scene = texture(u_sceneTexture, v_texCoord);+ fragColor = vec4(scene.rgb + texture(u_texture, v_texCoord).rgb, scene.a);+}+#pragma end++#pragma fragment wgsl+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+ let scene = textureSampleLevel(t1, s1, v.v_texCoord, 0.0);+ return vec4f(scene.rgb + textureSampleLevel(t0, s0, v.v_texCoord, 0.0).rgb, scene.a);}#pragma end`;diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 8a96a41fe..c67c6653c 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3074,4 +3074,6 @@ export const NOTED = {{"note":"The engine's own examples and fixtures no longer commit a generated WebGPU shader twin"},"feat(editor): a project of the engine repo gets its shader twin derived as the file is served":{"note":"The engine's own examples and fixtures no longer commit a generated WebGPU shader twin"},+ "perf(render): the lens flare is worked out at half size and laid over the full-size picture":+ {"note":"Lens flare costs a third of what it did"},};diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex 0ecd0a54e..a69df30b2 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -501,6 +501,8 @@ export const SCENES = [// Post-process chain guards — bloom's first CI pixel coverage. The gamma scene pins the LDR multi-pass chain; the linear scene is the HDR discriminator: threshold 1.5 sits above anything an 8-bit chain can store, so glow OUTSIDE the sprite (x=0.64) proves over-range light survived the float intermediates — the same scene blooms NOTHING in gamma mode.{ id: "bloom-ldr", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/bloom-ldr.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/bloom-ldr.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "10", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.61,\"y\":0.5,\"rgb\":[0,130,0],\"tol\":35},{\"x\":0.70,\"y\":0.5,\"rgb\":[0,32,0],\"tol\":25},{\"x\":0.85,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":12}]" } },{ id: "hdr-bloom", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/hdr-bloom.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/hdr-bloom.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "10", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":25},{\"x\":0.64,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":35},{\"x\":0.85,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":12}]" } },+ // Lens flare, the first pixel coverage it has. A bright quad stands left of centre (the source at x=0.35), and the flare mirrors it through the centre: ghosts along the line to the other side, and a halo a fixed way out. Without the effect those places are black.+ { id: "hdr-lens-flare", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/hdr-lens-flare.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/hdr-lens-flare.textures.json", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "512", ESTELLA_VERIFY_STEPS: "10", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.75,\"y\":0.55,\"rgb\":[255,255,253],\"tol\":25},{\"x\":0.85,\"y\":0.8,\"rgb\":[255,255,182],\"tol\":35},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":12},{\"x\":0.5,\"y\":0.1,\"rgb\":[0,0,0],\"tol\":12},{\"x\":0.9,\"y\":0.1,\"rgb\":[0,0,0],\"tol\":12}]" } },// AgX: five patches of known scene-linear colour read against what Blender's own AgX displays for them (tools/render-align/agx_chart.py in the// star-neighbors game, look None): a grey ramp, a skin tone and a dim blue. The curve and the table are what is under test, on both backends.{ id: "agx-chart", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/agx-chart.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/agx-chart.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "10", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.1,\"y\":0.5,\"rgb\":[66,66,66],\"tol\":10},{\"x\":0.3,\"y\":0.5,\"rgb\":[136,136,136],\"tol\":10},{\"x\":0.5,\"y\":0.5,\"rgb\":[197,197,197],\"tol\":10},{\"x\":0.7,\"y\":0.5,\"rgb\":[160,135,125],\"tol\":10},{\"x\":0.9,\"y\":0.5,\"rgb\":[45,77,118],\"tol\":10}]" } },