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.

feat(postprocess): tonemap, FXAA, lens distortion, pixelate effects (T3)

yhh8c89a44bbec5
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index 035bacbe2..b61c9313b 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -112,3 +112,40 @@ registerEffect({
{ name: 'u_intensity', label: 'Intensity', min: 0, max: 20, step: 0.1, defaultValue: 3 },
],
});
+
+registerEffect({
+ type: 'tonemap',
+ label: 'Tonemap (ACES)',
+ factory: () => postProcessEffects.createTonemap(),
+ uniforms: [
+ { name: 'u_exposure', label: 'Exposure', min: -3, max: 3, step: 0.05, defaultValue: 0 },
+ ],
+});
+
+registerEffect({
+ type: 'fxaa',
+ label: 'FXAA',
+ factory: () => postProcessEffects.createFxaa(),
+ uniforms: [
+ { name: 'u_intensity', label: 'Intensity', min: 0, max: 1, step: 0.01, defaultValue: 1 },
+ ],
+});
+
+registerEffect({
+ type: 'lensDistortion',
+ label: 'Lens Distortion',
+ factory: () => postProcessEffects.createLensDistortion(),
+ uniforms: [
+ { name: 'u_strength', label: 'Strength', min: -1, max: 1, step: 0.01, defaultValue: 0 },
+ { name: 'u_zoom', label: 'Zoom', min: 0.5, max: 2, step: 0.01, defaultValue: 1 },
+ ],
+});
+
+registerEffect({
+ type: 'pixelate',
+ label: 'Pixelate',
+ factory: () => postProcessEffects.createPixelate(),
+ uniforms: [
+ { name: 'u_pixelSize', label: 'Pixel Size', min: 1, max: 64, step: 1, defaultValue: 4 },
+ ],
+});
diff --git a/sdk/src/postprocess/postProcessEffects.ts b/sdk/src/postprocess/postProcessEffects.ts
index 5f9bcca95..9e0b9b571 100644
--- a/sdk/src/postprocess/postProcessEffects.ts
+++ b/sdk/src/postprocess/postProcessEffects.ts
@@ -205,6 +205,142 @@ void main() {
float a = texture(u_texture, v_texCoord).a;
fragColor = vec4(r, g, b, a);
}
+`;
+ return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
+ },
+
+ createTonemap(): ShaderHandle {
+ // ACES filmic curve (Narkowicz approximation) — maps HDR/linear scene
+ // radiance into a display range with a filmic shoulder/toe. Unlike the
+ // grade/blur effects this always reshapes the curve (that is the point of
+ // tonemapping); only the exposure pre-multiply is identity at its default.
+ const fragmentSrc = `#version 300 es
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform float u_exposure; // stops; 0 = unchanged exposure
+out vec4 fragColor;
+
+vec3 aces(vec3 x) {
+ const float a = 2.51;
+ const float b = 0.03;
+ const float c = 2.43;
+ const float d = 0.59;
+ const float e = 0.14;
+ return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
+}
+
+void main() {
+ vec4 src = texture(u_texture, v_texCoord);
+ vec3 c = src.rgb * exp2(u_exposure);
+ fragColor = vec4(aces(c), src.a);
+}
+`;
+ return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
+ },
+
+ createFxaa(): ShaderHandle {
+ // Luma-based FXAA (Lottes' classic edge-directed blur). Reads only the
+ // built-in u_texture/u_resolution; u_intensity blends the AA result back
+ // toward the original so 0 is an exact no-op.
+ const fragmentSrc = `#version 300 es
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform vec2 u_resolution;
+uniform float u_intensity; // 0 = off (identity), 1 = full AA
+out vec4 fragColor;
+
+const float REDUCE_MIN = 1.0 / 128.0;
+const float REDUCE_MUL = 1.0 / 8.0;
+const float SPAN_MAX = 8.0;
+
+float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
+
+void main() {
+ vec2 inv = 1.0 / u_resolution;
+ vec4 srcM = texture(u_texture, v_texCoord);
+ vec3 rgbNW = texture(u_texture, v_texCoord + vec2(-1.0, -1.0) * inv).rgb;
+ vec3 rgbNE = texture(u_texture, v_texCoord + vec2( 1.0, -1.0) * inv).rgb;
+ vec3 rgbSW = texture(u_texture, v_texCoord + vec2(-1.0, 1.0) * inv).rgb;
+ vec3 rgbSE = texture(u_texture, v_texCoord + vec2( 1.0, 1.0) * inv).rgb;
+
+ float lM = luma(srcM.rgb);
+ float lNW = luma(rgbNW), lNE = luma(rgbNE), lSW = luma(rgbSW), lSE = luma(rgbSE);
+ float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));
+ float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));
+
+ vec2 dir;
+ dir.x = -((lNW + lNE) - (lSW + lSE));
+ dir.y = ((lNW + lSW) - (lNE + lSE));
+ float reduce = max((lNW + lNE + lSW + lSE) * 0.25 * REDUCE_MUL, REDUCE_MIN);
+ float rcpMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + reduce);
+ dir = clamp(dir * rcpMin, vec2(-SPAN_MAX), vec2(SPAN_MAX)) * inv;
+
+ vec3 rgbA = 0.5 * (
+ texture(u_texture, v_texCoord + dir * (1.0 / 3.0 - 0.5)).rgb +
+ texture(u_texture, v_texCoord + dir * (2.0 / 3.0 - 0.5)).rgb);
+ vec3 rgbB = rgbA * 0.5 + 0.25 * (
+ texture(u_texture, v_texCoord + dir * -0.5).rgb +
+ texture(u_texture, v_texCoord + dir * 0.5).rgb);
+
+ float lB = luma(rgbB);
+ vec3 aa = (lB < lMin || lB > lMax) ? rgbA : rgbB;
+ fragColor = vec4(mix(srcM.rgb, aa, clamp(u_intensity, 0.0, 1.0)), srcM.a);
+}
+`;
+ return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
+ },
+
+ createLensDistortion(): ShaderHandle {
+ // Radial lens warp: u_strength > 0 barrels (bulge), < 0 pincushions;
+ // u_zoom rescales to keep edges in frame. Identity at strength 0 / zoom 1
+ // (sample uv == source uv). Out-of-source taps resolve to transparent
+ // black so a warped edge does not smear.
+ const fragmentSrc = `#version 300 es
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform float u_strength; // 0 = none; + barrel, - pincushion
+uniform float u_zoom; // 1 = none
+out vec4 fragColor;
+
+void main() {
+ vec2 uv = v_texCoord * 2.0 - 1.0;
+ float r2 = dot(uv, uv);
+ vec2 warped = uv * (1.0 + u_strength * r2) / max(u_zoom, 0.0001);
+ vec2 suv = warped * 0.5 + 0.5;
+ if (suv.x < 0.0 || suv.x > 1.0 || suv.y < 0.0 || suv.y > 1.0) {
+ fragColor = vec4(0.0);
+ } else {
+ fragColor = texture(u_texture, suv);
+ }
+}
+`;
+ return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
+ },
+
+ createPixelate(): ShaderHandle {
+ // Snaps sampling to a grid of u_pixelSize-device-pixel blocks — the
+ // canonical retro/mosaic 2D look. u_pixelSize <= 1 samples per-texel
+ // (identity). Uses the built-in u_resolution for block sizing.
+ const fragmentSrc = `#version 300 es
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform vec2 u_resolution;
+uniform float u_pixelSize; // device pixels per block; <= 1 = identity
+out vec4 fragColor;
+
+void main() {
+ vec2 blocks = u_resolution / max(u_pixelSize, 1.0);
+ vec2 uv = (floor(v_texCoord * blocks) + 0.5) / blocks;
+ fragColor = texture(u_texture, uv);
+}
`;
return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
},
diff --git a/sdk/tests/postprocess.test.ts b/sdk/tests/postprocess.test.ts
index 0bfd158ff..167fa89a5 100644
--- a/sdk/tests/postprocess.test.ts
+++ b/sdk/tests/postprocess.test.ts
@@ -391,6 +391,10 @@ describe('PostProcess API', () => {
postProcessEffects.createVignette();
postProcessEffects.createGrayscale();
postProcessEffects.createChromaticAberration();
+ postProcessEffects.createTonemap();
+ postProcessEffects.createFxaa();
+ postProcessEffects.createLensDistortion();
+ postProcessEffects.createPixelate();
const calls = (Material.createShader as ReturnType<typeof vi.fn>).mock.calls;
const vertexShaders = calls.map((c: unknown[]) => c[0]);
@@ -401,6 +405,80 @@ describe('PostProcess API', () => {
});
});
+ // =========================================================================
+ // Extended effect library (tonemap / FXAA / lens distortion / pixelate)
+ // =========================================================================
+
+ describe('extended effects', () => {
+ it('should create an ACES tonemap shader with exposure', () => {
+ const handle = postProcessEffects.createTonemap();
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.stringContaining('a_position'),
+ expect.stringContaining('u_exposure'),
+ );
+ // ACES Narkowicz curve constant — guards the operator identity.
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('2.51'),
+ );
+ expect(handle).toBe(42);
+ });
+
+ it('should create an FXAA shader sampling by resolution', () => {
+ const handle = postProcessEffects.createFxaa();
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_resolution'),
+ );
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_intensity'),
+ );
+ expect(handle).toBe(42);
+ });
+
+ it('should create a lens distortion shader with strength and zoom', () => {
+ const handle = postProcessEffects.createLensDistortion();
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_strength'),
+ );
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_zoom'),
+ );
+ expect(handle).toBe(42);
+ });
+
+ it('should create a pixelate shader with pixel size', () => {
+ const handle = postProcessEffects.createPixelate();
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_pixelSize'),
+ );
+ expect(handle).toBe(42);
+ });
+
+ it('registers all four effects with matching factories and labels', () => {
+ expect(getEffectDef('tonemap')?.label).toBe('Tonemap (ACES)');
+ expect(getEffectDef('fxaa')?.label).toBe('FXAA');
+ expect(getEffectDef('lensDistortion')?.label).toBe('Lens Distortion');
+ expect(getEffectDef('pixelate')?.label).toBe('Pixelate');
+ });
+
+ it('uses identity defaults where the effect is meant to be tuned up from off', () => {
+ const def = (t: string) =>
+ Object.fromEntries((getEffectDef(t)?.uniforms ?? []).map(u => [u.name, u.defaultValue]));
+ // Tonemap: 2^0 = 1 exposure (the ACES curve itself always applies).
+ expect(def('tonemap')).toEqual({ u_exposure: 0 });
+ // Lens distortion: strength 0 + zoom 1 == sample uv unchanged (no-op).
+ expect(def('lensDistortion')).toEqual({ u_strength: 0, u_zoom: 1 });
+ // FXAA full strength by default; pixelate visibly chunky so it reads as on.
+ expect(def('fxaa')).toEqual({ u_intensity: 1 });
+ expect(def('pixelate')).toEqual({ u_pixelSize: 4 });
+ });
+ });
+
// =========================================================================
// WASM exception safety
// =========================================================================