From 339d223c09427551753d5ff3ab19441fe1132fa3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 31 Jan 2025 22:52:34 +0800 Subject: [PATCH 1/2] Optimize code size for scene-globals.ts --- cocos/scene-graph/scene-globals.ts | 141 +++++++++++++++-------------- 1 file changed, 74 insertions(+), 67 deletions(-) diff --git a/cocos/scene-graph/scene-globals.ts b/cocos/scene-graph/scene-globals.ts index 0f9a43bb472..04df8570e0d 100644 --- a/cocos/scene-graph/scene-globals.ts +++ b/cocos/scene-graph/scene-globals.ts @@ -28,7 +28,7 @@ import { import { TextureCube } from '../asset/assets/texture-cube'; import { CCFloat, CCInteger } from '../core/data/utils/attribute'; -import { Color, Quat, Vec3, Vec2, Vec4 } from '../core/math'; +import { Color, Quat, Vec3, Vec2, Vec4, v3 } from '../core/math'; import { Ambient } from '../render-scene/scene/ambient'; import { Shadows, ShadowType, ShadowSize } from '../render-scene/scene/shadows'; import { Skybox, EnvironmentLightingType } from '../render-scene/scene/skybox'; @@ -41,7 +41,7 @@ import { legacyCC } from '../core/global-exports'; import { Root } from '../root'; import { warnID } from '../core/platform/debug'; import { Material, MaterialPropertyFull } from '../asset/assets/material'; -import { cclegacy, macro } from '../core'; +import { cclegacy } from '../core'; import { Scene } from './scene'; import { NodeEventType } from './node-event'; import { PostSettings, ToneMappingType } from '../render-scene/scene/post-settings'; @@ -253,7 +253,7 @@ export class AmbientInfo { */ public activate (resource: Ambient): void { this._resource = resource; - this._resource.initialize(this); + resource.initialize(this); } } legacyCC.AmbientInfo = AmbientInfo; @@ -352,9 +352,10 @@ export class SkyboxInfo { set useHDR (val) { getPipelineSceneData().isHDR = val; this._useHDR = val; + const resource = this._resource; // Switch UI to and from LDR/HDR textures depends on HDR state - if (this._resource) { + if (resource) { if (this.envLightingType === EnvironmentLightingType.DIFFUSEMAP_WITH_REFLECTION) { if (this.diffuseMap === null) { this.envLightingType = EnvironmentLightingType.AUTOGEN_HEMISPHERE_DIFFUSE_WITH_REFLECTION; @@ -365,9 +366,9 @@ export class SkyboxInfo { } } - if (this._resource) { - this._resource.useHDR = this._useHDR; - this._resource.updateMaterialRenderInfo(); + if (resource) { + resource.useHDR = this._useHDR; + resource.updateMaterialRenderInfo(); } } get useHDR (): boolean { @@ -403,12 +404,13 @@ export class SkyboxInfo { warnID(15001); } - if (this._resource) { - this._resource.setEnvMaps(this._envmapHDR, this._envmapLDR); - this._resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); - this._resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); - this._resource.useDiffuseMap = this.applyDiffuseMap; - this._resource.envmap = val; + const resource = this._resource; + if (resource) { + resource.setEnvMaps(this._envmapHDR, this._envmapLDR); + resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); + resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); + resource.useDiffuseMap = this.applyDiffuseMap; + resource.envmap = val; } } get envmap (): TextureCube | null { @@ -563,13 +565,13 @@ export class SkyboxInfo { public activate (resource: Skybox): void { this.envLightingType = this._envLightingType; this._resource = resource; - this._resource.initialize(this); - this._resource.setEnvMaps(this._envmapHDR, this._envmapLDR); - this._resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); - this._resource.setSkyboxMaterial(this._editableMaterial); - this._resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); - this._resource.setRotationAngle(this._rotationAngle); - this._resource.activate(); // update global DS first + resource.initialize(this); + resource.setEnvMaps(this._envmapHDR, this._envmapLDR); + resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); + resource.setSkyboxMaterial(this._editableMaterial); + resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); + resource.setRotationAngle(this._rotationAngle); + resource.activate(); // update global DS first } /** @@ -584,12 +586,13 @@ export class SkyboxInfo { this.envLightingType = EnvironmentLightingType.HEMISPHERE_DIFFUSE; warnID(15001); } - if (this._resource) { - this._resource.setEnvMaps(this._envmapHDR, this._envmapLDR); - this._resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); - this._resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); - this._resource.useDiffuseMap = this.applyDiffuseMap; - this._resource.envmap = val; + const resource = this._resource; + if (resource) { + resource.setEnvMaps(this._envmapHDR, this._envmapLDR); + resource.setDiffuseMaps(this._diffuseMapHDR, this._diffuseMapLDR); + resource.setReflectionMaps(this._reflectionHDR, this._reflectionLDR); + resource.useDiffuseMap = this.applyDiffuseMap; + resource.envmap = val; } } @@ -605,10 +608,11 @@ export class SkyboxInfo { * @zh 设置此属性的 pass 索引,如果没有指定,则会设置此属性到所有 pass 上。 */ public setMaterialProperty (name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number): void { - if (!this._resource) return; - if (this._resource.enabled && this._resource.editableMaterial) { - this._resource.editableMaterial.setProperty(name, val, passIdx); - this._resource.editableMaterial.passes.forEach((pass) => { + const resource = this._resource; + if (!resource) return; + if (resource.enabled && resource.editableMaterial) { + resource.editableMaterial.setProperty(name, val, passIdx); + resource.editableMaterial.passes.forEach((pass) => { pass.update(); }); } @@ -634,10 +638,11 @@ export class FogInfo { set enabled (val: boolean) { if (this._enabled === val) return; this._enabled = val; - if (this._resource) { - this._resource.enabled = val; + const resource = this._resource; + if (resource) { + resource.enabled = val; if (val) { - this._resource.type = this._type; + resource.type = this._type; } } } @@ -656,10 +661,11 @@ export class FogInfo { set accurate (val: boolean) { if (this._accurate === val) return; this._accurate = val; - if (this._resource) { - this._resource.accurate = val; + const resource = this._resource; + if (resource) { + resource.accurate = val; if (val) { - this._resource.type = this._type; + resource.type = this._type; } } } @@ -835,8 +841,8 @@ export class FogInfo { */ public activate (resource: Fog): void { this._resource = resource; - this._resource.initialize(this); - this._resource.activate(); + resource.initialize(this); + resource.activate(); } } @@ -855,10 +861,11 @@ export class ShadowsInfo { set enabled (val: boolean) { if (this._enabled === val) return; this._enabled = val; - if (this._resource) { - this._resource.enabled = val; + const resource = this._resource; + if (resource) { + resource.enabled = val; if (val) { - this._resource.type = this._type; + resource.type = this._type; } } } @@ -964,10 +971,11 @@ export class ShadowsInfo { @type(ShadowSize) @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) set shadowMapSize (value: number) { + const resource = this._resource; this._size.set(value, value); - if (this._resource) { - this._resource.size.set(value, value); - this._resource.shadowMapDirty = true; + if (resource) { + resource.size.set(value, value); + resource.shadowMapDirty = true; } } get shadowMapSize (): number { @@ -1012,8 +1020,8 @@ export class ShadowsInfo { */ public activate (resource: Shadows): void { this._resource = resource; - this._resource.initialize(this); - this._resource.activate(); + resource.initialize(this); + resource.activate(); } } legacyCC.ShadowsInfo = ShadowsInfo; @@ -1112,7 +1120,7 @@ export class OctreeInfo { */ public activate (resource: Octree): void { this._resource = resource; - this._resource.initialize(this); + resource.initialize(this); } } legacyCC.OctreeInfo = OctreeInfo; @@ -1192,7 +1200,7 @@ export class SkinInfo { */ public activate (resource: Skin): void { this._resource = resource; - this._resource.initialize(this); + resource.initialize(this); } } legacyCC.SkinInfo = SkinInfo; @@ -1224,8 +1232,8 @@ export class PostSettingsInfo { public activate (resource: PostSettings): void { this._resource = resource; - this._resource.initialize(this); - this._resource.activate(); + resource.initialize(this); + resource.activate(); } } @@ -1430,7 +1438,7 @@ export class LightProbeInfo { public activate (scene: Scene, resource: LightProbes): void { this._scene = scene; this._resource = resource; - this._resource.initialize(this); + resource.initialize(this); } public onProbeBakeFinished (): void { @@ -1449,10 +1457,9 @@ export class LightProbeInfo { node.emit(NodeEventType.LIGHT_PROBE_BAKING_CHANGED); - for (let i = 0; i < node.children.length; i++) { - const child = node.children[i]; + node.children.forEach((child) => { this.onProbeBakingChanged(child); - } + }); } public clearSHCoefficients (): void { @@ -1460,10 +1467,9 @@ export class LightProbeInfo { return; } - const probes = this._data.probes; - for (let i = 0; i < probes.length; i++) { - probes[i].coefficients.length = 0; - } + this._data.probes.forEach((probe) => { + probe.coefficients.length = 0; + }); this.clearAllSHUBOs(); } @@ -1526,8 +1532,9 @@ export class LightProbeInfo { const points: Vec3[] = []; for (let i = 0; i < this._nodes.length; i++) { - const node = this._nodes[i].node; - const probes = this._nodes[i].probes; + const probeNode = this._nodes[i]; + const node = probeNode.node; + const probes = probeNode.probes; const worldPosition = node.worldPosition; if (!probes) { @@ -1535,7 +1542,7 @@ export class LightProbeInfo { } for (let j = 0; j < probes.length; j++) { - const position = new Vec3(0, 0, 0); + const position = v3(); Vec3.add(position, probes[j], worldPosition); points.push(position); } @@ -1567,9 +1574,9 @@ export class LightProbeInfo { } const models = renderScene.models; - for (let i = 0; i < models.length; i++) { - models[i].clearSHUBOs(); - } + models.forEach((model) => { + model.clearSHUBOs(); + }); } private resetAllTetraIndices (): void { @@ -1583,9 +1590,9 @@ export class LightProbeInfo { } const models = renderScene.models; - for (let i = 0; i < models.length; i++) { - models[i].tetrahedronIndex = -1; - } + models.forEach((model) => { + model.tetrahedronIndex = -1; + }); } } From d21c9ee97bc3128a79f8b56e628fa7edd4ca53c6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 31 Jan 2025 23:23:23 +0800 Subject: [PATCH 2/2] Update --- cocos/scene-graph/scene-globals.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cocos/scene-graph/scene-globals.ts b/cocos/scene-graph/scene-globals.ts index 04df8570e0d..45496cc7e7c 100644 --- a/cocos/scene-graph/scene-globals.ts +++ b/cocos/scene-graph/scene-globals.ts @@ -610,9 +610,10 @@ export class SkyboxInfo { public setMaterialProperty (name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number): void { const resource = this._resource; if (!resource) return; - if (resource.enabled && resource.editableMaterial) { - resource.editableMaterial.setProperty(name, val, passIdx); - resource.editableMaterial.passes.forEach((pass) => { + const editableMaterial = resource.editableMaterial; + if (resource.enabled && editableMaterial) { + editableMaterial.setProperty(name, val, passIdx); + editableMaterial.passes.forEach((pass) => { pass.update(); }); }