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(postprocess): Color Grade contrast keeps a dark scene in a linear project

yhh86b1fa433248
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 52f638122..e8ea9983d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -60,6 +60,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Color Grade contrast no longer blacks out a dark scene in a linear project.** It turned on 0.5, which is well above mid-grey in linear light, so raising it took everything dark to pure black: at 1.4, a night scene. It now turns on mid-grey, as a curve that keeps what is dark. Linear projects that set a contrast above 1 will look a little brighter in their shadows.
+
- **A WeChat mini-game opens in WeChat DevTools.** A package stopped there at "BigInt64Array is not defined" while a phone ran it, as the engine and its side modules needed a global DevTools' game realm does not have. They no longer do.
- **An agent is told when a child it adds inside a prefab instance will not last.** A scene stores an instance as its root and its changes, so such a child lost its parent on saving, and an exported game dropped it, with no word. `apply_scene_ops` now warns, and says what to do instead.
diff --git a/desktop b/desktop
index 3eec347b0..4b6834bd7 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 3eec347b0cda02c7f876daa0e531aa994e3ebc2b
+Subproject commit 4b6834bd7ab4aecc7b4faeab4d06146414bda5ba
diff --git a/sdk/src/postprocess/sources/basic.ts b/sdk/src/postprocess/sources/basic.ts
index 1c0c1e1be..d7fe581d7 100644
--- a/sdk/src/postprocess/sources/basic.ts
+++ b/sdk/src/postprocess/sources/basic.ts
@@ -6,6 +6,7 @@
*/
import type { ShaderHandle } from '../../render/material';
import { Material } from '../../render/material';
+import { linearColorSpace } from '../../ecs/env';
import { registerEffectSources, registerComputeSource } from '../effectSources';
import { SHARPEN_WGSL } from '../computeEffects';
import { lensEffects } from '../lensEffects';
@@ -404,6 +405,11 @@ void main() {
return Material.compileShader(source);
},
createColorGrade(): ShaderHandle {
+ // Contrast about mid-grey, which is 0.5 of a gamma-encoded picture and 0.18 of a linear one. Linear light gets a power curve about it, as
+ // a straight line through 0.18 sends what is dark to black: at 1.4 everything under 0.05, the whole of a night scene.
+ const linear = linearColorSpace();
+ const contrastGlsl = linear ? 'c = 0.18 * pow(max(c, vec3(1e-6)) / 0.18, vec3(u_contrast));' : 'c = (c - 0.5) * u_contrast + 0.5;';
+ const contrastWgsl = linear ? 'c = 0.18 * pow(max(c, vec3f(1e-6)) / 0.18, vec3f(mc.u_contrast));' : 'c = (c - 0.5) * mc.u_contrast + 0.5;';
const source = `#pragma shader "PP Color Grade"
#pragma version 300 es
#pragma domain PostProcess
@@ -433,7 +439,7 @@ void main() {
c.g *= 1.0 + u_tint * 0.2;
// Contrast about mid-grey.
- c = (c - 0.5) * u_contrast + 0.5;
+ ${contrastGlsl}
// Saturation about Rec.709 luma.
float luma = dot(c, vec3(0.2126, 0.7152, 0.0722));
@@ -454,7 +460,7 @@ void main() {
c.b *= 1.0 - mc.u_temperature * 0.2;
c.g *= 1.0 + mc.u_tint * 0.2;
- c = (c - 0.5) * mc.u_contrast + 0.5;
+ ${contrastWgsl}
let luma = dot(c, vec3f(0.2126, 0.7152, 0.0722));
c = mix(vec3f(luma), c, mc.u_saturation);
diff --git a/sdk/tests/postprocess.test.ts b/sdk/tests/postprocess.test.ts
index 38bb8d93c..c8ac0f217 100644
--- a/sdk/tests/postprocess.test.ts
+++ b/sdk/tests/postprocess.test.ts
@@ -11,6 +11,7 @@ vi.mock('../src/render/material', () => ({
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { PostProcessAPI, PostProcessStack, postProcessEffects, initPostProcessAPI, shutdownPostProcessAPI, getEffectDef } from '../src/postprocess';
import { Material } from '../src/render/material';
+import { setLinearColorSpace } from '../src/ecs/env';
import type { ESEngineModule } from '../src/wasm';
import { registerBasicEffectSources } from '../src/postprocess/sources/basic';
import { registerAtmosphereEffectSources } from '../src/postprocess/sources/atmosphere';
@@ -415,6 +416,25 @@ describe('PostProcess API', () => {
});
});
+ it('colorGrade takes contrast about the mid-grey of the colour space it runs in', () => {
+ const sourceFor = (linear: boolean): string => {
+ setLinearColorSpace(linear);
+ try {
+ postProcessEffects.createColorGrade();
+ return lastSource();
+ } finally {
+ setLinearColorSpace(false);
+ }
+ };
+ const gamma = sourceFor(false);
+ expect(gamma).toContain('c = (c - 0.5) * u_contrast + 0.5;');
+ expect(gamma).toContain('c = (c - 0.5) * mc.u_contrast + 0.5;');
+ const linear = sourceFor(true);
+ expect(linear).toContain('c = 0.18 * pow(max(c, vec3(1e-6)) / 0.18, vec3(u_contrast));');
+ expect(linear).toContain('c = 0.18 * pow(max(c, vec3f(1e-6)) / 0.18, vec3f(mc.u_contrast));');
+ expect(linear).not.toContain('- 0.5');
+ });
+
it('every effect ships a WGSL twin and stays fragment-only', () => {
postProcessEffects.createLutGrade();
postProcessEffects.createBlur();
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index e1d3fcf1f..1260c3890 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3884,6 +3884,8 @@ export const NOTED = {
{"note":"A material's normal and metal-rough maps are read as data in the editor's Play."},
"fix(minigame): no mini-game module needs BigInt64Array, so WeChat DevTools boots the game":
{"note":"A WeChat mini-game opens in WeChat DevTools."},
+ "fix(postprocess): Color Grade contrast keeps a dark scene in a linear project":
+ {"note":"Color Grade contrast no longer blacks out a dark scene in a linear project."},
"feat(look): pictures laid side by side, one outlined over another, and their exact colours":
{"note":"An AI can put the game beside the pictures it is imitating."},
"feat(mcp): reference_compare, field_sweep and motion_sheet show the game beside what it imitates, at several values and over time":