Skip to content

Commit e27fe36

Browse files
committed
refactor(rendering): deduplicate reflection probe camera setup
1 parent efe7258 commit e27fe36

1 file changed

Lines changed: 28 additions & 37 deletions

File tree

cocos/render-scene/scene/reflection-probe.ts

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -331,22 +331,7 @@ export class ReflectionProbe {
331331
*/
332332
public renderPreviewPlanarReflection (sourceCamera: Camera): Camera {
333333
if (!this._previewCamera) {
334-
const root = cclegacy.director.root;
335-
const cameraNode = new Node(`${this.cameraNode.name} Preview Reflection`);
336-
const previewCamera = root.createCamera();
337-
previewCamera.initialize({
338-
name: cameraNode.name,
339-
node: cameraNode,
340-
projection: CameraProjection.PERSPECTIVE,
341-
window: root.mainWindow,
342-
priority: 0,
343-
cameraType: CameraType.DEFAULT,
344-
trackingType: TrackingType.NO_TRACKING,
345-
});
346-
previewCamera.setViewportInOrientedSpace(new Rect(0, 0, 1, 1));
347-
previewCamera.fovAxis = CameraFOVAxis.VERTICAL;
348-
previewCamera.visibility = this._visibility;
349-
this._previewCamera = previewCamera;
334+
this._createCamera(new Node(`${this.cameraNode.name} Preview Reflection`), true);
350335
}
351336
const previewCamera = this._previewCamera!;
352337
this._syncCameraParams(sourceCamera, previewCamera);
@@ -463,36 +448,42 @@ export class ReflectionProbe {
463448
targetCamera.resize(camera.width, camera.height);
464449
}
465450

466-
private _createCamera (cameraNode: Node): Camera | null {
451+
private _createCamera (cameraNode: Node, preview = false): Camera | null {
467452
const root = cclegacy.director.root;
468-
if (!this._camera) {
469-
this._camera = root.createCamera();
470-
if (!this._camera) return null;
471-
this._camera.initialize({
453+
let camera = preview ? this._previewCamera : this._camera;
454+
if (!camera) {
455+
camera = root.createCamera();
456+
if (!camera) return null;
457+
camera.initialize({
472458
name: cameraNode.name,
473459
node: cameraNode,
474460
projection: CameraProjection.PERSPECTIVE,
475-
window: EDITOR ? root && root.mainWindow : root && root.tempWindow,
461+
window: preview || EDITOR ? root.mainWindow : root.tempWindow,
476462
priority: 0,
477463
cameraType: CameraType.DEFAULT,
478464
trackingType: TrackingType.NO_TRACKING,
479465
});
466+
if (preview) {
467+
this._previewCamera = camera;
468+
} else {
469+
this._camera = camera;
470+
}
480471
}
481-
this._camera.setViewportInOrientedSpace(new Rect(0, 0, 1, 1));
482-
this._camera.fovAxis = CameraFOVAxis.VERTICAL;
483-
this._camera.fov = toRadian(90);
484-
this._camera.orthoHeight = 10;
485-
this._camera.nearClip = 1;
486-
this._camera.farClip = 1000;
487-
this._camera.clearColor = this._backgroundColor;
488-
this._camera.clearDepth = 1.0;
489-
this._camera.clearStencil = 0.0;
490-
this._camera.clearFlag = this._clearFlag;
491-
this._camera.visibility = this._visibility;
492-
this._camera.aperture = CameraAperture.F16_0;
493-
this._camera.shutter = CameraShutter.D125;
494-
this._camera.iso = CameraISO.ISO100;
495-
return this._camera;
472+
camera.setViewportInOrientedSpace(new Rect(0, 0, 1, 1));
473+
camera.fovAxis = CameraFOVAxis.VERTICAL;
474+
camera.fov = toRadian(90);
475+
camera.orthoHeight = 10;
476+
camera.nearClip = 1;
477+
camera.farClip = 1000;
478+
camera.clearColor = this._backgroundColor;
479+
camera.clearDepth = 1.0;
480+
camera.clearStencil = 0.0;
481+
camera.clearFlag = this._clearFlag;
482+
camera.visibility = this._visibility;
483+
camera.aperture = CameraAperture.F16_0;
484+
camera.shutter = CameraShutter.D125;
485+
camera.iso = CameraISO.ISO100;
486+
return camera;
496487
}
497488

498489
private _resetCameraParams (): void {

0 commit comments

Comments
 (0)