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 scene's linear fog becomes a global distance-fog volume, and the scene settings report counts only those that are switched on
yhhdef4f851fe39
diff --git a/pipeline/src/import/cocos/convert.ts b/pipeline/src/import/cocos/convert.tsindex 8d1eb51e7..e4ecdcd16 100644--- a/pipeline/src/import/cocos/convert.ts+++ b/pipeline/src/import/cocos/convert.ts@@ -500,8 +500,8 @@ export function convertScene(doc: CocosDocument, ctx: ConvertContext): SceneFileif (!n.instance) n.children.forEach(assign);};doc.roots.forEach(assign);- // what is left of the scene's settings is not carried: the ambient light is- const dropped = doc.globals.filter((g) => !(g === 'ambient' && doc.ambient));+ // what is left of the scene's settings that are on is not carried: the ambient light and a linear fog are+ const dropped = doc.globals.filter((g) => !(g === 'ambient' && doc.ambient) && !(g === 'fog' && doc.fog));if (dropped.length) ctx.report.note('scene-globals-dropped', `${ctx.where}: ${dropped.join(',')}`);const walk = (n: CocosNode, parent: SceneEntity | null): void => {@@ -556,5 +556,26 @@ export function convertScene(doc: CocosDocument, ctx: ConvertContext): SceneFile],});}+ // The scene's fog is a setting of the scene in Cocos and a global volume in the engine: the distance fog the post-process stack already has.+ if (doc.fog) {+ const { start, end, color } = doc.fog;+ entities.push({+ id: next++, name: 'Fog', parent: null, children: [], visible: true,+ components: [+ { 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 } } },+ {+ type: 'PostProcessVolume',+ data: {+ effects: [{+ type: 'distanceFog', enabled: true,+ uniforms: { u_fogNear: start, u_fogFar: end },+ colors: { u_fogColor: { r: color.r, g: color.g, b: color.b, a: 1 } },+ }],+ isGlobal: true, shape: 'box', size: { x: 1, y: 1, z: 0 }, priority: 0, weight: 1, blendDistance: 0,+ },+ },+ ],+ });+ }return { version: 4, name: doc.name, entities };}diff --git a/pipeline/src/import/cocos/document.ts b/pipeline/src/import/cocos/document.tsindex 58f7e26ad..e93b9a367 100644--- a/pipeline/src/import/cocos/document.ts+++ b/pipeline/src/import/cocos/document.ts@@ -67,10 +67,12 @@ export interface CocosDocument {kind: 'prefab' | 'scene';name: string;roots: CocosNode[];- /** Scene-level settings (skybox, fog, shadows...) that were present and are not carried over. */+ /** Scene-level settings (skybox, fog, shadows...) that are switched on. The ambient light and a linear fog are carried; the rest are not. */globals: string[];/** The scene's ambient light, as the low-dynamic-range sky colour and the illumination it is lit by; absent when the scene sets none. */ambient?: { color: { r: number; g: number; b: number }; intensity: number };+ /** The scene's fog when it is switched on and falls off linearly between two distances (the other falloffs are not carried); colour is 0..1. */+ fog?: { start: number; end: number; color: { r: number; g: number; b: number } };/** Node ids that had to be made unique because the file repeats them. */duplicateIds: number;}@@ -109,6 +111,7 @@ export function parseCocosDocument(file: unknown, fallbackName = ''): CocosDocumconst head = isObj(objs[0]) ? objs[0] : null;const globals: string[] = [];let ambient: CocosDocument['ambient'];+ let fog: CocosDocument['fog'];let rootRefs: unknown[];let kind: CocosDocument['kind'];let name = fallbackName;@@ -123,7 +126,23 @@ export function parseCocosDocument(file: unknown, fallbackName = ''): CocosDocumname = typeof scene._name === 'string' && scene._name ? scene._name : name;rootRefs = Array.isArray(scene._children) ? scene._children : [];const g = at(scene._globals);- if (g) for (const k of ['ambient', 'shadows', 'skybox', 'fog', 'octree', 'lightProbeInfo']) if (g[k]) globals.push(k);+ // A setting the scene leaves switched off changes nothing drawn, so only a setting that is on counts as one the scene has.+ // The skybox is `_skybox` in the file, the rest are named as they are here.+ const settings: Array<[string, unknown]> = g+ ? [['ambient', g.ambient], ['shadows', g.shadows], ['skybox', g._skybox ?? g.skybox], ['fog', g.fog], ['octree', g.octree], ['lightProbeInfo', g.lightProbeInfo]]+ : [];+ for (const [k, ref] of settings) {+ const s = at(ref);+ if (s && (k === 'ambient' || s._enabled === true)) globals.push(k);+ }+ const fogInfo = g ? at(g.fog) : null;+ if (fogInfo && fogInfo._enabled === true && num(fogInfo._type, 0) === 0) {+ const c = isObj(fogInfo._fogColor) ? fogInfo._fogColor : {};+ fog = {+ start: num(fogInfo._fogStart, 0), end: num(fogInfo._fogEnd, 300),+ color: { r: num(c.r, 200) / 255, g: num(c.g, 200) / 255, b: num(c.b, 200) / 255 },+ };+ }const amb = g ? at(g.ambient) : null;const sky = amb && isObj(amb._skyColorLDR) ? amb._skyColorLDR : null;if (amb && sky && typeof sky.x === 'number' && typeof sky.y === 'number' && typeof sky.z === 'number') {@@ -259,5 +278,5 @@ export function parseCocosDocument(file: unknown, fallbackName = ''): CocosDocumnode.children.forEach(resolveNode);};roots.forEach(resolveNode);- return { kind, name, roots, globals, ambient, duplicateIds };+ return { kind, name, roots, globals, ambient, fog, duplicateIds };}diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.tsindex ccf5aec05..804e43881 100644--- a/pipeline/src/import/cocos/importProject.ts+++ b/pipeline/src/import/cocos/importProject.ts@@ -32,7 +32,7 @@ import { convertPass, isOutlinePass, type PassConversion } from './effectConvert* 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 = 20;+export const COCOS_IMPORT_REVISION = 21;/** 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/tests/cocos-import-ambient.test.ts b/pipeline/tests/cocos-import-ambient.test.tsindex 9cc455b90..1100bd75a 100644--- a/pipeline/tests/cocos-import-ambient.test.ts+++ b/pipeline/tests/cocos-import-ambient.test.ts@@ -12,9 +12,11 @@ import { importCocosProject } from '../src/import/cocos';const scene = [{ __type__: 'cc.SceneAsset', _name: 'main', scene: { __id__: 1 } },{ __type__: 'cc.Scene', _name: 'main', _children: [], _globals: { __id__: 2 } },- { __type__: 'cc.SceneGlobals', ambient: { __id__: 3 }, shadows: { __id__: 4 } },+ { __type__: 'cc.SceneGlobals', ambient: { __id__: 3 }, shadows: { __id__: 4 }, _skybox: { __id__: 5 }, fog: { __id__: 6 } },{ __type__: 'cc.AmbientInfo', _skyColorLDR: { __type__: 'cc.Vec4', x: 0.5, y: 0.75, z: 1, w: 0.8 }, _skyIllumLDR: 0.8 },- { __type__: 'cc.ShadowsInfo', _enabled: false },+ { __type__: 'cc.ShadowsInfo', _enabled: true },+ { __type__: 'cc.SkyboxInfo', _enabled: false },+ { __type__: 'cc.FogInfo', _enabled: true, _type: 0, _fogStart: 20, _fogEnd: 200, _fogColor: { __type__: 'cc.Color', r: 51, g: 102, b: 255, a: 255 } },];describe('a Cocos scene with an ambient light', () => {@@ -39,8 +41,21 @@ describe('a Cocos scene with an ambient light', () => {expect(light?.data).toMatchObject({ type: 2, intensity: 0.8, color: { r: 0.5, g: 0.75, b: 1 } });});- it('still says what of the scene settings it did not carry', () => {- expect(report.notes['scene-globals-dropped']?.examples.join(' ')).toContain('shadows');- expect(report.notes['scene-globals-dropped']?.examples.join(' ')).not.toContain('ambient');+ it('is a global distance fog volume when the scene turns a linear fog on', () => {+ const volume = doc.entities.find((e) => e.name === 'Fog')?.components.find((c) => c.type === 'PostProcessVolume');+ expect(volume?.data).toMatchObject({ isGlobal: true });+ expect(volume?.data.effects[0]).toMatchObject({+ type: 'distanceFog', enabled: true,+ uniforms: { u_fogNear: 20, u_fogFar: 200 },+ colors: { u_fogColor: { r: 0.2, g: 0.4, b: 1, a: 1 } },+ });+ });++ it('says only what of the scene settings that are on it did not carry', () => {+ const said = report.notes['scene-globals-dropped']?.examples.join(' ') ?? '';+ expect(said).toContain('shadows');+ expect(said).not.toContain('ambient');+ expect(said).not.toContain('fog');+ expect(said).not.toContain('skybox');});});diff --git a/pipeline/tests/cocos-import.test.ts b/pipeline/tests/cocos-import.test.tsindex 8ff8e5f68..6da83cb2f 100644--- a/pipeline/tests/cocos-import.test.ts+++ b/pipeline/tests/cocos-import.test.ts@@ -97,7 +97,7 @@ const scene = [{ __type__: 'cc.Node', _name: 'Canvas', _parent: { __id__: 1 }, _children: [{ __id__: 3 }], _active: true, _components: [], _prefab: null, _lpos: vec(0, 0), _lrot: ident, _lscale: one, _layer: 33554432, _id: 'canvas' },{ __type__: 'cc.Node', _objFlags: 0, _parent: { __id__: 2 }, _prefab: { __id__: 4 }, _id: '' },{ __type__: 'cc.PrefabInfo', root: { __id__: 3 }, asset: { __uuid__: PANEL }, fileId: 'panelRoot', instance: { __id__: 6 } },- { __type__: 'cc.SceneGlobals', skybox: { __id__: 5 } },+ { __type__: 'cc.SceneGlobals', skybox: { __id__: 5 }, _enabled: true },{ __type__: 'cc.PrefabInstance', fileId: 'inst', prefabRootNode: { __id__: 2 }, mountedChildren: [], mountedComponents: [], removedComponents: [], propertyOverrides: [{ __id__: 7 }] },{ __type__: 'CCPropertyOverrideInfo', targetInfo: { __id__: 8 }, propertyPath: ['_lpos'], value: vec(9, 9) },{ __type__: 'cc.TargetInfo', localID: ['instB', 'itemRoot'] },