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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(cocos): a vertex program that reads light-probe data only under the probe macro converts, since a material is given none: the terrain effect that draws the ground was refused for it
yhhb45ad6e97454
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.tsindex 602c60858..352e59957 100644--- a/pipeline/src/import/cocos/effectConvert.ts+++ b/pipeline/src/import/cocos/effectConvert.ts@@ -288,11 +288,49 @@ interface Emulated {displaces: boolean;}+/**+ * A vertex program with the branches it takes only for a light probe taken out. A material is not given probe data, so `CC_USE_LIGHT_PROBE` is off for it, and a+ * program that mentions probes only under that macro does not read them.+ */+function withoutLightProbeBranches(program: string): string {+ interface Frame { probe: boolean; dropping: boolean; reopened: boolean }+ const stack: Frame[] = [];+ const out: string[] = [];+ const emitting = (): boolean => stack.every((f) => !f.dropping);+ for (const line of program.split('\n')) {+ const directive = /^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b(.*)$/.exec(line);+ if (!directive) {+ if (emitting()) out.push(line);+ continue;+ }+ const [, kind, rest] = directive as unknown as [string, string, string];+ if (kind === 'if' || kind === 'ifdef' || kind === 'ifndef') {+ const probe = kind !== 'ifndef' && /^\s*(?:defined\s*\(\s*)?CC_USE_LIGHT_PROBE\b/.test(rest);+ const emit = emitting();+ stack.push({ probe, dropping: probe, reopened: false });+ if (emit && !probe) out.push(line);+ } else if (kind === 'endif') {+ const frame = stack.pop();+ if (emitting() && !(frame?.probe && !frame.reopened)) out.push(line);+ } else {+ // #else / #elif: the side of a probe branch that comes after it is the one a material takes+ const frame = stack[stack.length - 1];+ if (frame?.probe && frame.dropping) {+ frame.dropping = false;+ if (kind === 'elif') { frame.reopened = true; if (emitting()) out.push('#if' + rest); }+ } else if (emitting()) {+ out.push(line);+ }+ }+ }+ return out.join('\n');+}+function emulateVertex(program: string, entry: string): Emulated | { error: string } {const outputs = new Set<string>();const attributes = new Map<string, string>(Object.entries(STANDARD_ATTRIBUTES));const approximations: string[] = [];- let text = program;+ let text = withoutLightProbeBranches(program);// Declarations: an attribute is a global the setup fills; an output is a global the fragment then reads.text = text.replace(/^[ \t]*(?:layout\s*\([^)]*\)\s*)?in\s+(?:(?:highp|mediump|lowp|flat)\s+)*(\w+)\s+(a_\w+)\s*;[ \t]*$/gm, (_m, type: string, name: string) => {attributes.set(name, type);diff --git a/pipeline/tests/cocos-effect-probe.test.ts b/pipeline/tests/cocos-effect-probe.test.tsnew file mode 100644index 000000000..2150590f3--- /dev/null+++ b/pipeline/tests/cocos-effect-probe.test.ts@@ -0,0 +1,68 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A vertex program that reads light-probe data only under `CC_USE_LIGHT_PROBE` does not read it for a material, which is given none: such a pass converts,+ * and one that reads probes without that guard still does not.+ */+import { describe, expect, it } from 'vitest';+import { convertPass } from '../src/import/cocos/effectConvert';+import { parseEffect } from '../src/import/cocos/effects';++const effectWith = (vertexBody: string): string => `+CCEffect %{+ techniques:+ - name: opaque+ passes:+ - vert: probe-vs:vert+ frag: probe-fs:frag+ properties:+ mainColor: { value: [1, 1, 1, 1], editor: { type: color } }+}%++CCProgram probe-vs %{+ precision highp float;+ in vec3 a_position;+${vertexBody}+}%++CCProgram probe-fs %{+ precision highp float;+ uniform Constant {+ vec4 mainColor;+ };+ vec4 frag () { return mainColor; }+}%+`;++const convert = (vertexBody: string) => {+ const effect = parseEffect(effectWith(vertexBody));+ return convertPass(effect, effect.techniques[0]!.passes[0]!, 'probe.opaque.0');+};++describe('a vertex program that mentions light probes', () => {+ it('converts when it reads them only under the probe macro', () => {+ const converted = convert(`+ #if CC_USE_LIGHT_PROBE+ in vec4 a_sh_linear_const_r;+ out vec3 v_sh;+ vec3 probe () { return CCGetSH(a_sh_linear_const_r); }+ #endif+ vec4 vert () {+ #if CC_USE_LIGHT_PROBE+ vec3 sh = probe();+ #else+ vec3 sh = vec3(0.0);+ #endif+ return vec4(a_position + sh, 1.0);+ }`);+ expect(converted.unsupported).toBeNull();+ expect(converted.shader?.source).toContain('cocos_vert');+ });++ it('is still refused when it reads them outside that guard', () => {+ const converted = convert(`+ in vec4 a_sh_linear_const_r;+ vec4 vert () { return vec4(a_position + CCGetSH(a_sh_linear_const_r), 1.0); }`);+ expect(converted.unsupported).toContain('light-probe');+ });+});