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(cocos): a converted effect encodes its output for the display in the gamma colour space, and a converted material is not tinted twice by the renderer

yhh6e12dc6ce333
diff --git a/pipeline/src/import/cocos/components.ts b/pipeline/src/import/cocos/components.ts
index aeaa6abb9..afb3fda46 100644
--- a/pipeline/src/import/cocos/components.ts
+++ b/pipeline/src/import/cocos/components.ts
@@ -639,7 +639,10 @@ function applyMaterial(data: Raw, c: CocosComponent, ctx: ComponentContext, wher
if (converted) data.material = converted.ref;
if (info.texture) data.texture = ctx.assetRef(info.texture);
if (info.normal) data.normalMap = ctx.assetRef(info.normal);
- if (info.color) data.color = info.color;
+ // A converted material holds its own colour (`mainColor`), and the renderer's colour reaches its shader as the vertex colour, which Cocos leaves
+ // white: the tint put there as well is applied twice, and read as sRGB besides, which is how a wall comes out a third as bright as it is.
+ if (converted) data.color = { r: 1, g: 1, b: 1, a: 1 };
+ else if (info.color) data.color = info.color;
data.lit = !info.unlit;
data.opaque = !info.transparent;
// what the effect's pass blends is what the draw does: a mesh the engine takes for opaque is drawn without blending, and a fog that fades out would draw black
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.ts
index 3275dd800..45da97c41 100644
--- a/pipeline/src/import/cocos/effectConvert.ts
+++ b/pipeline/src/import/cocos/effectConvert.ts
@@ -100,11 +100,18 @@ in highp vec3 v_worldNormal;
#define cocos_normal vec3(0.0, 0.0, 1.0)
#endif`;
+// A Cocos fragment works in linear light, decoding its textures and colours, and ends in \`CCFragOutput\`, which encodes for the display. The engine's
+// linear colour space encodes in the pass that ends the frame, so there the output stays as it is; in the gamma one the shader's own result is what
+// reaches the screen, and it is encoded here, or everything the shader lit comes out dark.
const PRELUDE = `// Cocos built-ins the converted fragment calls.
-#define CCFragOutput(c) (c)
#define cc_time u_time
vec3 LinearToSRGB(vec3 c) { return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(vec3(0.0031308), c)); }
vec3 SRGBToLinear(vec3 c) { return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(vec3(0.04045), c)); }
+#ifdef ES_LINEAR
+#define CCFragOutput(c) (c)
+#else
+#define CCFragOutput(c) vec4(LinearToSRGB((c).rgb), (c).a)
+#endif
`;
/**
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index 782576f18..d6b246038 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -32,7 +32,7 @@ import { convertPass, isOutlinePass, type PassConversion } from './effectConvert
* importer that made less (or differently), and is brought up to date when it is opened. Raise it when the converted output changes in a
* way an existing project needs to pick up: a new generated file, a field a document or a manifest now carries.
*/
-export const COCOS_IMPORT_REVISION = 22;
+export const COCOS_IMPORT_REVISION = 24;
/** Whether the project at @p projectRoot was written by an importer of this revision. A project that was not converted has nothing to bring up. */
export function cocosImportIsCurrent(projectRoot: string): boolean {