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(cocos): an effect includes its chunks and its vertex program is carried into the fragment, so the project materials convert for meshes as well as sprites

yhh522b2a9ee44c
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.ts
index 181eb0f06..bc0a69e53 100644
--- a/pipeline/src/import/cocos/effectConvert.ts
+++ b/pipeline/src/import/cocos/effectConvert.ts
@@ -75,34 +75,6 @@ function stateOf(pass: EffectPass): PassConversion['state'] {
// Programs
// ---------------------------------------------------------------------------
-const stripComments = (s: string): string => s.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\n]*/g, '');
-
-/** The names a vertex stage may use and still be the engine's standard transform. */
-const STANDARD_VERTEX_TOKENS = new Set([
- 'cc_matWorld', 'cc_matView', 'cc_matProj', 'cc_matViewProj', 'a_position', 'a_texCoord', 'a_color', 'a_normal',
-]);
-
-/** Whether a vertex stage only places the vertex: nothing in it needs more than the engine's own. Null when it does; else why not. */
-function customVertexReason(ref: string, effect: CocosEffect): string | null {
- const [program, entry] = ref.split(':');
- if (!program) return 'the pass names no vertex program';
- const found = effect.programs.get(program);
- // A chunk of the Creator install (`legacy/main-functions/general-vs`): the standard vertex stage of an unlit or lit mesh.
- // A program the Creator install supplies (`general-vs`, `legacy/main-functions/general-vs`): the standard vertex stage of a mesh or a quad.
- if (!found) return /^(?:(?:general|unlit|sprite|standard)-vs|legacy\/main-functions\/(?:general|unlit|standard)-vs)$/.test(program) ? null : `vertex program ${program} is not in the effect`;
- const code = stripComments(found.source);
- // Its own entry only: a helper another stage shares may use more.
- void entry;
- if (/\b#include\s*<legacy\/main-functions\/(general|unlit)-vs>/.test(code) && !/\bvoid\s+main\b/.test(code)) return null;
- const used = new Set<string>([...code.matchAll(/\b((?:cc|a)_[A-Za-z0-9_]+)\b/g)].map((m) => m[1]!));
- const extra = [...used].filter((u) => !STANDARD_VERTEX_TOKENS.has(u) && !u.startsWith('a_'));
- const attrs = [...used].filter((u) => u.startsWith('a_') && !STANDARD_VERTEX_TOKENS.has(u));
- if (extra.length > 0) return `its vertex program reads ${extra.join(', ')}`;
- if (attrs.length > 0) return `its vertex program reads the attributes ${attrs.join(', ')}`;
- if (/\btexture\s*\(|\bsin\s*\(|\bcos\s*\(/.test(code)) return 'its vertex program computes per-vertex displacement';
- return null;
-}
-
/** Cocos names for what the engine's vertex stage hands the fragment. */
const VARYINGS: Record<string, { name: string; glsl: string; mesh?: boolean }> = {
v_uv: { name: 'v_texCoord', glsl: 'in vec2 v_texCoord;' },
@@ -128,7 +100,6 @@ in highp vec3 v_worldNormal;
const PRELUDE = `// Cocos built-ins the converted fragment calls.
#define CCFragOutput(c) (c)
-#define CC_APPLY_FOG(c, p)
#define cc_time u_time
#define CC_HANDLE_RT_SAMPLE_FLIP(uv)
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)); }
@@ -168,33 +139,187 @@ function numbersOf(value: unknown, n: number): number[] {
const ARITY: Record<string, number> = { float: 1, int: 1, vec2: 2, vec3: 3, vec4: 4, color: 4 };
+// ---------------------------------------------------------------------------
+// Includes
+// ---------------------------------------------------------------------------
+
+export interface EffectOptions {
+ /** The text of a chunk by its include name (`common/data/unpack`, a project's `tower_defense_death_dissolve`), or null when there is none. */
+ chunk?(name: string): string | null;
+}
+
+/** Chunks of Cocos that the prelude gives (a fragment's built-ins, the scene's uniform blocks): they are left out rather than read. */
+const PROVIDED = /(?:^|\/)(?:fog-fs|output|output-standard|embedded-alpha|alpha-test|common-define|cc-global|cc-local|cc-environment|cc-forward-light|cc-shadow|gamma|aces)$/;
+
+/** Chunks that make up Cocos's vertex input and its world matrix. The vertex stage is emulated (see below) with the prelude's version of them. */
+const VERTEX_STRUCTURE = /(?:^|\/)(?:input|input-standard|decode-base|decode-standard|local-batch|fog-vs|shadow-map-vs|morph|skinning)$/;
+
+const PROVIDED_AND_STRUCTURE = new RegExp(`${PROVIDED.source}|${VERTEX_STRUCTURE.source}`);
+
+/**
+ * A program's text with each `#include <name>` that is not the prelude's replaced by the chunk it names: a program of the same effect first, then
+ * the project's chunks and the Creator install's, through {@link EffectOptions.chunk}. A chunk is read once for the whole shader, since a second
+ * copy would declare its functions twice. What cannot be found is returned, so the pass can say what it lacked.
+ */
+function expandIncludes(
+ source: string, effect: CocosEffect, options: EffectOptions, seen: Set<string>, missing: string[], depth = 0,
+ skip: RegExp = PROVIDED,
+): string {
+ return source.replace(/^[ \t]*#[ \t]*include[ \t]*<([^>]+)>[ \t]*$/gm, (line, name: string) => {
+ if (skip.test(name)) return '';
+ if (seen.has(name)) return '';
+ seen.add(name);
+ const text = effect.programs.get(name)?.source ?? options.chunk?.(name) ?? null;
+ if (text === null) { missing.push(name); return line; }
+ if (depth > 12) { missing.push(`${name} (too deep)`); return line; }
+ return expandIncludes(text, effect, options, seen, missing, depth + 1, skip);
+ });
+}
+
+// ---------------------------------------------------------------------------
+// The vertex stage, carried into the fragment
+// ---------------------------------------------------------------------------
+
+/**
+ * What a vertex program of an effect hands to its fragment is almost always the surface made ready for shading: the world position and normal, the
+ * uv scaled and offset by the material's tiling, a second uv, the vertex colour. The engine's vertex stage supplies the first of those and the uv,
+ * and the rest are arithmetic on them. So the vertex program is not given a stage of its own: it is run in the fragment, ahead of the fragment's own
+ * function, with the attributes it reads standing for what the engine's stage passes down. The result is the same for everything that is a function
+ * of the vertex's own values and the material's (a tiling, a world position, a tangent frame), and costs a little more per pixel than per vertex.
+ * What it cannot reproduce is where the vertex is drawn: a displacement of the position is not applied, which the pass says.
+ */
+const VERTEX_PRELUDE = `// The vertex stage of the effect, run per fragment on what the engine's vertex stage hands down.
+struct StandardVertInput { highp vec4 position; vec3 normal; vec4 tangent; };
+#define CCGetWorldMatrix(m) m = mat4(1.0)
+#define CCGetWorldMatrixFull(m, mit) m = mat4(1.0); mit = mat4(1.0)
+#define CC_HANDLE_GET_CLIP_FLIP(p) p
+#define CCVertInput(In) CCDecode(In)
+void CCDecode(out StandardVertInput In) { In.position = vec4(a_position, 1.0); In.normal = a_normal; In.tangent = a_tangent; }
+void CCDecode(out vec4 position) { position = vec4(a_position, 1.0); }
+`;
+
+const ATTRIBUTE_SOURCES: Record<string, string> = {
+ a_position: 'cocos_position', a_normal: 'cocos_normal', a_tangent: 'cocos_tangent', a_texCoord: 'v_texCoord', a_color: 'v_color',
+ a_texCoord1: 'v_texCoord', a_uv0: 'v_texCoord',
+};
+
+/** The attributes the vertex stage always has: declared whether or not the program says so, since the prelude's decode reads them. */
+const STANDARD_ATTRIBUTES: Record<string, string> = { a_position: 'vec3', a_normal: 'vec3', a_texCoord: 'vec2', a_tangent: 'vec4' };
+
+/** The varyings the engine's vertex stage owns. */
+const ENGINE_VARYING_NAMES = new Set(['v_color', 'v_texCoord', 'v_worldPos', 'v_worldXYZ', 'v_worldNormal', 'v_worldTangent']);
+
+const TANGENT_VARYING = `#ifdef MESH_TANGENTS
+in highp vec4 v_worldTangent;
+#define cocos_tangent v_worldTangent
+#else
+#define cocos_tangent vec4(1.0, 0.0, 0.0, 1.0)
+#endif`;
+
+interface Emulated {
+ /** The program, as GLSL that declares what it wrote as globals and has the entry `cocos_vert`. */
+ source: string;
+ /** The attributes it reads, as globals. */
+ attributes: string;
+ /** The names the vertex program writes for the fragment. */
+ outputs: Set<string>;
+ /** The assignments that give the attributes it declares their values. */
+ attributeSetup: string[];
+ approximations: string[];
+}
+
+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;
+ // 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);
+ return '';
+ });
+ text = text.replace(/^[ \t]*(?:layout\s*\([^)]*\)\s*)?(?:flat\s+)?out\s+(?:(?:highp|mediump|lowp|flat)\s+)*(\w+)\s+(\w+)\s*;[ \t]*$/gm, (_m, type: string, name: string) => {
+ outputs.add(name);
+ return `${type} ${name};`;
+ });
+ const unknown = [...attributes.keys()].filter((a) => !(a in ATTRIBUTE_SOURCES) && !/^a_(?:matWorld|instanced|jointAnim|sh_|lightingMap|localShadow|vertexId|joints|weights|texCoord[2-9]|color1|dir|age)/.test(a));
+ if (unknown.length > 0) return { error: `its vertex program reads the attributes ${unknown.join(', ')}, which the engine's vertex stage does not carry` };
+ if (/\ba_sh_|\bCCGetSH|\bCC_USE_LIGHT_PROBE/.test(text)) return { error: 'its vertex program reads light-probe data, which a material does not get' };
+ if ([...attributes.keys()].some((a) => /^a_(?:joints|weights)$/.test(a))) return { error: 'its vertex program skins the mesh, which the converted material does not' };
+ if ([...attributes.keys()].some((a) => /^a_(?:matWorld|instanced|dir|age)/.test(a))) return { error: 'its vertex program reads per-instance data, which the engine\'s own instancing does not give a material' };
+ const entryRe = new RegExp(`\\bvec4\\s+${entry}\\s*\\(\\s*\\)`);
+ if (!entryRe.test(text)) return { error: `the vertex entry ${entry}() is not there` };
+ text = text.replace(entryRe, 'vec4 cocos_vert()');
+ if (/\bcc_(?:time)\b[^;]*\b(?:position|pos)\b|\b(?:position|pos)\b[^;]*\bcc_time\b/.test(text) || /\bsin\s*\([^;]*\b(?:position|pos)\b/.test(text)) {
+ approximations.push('a displacement of the vertex position is not applied');
+ }
+ const setup: string[] = [];
+ const declared: string[] = [];
+ for (const [name, type] of attributes) {
+ declared.push(`${type} ${name};`);
+ const source = ATTRIBUTE_SOURCES[name];
+ if (source) setup.push(` ${name} = ${source}${coerce(type, name)};`);
+ if (name === 'a_texCoord1') approximations.push('the second uv is the first until the engine carries one');
+ }
+ return { source: text, attributes: declared.join('\n'), outputs, attributeSetup: setup, approximations };
+}
+
+/** The conversion that makes an engine value of one width fit the type an attribute is declared with. */
+function coerce(type: string, name: string): string {
+ const width = { float: 1, vec2: 2, vec3: 3, vec4: 4 }[type] ?? 4;
+ const from = name === 'a_color' ? 4 : name === 'a_tangent' ? 4 : name === 'a_position' || name === 'a_normal' ? 3 : 2;
+ if (width === from) return '';
+ if (width < from) return `.${'xyzw'.slice(0, width)}`;
+ return '';
+}
+
+// ---------------------------------------------------------------------------
+// One pass
+// ---------------------------------------------------------------------------
+
/** Convert one pass. The effect's name is the shader's. */
-export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: string): PassConversion {
+export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: string, options: EffectOptions = {}): PassConversion {
const state = stateOf(pass);
- const vertex = customVertexReason(pass.vert, effect);
- if (vertex) return { shader: null, unsupported: vertex, state };
+ const fail = (unsupported: string): PassConversion => ({ shader: null, unsupported, state });
const [fragProgram, fragEntry = 'frag'] = pass.frag.split(':');
- const program = effect.programs.get(fragProgram ?? '');
- if (!program) return { shader: null, unsupported: `fragment program ${fragProgram} is not in the effect`, state };
+ const [vertProgram, vertEntry = 'vert'] = pass.vert.split(':');
+ const programText = (name: string | undefined): string | null => (name ? effect.programs.get(name)?.source ?? options.chunk?.(name) ?? null : null);
+ const fragSource = programText(fragProgram);
+ if (fragSource === null) return fail(`fragment program ${fragProgram} is not in the effect`);
+ // A vertex stage the Creator install supplies (`general-vs`, `legacy/main-functions/general-vs`) is the standard one: the engine's.
+ const vertSource = programText(vertProgram);
+ const standardVertex = vertSource === null
+ && /^(?:(?:general|unlit|sprite|standard)-vs|legacy\/main-functions\/(?:general|unlit|standard)-vs|builtin\/.*)$/.test(vertProgram ?? '');
+ if (vertSource === null && !standardVertex) return fail(`vertex program ${vertProgram} is not in the effect`);
const approximations: string[] = [];
const props = new Map(pass.properties.map((p) => [p.name, p]));
const params: ConvertedShader['params'] = [];
const declared = new Set<string>();
- let body = program.source;
-
- // The programs it includes: named by what they give, which the prelude gives.
- const includes = [...body.matchAll(/#include\s*<([^>]+)>/g)].map((m) => m[1]!);
- for (const inc of includes) {
- if (/fog-fs|output|embedded-alpha|alpha-test|common-define|cc-global|cc-local|gamma/.test(inc)) continue;
- return { shader: null, unsupported: `it includes ${inc}, which the engine has no counterpart for`, state };
+ const missing: string[] = [];
+ const seen = new Set<string>();
+
+ // The vertex program, carried into the fragment.
+ let vertex: Emulated | null = null;
+ let vertexText = '';
+ if (vertSource !== null) {
+ const expanded = expandIncludes(vertSource, effect, options, seen, missing, 0, PROVIDED_AND_STRUCTURE);
+ const emulated = emulateVertex(expanded, vertEntry);
+ if ('error' in emulated) return fail(emulated.error);
+ vertex = emulated;
+ vertexText = emulated.source;
+ approximations.push(...emulated.approximations);
}
- if (includes.some((i) => /fog-fs/.test(i))) approximations.push('fog is not applied');
+ let body = expandIncludes(fragSource, effect, options, seen, missing);
+ if (missing.length > 0) return fail(`it includes ${[...new Set(missing)].join(', ')}, which the engine has no counterpart for`);
+ if (/fog-fs|fog-vs/.test(fragSource + (vertSource ?? ''))) approximations.push('fog is not applied');
- body = body.replace(/^\s*#include\s*<[^>]+>\s*$/gm, '').replace(/^\s*precision\s+\w+\s+float\s*;\s*$/gm, '').replace(/^\s*#pragma\s+builtin\([^)]*\)\s*$/gm, '');
+ const strip = (text: string): string => text.replace(/^[ \t]*#[ \t]*pragma[ \t]+define[ \t]+(?!-meta)/gm, '#define ').replace(/^\s*#include\s*<[^>]+>\s*$/gm, '').replace(/^\s*precision\s+\w+\s+float\s*;\s*$/gm, '').replace(/^\s*#pragma\s+(?:builtin|define-meta|format)\([^)]*\)\s*$/gm, '').replace(/^\s*#pragma\s+define-meta[^\n]*$/gm, '');
+ body = strip(body);
+ vertexText = strip(vertexText);
- // The Constant block's members become parameters; the block goes.
- body = body.replace(/(?:layout\s*\([^)]*\)\s*)?uniform\s+(\w+)\s*\{([\s\S]*?)\}\s*;?/g, (_m, block: string, members: string) => {
+ // The Constant blocks' members become parameters, the vertex program's as well as the fragment's; the blocks go.
+ const blocks = (text: string): string => text.replace(/(?:layout\s*\([^)]*\)\s*)?uniform\s+(\w+)\s*\{([\s\S]*?)\}\s*;?/g, (_m, block: string, members: string) => {
if (block === 'AlphaTestConstants' || block === 'ALPHA_TEST_CONSTANTS') { members = 'float alphaThreshold;'; }
for (const decl of members.split(';')) {
const m = /^\s*(?:highp\s+|mediump\s+|lowp\s+)?(float|int|bool|vec2|vec3|vec4)\s+(\w+)\s*$/.exec(decl.replace(/\/\/[^\n]*/g, '').trim());
@@ -208,9 +333,11 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
}
return '';
});
+ body = blocks(body);
+ vertexText = blocks(vertexText);
// Samplers: `uniform sampler2D name;` is a texture parameter, bound by the engine.
- body = body.replace(/(?:layout\s*\([^)]*\)\s*)?uniform\s+(?:highp\s+|mediump\s+|lowp\s+)?sampler(\w+)\s+(\w+)\s*;/g, (_m, dim: string, name: string) => {
+ const samplers = (text: string): string => text.replace(/(?:layout\s*\([^)]*\)\s*)?uniform\s+(?:highp\s+|mediump\s+|lowp\s+)?sampler(\w+)\s+(\w+)\s*;/g, (_m, dim: string, name: string) => {
if (dim !== '2D') { approximations.push(`${name} is a sampler${dim}, which has no counterpart; it reads as a 2D texture`); }
if (!declared.has(name)) {
declared.add(name);
@@ -219,26 +346,56 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
}
return '';
});
+ body = samplers(body);
+ vertexText = samplers(vertexText);
- // Varyings: Cocos's names for what the engine's vertex stage supplies. One it does not supply is the pass's reason not to convert.
+ // Varyings: what the vertex program wrote is a global of the emulation; the rest are the engine's, under Cocos's names. A name that is neither is
+ // the pass's reason not to convert.
const needed = new Set<string>();
const renames = new Map<string, string>();
+ const produced = vertex?.outputs ?? new Set<string>();
body = body.replace(/^\s*in\s+(?:highp\s+|mediump\s+|lowp\s+)?(\w+)\s+(\w+)\s*;\s*$/gm, (_m, _type: string, name: string) => {
+ if (produced.has(name)) return '';
const v = VARYINGS[name];
if (v) { needed.add(v.glsl); renames.set(name, v.name); return ''; }
if (name === 'v_position' || name === 'v_worldPos') { needed.add(POSITION_VARYING); renames.set(name, 'cocos_position'); return ''; }
if (name === 'v_normal') { needed.add(NORMAL_VARYING); renames.set(name, 'cocos_normal'); return ''; }
+ if (name === 'v_tangent') { needed.add(TANGENT_VARYING); renames.set(name, 'cocos_tangent.xyz'); return ''; }
needed.add(`/* the vertex stage does not supply ${name} */`);
return '';
});
- const missing = [...needed].filter((n) => n.startsWith('/*'));
- if (missing.length > 0) return { shader: null, unsupported: `its fragment reads ${missing.map((m) => /supply (\w+)/.exec(m)![1]).join(', ')}, which the engine's vertex stage does not supply`, state };
+ const unsupplied = [...needed].filter((n) => n.startsWith('/*'));
+ if (unsupplied.length > 0) return fail(`its fragment reads ${unsupplied.map((m) => /supply (\w+)/.exec(m)![1]).join(', ')}, which the engine's vertex stage does not supply`);
for (const [from, to] of renames) if (from !== to) body = body.replace(new RegExp(`\\b${from}\\b`, 'g'), to);
+ // A vertex output named like one of the engine's varyings (`v_color`) is the emulation's own value, not the engine's: it goes by another name.
+ for (const name of produced) {
+ if (!ENGINE_VARYING_NAMES.has(name)) continue;
+ const own = `cocos_${name}`;
+ const re = new RegExp(`\\b${name}\\b`, 'g');
+ body = body.replace(re, own);
+ vertexText = vertexText.replace(re, own);
+ }
+ if (vertex) {
+ // The emulation reads the engine's values through these, so it needs them declared even where the fragment itself does not.
+ needed.add(VARYINGS.v_uv!.glsl);
+ needed.add(VARYINGS.v_color!.glsl);
+ needed.add(POSITION_VARYING);
+ needed.add(NORMAL_VARYING);
+ needed.add(TANGENT_VARYING);
+ }
const usesPosition = needed.has(POSITION_VARYING);
if (usesPosition) approximations.push('v_position is the world position of the draw: the engine gives the full position to meshes with normals, and the plane position to sprites');
// GLSL 1.00 spellings of the 3.00 functions, which Cocos's own compiler accepts.
- body = body.replace(/\btexture2D\s*\(/g, 'texture(').replace(/\btextureCube\s*\(/g, 'texture(').replace(/\btexture2DLodEXT\s*\(/g, 'textureLod(');
+ const modern = (text: string): string => text.replace(/\btexture2D\s*\(/g, 'texture(').replace(/\btextureCube\s*\(/g, 'texture(').replace(/\btexture2DLodEXT\s*\(/g, 'textureLod(');
+ body = modern(body);
+ vertexText = modern(vertexText);
+
+ // Fog and shadow transfers are statements with the arguments of whichever overload the effect uses, and GLSL ES has no variadic macros, so they are
+ // taken out as statements.
+ const noStatements = (text: string): string => text.replace(/\b(?:CC_APPLY_FOG|CC_TRANSFER_FOG|CC_TRANSFER_SHADOW|CC_TRANSFER_SH)\s*\([^;]*\)\s*;/g, '');
+ body = noStatements(body);
+ vertexText = noStatements(vertexText);
// An atlas whose alpha is kept apart (ETC1) is sampled by a helper; here a texture holds its own alpha, so it is a plain sample. Spelt out, not
// a macro: the engine's twin tool finds a batch texture by the form `texture(u_textures[N], …)`.
@@ -257,30 +414,37 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
cc_ambientSky: [1, 1, 1, 1], cc_ambientGround: [1, 1, 1, 1], cc_cameraPos: [0, 0, 0, 1], cc_nearFar: [0.1, 1000, 0, 0],
cc_screenSize: [1, 1, 1, 1], cc_exposure: [1, 1, 0, 0], cc_mainLitColor: [1, 1, 1, 1], cc_mainLitDir: [0, 0, -1, 0],
};
+ const everything = `${vertexText}\n${body}`;
for (const [name, value] of Object.entries(sceneValues)) {
- if (!new RegExp(`\\b${name}\\b`).test(body) || declared.has(name)) continue;
+ if (!new RegExp(`\\b${name}\\b`).test(everything) || declared.has(name)) continue;
declared.add(name);
params.push({ name, type: 'vec4', value });
approximations.push(`${name} is a parameter of the material, not the scene's`);
}
+ // The camera's matrices have no value at the fragment that means the same: the emulation's matrices are the identity.
+ const matrices = ['cc_matView', 'cc_matProj', 'cc_matViewProj', 'cc_matWorld', 'cc_matWorldIT', 'cc_matViewInv'].filter((m) => new RegExp(`\\b${m}\\b`).test(everything));
+ const matrixDefs = matrices.map((m) => `#define ${m} mat4(1.0)`).join('\n');
// Alpha test: a parameter and a discard, behind the switch the effect uses.
+ let alphaTest = '';
if (/\bALPHA_TEST\s*\(/.test(body)) {
if (!declared.has('alphaThreshold')) { declared.add('alphaThreshold'); params.push({ name: 'alphaThreshold', type: 'float', value: [0.5] }); }
- body = `#define ALPHA_TEST(c) if ((c).a < alphaThreshold) discard;\n${body}`;
+ alphaTest = '#define ALPHA_TEST(c) if ((c).a < alphaThreshold) discard;';
}
// The entry: `vec4 frag()` is the fragment's colour.
const entry = new RegExp(`\\bvec4\\s+${fragEntry}\\s*\\(\\s*\\)`);
- if (!entry.test(body)) return { shader: null, unsupported: `the fragment entry ${fragEntry}() is not there`, state };
+ if (!entry.test(body)) return fail(`the fragment entry ${fragEntry}() is not there`);
body = body.replace(entry, 'vec4 cocos_frag()');
// Cocos reads a macro that is not defined as 0 in `#if`; GLSL ES refuses an undefined name there. A switch is defined when it is on.
- body = body.replace(/^([ \t]*#[ \t]*(?:el)?if)([ \t]+.*)$/gm, (_m, directive: string, expr: string) => `${directive}${definedForm(expr)}`);
+ const definedFormed = (text: string): string => text.replace(/^([ \t]*#[ \t]*(?:el)?if)([ \t]+.*)$/gm, (_m, directive: string, expr: string) => `${directive}${definedForm(expr)}`);
+ body = definedFormed(body);
+ vertexText = definedFormed(vertexText);
// Macros: what the code branches on is a switch a material turns on.
- const found = macrosIn(body);
- const switches = found.switches.filter((s) => !/^(MESH_NORMALS|GL_ES|CC_[A-Z_]+_ENABLED)$/.test(s) && !found.compared.includes(s));
+ const found = macrosIn(`${vertexText}\n${body}`);
+ const switches = found.switches.filter((s) => !/^(MESH_NORMALS|MESH_TANGENTS|GL_ES|CC_[A-Z_]+_ENABLED)$/.test(s) && !found.compared.includes(s));
if (found.compared.length > 0) approximations.push(`${found.compared.join(', ')} is compared to a number; the engine's switches are on or off`);
const lines: string[] = [
@@ -294,8 +458,13 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
? `#pragma param ${p.name} texture default(${p.value})`
: `#pragma param ${p.name} ${p.type} default(${csv(p.value as number[])})`);
}
- lines.push('', '#pragma fragment', 'precision mediump float;', '', ...[...needed].filter((n) => !n.startsWith('/*')).map((n) => n), '',
- ...(usesSpriteTexture ? ['uniform sampler2D u_textures[8];'] : []), 'out vec4 fragColor;', '', PRELUDE, body.trim(), '', 'void main() {', ' fragColor = cocos_frag();', '}', '#pragma end', '');
+ const main = ['void main() {'];
+ if (vertex) main.push(...vertex.attributeSetup, ' cocos_vert();');
+ main.push(' fragColor = cocos_frag();', '}');
+ lines.push('', '#pragma fragment', 'precision mediump float;', '', ...[...needed].filter((n) => !n.startsWith('/*')), '',
+ ...(usesSpriteTexture ? ['uniform sampler2D u_textures[8];'] : []), 'out vec4 fragColor;', '', PRELUDE,
+ ...(matrixDefs ? [matrixDefs, ''] : []), ...(alphaTest ? [alphaTest, ''] : []),
+ ...(vertex ? [vertex.attributes, VERTEX_PRELUDE, vertexText.trim(), ''] : []), body.trim(), '', ...main, '#pragma end', '');
return { shader: { source: lines.join('\n'), switches, params, approximations }, unsupported: null, state };
}
@@ -313,11 +482,11 @@ export interface EffectConversion {
}
/** Convert every pass of an effect. @p stem names the shaders. */
-export function convertEffect(effect: CocosEffect, stem: string): EffectConversion {
+export function convertEffect(effect: CocosEffect, stem: string, options: EffectOptions = {}): EffectConversion {
return {
techniques: effect.techniques.map((t) => ({
name: t.name,
- passes: t.passes.map((p, i) => convertPass(effect, p, `${stem}.${t.name}.${i}`)),
+ passes: t.passes.map((p, i) => convertPass(effect, p, `${stem}.${t.name}.${i}`, options)),
})),
};
}
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index a4a0737e9..472a98983 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -13,7 +13,7 @@ import { copyFileSync, existsSync, linkSync, mkdirSync, readdirSync, readFileSyn
import path from 'node:path';
import { isInsideRoot } from '../../fs/pathSandbox';
import { buildScriptIndex } from './scripts';
-import { buildCocosAssetIndex, cocosEditorAssets, type CocosAssetEntry, type CocosAssetIndex } from './assets';
+import { buildCocosAssetIndex, cocosEditorAssets, INTERNAL_FOLDER, type CocosAssetEntry, type CocosAssetIndex } from './assets';
import type { AssetHints, SliceBorder } from './components';
import { convertPrefab, convertScene, type ConvertContext } from './convert';
import { parseCocosDocument, type CocosDocument } from './document';
@@ -30,7 +30,7 @@ import { convertEffect, isOutlinePass, type PassConversion } from './effectConve
* 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 = 2;
+export const COCOS_IMPORT_REVISION = 3;
/** 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 {
@@ -319,7 +319,8 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo
const progress = (stage: CocosImportProgress['stage'], done: number, total: number, item?: string): void => opts.onProgress?.({ stage, done, total, ...(item ? { item } : {}) });
progress('index', 0, 1);
const version = (() => { try { return (JSON.parse(readFileSync(path.join(source, 'package.json'), 'utf8')) as { creator?: { version?: string } }).creator?.version; } catch { return undefined; } })();
- const index = buildCocosAssetIndex(assetsDir, cocosEditorAssets(version));
+ const editorAssets = cocosEditorAssets(version);
+ const index = buildCocosAssetIndex(assetsDir, editorAssets);
const outRef = (assetRel: string): string => `assets/${into}/${assetRel}`;
const esRel = (assetRel: string, ext: string): string => assetRel.replace(/\.[^./]+$/, ext);
@@ -625,6 +626,14 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo
// A material of the project's own effect, taken across: the effect's pass becomes a shader of the project, the material's settings become a
// material of it, and the renderers that wore the Cocos material wear that one. Null when the pass cannot be given the engine's vertex stage.
+ // The chunks an effect includes: the project's own (by their path under the assets folder, or just their name), then the Creator install's.
+ const projectChunks = [...index.byUuid.values()].filter((e) => e.rel.endsWith('.chunk') && !e.rel.startsWith(`${INTERNAL_FOLDER}/`));
+ const chunkText = (name: string): string | null => {
+ const own = projectChunks.find((e) => { const rel = e.rel.replace(/\.chunk$/, ''); return rel === name || rel.endsWith(`/${name}`); });
+ if (own) return readFileSync(own.file, 'utf8');
+ const installed = editorAssets ? path.join(editorAssets, 'chunks', `${name}.chunk`) : null;
+ return installed && existsSync(installed) ? readFileSync(installed, 'utf8') : null;
+ };
type EffectTechnique = { name: string; passes: PassConversion[]; passCount: number; outline: boolean[] };
const effects = new Map<string, EffectTechnique[] | null>();
const convertedMaterials = new Map<string, { ref: string } | null>();
@@ -634,7 +643,7 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo
try {
const parsed = parseEffect(readFileSync(entry.file, 'utf8'));
const stem = entry.rel.replace(/^assets\//, '').replace(/\.effect$/, '');
- result = convertEffect(parsed, stem).techniques.map((t, i) => ({ name: t.name, passes: t.passes, passCount: t.passes.length, outline: parsed.techniques[i]!.passes.map(isOutlinePass) }));
+ result = convertEffect(parsed, stem, { chunk: chunkText }).techniques.map((t, i) => ({ name: t.name, passes: t.passes, passCount: t.passes.length, outline: parsed.techniques[i]!.passes.map(isOutlinePass) }));
} catch (err) {
report.note('effect-unreadable', `${entry.rel}: ${(err as Error).message.split('\n')[0]}`);
}
diff --git a/pipeline/tests/cocos-effect-convert.test.ts b/pipeline/tests/cocos-effect-convert.test.ts
index 69d55be4b..8189750bb 100644
--- a/pipeline/tests/cocos-effect-convert.test.ts
+++ b/pipeline/tests/cocos-effect-convert.test.ts
@@ -97,13 +97,13 @@ describe('converting a pass', () => {
expect(source).not.toMatch(/uniform sampler2D mainTexture/);
});
- it('takes the engine\'s vertex stage, and the varyings it supplies under their engine names', () => {
+ it('runs the effect vertex program ahead of the fragment, on what the engine vertex stage hands down', () => {
const source = pass.shader!.source;
expect(source).not.toContain('#pragma vertex');
expect(source).toContain('in vec2 v_texCoord;');
- expect(source).toContain('in vec4 v_color;');
- expect(source).toContain('texture(mainTexture, v_texCoord) * v_color');
- expect(source).toContain('fragColor = cocos_frag();');
+ expect(source).toContain('vec4 cocos_vert()');
+ expect(source).toContain('a_texCoord = v_texCoord;');
+ expect(source).toContain(' cocos_vert();\n fragColor = cocos_frag();');
});
it('makes the macros it branches on switches, asked as defined() because an undefined name is an error in GLSL ES', () => {
@@ -124,13 +124,20 @@ describe('converting a pass', () => {
});
});
-describe('a pass the engine\'s vertex stage cannot stand in for', () => {
+describe('a pass whose vertex program does more than place the vertex', () => {
const SWAY = SPRITE_ADD.replace('vec4 pos = cc_matViewProj * vec4(a_position, 1);', 'vec4 pos = cc_matViewProj * vec4(a_position + vec3(sin(cc_time.x), 0, 0), 1);');
- it('is not converted, and says what its vertex program does', () => {
+ it('is converted without the displacement, and says so', () => {
const swayed = convertEffect(parseEffect(SWAY), 'sway').techniques[0]!.passes[0]!;
- expect(swayed.shader).toBeNull();
- expect(swayed.unsupported).toMatch(/cc_time|displacement/);
+ expect(swayed.unsupported).toBeNull();
+ expect(swayed.shader!.approximations).toContain('a displacement of the vertex position is not applied');
+ });
+
+ it('is not converted when its vertex program skins the mesh', () => {
+ const skinned = SPRITE_ADD.replace('in vec4 a_color;', 'in vec4 a_color;\n in vec4 a_joints;');
+ const converted = convertEffect(parseEffect(skinned), 'skin').techniques[0]!.passes[0]!;
+ expect(converted.shader).toBeNull();
+ expect(converted.unsupported).toMatch(/a_joints|skins/);
});
it('is not converted when its fragment reads something the vertex stage does not supply', () => {