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): color grading effect (T3)

yhh23a994b13b14
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index 9600fd980..035bacbe2 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -91,6 +91,19 @@ registerEffect({
],
});
+registerEffect({
+ type: 'colorGrade',
+ label: 'Color Grade',
+ factory: () => postProcessEffects.createColorGrade(),
+ uniforms: [
+ { name: 'u_exposure', label: 'Exposure', min: -3, max: 3, step: 0.05, defaultValue: 0 },
+ { name: 'u_contrast', label: 'Contrast', min: 0, max: 2, step: 0.01, defaultValue: 1 },
+ { name: 'u_saturation', label: 'Saturation', min: 0, max: 2, step: 0.01, defaultValue: 1 },
+ { name: 'u_temperature', label: 'Temperature', min: -1, max: 1, step: 0.01, defaultValue: 0 },
+ { name: 'u_tint', label: 'Tint', min: -1, max: 1, step: 0.01, defaultValue: 0 },
+ ],
+});
+
registerEffect({
type: 'chromaticAberration',
label: 'Chromatic Aberration',
diff --git a/sdk/src/postprocess/postProcessEffects.ts b/sdk/src/postprocess/postProcessEffects.ts
index d099bdb17..5f9bcca95 100644
--- a/sdk/src/postprocess/postProcessEffects.ts
+++ b/sdk/src/postprocess/postProcessEffects.ts
@@ -149,6 +149,44 @@ void main() {
return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
},
+ createColorGrade(): ShaderHandle {
+ const fragmentSrc = `#version 300 es
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform float u_exposure; // stops; 0 = unchanged
+uniform float u_contrast; // 1 = unchanged
+uniform float u_saturation; // 1 = unchanged
+uniform float u_temperature; // -1 cool .. +1 warm
+uniform float u_tint; // -1 green .. +1 magenta
+out vec4 fragColor;
+
+void main() {
+ vec4 src = texture(u_texture, v_texCoord);
+ vec3 c = src.rgb;
+
+ // Exposure (stops): 2^EV.
+ c *= exp2(u_exposure);
+
+ // White balance: warm/cool on R/B, green/magenta on G. Identity at 0.
+ c.r *= 1.0 + u_temperature * 0.2;
+ c.b *= 1.0 - u_temperature * 0.2;
+ c.g *= 1.0 + u_tint * 0.2;
+
+ // Contrast about mid-grey.
+ c = (c - 0.5) * u_contrast + 0.5;
+
+ // Saturation about Rec.709 luma.
+ float luma = dot(c, vec3(0.2126, 0.7152, 0.0722));
+ c = mix(vec3(luma), c, u_saturation);
+
+ fragColor = vec4(clamp(c, 0.0, 1.0), src.a);
+}
+`;
+ return Material.createShader(POSTPROCESS_VERTEX, fragmentSrc);
+ },
+
createChromaticAberration(): ShaderHandle {
const fragmentSrc = `#version 300 es
precision highp float;
diff --git a/sdk/tests/postprocess.test.ts b/sdk/tests/postprocess.test.ts
index 9b97aad76..0bfd158ff 100644
--- a/sdk/tests/postprocess.test.ts
+++ b/sdk/tests/postprocess.test.ts
@@ -8,7 +8,7 @@ vi.mock('../src/material', () => ({
}));
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
-import { PostProcessApi, PostProcessStack, postProcessEffects, initPostProcessAPI, shutdownPostProcessAPI } from '../src/postprocess';
+import { PostProcessApi, PostProcessStack, postProcessEffects, initPostProcessAPI, shutdownPostProcessAPI, getEffectDef } from '../src/postprocess';
import { Material } from '../src/material';
import type { ESEngineModule } from '../src/wasm';
@@ -363,6 +363,29 @@ describe('PostProcess API', () => {
expect(handle).toBe(42);
});
+ it('should create color grade shader using Material.createShader', () => {
+ const handle = postProcessEffects.createColorGrade();
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.stringContaining('a_position'),
+ expect.stringContaining('u_exposure'),
+ );
+ expect(Material.createShader).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.stringContaining('u_saturation'),
+ );
+ expect(handle).toBe(42);
+ });
+
+ it('registers colorGrade with identity defaults (no-op at defaults)', () => {
+ const def = getEffectDef('colorGrade');
+ expect(def?.label).toBe('Color Grade');
+ const defaults = Object.fromEntries((def?.uniforms ?? []).map(u => [u.name, u.defaultValue]));
+ // Identity: 2^0=1, contrast 1, saturation 1, no white-balance shift.
+ expect(defaults).toEqual({
+ u_exposure: 0, u_contrast: 1, u_saturation: 1, u_temperature: 0, u_tint: 0,
+ });
+ });
+
it('should use the shared POSTPROCESS_VERTEX shader for all effects', () => {
postProcessEffects.createBlur();
postProcessEffects.createVignette();