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 project effect is judged by its fragment stage and the features a material switches on, and a sprite-stencil mask maps to the engine stencil mask
yhh0c2e440f22df
diff --git a/pipeline/src/import/cocos/components.ts b/pipeline/src/import/cocos/components.tsindex cd971fabd..6b79e4db2 100644--- a/pipeline/src/import/cocos/components.ts+++ b/pipeline/src/import/cocos/components.ts@@ -339,12 +339,18 @@ function contentRectData(box: UiBox): ComponentData {}/**- * `cc.Mask` as the engine's `UIMask`, clipping to the node's rectangle. The rectangle mask is exact; ellipse, graphics and image masks- * clip to the same rectangle rather than to their shape (the engine's shaped clip is a stencil cut from a sprite on the node, which a- * Cocos mask does not carry), so those are counted as partial.+ * `cc.Mask` as the engine's `UIMask`. A rectangle mask clips to the node's rectangle, exactly; a sprite-stencil mask clips to the shape the node's+ * sprite draws, which is the engine's stencil mode. Ellipse and graphics masks clip to the rectangle instead of to their shape, so those are+ * counted as partial.*/function mask(c: CocosComponent, ctx: ComponentContext): ComponentData {- ctx.report.component('cc.Mask', num(c.data._type, 0) === 0 ? 'mapped' : 'partial');+ const type = num(c.data._type, 0);+ // A sprite-stencil mask clips to the shape its node's sprite draws, above an alpha threshold: the engine's stencil mode.+ if (type === 3) {+ ctx.report.component('cc.Mask', 'mapped');+ return { type: 'UIMask', data: { enabled: c.enabled, mode: 1, alphaCutoff: Math.max(num(c.data._alphaThreshold, 0.1), 0.001) } };+ }+ ctx.report.component('cc.Mask', type === 0 ? 'mapped' : 'partial');return { type: 'UIMask', data: { enabled: c.enabled, mode: 0, alphaCutoff: 0 } };}diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.tsindex f6830ea56..fb2f40e60 100644--- a/pipeline/src/import/cocos/importProject.ts+++ b/pipeline/src/import/cocos/importProject.ts@@ -324,6 +324,25 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impoif ('x' in o) return { r: n(o.x, 1), g: n(o.y, 1), b: n(o.z, 1), a: n(o.w, 1) };return undefined;};+ // What a project's own effect is, read from its source: whether its main pass lights the surface (it names the unlit or the standard fragment+ // stage), which is how the project's effects are built even when they add outlines, a toon light or a dissolve behind switches.+ const effectKinds = new Map<string, { unlit: boolean; known: boolean }>();+ const effectKind = (effect: CocosAssetEntry | undefined): { unlit: boolean; known: boolean } => {+ if (!effect) return { unlit: false, known: false };+ const hit = effectKinds.get(effect.file);+ if (hit) return hit;+ let kind = { unlit: /unlit/i.test(effect.rel), known: false };+ try {+ const text = readFileSync(effect.file, 'utf8');+ const frags = [...text.matchAll(/frag:\s*([A-Za-z0-9_-]+)-fs/g)].map((m) => m[1]!.toLowerCase());+ const main = frags.filter((f) => f !== 'outline');+ if (main.length > 0) kind = { unlit: main.every((f) => f.includes('unlit')), known: main.every((f) => f.includes('unlit') || f.includes('standard')) };+ } catch {+ // an effect that cannot be read is judged by its name+ }+ effectKinds.set(effect.file, kind);+ return kind;+ };const materialInfo = (materialUuid: string): MaterialInfo | null => {if (materials.has(materialUuid)) return materials.get(materialUuid) ?? null;let info: MaterialInfo | null = null;@@ -332,6 +351,7 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impoif (mat) {const json = JSON.parse(readFileSync(mat.file, 'utf8')) as {_effectAsset?: { __uuid__?: string };+ _defines?: Array<Record<string, unknown>>;_props?: Array<Record<string, unknown>>;_states?: Array<{ blendState?: { targets?: Array<{ blend?: boolean; blendSrc?: number; blendDst?: number }> }; rasterizerState?: { cullMode?: number } }>;};@@ -346,15 +366,19 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impoconst effectEntry = index.byUuid.get(effectUuid);const effectName = (effectEntry ? path.basename(effectEntry.rel) : '').toLowerCase();const color = colorOf(props.mainColor) ?? colorOf(props.albedo);+ const kind = effectKind(effectEntry);+ // the optional features a material switches on (a toon light, an outline, a dissolve) are what a plain texture and tint do not say+ const switched = [...new Set((json._defines ?? []).flatMap((pass) => Object.entries(pass).filter(([, on]) => on === true).map(([name]) => name)))]+ .filter((name) => !['USE_ALBEDO_MAP', 'USE_TEXTURE', 'USE_VERTEX_COLOR', 'USE_ALPHA_TEST', 'USE_INSTANCING', 'USE_BATCHING', 'CC_SUPPORT_CASCADED_SHADOW_MAP'].includes(name));info = {texture: entryOf(props.mainTexture) ?? entryOf(props.albedoMap),...(color ? { color } : {}),normal: entryOf(props.normalMap),additive: target?.blend === true && target.blendDst === 1,transparent: target?.blend === true,- unlit: effectUuid === BUILTIN_UNLIT || effectName.includes('unlit'),+ unlit: effectUuid === BUILTIN_UNLIT || effectName.includes('unlit') || kind.unlit,cullBack: (json._states?.[0]?.rasterizerState?.cullMode ?? 2) === 2,- builtinEffect: BUILTIN_EFFECTS.has(effectUuid),+ builtinEffect: BUILTIN_EFFECTS.has(effectUuid) || (kind.known && switched.length === 0),};}} catch {diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex d78926788..2034016cf 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'feat(cocos): a project effect is judged by its fragment stage and the features a material switches on, and a sprite-stencil mask maps to the engine stencil mask':+ {"internal":"The Cocos Creator compatibility layer and importer have not shipped; nothing a creator reaches without opting in."},'feat(particle): an emitter has its own simulation speed, and the Cocos simulation speed, noise, trail and burst time map onto the emitter':{"note":"An emitter runs on its own clock."},'feat(cocos): PageView, PageViewIndicator, ScrollBar, SafeArea and WebView components, and the importer maps SpriteRenderer, Spine skeletons, ParticleSystem2D, Terrain and the players and animators whose clip or graph is missing':