Skip to content

Commit

Permalink
add fun selectBakedReflectionProbe
Browse files Browse the repository at this point in the history
  • Loading branch information
xubing0906 committed Aug 15, 2023
1 parent 11281e4 commit 42aa59c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 54 deletions.
4 changes: 2 additions & 2 deletions cocos/3d/framework/mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions cocos/3d/reflection-probe/reflection-probe-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -312,7 +312,7 @@ export class ReflectionProbe extends Component {

public onLoad (): void {
this._createProbe();
if (Editor) {
if (EDITOR) {
ReflectionProbeManager.probeManager.registerEvent();
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}
Expand Down
101 changes: 54 additions & 47 deletions cocos/3d/reflection-probe/reflection-probe-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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模式的预览球
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 42aa59c

Please sign in to comment.