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's uniform that properties are lanes of starts as those lanes put together, so a fragment that scales by it no longer draws black

yhh89bfc1e692b7
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.ts
index 3bd7c5218..602c60858 100644
--- a/pipeline/src/import/cocos/effectConvert.ts
+++ b/pipeline/src/import/cocos/effectConvert.ts
@@ -358,6 +358,18 @@ export function convertPass(
const approximations: string[] = [];
const props = new Map(pass.properties.map((p) => [p.name, p]));
+ // A property that says `target: colorScaleAndCutoff.xyz` is part of a vec4 uniform, and what the uniform starts as is those parts put together:
+ // the uniform has no property of its own to say it.
+ const packedDefaults = new Map<string, number[]>();
+ for (const p of pass.properties) {
+ const m = p.target ? /^(\w+)\.([xyzwrgba]+)$/.exec(p.target) : null;
+ if (!m || p.value === null || p.value === undefined) continue;
+ const lanes = [...m[2]!].map((c) => 'xyzw'.indexOf(c) >= 0 ? 'xyzw'.indexOf(c) : 'rgba'.indexOf(c));
+ const whole = packedDefaults.get(m[1]!) ?? [0, 0, 0, 0];
+ const given = numbersOf(p.value, lanes.length);
+ lanes.forEach((lane, i) => { whole[lane] = given[i] ?? 0; });
+ packedDefaults.set(m[1]!, whole);
+ }
const params: ConvertedShader['params'] = [];
const declared = new Set<string>();
const missing: string[] = [];
@@ -403,7 +415,7 @@ export function convertPass(
declared.add(name);
const prop = props.get(name);
const type = paramTypeOf(glsl, prop);
- params.push({ name, type, value: numbersOf(prop?.value ?? (name === 'alphaThreshold' ? 0.5 : 0), ARITY[type]!) });
+ params.push({ name, type, value: numbersOf(prop?.value ?? packedDefaults.get(name) ?? (name === 'alphaThreshold' ? 0.5 : 0), ARITY[type]!) });
}
return '';
});
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index deca7a0e6..5b9759f0f 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -31,7 +31,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 = 14;
+export const COCOS_IMPORT_REVISION = 15;
/** 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 {
diff --git a/pipeline/tests/cocos-effect-packed.test.ts b/pipeline/tests/cocos-effect-packed.test.ts
new file mode 100644
index 000000000..e00870c9d
--- /dev/null
+++ b/pipeline/tests/cocos-effect-packed.test.ts
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file A uniform that properties are parts of (`colorScale: { value: [1,1,1], target: colorScaleAndCutoff.xyz }`) starts as those parts put together, not as
+ * zero: a fragment that multiplies by it would otherwise draw black.
+ */
+import { describe, expect, it } from 'vitest';
+import { convertPass } from '../src/import/cocos/effectConvert';
+import { parseEffect } from '../src/import/cocos/effects';
+
+const EFFECT = `
+CCEffect %{
+ techniques:
+ - name: opaque
+ passes:
+ - vert: unlit-vs:vert
+ frag: unlit-fs:frag
+ properties:
+ mainColor: { value: [1, 1, 1, 1], linear: true, editor: { type: color } }
+ colorScale: { value: [1, 0.5, 0.25], target: colorScaleAndCutoff.xyz }
+ alphaThreshold: { value: 0.4, target: colorScaleAndCutoff.w }
+}%
+
+CCProgram unlit-vs %{
+ precision highp float;
+ in vec3 a_position;
+ vec4 vert () { return vec4(a_position, 1.0); }
+}%
+
+CCProgram unlit-fs %{
+ precision highp float;
+ uniform Constant {
+ vec4 mainColor;
+ vec4 colorScaleAndCutoff;
+ };
+ vec4 frag () { vec4 o = mainColor; o.rgb *= colorScaleAndCutoff.xyz; return o; }
+}%
+`;
+
+describe('a uniform made of properties', () => {
+ it('starts as the properties it is made of', () => {
+ const effect = parseEffect(EFFECT);
+ const technique = effect.techniques[0]!;
+ const converted = convertPass(effect, technique.passes[0]!, 'packed.opaque.0');
+ const param = converted.shader?.params.find((p) => p.name === 'colorScaleAndCutoff');
+ expect(param?.value).toEqual([1, 0.5, 0.25, 0.4]);
+ });
+});