From 42aa59c7878623ac93a8b1ad7c924a6bc835eaf9 Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Tue, 15 Aug 2023 16:08:00 +0800 Subject: [PATCH] add fun selectBakedReflectionProbe --- cocos/3d/framework/mesh-renderer.ts | 4 +- .../reflection-probe-component.ts | 10 +- .../reflection-probe-manager.ts | 101 ++++++++++-------- 3 files changed, 61 insertions(+), 54 deletions(-) diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index 3b1c4f61ed5..5869d9a66a5 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -1143,12 +1143,12 @@ export class MeshRenderer extends ModelRenderer { if (this.bakeSettings.reflectionProbe === ReflectionProbeType.BAKED_CUBEMAP || this.bakeSettings.reflectionProbe === ReflectionProbeType.BLEND_PROBES || this.bakeSettings.reflectionProbe === ReflectionProbeType.BLEND_PROBES_AND_SKYBOX) { - cclegacy.internal.reflectionProbeManager.selectBakedReflectionProbe(this._model); + cclegacy.internal.reflectionProbeManager.selectReflectionProbe(this._model); if (!cclegacy.internal.reflectionProbeManager.getUsedReflectionProbe(this._model, false)) { warnID(16302); } } else if (this.bakeSettings.reflectionProbe === ReflectionProbeType.PLANAR_REFLECTION) { - cclegacy.internal.reflectionProbeManager.updateUsePlanarModels(this._model); + cclegacy.internal.reflectionProbeManager.selectPlanarReflectionProbe(this._model); if (!cclegacy.internal.reflectionProbeManager.getUsedReflectionProbe(this._model, true)) { warnID(16302); } diff --git a/cocos/3d/reflection-probe/reflection-probe-component.ts b/cocos/3d/reflection-probe/reflection-probe-component.ts index 2b724b74472..bce57579bda 100644 --- a/cocos/3d/reflection-probe/reflection-probe-component.ts +++ b/cocos/3d/reflection-probe/reflection-probe-component.ts @@ -114,7 +114,7 @@ export class ReflectionProbe extends Component { absolute(this._size); this.probe.size = this._size; if (this.probe) { - ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.onUpdateProbes(); ReflectionProbeManager.probeManager.updateProbeData(); } } @@ -263,7 +263,7 @@ export class ReflectionProbe extends Component { set cubemap (val: TextureCube | null) { this._cubemap = val; this.probe.cubemap = val; - ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.onUpdateProbes(); } get cubemap (): TextureCube | null { @@ -312,7 +312,7 @@ export class ReflectionProbe extends Component { public onLoad (): void { this._createProbe(); - if (Editor) { + if (EDITOR) { ReflectionProbeManager.probeManager.registerEvent(); } } @@ -325,7 +325,7 @@ export class ReflectionProbe extends Component { this._probe.updateProbeId(this._probeId); } ReflectionProbeManager.probeManager.register(this._probe); - ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.onUpdateProbes(); this._probe.enable(); } this.node.on(Node.EventType.TRANSFORM_CHANGED, this._onProbeTransformChanged, this); @@ -390,7 +390,7 @@ export class ReflectionProbe extends Component { private _onProbeTransformChanged (type: TransformBit): void { this.probe.updateBoundingBox(); if (type & Node.TransformBit.POSITION) { - ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.onUpdateProbes(); ReflectionProbeManager.probeManager.updateProbeData(); } } diff --git a/cocos/3d/reflection-probe/reflection-probe-manager.ts b/cocos/3d/reflection-probe/reflection-probe-manager.ts index 54d0ed79200..2037e08b59c 100644 --- a/cocos/3d/reflection-probe/reflection-probe-manager.ts +++ b/cocos/3d/reflection-probe/reflection-probe-manager.ts @@ -65,22 +65,26 @@ export class ReflectionProbeManager { set updateForRuntime (val: boolean) { this._updateForRuntime = val; } + get updateForRuntime (): boolean { return this._updateForRuntime; } + /** + * @engineInternal + */ public registerEvent (): void { if (!this._registeredEvent) { cclegacy.director.on(cclegacy.Director.EVENT_BEFORE_UPDATE, this.onUpdateProbes, this); this._registeredEvent = true; } } + /** - * @en refresh all reflection probe - * @zh 刷新所有反射探针 + * @en Refresh all reflection probe. + * @zh 刷新所有反射探针。 */ - public onUpdateProbes (forceUpdate = false): void { - if (!EDITOR && !this._updateForRuntime) return; + public onUpdateProbes (): void { if (this._probes.length === 0) return; const scene = cclegacy.director.getScene(); if (!scene || !scene.renderScene) { @@ -90,51 +94,19 @@ export class ReflectionProbeManager { for (let i = 0; i < models.length; i++) { const model = models[i]; if (!model.node) continue; - if ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) && (model.node.hasChangedFlags || forceUpdate)) { + if (model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) { if (model.reflectionProbeType === ReflectionProbeType.BAKED_CUBEMAP || this._isUsedBlending(model)) { - this.selectBakedReflectionProbe(model); + this.selectReflectionProbe(model); } else if (model.reflectionProbeType === ReflectionProbeType.PLANAR_REFLECTION) { - this.updateUsePlanarModels(model); + this.selectPlanarReflectionProbe(model); } } } } /** - * @en Selecting the appropriate reflection probe for the model, it will use the closest one based on distance. - * @zh 为模型选择适用的反射探针,会使用距离最近的。 - * @param model select for this model - */ - public selectBakedReflectionProbe (model: Model): void { - if (model.node && model.worldBounds && ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK))) { - model.updateWorldBound(); - const nearest = this._getNearestProbe(model); - if (!nearest) { - //not in the range of any probe,set default texture for the model - this._updateCubemapOfModel(model, null); - this._useCubeModels.delete(model); - } else if (this._useCubeModels.has(model)) { - const old = this._useCubeModels.get(model); - // if used other probe,reset texture - if (old !== nearest) { - this._useCubeModels.set(model, nearest); - } - nearest.needRefresh = true; - } else { - this._useCubeModels.set(model, nearest); - nearest.needRefresh = true; - } - } - - for (let i = 0; i < this._probes.length; i++) { - if ((this._probes[i].needRefresh && this._probes[i].probeType === ProbeType.CUBE) || this._isUsedBlending(model)) { - this.updateBakedCubemap(this._probes[i]); - } - } - } - - /** - * @engineInternal + * @en filter models that use planar reflection. + * @zh 筛选使用平面反射的模型 */ public filterModelsForPlanarReflection (): void { if (this._probes.length === 0) return; @@ -147,7 +119,7 @@ export class ReflectionProbeManager { const model = models[i]; if (!model.node) continue; if ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) && model.reflectionProbeType === ReflectionProbeType.PLANAR_REFLECTION) { - this.updateUsePlanarModels(model); + this.selectPlanarReflectionProbe(model); } } } @@ -219,14 +191,16 @@ export class ReflectionProbeManager { } /** - * @engineInternal + * @en Get all reflection probes in the scene. + * @zh 获取场景中所有的反射探针 */ public getProbes (): ReflectionProbe[] { return this._probes; } /** - * @engineInternal + * @en Get reflection probe by id. + * @zh 根据id获取反射探针 */ public getProbeById (probeId: number): ReflectionProbe | null { for (let i = 0; i < this._probes.length; i++) { @@ -304,7 +278,7 @@ export class ReflectionProbeManager { * @param probe update the model for reflection probe * @engineInternal */ - public updateUsePlanarModels (model: Model): void { + public selectPlanarReflectionProbe (model: Model): void { if (!model.node || !model.worldBounds || model.reflectionProbeType !== ReflectionProbeType.PLANAR_REFLECTION) return; for (let i = 0; i < this._probes.length; i++) { const probe = this._probes[i]; @@ -334,6 +308,39 @@ export class ReflectionProbeManager { } } + /** + * @en Selecting the appropriate reflection probe for the model, it will use the closest one based on distance. + * @zh 为模型选择适用的反射探针,会使用距离最近的。 + * @param model select for this model + */ + public selectReflectionProbe (model: Model): void { + if (model.node && model.worldBounds && ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK))) { + model.updateWorldBound(); + const nearest = this._getNearestProbe(model); + if (!nearest) { + //not in the range of any probe,set default texture for the model + this._updateCubemapOfModel(model, null); + this._useCubeModels.delete(model); + } else if (this._useCubeModels.has(model)) { + const old = this._useCubeModels.get(model); + // if used other probe,reset texture + if (old !== nearest) { + this._useCubeModels.set(model, nearest); + } + nearest.needRefresh = true; + } else { + this._useCubeModels.set(model, nearest); + nearest.needRefresh = true; + } + } + + for (let i = 0; i < this._probes.length; i++) { + if ((this._probes[i].needRefresh && this._probes[i].probeType === ProbeType.CUBE) || this._isUsedBlending(model)) { + this.updateBakedCubemap(this._probes[i]); + } + } + } + /** * @en Update the preview sphere of the Reflection Probe cube mode. * @zh 更新反射探针cube模式的预览球 @@ -552,14 +559,14 @@ export class ReflectionProbeManager { const p = this._useCubeModels.get(key); if (p !== undefined && p === probe) { this._useCubeModels.delete(key); - this.selectBakedReflectionProbe(key); + this.selectReflectionProbe(key); } } for (const key of this._usePlanarModels.keys()) { const p = this._usePlanarModels.get(key); if (p !== undefined && p === probe) { this._usePlanarModels.delete(key); - this.updateUsePlanarModels(key); + this.selectPlanarReflectionProbe(key); } } }