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): a particle effect is converted on the engine particle stage, and a particle takes the converted material

yhh7fc2d1b1ec3a
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.ts
index 423230580..87e656811 100644
--- a/pipeline/src/import/cocos/effectConvert.ts
+++ b/pipeline/src/import/cocos/effectConvert.ts
@@ -79,6 +79,7 @@ function stateOf(pass: EffectPass): PassConversion['state'] {
const VARYINGS: Record<string, { name: string; glsl: string; mesh?: boolean }> = {
v_uv: { name: 'v_texCoord', glsl: 'in vec2 v_texCoord;' },
uv0: { name: 'v_texCoord', glsl: 'in vec2 v_texCoord;' },
+ uv: { name: 'v_texCoord', glsl: 'in vec2 v_texCoord;' },
v_color: { name: 'v_color', glsl: 'in vec4 v_color;' },
color: { name: 'v_color', glsl: 'in vec4 v_color;' },
};
@@ -291,6 +292,9 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
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`);
+ // A particle's vertex program builds the billboard from the emitter's own data, which is what the engine's particle stage does: that stage is the
+ // one this pass is drawn with, and only its fragment is converted. The fragment's `uv` and `color` are what the particle stage hands down.
+ const particleVertex = /particle|trail/i.test(vertProgram ?? '');
const approximations: string[] = [];
const props = new Map(pass.properties.map((p) => [p.name, p]));
@@ -302,7 +306,7 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
// The vertex program, carried into the fragment.
let vertex: Emulated | null = null;
let vertexText = '';
- if (vertSource !== null) {
+ if (vertSource !== null && !particleVertex) {
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);
@@ -355,6 +359,7 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
// the pass's reason not to convert.
const needed = new Set<string>();
const renames = new Map<string, string>();
+ const mapped = new Set<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 '';
@@ -362,10 +367,15 @@ export function convertPass(effect: CocosEffect, pass: EffectPass, shaderName: s
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 ''; }
+ // What a particle's own vertex program computes for its fragment, stood for by what the engine's particle stage gives: the mask's uv is the uv.
+ if (name === 'uvmask') { needed.add(VARYINGS.v_uv!.glsl); renames.set(name, 'v_texCoord'); mapped.add('uvmask is the uv'); return ''; }
+ if (name === 'mPosition') { needed.add(POSITION_VARYING); renames.set(name, 'cocos_position'); return ''; }
+ if (name === 'mNormal') { 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 '';
});
+ for (const m of mapped) approximations.push(m);
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);
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index bc7de7055..b20fb1713 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -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 = 4;
+export const COCOS_IMPORT_REVISION = 5;
/** 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/src/import/cocos/particle.ts b/pipeline/src/import/cocos/particle.ts
index eb0492742..633492c6b 100644
--- a/pipeline/src/import/cocos/particle.ts
+++ b/pipeline/src/import/cocos/particle.ts
@@ -19,6 +19,7 @@ export interface ParticleContext {
report: ImportReport;
/** A material's main texture and whether it blends additively; null when the project has no such material. */
material?(uuid: string): { texture: CocosAssetEntry | null; additive: boolean } | null;
+ convertedMaterial?(uuid: string): { ref: string } | null;
assetRef(entry: CocosAssetEntry): string;
}
@@ -267,6 +268,9 @@ export function particleSystem(c: { enabled: boolean; data: Raw }, ctx: Particle
if (material) {
if (material.texture) out.texture = ctx.assetRef(material.texture);
out.blendMode = material.additive ? 1 : 0;
+ // The project's own effect for it, when it was converted: its fragment draws the particle.
+ const converted = typeof matUuid === 'string' ? ctx.convertedMaterial?.(matUuid) ?? null : null;
+ if (converted) out.material = converted.ref;
} else {
lose('particle-no-material');
}