Skip to content

Commit

Permalink
Optimize code size for scene-globals.ts (#18269)
Browse files Browse the repository at this point in the history
* Optimize code size for scene-globals.ts
  • Loading branch information
dumganhar authored Feb 5, 2025
1 parent 6409fdc commit f80f8c8
Showing 1 changed file with 75 additions and 67 deletions.
142 changes: 75 additions & 67 deletions cocos/scene-graph/scene-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

/**
Expand All @@ -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;
}
}

Expand All @@ -605,10 +608,12 @@ 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;
const editableMaterial = resource.editableMaterial;
if (resource.enabled && editableMaterial) {
editableMaterial.setProperty(name, val, passIdx);
editableMaterial.passes.forEach((pass) => {
pass.update();
});
}
Expand All @@ -634,10 +639,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;
}
}
}
Expand All @@ -656,10 +662,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;
}
}
}
Expand Down Expand Up @@ -835,8 +842,8 @@ export class FogInfo {
*/
public activate (resource: Fog): void {
this._resource = resource;
this._resource.initialize(this);
this._resource.activate();
resource.initialize(this);
resource.activate();
}
}

Expand All @@ -855,10 +862,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;
}
}
}
Expand Down Expand Up @@ -964,10 +972,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 {
Expand Down Expand Up @@ -1012,8 +1021,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;
Expand Down Expand Up @@ -1112,7 +1121,7 @@ export class OctreeInfo {
*/
public activate (resource: Octree): void {
this._resource = resource;
this._resource.initialize(this);
resource.initialize(this);
}
}
legacyCC.OctreeInfo = OctreeInfo;
Expand Down Expand Up @@ -1192,7 +1201,7 @@ export class SkinInfo {
*/
public activate (resource: Skin): void {
this._resource = resource;
this._resource.initialize(this);
resource.initialize(this);
}
}
legacyCC.SkinInfo = SkinInfo;
Expand Down Expand Up @@ -1224,8 +1233,8 @@ export class PostSettingsInfo {

public activate (resource: PostSettings): void {
this._resource = resource;
this._resource.initialize(this);
this._resource.activate();
resource.initialize(this);
resource.activate();
}
}

Expand Down Expand Up @@ -1430,7 +1439,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 {
Expand All @@ -1449,21 +1458,19 @@ 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 {
if (!this._data) {
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();
}
Expand Down Expand Up @@ -1526,16 +1533,17 @@ 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) {
continue;
}

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);
}
Expand Down Expand Up @@ -1567,9 +1575,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 {
Expand All @@ -1583,9 +1591,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;
});
}
}

Expand Down

0 comments on commit f80f8c8

Please sign in to comment.