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 3, 2023
1 parent d73096a commit 11281e4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cocos/3d/framework/mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ 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.updateUseCubeModels(this._model);
cclegacy.internal.reflectionProbeManager.selectBakedReflectionProbe(this._model);
if (!cclegacy.internal.reflectionProbeManager.getUsedReflectionProbe(this._model, false)) {
warnID(16302);
}
Expand Down
4 changes: 3 additions & 1 deletion cocos/3d/reflection-probe/reflection-probe-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ export class ReflectionProbe extends Component {

public onLoad (): void {
this._createProbe();
ReflectionProbeManager.probeManager.registerEvent();
if (Editor) {
ReflectionProbeManager.probeManager.registerEvent();
}
}

onEnable (): void {
Expand Down
103 changes: 66 additions & 37 deletions cocos/3d/reflection-probe/reflection-probe-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,50 @@ export class ReflectionProbeManager {
if (!model.node) continue;
if ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) && (model.node.hasChangedFlags || forceUpdate)) {
if (model.reflectionProbeType === ReflectionProbeType.BAKED_CUBEMAP || this._isUsedBlending(model)) {
this.updateUseCubeModels(model);
this.selectBakedReflectionProbe(model);
} else if (model.reflectionProbeType === ReflectionProbeType.PLANAR_REFLECTION) {
this.updateUsePlanarModels(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
*/
public filterModelsForPlanarReflection (): void {
if (this._probes.length === 0) return;
const scene = cclegacy.director.getScene();
Expand All @@ -116,6 +152,9 @@ export class ReflectionProbeManager {
}
}

/**
* @engineInternal
*/
public clearPlanarReflectionMap (probe: ReflectionProbe): void {
for (const entry of this._usePlanarModels.entries()) {
if (entry[1] === probe) {
Expand All @@ -124,6 +163,9 @@ export class ReflectionProbeManager {
}
}

/**
* @engineInternal
*/
public register (probe: ReflectionProbe): void {
const index = this._probes.indexOf(probe);
if (index === -1) {
Expand All @@ -132,6 +174,9 @@ export class ReflectionProbeManager {
}
}

/**
* @engineInternal
*/
public unregister (probe: ReflectionProbe): void {
for (let i = 0; i < this._probes.length; i++) {
if (this._probes[i] === probe) {
Expand All @@ -145,6 +190,9 @@ export class ReflectionProbeManager {
this.updateProbeData();
}

/**
* @engineInternal
*/
public exists (probeId: number): boolean {
if (this._probes.length === 0) return false;
for (let i = 0; i < this._probes.length; i++) {
Expand All @@ -155,6 +203,9 @@ export class ReflectionProbeManager {
return false;
}

/**
* @engineInternal
*/
public getNewReflectionProbeId (): number {
let probeId = 0;
// eslint-disable-next-line no-constant-condition
Expand All @@ -167,10 +218,16 @@ export class ReflectionProbeManager {
}
}

/**
* @engineInternal
*/
public getProbes (): ReflectionProbe[] {
return this._probes;
}

/**
* @engineInternal
*/
public getProbeById (probeId: number): ReflectionProbe | null {
for (let i = 0; i < this._probes.length; i++) {
if (this._probes[i].getProbeId() === probeId) {
Expand All @@ -180,10 +237,16 @@ export class ReflectionProbeManager {
return null;
}

/**
* @engineInternal
*/
public clearAll (): void {
this._probes = [];
}

/**
* @engineInternal
*/
public getProbeByCamera (camera: Camera): ReflectionProbe | null {
for (let i = 0; i < this._probes.length; i++) {
if (this._probes[i].camera === camera) {
Expand Down Expand Up @@ -271,40 +334,6 @@ export class ReflectionProbeManager {
}
}

/**
* @en Update objects using reflection probe for bake cubemap.
* @zh 更新使用反射探针烘焙cubemap的物体。
* @param model update the model for reflection probe
* @engineInternal
*/
public updateUseCubeModels (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 @@ -441,7 +470,7 @@ export class ReflectionProbeManager {
return null;
}

/**
/**
* @en Set reflection probe used by the model.
* @zh 手动设置模型使用的反射探针。
* @param model set the probe for this model
Expand Down Expand Up @@ -523,7 +552,7 @@ export class ReflectionProbeManager {
const p = this._useCubeModels.get(key);
if (p !== undefined && p === probe) {
this._useCubeModels.delete(key);
this.updateUseCubeModels(key);
this.selectBakedReflectionProbe(key);
}
}
for (const key of this._usePlanarModels.keys()) {
Expand Down

0 comments on commit 11281e4

Please sign in to comment.