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
feat(cocos): a mesh of several primitives is drawn whole with the material of each slot, an effect's outline pass is drawn as a second renderer, skinned parts take their material override, and a macro that defines functions is no longer cut up by the pruning
yhh073140755b6f
diff --git a/pipeline/src/import/cocos/components.ts b/pipeline/src/import/cocos/components.tsindex afb3fda46..2500277f8 100644--- a/pipeline/src/import/cocos/components.ts+++ b/pipeline/src/import/cocos/components.ts@@ -63,7 +63,7 @@ export interface ComponentContext {material?(uuid: string): MaterialInfo | null;/** The Estella material a Cocos material of the project's own effect became (its shader converted), by its project path; null when the effect could not be converted. *//** The converted material for a project material, and whether it blends (its effect's pass says so, which the material file does not). */- convertedMaterial?(uuid: string): { ref: string; blends?: boolean } | null;+ convertedMaterial?(uuid: string): { ref: string; blends?: boolean; outline?: string } | null;/*** The engine MeshRenderer data of the part of a model a Cocos mesh sub-asset names, once that model was brought in. `exact` is false when* the part was picked because the model draws only one.@@ -537,7 +537,7 @@ function findByName(root: CocosNode, name: string): CocosNode | null {* `cc.SkinnedMeshRenderer`: a part of a model bound to bones that are nodes of this document. The mesh is the model's own; the joints are the* nodes under the skinning root that carry the names the model's skin binds, in the order the model's vertices index them.*/-function skinnedMeshRenderer(c: CocosComponent, ctx: ComponentContext, where: string): { component: ComponentData; extra: ComponentData[] } | null {+function skinnedMeshRenderer(c: CocosComponent, ctx: ComponentContext, where: string): { component: ComponentData; extra: ComponentData[]; parts: ComponentData[][] } | null {const mesh = meshRenderer(c, ctx, where);if (!mesh) return null;const meshUuid = uuidOf(c.data._mesh);@@ -547,7 +547,7 @@ function skinnedMeshRenderer(c: CocosComponent, ctx: ComponentContext, where: stif (!names || names.length === 0 || !root) {ctx.report.component('cc.SkinnedMeshRenderer', 'partial');ctx.report.note('skin-not-bound', where);- return { component: mesh.component, extra: [] };+ return { component: mesh.component, extra: [], parts: (mesh.more ?? []).map((m) => [m]) };}const joints = names.map((name) => {const node = findByName(root, name);@@ -556,7 +556,7 @@ function skinnedMeshRenderer(c: CocosComponent, ctx: ComponentContext, where: stconst missing = names.filter((name) => !findByName(root, name)).length;ctx.report.component('cc.SkinnedMeshRenderer', missing > 0 || mesh.partial ? 'partial' : 'mapped');if (missing > 0) ctx.report.note('skin-joint-missing', `${where}: ${missing} of ${names.length}`);- return { component: mesh.component, extra: [{ type: 'MeshSkin', data: { joints } }] };+ return { component: mesh.component, extra: [{ type: 'MeshSkin', data: { joints } }], parts: (mesh.more ?? []).map((m) => [m, { type: 'MeshSkin', data: { joints: structuredClone(joints) } }]) };}/** `cc.Animation`: the clip it plays by default, as a timeline that plays on the node, from the start if the node asks. */@@ -627,15 +627,16 @@ const ENGINE_PRIMITIVE_SIZE = 100;** @returns `found`: the project had the material; `exact`: it was one of the built-in effects, so nothing of its look was approximated.*/-function applyMaterial(data: Raw, c: CocosComponent, ctx: ComponentContext, where: string): { found: boolean; exact: boolean } {- const uuids = (Array.isArray(c.data._materials) ? c.data._materials : []).map(uuidOf).filter(Boolean);- const info = uuids[0] ? ctx.material?.(uuids[0]) ?? null : null;+function applyMaterial(data: Raw, c: CocosComponent, ctx: ComponentContext, where: string, slot = 0): { found: boolean; exact: boolean; outline?: string } {+ const uuids = (Array.isArray(c.data._materials) ? c.data._materials : []).map(uuidOf);+ const uuid = uuids[slot] ?? '';+ const info = uuid ? ctx.material?.(uuid) ?? null : null;if (!info) {- if (uuids.length > 0) ctx.report.note('mesh-material-missing', where);+ if (uuid !== '') ctx.report.note('mesh-material-missing', where);return { found: false, exact: false };}delete data.material;- const converted = ctx.convertedMaterial?.(uuids[0]!);+ const converted = ctx.convertedMaterial?.(uuid);if (converted) data.material = converted.ref;if (info.texture) data.texture = ctx.assetRef(info.texture);if (info.normal) data.normalMap = ctx.assetRef(info.normal);@@ -648,13 +649,9 @@ function applyMaterial(data: Raw, c: CocosComponent, ctx: ComponentContext, wher// 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 blackif (converted?.blends !== undefined) data.opaque = !converted.blends;data.cullBackfaces = info.cullBack;- let exact = info.builtinEffect || converted !== null && converted !== undefined;+ const exact = info.builtinEffect || converted !== null && converted !== undefined;if (!exact) ctx.report.note('mesh-custom-effect', where);- if (uuids.length > 1) {- exact = false;- ctx.report.note('mesh-several-materials', where);- }- return { found: true, exact };+ return { found: true, exact, ...(converted?.outline ? { outline: converted.outline } : {}) };}/**@@ -671,7 +668,7 @@ export function meshMaterialFields(uuid: string, ctx: ComponentContext, where: s}/** `cc.MeshRenderer`: the mesh is the model's own part, drawn with the model's own material until the Cocos material is carried across. */-function meshRenderer(c: CocosComponent, ctx: ComponentContext, where: string): { component: ComponentData; scale?: number; partial: boolean } | null {+function meshRenderer(c: CocosComponent, ctx: ComponentContext, where: string): { component: ComponentData; scale?: number; partial: boolean; more?: ComponentData[] } | null {const meshUuid = uuidOf(c.data._mesh);const hasMaterial = Array.isArray(c.data._materials) && c.data._materials.some((m) => uuidOf(m) !== '');if (meshUuid.startsWith(COCOS_PRIMITIVES)) {@@ -693,26 +690,33 @@ function meshRenderer(c: CocosComponent, ctx: ComponentContext, where: string):const applied = applyMaterial(data, c, ctx, where);return { component: { type: 'MeshRenderer', data }, partial: meshUuid !== '' || !applied.exact && hasMaterial };}- const materials = (Array.isArray(c.data._materials) ? c.data._materials : []).map(uuidOf).filter(Boolean);+ const materials = (Array.isArray(c.data._materials) ? c.data._materials : []).map(uuidOf);let partial = false;- if (found.components.length > 1) {- partial = true;- ctx.report.note('mesh-several-primitives', where);- }if (!found.exact) {partial = true;ctx.report.note('mesh-guessed-from-model', where);}- const data: Raw = { ...found.components[0]!, enabled: c.enabled };- if (materials.length > 0) {- const applied = applyMaterial(data, c, ctx, where);- if (!applied.exact) partial = true;- if (!applied.found) {- partial = true;- ctx.report.note('mesh-material-from-model', where);+ // A mesh of several primitives is drawn by one renderer in Cocos, each primitive with the material of its own slot; here each is a renderer, the+ // first on this node and the others on parts that stand with it.+ const outlines: ComponentData[] = [];+ const renderers = found.components.map((part, slot) => {+ const data: Raw = { ...part, enabled: c.enabled };+ if (materials.some((m) => m !== '')) {+ const applied = applyMaterial(data, c, ctx, where, slot);+ if (!applied.exact) partial = true;+ if (!applied.found) {+ partial = true;+ ctx.report.note('mesh-material-from-model', where);+ }+ // An outline is the same mesh drawn a second time, pushed out along its normals and shown from behind: a renderer of its own+ if (applied.outline) {+ outlines.push({ type: 'MeshRenderer', data: { mesh: part.mesh, enabled: c.enabled, material: applied.outline, lit: false, opaque: true, cullBackfaces: false } });+ }}- }- return { component: { type: 'MeshRenderer', data }, partial };+ return { type: 'MeshRenderer', data } as ComponentData;+ });+ const more = [...renderers.slice(1), ...outlines];+ return { component: renderers[0]!, partial, ...(more.length > 0 ? { more } : {}) };}/** `cc.Graphics`: the style a scene stores; what is drawn is what scripts draw with it. */@@ -871,6 +875,8 @@ const FOLDED = new Set(['cc.UITransform', 'cc.LabelOutline', 'cc.LabelShadow', 'export interface ConvertedNode {components: ComponentData[];+ /** The primitives of a mesh beyond the first, each as the components of a part that stands with this node. */+ parts?: ComponentData[][];/** Everything not mapped, verbatim, for whatever consumes it next. */metadata?: Raw;}@@ -886,6 +892,7 @@ export function convertNode(node: CocosNode, ctx: ComponentContext, where: strinlet unitScale = 1;// components a mapped one brings with it, added after the loopconst extra: ComponentData[] = [];+ const parts: ComponentData[][] = [];for (const c of node.components) {let out: ComponentData | null = null;@@ -952,12 +959,16 @@ export function convertNode(node: CocosNode, ctx: ComponentContext, where: strinout = mesh?.component ?? null;if (mesh) ctx.report.component('cc.MeshRenderer', mesh.partial ? 'partial' : 'mapped');if (mesh?.scale !== undefined) unitScale = mesh.scale;+ if (mesh?.more) parts.push(...mesh.more.map((m) => [m]));break;}case 'cc.SkinnedMeshRenderer': {const skinned = skinnedMeshRenderer(c, ctx, where);out = skinned?.component ?? null;- if (skinned) extra.push(...skinned.extra);+ if (skinned) {+ extra.push(...skinned.extra);+ parts.push(...skinned.parts);+ }break;}case 'cc.animation.AnimationController':@@ -1043,7 +1054,7 @@ export function convertNode(node: CocosNode, ctx: ComponentContext, where: strin// outside the mask of a UI camera (layer 25) and never drawn by it.const layerBit = node.layer > 0 && (node.layer & (node.layer - 1)) === 0 ? Math.log2(node.layer) : -1;if (layerBit >= 0 && layerBit < 32) {- for (const c of [...components, ...extra]) if (LAYERED.has(c.type) && !(c.data.layer)) c.data.layer = layerBit;+ for (const c of [...components, ...extra, ...parts.flat()]) if (LAYERED.has(c.type) && !(c.data.layer)) c.data.layer = layerBit;}components.push(...extra);if (unitScale !== 1) {@@ -1056,5 +1067,5 @@ export function convertNode(node: CocosNode, ctx: ComponentContext, where: strinif (box) cocos.ui = { width: box.width, height: box.height, anchorX: box.ax, anchorY: box.ay };if (node.layer !== 33554432 && node.layer !== 0) cocos.layer = node.layer;const metadata = Object.keys(cocos).length ? { cocos } : undefined;- return metadata ? { components, metadata } : { components };+ return { components, ...(parts.length > 0 ? { parts } : {}), ...(metadata ? { metadata } : {}) };}diff --git a/pipeline/src/import/cocos/convert.ts b/pipeline/src/import/cocos/convert.tsindex 96bf77fd7..107b84396 100644--- a/pipeline/src/import/cocos/convert.ts+++ b/pipeline/src/import/cocos/convert.ts@@ -339,7 +339,9 @@ function modelInstanceOverrides(node: CocosNode, model: { rootId: string; name:out.push({ prefabEntityId: part, type: 'property', componentType: 'MeshRenderer', propertyName, value });}}- const inner = inst.overrides.length - roots.length - animation.used - materials;+ // where a skinned part finds its skeleton is the model's own: the engine's import binds the part to its joints+ const skeleton = inst.overrides.filter((o) => o.path[0] === '_skinningRoot').length;+ const inner = inst.overrides.length - roots.length - animation.used - materials - skeleton;if (inner > 0) ctx.report.note('model-instance-overrides', `${ctx.where}: ${node.name} (${inner} on the model's own parts)`);if (animation.clip) {const timeline = ctx.clipOn?.(animation.clip, model.paths, `${ctx.where}: ${node.name}`);@@ -440,6 +442,11 @@ function indexNodes(roots: CocosNode[]): Map<string, CocosNode> {return nodes;}+/** A part that stands with its node, where the node stands. */+function partTransform(): ComponentData {+ return { type: 'Transform', data: { position: { x: 0, y: 0, z: 0 }, rotation: { x: 0, y: 0, z: 0, w: 1 }, scale: { x: 1, y: 1, z: 1 } } };+}+export function convertPrefab(doc: CocosDocument, ctx: ConvertContext): PrefabFile {ctx.nodeById = indexNodes(doc.roots);const root = doc.roots[0];@@ -467,6 +474,12 @@ export function convertPrefab(doc: CocosDocument, ctx: ConvertContext): PrefabFiconst conv = convertNode(n, ctx, here);entity.components = conv.components;if (conv.metadata) entity.metadata = conv.metadata;+ // the primitives of a mesh beyond the first stand with it, each a part of its own+ for (const [i, part] of (conv.parts ?? []).entries()) {+ const id = `${n.id}|part${i + 1}`;+ entities.push({ prefabEntityId: id, name: `${n.name}_${i + 1}`, parent: n.id, children: [], components: [partTransform(), ...part], visible: true });+ entity.children.push(id);+ }for (const c of n.children) {entity.children.push(c.id);walk(c, n.id);@@ -548,6 +561,11 @@ export function convertScene(doc: CocosDocument, ctx: ConvertContext): SceneFileif ('metadata' in conv && conv.metadata) entity.metadata = conv.metadata;entities.push(entity);parent?.children.push(id);+ for (const [i, part] of ('parts' in conv ? conv.parts ?? [] : []).entries()) {+ const partId = next++;+ entities.push({ id: partId, name: `${n.name}_${i + 1}`, parent: id, children: [], components: [partTransform(), ...part], visible: true });+ entity.children.push(partId);+ }if (!n.instance) for (const c of n.children) walk(c, entity);};for (const r of doc.roots) walk(r, null);diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.tsindex bf683856c..1af2a5782 100644--- a/pipeline/src/import/cocos/effectConvert.ts+++ b/pipeline/src/import/cocos/effectConvert.ts@@ -125,11 +125,22 @@ export function pruneUnreachable(source: string, roots: readonly string[]): strilet depth = 0;let open: { name: string; start: number } | null = null;const header = /(?:^|\n)[ \t]*(?:(?:highp|mediump|lowp|const|inout|in|out)\s+)*\w+(?:\[\d*\])?\s+(\w+)\s*\([^;{}]*\)\s*\{/y;- for (let i = 0; i < source.length; i++) {- const c = source[i]!;+ // What a macro defines (a directive whose lines end in a backslash) is not a function of the shader: its text is the macro's to expand, and cutting+ // a function out of it leaves the macro running on into the lines after it. It is blanked for the reading of the structure, same length.+ const structure = ((): string => {+ const lines = source.split('\n');+ let inDirective = false;+ return lines.map((line) => {+ const starts = inDirective || /^[ \t]*#/.test(line);+ inDirective = starts && /\\[ \t]*$/.test(line);+ return starts ? line.replace(/[^\n]/g, ' ') : line;+ }).join('\n');+ })();+ for (let i = 0; i < structure.length; i++) {+ const c = structure[i]!;if (depth === 0 && !open && (c === '\n' || i === 0)) {header.lastIndex = i;- const m = header.exec(source);+ const m = header.exec(structure);if (m && !/^(?:if|for|while|switch|return)$/.test(m[1]!)) {const braceAt = i + m[0].length - 1;open = { name: m[1]!, start: i === 0 && source[0] !== '\n' ? 0 : i + 1 };diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.tsindex 8b0d39318..10128dac9 100644--- a/pipeline/src/import/cocos/importProject.ts+++ b/pipeline/src/import/cocos/importProject.ts@@ -27,13 +27,14 @@ import type { MaterialInfo } from './components';import { parseEffect, type CocosEffect, type EffectPass } from './effects';import { convertPass, isOutlinePass, type PassConversion } from './effectConvert';import { dominantAmbient } from './sceneAmbient';+import { OUTLINE_SHADER } from './outlineShader';/*** The revision of what an import writes. It is recorded in the state file; a converted project whose record is older was written by an* 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 = 25;+export const COCOS_IMPORT_REVISION = 27;/** 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 {@@ -740,7 +741,7 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impotype EffectTechnique = { name: string; passes: EffectPass[]; outline: boolean[] };type ParsedEffect = { effect: CocosEffect; techniques: EffectTechnique[] };const effects = new Map<string, ParsedEffect | null>();- const convertedMaterials = new Map<string, { ref: string; blends?: boolean } | null>();+ const convertedMaterials = new Map<string, { ref: string; blends?: boolean; outline?: string } | null>();const effectOf = (entry: CocosAssetEntry): ParsedEffect | null => {if (effects.has(entry.file)) return effects.get(entry.file) ?? null;let result: ParsedEffect | null = null;@@ -820,9 +821,39 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo// what was converted before the check is converted again with its answershaders.clear();};- const convertedMaterial = (materialUuid: string): { ref: string; blends?: boolean } | null => {+ /**+ * The outline a material asks for, as a material of its own: the shared outline shader with the width and colour the Cocos material gave its outline+ * pass. Null when the effect has none, or the material does not turn it on.+ */+ const outlineMaterial = (mat: CocosAssetEntry, json: MaterialJson, technique: EffectTechnique): string | null => {+ const at = technique.outline.findIndex(Boolean);+ const pass = at >= 0 ? technique.passes[at] : undefined;+ if (!pass) return null;+ const on = pass.inherits ? (json._defines?.[at] ?? {})[pass.inherits] : true;+ if (on !== true && on !== 1 && on !== 'true') return null;+ const props = (json._props?.[at] ?? {}) as Record<string, unknown>;+ const declared = (name: string): unknown => pass.properties.find((p) => p.name === name)?.value;+ const width = numberOf(props.lineWidth ?? declared('lineWidth'), 10);+ const bias = numberOf(props.depthBias ?? declared('depthBias'), 0);+ const given = (props.baseColor ?? declared('baseColor')) as Record<string, unknown> | number[] | undefined;+ const colour = Array.isArray(given)+ ? { r: numberOf(given[0], 0), g: numberOf(given[1], 0), b: numberOf(given[2], 0), a: 1 }+ : given && typeof given === 'object' && 'r' in given+ ? { r: numberOf(given.r, 0) / 255, g: numberOf(given.g, 0) / 255, b: numberOf(given.b, 0) / 255, a: 1 }+ : { r: 0, g: 0, b: 0, a: 1 };+ const shaderRel = outRef('effects/cocos-outline.esshader');+ writeText(shaderRel, OUTLINE_SHADER);+ const materialRel = outRef(`materials/${mat.rel.replace(/^assets\//, '').replace(/\.mtl$/, '')}.outline.esmaterial`);+ write(materialRel, {+ version: '1.0', type: 'material', shader: path.posix.relative(path.posix.dirname(materialRel), shaderRel),+ blendMode: 9, depthTest: true, depthWrite: true, cull: 2,+ properties: { lineWidth: width, depthBias: bias, baseColor: colour },+ });+ return materialRel;+ };+ const convertedMaterial = (materialUuid: string): { ref: string; blends?: boolean; outline?: string } | null => {if (convertedMaterials.has(materialUuid)) return convertedMaterials.get(materialUuid) ?? null;- let result: { ref: string; blends?: boolean } | null = null;+ let result: { ref: string; blends?: boolean; outline?: string } | null = null;try {const mat = index.byUuid.get(materialUuid);if (mat && mat.rel.endsWith('.mtl')) {@@ -911,6 +942,8 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo});report.note('effect-converted', `${effectEntry.rel} (${technique.name})`);result = { ref: materialRel, blends: blendMode !== 9 };+ const outline = outlineMaterial(mat, json, technique);+ if (outline) result.outline = outline;}}}diff --git a/pipeline/src/import/cocos/models.ts b/pipeline/src/import/cocos/models.tsindex a960c2675..ade76106e 100644--- a/pipeline/src/import/cocos/models.ts+++ b/pipeline/src/import/cocos/models.ts@@ -115,7 +115,7 @@ export class CocosModels {try {const objs = JSON.parse(readFileSync(file, 'utf8')) as Raw[];for (const o of objs) {- if (!isObj(o) || o.__type__ !== 'cc.MeshRenderer') continue;+ if (!isObj(o) || (o.__type__ !== 'cc.MeshRenderer' && o.__type__ !== 'cc.SkinnedMeshRenderer')) continue;const info = isObj(o.__prefab) && typeof o.__prefab.__id__ === 'number' ? objs[o.__prefab.__id__] : null;const node = isObj(o.node) && typeof o.node.__id__ === 'number' ? objs[o.node.__id__] : null;if (isObj(info) && typeof info.fileId === 'string' && isObj(node) && typeof node._name === 'string') found.set(info.fileId, node._name);diff --git a/pipeline/src/import/cocos/outlineShader.ts b/pipeline/src/import/cocos/outlineShader.tsnew file mode 100644index 000000000..7c04c3d26--- /dev/null+++ b/pipeline/src/import/cocos/outlineShader.ts@@ -0,0 +1,38 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The outline a Cocos effect draws as a pass of its own.+ *+ * Cocos draws such a mesh twice: first pushed out along its normals and shown from behind (a front-face cull), in one colour; then as the surface. The+ * engine has one material to a renderer, so the pass is a material of its own on a second renderer that stands with the first, drawn with this shader.+ * The push is in the mesh's own space, by the width the Cocos effect gives in view space, which is about the same for a model drawn near its own size.+ */+export const OUTLINE_SHADER = `#pragma shader "cocos-outline"+#pragma version 300 es+#pragma domain Lit+#pragma feature MESH_NORMALS+#pragma param lineWidth float default(10)+#pragma param depthBias float default(0)+#pragma param baseColor color default(0,0,0,1)++#pragma vertexoffset+void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {+ float len = length(localNormal);+ vec3 dir = len > 0.0001 ? localNormal / len : vec3(0.0);+ local += dir * (lineWidth * 0.001);+}+#pragma end++#pragma fragment+precision mediump float;+out vec4 fragColor;++void main() {+#ifdef ES_LINEAR+ fragColor = vec4(baseColor.rgb, 1.0);+#else+ fragColor = vec4(linearToSrgb(baseColor.rgb), 1.0);+#endif+}+#pragma end+`;diff --git a/pipeline/tests/cocos-effect-convert.test.ts b/pipeline/tests/cocos-effect-convert.test.tsindex d33f40081..82050b4fc 100644--- a/pipeline/tests/cocos-effect-convert.test.ts+++ b/pipeline/tests/cocos-effect-convert.test.ts@@ -220,4 +220,23 @@ describe('what an entry does not reach', () => {const source = ['float a() { return 1.0; }', '#define USE_A a()', 'vec4 cocos_frag() { return vec4(USE_A); }'].join('\n');expect(pruneUnreachable(source, ['cocos_frag'])).toContain('float a()');});++ it('leaves a macro that defines functions as it is, for the preprocessor to expand', () => {+ const source = [+ '#define DEFINE_PACK(type) \\',+ 'highp type unpackData (type a, type b) {\\',+ ' return a + b;\\',+ '}\\',+ 'void packData (out type a, highp type d) {\\',+ ' a = fract(d);\\',+ '}',+ '',+ 'DEFINE_PACK(float)',+ 'vec4 cocos_frag() { return vec4(1.0); }',+ ].join('\n');+ const pruned = pruneUnreachable(source, ['cocos_frag']);+ expect(pruned).toContain('return a + b;\\');+ expect(pruned).toContain('a = fract(d);\\');+ expect(pruned).toContain('DEFINE_PACK(float)');+ });});diff --git a/pipeline/tests/cocos-import-multi-material.test.ts b/pipeline/tests/cocos-import-multi-material.test.tsnew file mode 100644index 000000000..e0ce22d54--- /dev/null+++ b/pipeline/tests/cocos-import-multi-material.test.ts@@ -0,0 +1,103 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A Cocos mesh of two primitives with a material in each slot is drawn whole: the first primitive on the node, the second on a part that+ * stands with it, each with the material of its own slot.+ */+import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';+import { importCocosProject } from '../src/import/cocos';++const MODEL = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';+const PREFAB = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';+const MAT_A = 'dddddddd-dddd-4ddd-8ddd-dddddddddddd';+const MAT_B = 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee';+const STANDARD = 'a3cd009f-0ab0-420d-9278-b9fdab939bbc';++const vec = (x: number, y: number, z = 0) => ({ __type__: 'cc.Vec3', x, y, z });+const ident = { __type__: 'cc.Quat', x: 0, y: 0, z: 0, w: 1 };++/** One mesh of two triangles, each its own primitive, each with its own glTF material. */+function twoPrimitiveGltf(): string {+ const buffer = Buffer.alloc(80);+ [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1].forEach((v, i) => buffer.writeFloatLE(v, i * 4));+ [0, 1, 2, 0, 1, 2].forEach((v, i) => buffer.writeUInt16LE(v, 72 + (i % 3) * 2 + Math.floor(i / 3) * 0));+ return JSON.stringify({+ asset: { version: '2.0' }, scene: 0, scenes: [{ nodes: [0] }],+ nodes: [{ name: 'pair', mesh: 0 }],+ materials: [{ name: 'a' }, { name: 'b' }],+ meshes: [{ name: 'pair', primitives: [+ { attributes: { POSITION: 0 }, indices: 2, material: 0 },+ { attributes: { POSITION: 1 }, indices: 2, material: 1 },+ ] }],+ accessors: [+ { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 0], max: [1, 1, 0] },+ { bufferView: 1, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 1], max: [1, 1, 1] },+ { bufferView: 2, componentType: 5123, count: 3, type: 'SCALAR' },+ ],+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: 36 }, { buffer: 0, byteOffset: 36, byteLength: 36 }, { buffer: 0, byteOffset: 72, byteLength: 6 }],+ buffers: [{ byteLength: 80, uri: `data:application/octet-stream;base64,${buffer.toString('base64')}` }],+ });+}++const prefab = [+ { __type__: 'cc.Prefab', _name: 'pair', data: { __id__: 1 } },+ { __type__: 'cc.Node', _name: 'Pair', _parent: null, _children: [], _active: true, _components: [{ __id__: 2 }], _prefab: { __id__: 3 }, _lpos: vec(0, 0), _lrot: ident, _lscale: vec(1, 1, 1), _layer: 1073741824 },+ { __type__: 'cc.MeshRenderer', node: { __id__: 1 }, _enabled: true, __prefab: { __id__: 4 }, _materials: [{ __uuid__: MAT_A }, { __uuid__: MAT_B }], _mesh: { __uuid__: `${MODEL}@00a01` } },+ { __type__: 'cc.PrefabInfo', root: { __id__: 1 }, asset: { __id__: 0 }, fileId: 'pairRoot', instance: null },+ { __type__: 'cc.CompPrefabInfo', fileId: 'pairMesh' },+];++const meta = (uuid: string, importer: string, extra: object = {}) => ({ ver: '1', importer, uuid, subMetas: {}, userData: {}, ...extra });+const material = (r: number) => ({+ __type__: 'cc.Material', _effectAsset: { __uuid__: STANDARD }, _techIdx: 0,+ _props: [{ mainColor: { __type__: 'cc.Color', r, g: 0, b: 0, a: 255 } }],+ _states: [{ rasterizerState: { cullMode: 2 } }],+});++let root: string;+let out: string;++beforeAll(async () => {+ root = mkdtempSync(path.join(tmpdir(), 'cocos-multi-'));+ const src = path.join(root, 'creator');+ out = path.join(root, 'estella');+ const put = (rel: string, v: unknown): void => {+ const f = path.join(src, 'assets', rel);+ mkdirSync(path.dirname(f), { recursive: true });+ writeFileSync(f, typeof v === 'string' ? v : JSON.stringify(v));+ };+ put('models/pair/pair.gltf', twoPrimitiveGltf());+ put('models/pair/pair.gltf.meta', meta(MODEL, 'gltf', {+ subMetas: { '00a01': { importer: 'gltf-mesh', uuid: `${MODEL}@00a01`, name: 'pair.mesh', userData: { gltfIndex: 0 } } },+ }));+ put('mat/a.mtl', material(255));+ put('mat/a.mtl.meta', meta(MAT_A, 'material'));+ put('mat/b.mtl', material(10));+ put('mat/b.mtl.meta', meta(MAT_B, 'material'));+ put('props/pair.prefab', prefab);+ put('props/pair.prefab.meta', meta(PREFAB, 'prefab'));+ await importCocosProject({ source: src, out, only: ['props/'] });+});++afterAll(() => rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }));++describe('a mesh of two primitives', () => {+ it('is two renderers, one on the node and one on a part beside it', () => {+ const doc = JSON.parse(readFileSync(path.join(out, 'assets/cocos/props/pair.esprefab'), 'utf8'));+ const holders = doc.entities.filter((e: any) => e.components.some((c: any) => c.type === 'MeshRenderer'));+ expect(holders.map((e: any) => e.name)).toEqual(['Pair', 'Pair_1']);+ const part = holders[1];+ expect(part.parent).toBe(holders[0].prefabEntityId);+ expect(holders[0].children).toContain(part.prefabEntityId);+ });++ it('draws each primitive with the colour of its own slot', () => {+ const doc = JSON.parse(readFileSync(path.join(out, 'assets/cocos/props/pair.esprefab'), 'utf8'));+ const colour = (name: string): number => doc.entities.find((e: any) => e.name === name).components.find((c: any) => c.type === 'MeshRenderer').data.color.r;+ expect(colour('Pair')).toBeCloseTo(1, 2);+ expect(colour('Pair_1')).toBeCloseTo(10 / 255, 2);+ });+});diff --git a/pipeline/tests/cocos-import-outline.test.ts b/pipeline/tests/cocos-import-outline.test.tsnew file mode 100644index 000000000..85ef7cd04--- /dev/null+++ b/pipeline/tests/cocos-import-outline.test.ts@@ -0,0 +1,153 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A material whose effect has an outline pass, switched on, draws the mesh a second time: the outline is a material of its own, with the width+ * and colour the Cocos material gave it, on a renderer that stands with the first.+ */+import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';+import { importCocosProject } from '../src/import/cocos';++const MODEL = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';+const PREFAB = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';+const EFFECT = 'cccccccc-cccc-4ccc-8ccc-cccccccccccc';+const MAT_ON = 'dddddddd-dddd-4ddd-8ddd-dddddddddddd';+const MAT_OFF = 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee';++const vec = (x: number, y: number, z = 0) => ({ __type__: 'cc.Vec3', x, y, z });+const ident = { __type__: 'cc.Quat', x: 0, y: 0, z: 0, w: 1 };++const gltf = (): string => {+ const buffer = Buffer.alloc(44);+ [0, 0, 0, 1, 0, 0, 0, 1, 0].forEach((v, i) => buffer.writeFloatLE(v, i * 4));+ [0, 1, 2].forEach((v, i) => buffer.writeUInt16LE(v, 36 + i * 2));+ return JSON.stringify({+ asset: { version: '2.0' }, scene: 0, scenes: [{ nodes: [0] }],+ nodes: [{ name: 'tri', mesh: 0 }],+ meshes: [{ name: 'tri', primitives: [{ attributes: { POSITION: 0 }, indices: 1 }] }],+ accessors: [+ { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 0], max: [1, 1, 0] },+ { bufferView: 1, componentType: 5123, count: 3, type: 'SCALAR' },+ ],+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: 36 }, { buffer: 0, byteOffset: 36, byteLength: 6 }],+ buffers: [{ byteLength: 44, uri: `data:application/octet-stream;base64,${buffer.toString('base64')}` }],+ });+};++const EFFECT_TEXT = `+CCEffect %{+ techniques:+ - name: opaque+ passes:+ - switch: USE_OUTLINE_PASS+ vert: outline-vs:vert+ frag: outline-fs:frag+ rasterizerState: { cullMode: front }+ properties:+ lineWidth: { value: 10, target: outlineParams.x }+ baseColor: { editor: { type: color } }+ - vert: main-vs:vert+ frag: main-fs:frag+ properties:+ mainColor: { value: [1, 1, 1, 1], editor: { type: color } }+}%+CCProgram outline-vs %{+ precision highp float;+ #include <builtin/uniforms/cc-global>+ in vec3 a_position;+ uniform OutlineVert { vec4 outlineParams; };+ vec4 vert () { return cc_matViewProj * vec4(a_position, 1); }+}%+CCProgram outline-fs %{+ precision highp float;+ uniform OutlineFrag { vec4 baseColor; };+ vec4 frag () { return vec4(baseColor.rgb, 1.0); }+}%+CCProgram main-vs %{+ precision highp float;+ #include <builtin/uniforms/cc-global>+ in vec3 a_position;+ in vec2 a_texCoord;+ out vec2 uv0;+ vec4 vert () { uv0 = a_texCoord; return cc_matViewProj * vec4(a_position, 1); }+}%+CCProgram main-fs %{+ precision highp float;+ in vec2 uv0;+ uniform Constant { vec4 mainColor; };+ vec4 frag () { return mainColor; }+}%+`;++const prefab = [+ { __type__: 'cc.Prefab', _name: 'tri', data: { __id__: 1 } },+ { __type__: 'cc.Node', _name: 'Drawn', _parent: null, _children: [], _active: true, _components: [{ __id__: 2 }], _prefab: { __id__: 3 }, _lpos: vec(0, 0), _lrot: ident, _lscale: vec(1, 1, 1), _layer: 1073741824 },+ { __type__: 'cc.MeshRenderer', node: { __id__: 1 }, _enabled: true, __prefab: { __id__: 4 }, _materials: [{ __uuid__: MAT_ON }], _mesh: { __uuid__: `${MODEL}@00a01` } },+ { __type__: 'cc.PrefabInfo', root: { __id__: 1 }, asset: { __id__: 0 }, fileId: 'triRoot', instance: null },+ { __type__: 'cc.CompPrefabInfo', fileId: 'triMesh' },+];++const meta = (uuid: string, importer: string, extra: object = {}) => ({ ver: '1', importer, uuid, subMetas: {}, userData: {}, ...extra });+const material = (outline: boolean) => ({+ __type__: 'cc.Material', _effectAsset: { __uuid__: EFFECT }, _techIdx: 0,+ _defines: [{ USE_OUTLINE_PASS: outline }, {}],+ _props: [{ lineWidth: 8, baseColor: { __type__: 'cc.Color', r: 255, g: 0, b: 0, a: 255 } }, { mainColor: { __type__: 'cc.Color', r: 255, g: 255, b: 255, a: 255 } }],+});++let root: string;+let out: string;++beforeAll(async () => {+ root = mkdtempSync(path.join(tmpdir(), 'cocos-outline-'));+ const src = path.join(root, 'creator');+ out = path.join(root, 'estella');+ const put = (rel: string, v: unknown): void => {+ const f = path.join(src, 'assets', rel);+ mkdirSync(path.dirname(f), { recursive: true });+ writeFileSync(f, typeof v === 'string' ? v : JSON.stringify(v));+ };+ put('models/tri/tri.gltf', gltf());+ put('models/tri/tri.gltf.meta', meta(MODEL, 'gltf', {+ subMetas: { '00a01': { importer: 'gltf-mesh', uuid: `${MODEL}@00a01`, name: 'tri.mesh', userData: { gltfIndex: 0 } } },+ }));+ put('fx/outlined.effect', EFFECT_TEXT);+ put('fx/outlined.effect.meta', meta(EFFECT, 'effect'));+ put('mat/on.mtl', material(true));+ put('mat/on.mtl.meta', meta(MAT_ON, 'material'));+ put('mat/off.mtl', material(false));+ put('mat/off.mtl.meta', meta(MAT_OFF, 'material'));+ put('props/tri.prefab', prefab);+ put('props/tri.prefab.meta', meta(PREFAB, 'prefab'));+ await importCocosProject({ source: src, out, only: ['props/'] });+});++afterAll(() => rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }));++const json = (rel: string): any => JSON.parse(readFileSync(path.join(out, rel), 'utf8'));++describe('a material with its outline switched on', () => {+ it('has an outline material with the width and colour it gave the pass', () => {+ const m = json('assets/cocos/materials/mat/on.outline.esmaterial');+ expect(m.properties.lineWidth).toBe(8);+ expect(m.properties.baseColor).toMatchObject({ r: 1, g: 0, b: 0 });+ expect(m.cull).toBe(2);+ expect(existsSync(path.join(out, 'assets/cocos/effects/cocos-outline.esshader'))).toBe(true);+ });++ it('draws the mesh a second time, on a renderer beside the first', () => {+ const doc = json('assets/cocos/props/tri.esprefab');+ const drawn = doc.entities.find((e: any) => e.name === 'Drawn');+ const part = doc.entities.find((e: any) => e.name === 'Drawn_1');+ expect(part.parent).toBe(drawn.prefabEntityId);+ const first = drawn.components.find((c: any) => c.type === 'MeshRenderer').data;+ const second = part.components.find((c: any) => c.type === 'MeshRenderer').data;+ expect(second.mesh).toBe(first.mesh);+ expect(second.material).toBe('assets/cocos/materials/mat/on.outline.esmaterial');+ });++ it('is not made for a material that leaves the outline off', () => {+ expect(existsSync(path.join(out, 'assets/cocos/materials/mat/off.outline.esmaterial'))).toBe(false);+ });+});