fix planar reflections for editor camera previews (#236)

This commit is contained in:
徐兵
2026-08-17 17:25:14 +08:00
committed by GitHub
parent 140068ddb1
commit e317c04512
6 changed files with 108 additions and 60 deletions
@@ -384,10 +384,10 @@ export class ReflectionProbe extends Component {
}
}
}
if (this.probeType === ProbeType.PLANAR && this.sourceCamera) {
if (!EDITOR_NOT_IN_PREVIEW && this.probeType === ProbeType.PLANAR && this.sourceCamera) {
if ((this.sourceCamera.node.hasChangedFlags & TransformBit.TRS)
|| !this._sourceCameraPos.equals(this.sourceCamera.node.getWorldPosition())) {
this._sourceCameraPos = this.sourceCamera.node.getWorldPosition();
this._sourceCameraPos.set(this.sourceCamera.node.getWorldPosition());
this.probe.renderPlanarReflection(this.sourceCamera.camera);
}
}
+71 -39
View File
@@ -67,11 +67,13 @@ export class ReflectionProbe {
protected readonly _size = v3(1, 1, 1);
/**
* @en Render cubemap's camera
* @zh 渲染cubemap的相机
* @en Camera used for probe rendering (cubemap capture or planar reflection).
* @zh 用于探针渲染的相机(cubemap 采集或平面反射)
*/
private _camera: Camera | null = null;
private _previewCamera: Camera | null = null;
/**
* @en Unique id of probe.
* @zh probe的唯一id
@@ -184,6 +186,9 @@ export class ReflectionProbe {
set visibility (val) {
this._visibility = val;
this._camera!.visibility = this._visibility;
if (this._previewCamera) {
this._previewCamera.visibility = this._visibility;
}
}
/**
@@ -320,6 +325,21 @@ export class ReflectionProbe {
this._needRender = true;
}
/**
* @engineInternal
* @mangle
*/
public renderPreviewPlanarReflection (sourceCamera: Camera): Camera {
if (!this._previewCamera) {
this._createCamera(new Node(`${this.cameraNode.name} Preview Reflection`), true);
}
const previewCamera = this._previewCamera!;
this._syncCameraParams(sourceCamera, previewCamera);
this._transformReflectionCamera(sourceCamera, previewCamera);
this._needRender = true;
return previewCamera;
}
public switchProbeType (type: ProbeType, sourceCamera: Camera | null): void {
if (type === ProbeType.CUBE) {
this._needRender = false;
@@ -357,6 +377,12 @@ export class ReflectionProbe {
this._camera.destroy();
this._camera = null;
}
if (this._previewCamera) {
const cameraNode = this._previewCamera.node;
this._previewCamera.destroy();
this._previewCamera = null;
cameraNode.destroy();
}
for (let i = 0; i < this.bakedCubeTextures.length; i++) {
this.bakedCubeTextures[i].destroy();
}
@@ -410,48 +436,54 @@ export class ReflectionProbe {
return true;
}
private _syncCameraParams (camera: Camera): void {
this.camera.projectionType = camera.projectionType;
this.camera.orthoHeight = camera.orthoHeight;
this.camera.nearClip = camera.nearClip;
this.camera.farClip = camera.farClip;
this.camera.fov = camera.fov;
this.camera.clearFlag = camera.clearFlag;
this.camera.clearColor = camera.clearColor;
this.camera.priority = camera.priority - 1;
this.camera.resize(camera.width, camera.height);
private _syncCameraParams (camera: Camera, targetCamera: Camera = this.camera): void {
targetCamera.projectionType = camera.projectionType;
targetCamera.orthoHeight = camera.orthoHeight;
targetCamera.nearClip = camera.nearClip;
targetCamera.farClip = camera.farClip;
targetCamera.fov = camera.fov;
targetCamera.clearFlag = camera.clearFlag;
targetCamera.clearColor = camera.clearColor;
targetCamera.priority = camera.priority - 1;
targetCamera.resize(camera.width, camera.height);
}
private _createCamera (cameraNode: Node): Camera | null {
private _createCamera (cameraNode: Node, preview = false): Camera | null {
const root = cclegacy.director.root;
if (!this._camera) {
this._camera = root.createCamera();
if (!this._camera) return null;
this._camera.initialize({
let camera = preview ? this._previewCamera : this._camera;
if (!camera) {
camera = root.createCamera();
if (!camera) return null;
camera.initialize({
name: cameraNode.name,
node: cameraNode,
projection: CameraProjection.PERSPECTIVE,
window: EDITOR ? root && root.mainWindow : root && root.tempWindow,
window: preview || EDITOR ? root.mainWindow : root.tempWindow,
priority: 0,
cameraType: CameraType.DEFAULT,
trackingType: TrackingType.NO_TRACKING,
});
if (preview) {
this._previewCamera = camera;
} else {
this._camera = camera;
}
}
this._camera.setViewportInOrientedSpace(new Rect(0, 0, 1, 1));
this._camera.fovAxis = CameraFOVAxis.VERTICAL;
this._camera.fov = toRadian(90);
this._camera.orthoHeight = 10;
this._camera.nearClip = 1;
this._camera.farClip = 1000;
this._camera.clearColor = this._backgroundColor;
this._camera.clearDepth = 1.0;
this._camera.clearStencil = 0.0;
this._camera.clearFlag = this._clearFlag;
this._camera.visibility = this._visibility;
this._camera.aperture = CameraAperture.F16_0;
this._camera.shutter = CameraShutter.D125;
this._camera.iso = CameraISO.ISO100;
return this._camera;
camera.setViewportInOrientedSpace(new Rect(0, 0, 1, 1));
camera.fovAxis = CameraFOVAxis.VERTICAL;
camera.fov = toRadian(90);
camera.orthoHeight = 10;
camera.nearClip = 1;
camera.farClip = 1000;
camera.clearColor = this._backgroundColor;
camera.clearDepth = 1.0;
camera.clearStencil = 0.0;
camera.clearFlag = this._clearFlag;
camera.visibility = this._visibility;
camera.aperture = CameraAperture.F16_0;
camera.shutter = CameraShutter.D125;
camera.iso = CameraISO.ISO100;
return camera;
}
private _resetCameraParams (): void {
@@ -478,10 +510,10 @@ export class ReflectionProbe {
return rt;
}
private _transformReflectionCamera (sourceCamera: Camera): void {
private _transformReflectionCamera (sourceCamera: Camera, targetCamera: Camera = this.camera): void {
const offset = Vec3.dot(this.node.worldPosition, this.node.up);
this._reflect(this._cameraWorldPos, sourceCamera.node.worldPosition, this.node.up, offset);
this.cameraNode.worldPosition = this._cameraWorldPos;
targetCamera.node.worldPosition = this._cameraWorldPos;
Vec3.transformQuat(this._forward, Vec3.FORWARD, sourceCamera.node.worldRotation);
this._reflect(this._forward, this._forward, this.node.up, 0);
@@ -494,14 +526,14 @@ export class ReflectionProbe {
Quat.fromViewUp(this._cameraWorldRotation, this._forward, this._up);
this.cameraNode.worldRotation = this._cameraWorldRotation;
targetCamera.node.worldRotation = this._cameraWorldRotation;
this.camera.update(true);
targetCamera.update(true);
// Transform the plane from world space to reflection camera space use the inverse transpose matrix
const viewSpaceProbe = new Vec4(this.node.up.x, this.node.up.y, this.node.up.z, -Vec3.dot(this.node.up, this.node.worldPosition));
viewSpaceProbe.transformMat4(this.camera.matView.clone().invert().transpose());
this.camera.calculateObliqueMat(viewSpaceProbe);
viewSpaceProbe.transformMat4(targetCamera.matView.clone().invert().transpose());
targetCamera.calculateObliqueMat(viewSpaceProbe);
}
private _reflect (out: Vec3, point: Vec3, normal: Vec3, offset: number): Vec3 {
@@ -27,7 +27,7 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow';
import { ReflectionProbeStage } from './reflection-probe-stage';
import { RenderFlowTag } from '../pipeline-serialization';
import { RenderPipeline } from '../render-pipeline';
import { Camera } from '../../render-scene/scene/camera';
import { Camera, CameraUsage } from '../../render-scene/scene/camera';
import { ProbeType, ReflectionProbe } from '../../render-scene/scene/reflection-probe';
import { cclegacy } from '../../core';
@@ -65,7 +65,13 @@ export class ReflectionProbeFlow extends RenderFlow {
const probes = cclegacy.internal.reflectionProbeManager.getProbes() as ReflectionProbe[];
for (let i = 0; i < probes.length; i++) {
if (probes[i].needRender) {
if (EDITOR || probes[i].probeType === ProbeType.PLANAR) {
if (probes[i].probeType === ProbeType.PLANAR) {
let reflectionCamera: Camera | undefined;
if (EDITOR && camera.cameraUsage === CameraUsage.PREVIEW) {
reflectionCamera = probes[i].renderPreviewPlanarReflection(camera);
}
this._renderStage(camera, probes[i], reflectionCamera);
} else if (EDITOR) {
this._renderStage(camera, probes[i]);
}
}
@@ -75,12 +81,12 @@ export class ReflectionProbeFlow extends RenderFlow {
public destroy (): void {
super.destroy();
}
private _renderStage (camera: Camera, probe: ReflectionProbe): void {
private _renderStage (camera: Camera, probe: ReflectionProbe, reflectionCamera?: Camera): void {
for (let i = 0; i < this._stages.length; i++) {
const probeStage = this._stages[i] as ReflectionProbeStage;
if (probe.probeType === ProbeType.PLANAR) {
cclegacy.internal.reflectionProbeManager.updatePlanarMap(probe, null);
probeStage.setUsageInfo(probe, probe.realtimePlanarTexture!.window!.framebuffer);
probeStage.setUsageInfo(probe, probe.realtimePlanarTexture!.window!.framebuffer, reflectionCamera);
probeStage.render(camera);
cclegacy.internal.reflectionProbeManager.updatePlanarMap(probe, probe.realtimePlanarTexture!.getGFXTexture());
} else {
@@ -55,6 +55,7 @@ export class ReflectionProbeStage extends RenderStage {
private _frameBuffer: Framebuffer | null = null;
private _renderArea = new Rect();
private _probe: ReflectionProbe | null = null;
private _reflectionCamera: Camera | null = null;
private _probeRenderQueue!: RenderReflectionProbeQueue;
private _rgbeColor = new Vec3();
@@ -68,9 +69,10 @@ export class ReflectionProbeStage extends RenderStage {
* @param probe
* @param frameBuffer
*/
public setUsageInfo (probe: ReflectionProbe, frameBuffer: Framebuffer): void {
public setUsageInfo (probe: ReflectionProbe, frameBuffer: Framebuffer, reflectionCamera?: Camera): void {
this._probe = probe;
this._frameBuffer = frameBuffer;
this._reflectionCamera = reflectionCamera ?? null;
}
public destroy (): void {
@@ -108,8 +110,9 @@ export class ReflectionProbeStage extends RenderStage {
public render (camera: Camera): void {
const pipeline = this._pipeline;
const cmdBuff = pipeline.commandBuffers[0];
this._probeRenderQueue.gatherRenderObjects(this._probe!, camera, cmdBuff);
pipeline.pipelineUBO.updateCameraUBO(this._probe!.camera);
const probeCamera = this._reflectionCamera ?? this._probe!.camera;
this._probeRenderQueue.gatherRenderObjects(this._probe!, camera, cmdBuff, probeCamera);
pipeline.pipelineUBO.updateCameraUBO(probeCamera);
this._renderArea.x = 0;
this._renderArea.y = 0;
@@ -118,10 +121,10 @@ export class ReflectionProbeStage extends RenderStage {
const renderPass = this._frameBuffer!.renderPass;
if (this._probe!.camera.clearFlag & ClearFlagBit.COLOR) {
this._rgbeColor.x = this._probe!.camera.clearColor.x;
this._rgbeColor.y = this._probe!.camera.clearColor.y;
this._rgbeColor.z = this._probe!.camera.clearColor.z;
if (probeCamera.clearFlag & ClearFlagBit.COLOR) {
this._rgbeColor.x = probeCamera.clearColor.x;
this._rgbeColor.y = probeCamera.clearColor.y;
this._rgbeColor.z = probeCamera.clearColor.z;
const rgbe = packRGBE(this._rgbeColor);
colors[0].x = rgbe.x;
colors[0].y = rgbe.y;
@@ -134,8 +137,8 @@ export class ReflectionProbeStage extends RenderStage {
this._frameBuffer!,
this._renderArea,
colors,
this._probe!.camera.clearDepth,
this._probe!.camera.clearStencil,
probeCamera.clearDepth,
probeCamera.clearStencil,
);
cmdBuff.bindDescriptorSet(SetIndex.GLOBAL, pipeline.descriptorSet);
@@ -79,13 +79,14 @@ export class RenderReflectionProbeQueue {
public constructor (pipeline: PipelineRuntime) {
this._pipeline = pipeline;
}
public gatherRenderObjects (probe: ReflectionProbe, camera: Camera, cmdBuff: CommandBuffer): void {
public gatherRenderObjects (probe: ReflectionProbe, camera: Camera, cmdBuff: CommandBuffer, reflectionCamera?: Camera): void {
this.clear();
const scene = camera.scene!;
const sceneData = this._pipeline.pipelineSceneData;
const skybox = sceneData.skybox;
const probeCamera = reflectionCamera ?? probe.camera;
if (skybox.enabled && skybox.model && (probe.camera.clearFlag & SkyBoxFlagValue.VALUE)) {
if (skybox.enabled && skybox.model && (probeCamera.clearFlag & SkyBoxFlagValue.VALUE)) {
this.add(skybox.model);
}
@@ -105,7 +106,7 @@ export class RenderReflectionProbeQueue {
if (geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) {
this.add(model);
}
} else if (geometry.intersect.aabbFrustum(model.worldBounds, probe.camera.frustum)) {
} else if (geometry.intersect.aabbFrustum(model.worldBounds, probeCamera.frustum)) {
this.add(model);
}
}
@@ -434,7 +434,8 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder
// Reflection Probe
cameraConfigs.enablePlanarReflectionProbe = cameraConfigs.isMainGameWindow
|| camera.cameraUsage === CameraUsage.SCENE_VIEW
|| camera.cameraUsage === CameraUsage.GAME_VIEW;
|| camera.cameraUsage === CameraUsage.GAME_VIEW
|| camera.cameraUsage === CameraUsage.PREVIEW;
// MSAA
cameraConfigs.enableMSAA = cameraConfigs.settings.msaa.enabled
@@ -551,7 +552,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder
ppl, camera, pplConfigs.mobileMaxSpotLightShadowMaps);
}
this._tryAddReflectionProbePasses(ppl, cameraConfigs, id, mainLight, camera.scene);
this._tryAddReflectionProbePasses(ppl, cameraConfigs, id, mainLight, camera.scene, camera);
if (cameraConfigs.remainingPasses > 0 || cameraConfigs.enableShadingScale) {
context.colorName = cameraConfigs.enableShadingScale
@@ -630,6 +631,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder
id: number,
mainLight: renderer.scene.DirectionalLight | null,
scene: renderer.RenderScene | null,
sourceCamera: renderer.scene.Camera,
): void {
const reflectionProbeManager = cclegacy.internal.reflectionProbeManager as ReflectionProbeManager | undefined;
if (!reflectionProbeManager) {
@@ -651,6 +653,10 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder
if (!cameraConfigs.enablePlanarReflectionProbe) {
continue;
}
let reflectionCamera = probe.camera;
if (EDITOR && sourceCamera.cameraUsage === CameraUsage.PREVIEW) {
reflectionCamera = probe.renderPreviewPlanarReflection(sourceCamera);
}
const window: renderer.RenderWindow = probe.realtimePlanarTexture!.window!;
const colorName = `PlanarProbeRT${probeID}`;
const depthStencilName = `PlanarProbeDS${probeID}`;
@@ -663,7 +669,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder
// Rendering
const probePass = ppl.addRenderPass(width, height, 'default');
probePass.name = `PlanarReflectionProbe${probeID}`;
this._buildReflectionProbePass(probePass, cameraConfigs, id, probe.camera,
this._buildReflectionProbePass(probePass, cameraConfigs, id, reflectionCamera,
colorName, depthStencilName, mainLight, scene);
} else if (EDITOR) {
for (let faceIdx = 0; faceIdx < probe.bakedCubeTextures.length; faceIdx++) {