From b2b0d09e1dce118eccaf419610d427bf8733ca5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=85=B5?= <49358166+xubing0906@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:37:11 +0800 Subject: [PATCH] reflection probe planar reflection for native engine (#13281) --- cc.config.json | 1 + cocos/3d/framework/mesh-renderer.ts | 2 + cocos/misc/reflection-probe-component.ts | 60 +++--- cocos/render-scene/scene/index.jsb.ts | 1 + .../scene/reflection-probe.jsb.ts | 43 ++++ cocos/render-scene/scene/reflection-probe.ts | 33 ++- native/CMakeLists.txt | 10 + native/cocos/math/Quaternion.cpp | 3 +- .../cocos/renderer/pipeline/PipelineUBO.cpp | 11 +- native/cocos/renderer/pipeline/PipelineUBO.h | 3 + .../pipeline/ReflectionProbeBatchedQueue.cpp | 197 ++++++++++++++++++ .../pipeline/ReflectionProbeBatchedQueue.h | 80 +++++++ .../pipeline/forward/ForwardPipeline.cpp | 5 + .../reflection-probe/ReflectionProbeFlow.cpp | 88 ++++++++ .../reflection-probe/ReflectionProbeFlow.h | 57 +++++ .../reflection-probe/ReflectionProbeStage.cpp | 100 +++++++++ .../reflection-probe/ReflectionProbeStage.h | 67 ++++++ native/cocos/scene/Model.h | 2 +- native/cocos/scene/ReflectionProbe.cpp | 175 ++++++++++++++++ native/cocos/scene/ReflectionProbe.h | 184 ++++++++++++++++ native/cocos/scene/ReflectionProbeManager.cpp | 52 +++++ native/cocos/scene/ReflectionProbeManager.h | 49 +++++ native/tools/swig-config/scene.i | 11 + .../tests/stats-query/cc.config.json | 1 + 24 files changed, 1181 insertions(+), 54 deletions(-) create mode 100644 cocos/render-scene/scene/reflection-probe.jsb.ts create mode 100644 native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp create mode 100644 native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h create mode 100644 native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.cpp create mode 100644 native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.h create mode 100644 native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.cpp create mode 100644 native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.h create mode 100644 native/cocos/scene/ReflectionProbe.cpp create mode 100644 native/cocos/scene/ReflectionProbe.h create mode 100644 native/cocos/scene/ReflectionProbeManager.cpp create mode 100644 native/cocos/scene/ReflectionProbeManager.h diff --git a/cc.config.json b/cc.config.json index 5610b44a33..77f5d2c9da 100644 --- a/cc.config.json +++ b/cc.config.json @@ -217,6 +217,7 @@ "cocos/render-scene/core/render-scene.ts": "cocos/render-scene/core/render-scene.jsb.ts", "cocos/render-scene/scene/submodel.ts": "cocos/render-scene/scene/submodel.jsb.ts", "cocos/render-scene/scene/index.ts": "cocos/render-scene/scene/index.jsb.ts", + "cocos/render-scene/scene/reflection-probe.ts": "cocos/render-scene/scene/reflection-probe.jsb.ts", "cocos/2d/renderer/native-2d.ts": "cocos/2d/renderer/native-2d.jsb.ts", "cocos/gfx/base/pipeline-state.ts": "cocos/gfx/base/pipeline-state.jsb.ts", "cocos/gfx/index.ts": "cocos/gfx/index.jsb.ts", diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index 6754e0ed11..16e1333820 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -852,6 +852,7 @@ export class MeshRenderer extends ModelRenderer { } protected _updateReflectionProbeRenderInfo () { + if (!this._model) { return; } if (this.reflectionProbe !== ReflectionProbeType.NONE) { for (let i = 0; i < this._materials.length; i++) { const mat = this.getMaterialInstance(i); @@ -860,6 +861,7 @@ export class MeshRenderer extends ModelRenderer { } } } + this._model.reflectionProbeType = this._reflectionProbeType; this._onUpdateReflectionProbeTexture(); } diff --git a/cocos/misc/reflection-probe-component.ts b/cocos/misc/reflection-probe-component.ts index 570c4e2d90..be77381328 100644 --- a/cocos/misc/reflection-probe-component.ts +++ b/cocos/misc/reflection-probe-component.ts @@ -28,47 +28,35 @@ import { CCObject, Color, Enum, size, Vec3 } from '../core'; import { TextureCube } from '../asset/assets'; import { scene } from '../render-scene'; -import { ProbeClearFlag, ProbeType } from '../render-scene/scene/reflection-probe'; import { CAMERA_DEFAULT_MASK } from '../rendering/define'; import { ReflectionProbeManager } from '../rendering/reflection-probe-manager'; import { Component } from '../scene-graph/component'; import { Layers } from '../scene-graph/layers'; import { Camera } from './camera-component'; +import { Node } from '../scene-graph'; +import { ProbeClearFlag, ProbeType } from '../render-scene/scene/reflection-probe'; export enum ProbeResolution { - /** - * @zh 分辨率 128 * 128。 - * @en renderTexture resolution 128 * 128. - * @readonly - */ - Low_128x128= 128, /** * @zh 分辨率 256 * 256。 * @en renderTexture resolution 256 * 256. * @readonly */ - Low_256x256= 256, + Low_256x256 = 256, /** * @zh 分辨率 512 * 512。 * @en renderTexture resolution 512 * 512. * @readonly */ - Medium_512x512= 512, + Medium_512x512 = 512, /** - * @zh 分辨率 1024 * 1024。 - * @en renderTexture resolution 1024 * 1024. + * @zh 分辨率 768 * 768 + * @en renderTexture resolution 768 * 768. * @readonly */ - High_1024x1024= 1024, - - /** - * @zh 分辨率 2048 * 2048。 - * @en renderTexture resolution 2048 * 2048. - * @readonly - */ - Ultra_2048x2048= 2048, + High_768x768 = 768, } @ccclass('cc.ReflectionProbe') @menu('Rendering/ReflectionProbe') @@ -135,13 +123,13 @@ export class ReflectionProbe extends Component { if (EDITOR) { this._objFlags |= CCObject.Flags.IsRotationLocked; } - } else { + } else { this._size.set(ReflectionProbe.DEFAULT_PLANER_SIZE); if (EDITOR && this._objFlags & CCObject.Flags.IsRotationLocked) { this._objFlags ^= CCObject.Flags.IsRotationLocked; } if (!this._sourceCamera) { - console.error('the reflection camera is invalid, please set the reflection camera'); + console.warn('the reflection camera is invalid, please set the reflection camera'); return; } this.probe.switchProbeType(value, this._sourceCamera.camera); @@ -276,10 +264,10 @@ export class ReflectionProbe extends Component { } } else { if (EDITOR) { - const cameraLst: scene.Camera[]|undefined = this.node.scene.renderScene?.cameras; + const cameraLst: scene.Camera[] | undefined = this.node.scene.renderScene?.cameras; if (cameraLst !== undefined) { for (let i = 0; i < cameraLst.length; ++i) { - const camera:scene.Camera = cameraLst[i]; + const camera: scene.Camera = cameraLst[i]; if (camera.name === 'Editor Camera') { this.probe.renderPlanarReflection(camera); break; @@ -305,16 +293,22 @@ export class ReflectionProbe extends Component { this._probeId = this.node.scene.getNewReflectionProbeId(); } this._probe = new scene.ReflectionProbe(this._probeId); - this._probe.initialize(this.node); - if (this.enabled) { - ReflectionProbeManager.probeManager.register(this._probe); + if (this._probe) { + const cameraNode = new Node('ReflectionProbeCamera'); + cameraNode.hideFlags |= CCObject.Flags.DontSave | CCObject.Flags.HideInHierarchy; + this.node.scene.addChild(cameraNode); + + this._probe.initialize(this.node, cameraNode); + if (this.enabled) { + ReflectionProbeManager.probeManager.register(this._probe); + } + this._probe.resolution = this._resolution; + this._probe.clearFlag = this._clearFlag; + this._probe.backgroundColor = this._backgroundColor; + this._probe.visibility = this._visibility; + this._probe.probeType = this._probeType; + this._probe.size = this._size; + this._probe.cubemap = this._cubemap!; } - this._probe.resolution = this._resolution; - this._probe.clearFlag = this._clearFlag; - this._probe.backgroundColor = this._backgroundColor; - this._probe.visibility = this._visibility; - this._probe.probeType = this._probeType; - this._probe.size = this._size; - this._probe.cubemap = this._cubemap!; } } diff --git a/cocos/render-scene/scene/index.jsb.ts b/cocos/render-scene/scene/index.jsb.ts index bd6af3c30d..16f48768d1 100644 --- a/cocos/render-scene/scene/index.jsb.ts +++ b/cocos/render-scene/scene/index.jsb.ts @@ -297,3 +297,4 @@ cclegacy.Skybox = Skybox; export * from './model'; export * from './submodel'; export * from './camera'; +export * from './reflection-probe'; diff --git a/cocos/render-scene/scene/reflection-probe.jsb.ts b/cocos/render-scene/scene/reflection-probe.jsb.ts new file mode 100644 index 0000000000..8f764bdd2a --- /dev/null +++ b/cocos/render-scene/scene/reflection-probe.jsb.ts @@ -0,0 +1,43 @@ +/* + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ +import { ClearFlagBit } from '../../gfx'; +import { SKYBOX_FLAG } from './camera'; +declare const jsb: any; + +export enum ProbeClearFlag { + SKYBOX = SKYBOX_FLAG | ClearFlagBit.DEPTH_STENCIL, + SOLID_COLOR = ClearFlagBit.ALL, +} + +export enum ProbeType { + CUBE = 0, + PLANAR = 1, +} + +export const ReflectionProbe = jsb.ReflectionProbe; +const reflectionProbeProto: any = jsb.ReflectionProbe.prototype; +reflectionProbeProto._ctor = function (id:number) { + this._probeId = id; +}; diff --git a/cocos/render-scene/scene/reflection-probe.ts b/cocos/render-scene/scene/reflection-probe.ts index c86066f544..76aa952aab 100644 --- a/cocos/render-scene/scene/reflection-probe.ts +++ b/cocos/render-scene/scene/reflection-probe.ts @@ -25,20 +25,20 @@ import { EDITOR } from 'internal:constants'; import { Camera, CameraAperture, CameraFOVAxis, CameraISO, CameraProjection, CameraShutter, CameraType, SKYBOX_FLAG, TrackingType } from './camera'; import { Node } from '../../scene-graph/node'; -import { CCObject, Color, Quat, Rect, toRadian, Vec2, Vec3, geometry, cclegacy, Size } from '../../core'; +import { Color, Quat, Rect, toRadian, Vec2, Vec3, geometry, cclegacy } from '../../core'; import { CAMERA_DEFAULT_MASK } from '../../rendering/define'; import { ClearFlagBit } from '../../gfx'; import { TextureCube } from '../../asset/assets/texture-cube'; import { RenderTexture } from '../../asset/assets/render-texture'; export enum ProbeClearFlag { - SKYBOX= SKYBOX_FLAG | ClearFlagBit.DEPTH_STENCIL, - SOLID_COLOR= ClearFlagBit.ALL, + SKYBOX = SKYBOX_FLAG | ClearFlagBit.DEPTH_STENCIL, + SOLID_COLOR = ClearFlagBit.ALL, } export enum ProbeType { - CUBE= 0, - PLANAR= 1, + CUBE = 0, + PLANAR = 1, } // right left up down front back const cameraDir: Vec3[] = [ @@ -234,6 +234,9 @@ export class ReflectionProbe { return this._boundingBox; } + set cameraNode (node:Node) { + this._cameraNode = node; + } get cameraNode () { return this._cameraNode!; } @@ -242,15 +245,12 @@ export class ReflectionProbe { this._probeId = id; } - public initialize (node: Node) { + public initialize (node: Node, cameraNode:Node) { this._node = node; - this._cameraNode = new Node('ReflectionProbeCamera'); - this._cameraNode.hideFlags |= CCObject.Flags.DontSave | CCObject.Flags.HideInHierarchy; - node.scene.addChild(this._cameraNode); - + this._cameraNode = cameraNode; const pos = this.node.getWorldPosition(); this._boundingBox = geometry.AABB.create(pos.x, pos.y, pos.z, this._size.x, this._size.y, this._size.z); - this._createCamera(); + this._createCamera(cameraNode); } public initBakedTextures () { @@ -353,14 +353,14 @@ export class ReflectionProbe { } } - private _createCamera () { + private _createCamera (cameraNode:Node) { const root = cclegacy.director.root; if (!this._camera) { this._camera = (cclegacy.director.root).createCamera(); if (!this._camera) return null; this._camera.initialize({ - name: this.cameraNode.name, - node: this.cameraNode, + name: cameraNode.name, + node: cameraNode, projection: CameraProjection.PERSPECTIVE, window: EDITOR ? cclegacy.director.root && cclegacy.director.root.mainWindow : cclegacy.director.root && cclegacy.director.root.tempWindow, @@ -420,9 +420,8 @@ export class ReflectionProbe { this._forward.normalize(); this._forward.negative(); - const up = new Vec3(); - Vec3.transformQuat(up, Vec3.UP, sourceCamera.node.worldRotation); - this._reflect(this._up, up, Vec3.UP, 0); + Vec3.transformQuat(this._up, Vec3.UP, sourceCamera.node.worldRotation); + this._reflect(this._up, this._up, Vec3.UP, 0); this._up.normalize(); Quat.fromViewUp(this._cameraWorldRotation, this._forward, this._up); diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index ba7cfb17c2..239b998cbb 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -1350,6 +1350,12 @@ cocos_source_files( cocos/renderer/pipeline/custom/RenderInterfaceImpl.cpp cocos/renderer/pipeline/custom/RenderInterfaceTypes.cpp cocos/renderer/pipeline/custom/RenderInterfaceTypes.h + cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.cpp + cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.h + cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.cpp + cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.h + cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp + cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h ) if (USE_GEOMETRY_RENDERER) @@ -1411,6 +1417,10 @@ cocos_source_files( cocos/scene/Octree.cpp cocos/scene/Shadow.h cocos/scene/Shadow.cpp + cocos/scene/ReflectionProbe.h + cocos/scene/ReflectionProbe.cpp + cocos/scene/ReflectionProbeManager.cpp + cocos/scene/ReflectionProbeManager.h ) ##### primitive diff --git a/native/cocos/math/Quaternion.cpp b/native/cocos/math/Quaternion.cpp index 9662c4e8f8..9ddf869036 100644 --- a/native/cocos/math/Quaternion.cpp +++ b/native/cocos/math/Quaternion.cpp @@ -360,7 +360,8 @@ void Quaternion::fromMat3(const Mat3 &m, Quaternion *out) { out->y = (m06 - m02) * s; out->z = (m01 - m03) * s; } else if ((m00 > m04) && (m00 > m08)) { - const float s = 2.F * sqrtf(1.F + m04 - m00 - m08); + //m00 - m04 - m08 consistent with ts engine, otherwise y-axis rotation greater than 90 degrees will not get the correct result + const float s = 2.F * sqrtf(1.F + m00 - m04 - m08); out->w = (m05 - m07) / s; out->x = 0.25F * s; diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index 94485d7f8b..956fc72054 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -90,7 +90,10 @@ void PipelineUBO::updateGlobalUBOView(const scene::Camera *camera, ccstd::array< } void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *output, const scene::Camera *camera) { - const auto *const scene = camera->getScene(); + updateCameraUBOView(pipeline, output, camera, camera->getScene()); +} + +void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *output, const scene::Camera *camera, const scene::RenderScene *scene) { const scene::DirectionalLight *mainLight = scene->getMainLight(); const auto *sceneData = pipeline->getPipelineSceneData(); const scene::Shadows *const shadowInfo = sceneData->getShadows(); @@ -492,8 +495,12 @@ void PipelineUBO::updateGlobalUBO(const scene::Camera *camera) { } void PipelineUBO::updateCameraUBO(const scene::Camera *camera) { + updateCameraUBO(camera, camera->getScene()); +} + +void PipelineUBO::updateCameraUBO(const scene::Camera *camera, const scene::RenderScene *scene) { auto *const cmdBuffer = _pipeline->getCommandBuffers()[0]; - updateCameraUBOView(_pipeline, _cameraUBOs.data(), camera); + updateCameraUBOView(_pipeline, _cameraUBOs.data(), camera, scene); cmdBuffer->updateBuffer(_cameraBuffer, _cameraUBOs.data()); } diff --git a/native/cocos/renderer/pipeline/PipelineUBO.h b/native/cocos/renderer/pipeline/PipelineUBO.h index 883423d1df..1d0b20ed78 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.h +++ b/native/cocos/renderer/pipeline/PipelineUBO.h @@ -34,6 +34,7 @@ namespace scene { class Camera; class Shadows; class DirectionalLight; +class RenderScene; } // namespace scene namespace pipeline { class RenderPipeline; @@ -42,6 +43,7 @@ class CC_DLL PipelineUBO final { public: static void updateGlobalUBOView(const scene::Camera *camera, ccstd::array *bufferView); static void updateCameraUBOView(const RenderPipeline *pipeline, float *output, const scene::Camera *camera); + static void updateCameraUBOView(const RenderPipeline *pipeline, float *output, const scene::Camera *camera, const scene::RenderScene *renderScene); static void updateShadowUBOView(const RenderPipeline *pipeline, ccstd::array *shadowBufferView, ccstd::array *csmBufferView, const scene::Camera *camera); static void updateShadowUBOLightView(const RenderPipeline *pipeline, ccstd::array *shadowBufferView, @@ -55,6 +57,7 @@ public: void destroy(); void updateGlobalUBO(const scene::Camera *camera); void updateCameraUBO(const scene::Camera *camera); + void updateCameraUBO(const scene::Camera *camera, const scene::RenderScene *scene); void updateMultiCameraUBO(GlobalDSManager *globalDSMgr, const ccstd::vector &cameras); void updateShadowUBO(const scene::Camera *camera); void updateShadowUBOLight(gfx::DescriptorSet *globalDS, const scene::Light *light, uint32_t level = 0U); diff --git a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp new file mode 100644 index 0000000000..24c9d69924 --- /dev/null +++ b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp @@ -0,0 +1,197 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "ReflectionProbeBatchedQueue.h" +#include "BatchedBuffer.h" +#include "Define.h" +#include "InstancedBuffer.h" +#include "PipelineSceneData.h" +#include "PipelineStateManager.h" +#include "RenderBatchedQueue.h" +#include "RenderInstancedQueue.h" +#include "core/geometry/AABB.h" +#include "forward/ForwardPipeline.h" +#include "gfx-base/GFXCommandBuffer.h" +#include "gfx-base/GFXDevice.h" +#include "renderer/core/ProgramLib.h" +#include "scene/ReflectionProbeManager.h" +#include "scene/Camera.h" +#include "scene/ReflectionProbe.h" +#include "scene/Skybox.h" +namespace cc { +namespace pipeline { + +ReflectionProbeBatchedQueue::ReflectionProbeBatchedQueue(RenderPipeline *pipeline) +: _phaseID(getPhaseID("default")), _phaseReflectMapID(getPhaseID("reflect-map")) { + _pipeline = pipeline; + _instancedQueue = ccnew RenderInstancedQueue; + _batchedQueue = ccnew RenderBatchedQueue; +} + +ReflectionProbeBatchedQueue::~ReflectionProbeBatchedQueue() { + destroy(); +} + +void ReflectionProbeBatchedQueue::destroy() { + CC_SAFE_DELETE(_batchedQueue) + CC_SAFE_DELETE(_instancedQueue) +} + +void ReflectionProbeBatchedQueue::gatherRenderObjects(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer, const scene::ReflectionProbe *probe) { + if (probe == nullptr) { + return; + } + clear(); + const PipelineSceneData *sceneData = _pipeline->getPipelineSceneData(); + + const scene::Skybox *skyBox = sceneData->getSkybox(); + const scene::RenderScene *const scene = camera->getScene(); + + if (static_cast(camera->getClearFlag()) & skyboxFlag) { + if (skyBox != nullptr && skyBox->isEnabled() && skyBox->getModel()) { + add(skyBox->getModel()); + } + } + + const auto &renderObjects = sceneData->getRenderObjects(); + for (const auto &ro : renderObjects) { + const auto *const model = ro.model; + const auto *modelWorldBounds = model->getWorldBounds(); + if (!modelWorldBounds) { + add(model); + continue; + } + const auto *probeBoundingBox = probe->getBoundingBox(); + if (modelWorldBounds->aabbAabb(*probeBoundingBox)) { + add(model); + } + } + + _instancedQueue->uploadBuffers(cmdBuffer); + _batchedQueue->uploadBuffers(cmdBuffer); +} + +void ReflectionProbeBatchedQueue::clear() { + _subModels.clear(); + _shaders.clear(); + _passes.clear(); + if (_instancedQueue) _instancedQueue->clear(); + if (_batchedQueue) _batchedQueue->clear(); +} + +void ReflectionProbeBatchedQueue::add(const scene::Model *model) { + for (const auto &subModel : model->getSubModels()) { + auto passIdx = getReflectMapPassIndex(subModel); + bool bUseReflectPass = true; + if (passIdx == -1) { + passIdx = getDefaultPassIndex(subModel); + bUseReflectPass = false; + } + if (passIdx == -1) { + continue; + } + + auto *pass = subModel->getPass(passIdx); + const auto batchingScheme = pass->getBatchingScheme(); + + if (batchingScheme == scene::BatchingSchemes::INSTANCING) { + auto *instancedBuffer = subModel->getPass(passIdx)->getInstancedBuffer(); + instancedBuffer->merge(subModel, passIdx); + _instancedQueue->add(instancedBuffer); + } else if (batchingScheme == scene::BatchingSchemes::VB_MERGING) { + auto *batchedBuffer = subModel->getPass(passIdx)->getBatchedBuffer(); + batchedBuffer->merge(subModel, passIdx, model); + _batchedQueue->add(batchedBuffer); + } else { // standard draw + if (!bUseReflectPass) { + auto &defines = pass->getDefines(); + defines["CC_USE_RGBE_OUTPUT"] = true; + subModel->onPipelineStateChanged(); + } + _subModels.emplace_back(subModel); + _shaders.emplace_back(subModel->getShader(passIdx)); + _passes.emplace_back(pass); + } + } +} + +void ReflectionProbeBatchedQueue::recordCommandBuffer(gfx::Device *device, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer) const { + _instancedQueue->recordCommandBuffer(device, renderPass, cmdBuffer); + _batchedQueue->recordCommandBuffer(device, renderPass, cmdBuffer); + + for (size_t i = 0; i < _subModels.size(); i++) { + const auto *const subModel = _subModels[i]; + auto *const shader = _shaders[i]; + const auto *pass = _passes[i]; + auto *const ia = subModel->getInputAssembler(); + auto *const pso = PipelineStateManager::getOrCreatePipelineState(pass, shader, ia, renderPass); + + cmdBuffer->bindPipelineState(pso); + cmdBuffer->bindDescriptorSet(materialSet, pass->getDescriptorSet()); + cmdBuffer->bindDescriptorSet(localSet, subModel->getDescriptorSet()); + cmdBuffer->bindInputAssembler(ia); + cmdBuffer->draw(ia); + } + resetMacro(); +} +void ReflectionProbeBatchedQueue::resetMacro() const { + for (size_t i = 0; i < _subModels.size(); i++) { + const auto *subModel = const_cast(_subModels[i]); + auto *pass = _passes[i]; + auto &defines = pass->getDefines(); + defines["CC_USE_RGBE_OUTPUT"] = false; + const_cast(subModel)->onPipelineStateChanged(); + } +} + +bool ReflectionProbeBatchedQueue::isUseReflectMapPass(const scene::SubModel *subModel) const { + auto passIdx = getReflectMapPassIndex(subModel); + return passIdx != -1; +} + +int ReflectionProbeBatchedQueue::getDefaultPassIndex(const scene::SubModel *subModel) const { + int i = 0; + for (const auto &pass : subModel->getPasses()) { + if (pass->getPhase() == _phaseID) { + return i; + } + ++i; + } + return -1; +} + +int ReflectionProbeBatchedQueue::getReflectMapPassIndex(const scene::SubModel *subModel) const { + int i = 0; + for (const auto &pass : subModel->getPasses()) { + if (pass->getPhase() == _phaseReflectMapID) { + return i; + } + ++i; + } + return -1; +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h new file mode 100644 index 0000000000..9c2d89729e --- /dev/null +++ b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h @@ -0,0 +1,80 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "Define.h" +#include "cocos/base/Macros.h" +namespace cc { +namespace scene { +class Camera; +class Pass; +class ReflectionProbe; +} // namespace scene +namespace pipeline { +struct RenderObject; +class RenderInstancedQueue; +class RenderBatchedQueue; +class RenderPipeline; + +//const uint32_t phaseID(PassPhase::getPhaseID("shadow-caster")); + +class CC_DLL ReflectionProbeBatchedQueue final { +public: + explicit ReflectionProbeBatchedQueue(RenderPipeline *); + ~ReflectionProbeBatchedQueue(); + + void destroy(); + void clear(); + void gatherRenderObjects(const scene::Camera *, gfx::CommandBuffer *, const scene::ReflectionProbe *probe); + void add(const scene::Model *); + void recordCommandBuffer(gfx::Device *, gfx::RenderPass *, gfx::CommandBuffer *) const; + + void resetMacro()const; + + bool isUseReflectMapPass(const scene::SubModel *subModel) const; + +private: + int getDefaultPassIndex(const scene::SubModel *subModel) const; + int getReflectMapPassIndex(const scene::SubModel *subModel) const; + + // weak reference + RenderPipeline *_pipeline{nullptr}; + // weak reference + ccstd::vector _subModels; + // weak reference + ccstd::vector _passes; + // weak reference + ccstd::vector _shaders; + // manage memory manually + RenderInstancedQueue *_instancedQueue{nullptr}; + // manage memory manually + RenderBatchedQueue *_batchedQueue{nullptr}; + uint32_t _phaseID{0}; + uint32_t _phaseReflectMapID{0}; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp index 20d4bd5fbf..a1d2753f5f 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp @@ -35,6 +35,7 @@ #include "profiler/Profiler.h" #include "scene/Camera.h" #include "scene/RenderScene.h" +#include "../reflection-probe/ReflectionProbeFlow.h" namespace cc { namespace pipeline { @@ -64,6 +65,10 @@ bool ForwardPipeline::initialize(const RenderPipelineInfo &info) { shadowFlow->initialize(ShadowFlow::getInitializeInfo()); _flows.emplace_back(shadowFlow); + auto *reflectionProbeFlow = ccnew ReflectionProbeFlow(); + reflectionProbeFlow->initialize(ReflectionProbeFlow::getInitializeInfo()); + _flows.emplace_back(reflectionProbeFlow); + auto *forwardFlow = ccnew ForwardFlow; forwardFlow->initialize(ForwardFlow::getInitializeInfo()); _flows.emplace_back(forwardFlow); diff --git a/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.cpp b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.cpp new file mode 100644 index 0000000000..2d0dea2219 --- /dev/null +++ b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "ReflectionProbeFlow.h" + +#include "ReflectionProbeStage.h" +#include "pipeline/PipelineSceneData.h" +#include "scene/ReflectionProbeManager.h" +#include "pipeline/RenderPipeline.h" +#include "profiler/Profiler.h" +#include "scene/Camera.h" +#include "scene/ReflectionProbe.h" +namespace cc { +namespace pipeline { +RenderFlowInfo ReflectionProbeFlow::initInfo = { + "ReflectionProbeFlow", + static_cast(0), + static_cast(RenderFlowTag::SCENE), + {}, +}; +const RenderFlowInfo &ReflectionProbeFlow::getInitializeInfo() { return ReflectionProbeFlow::initInfo; } + +ReflectionProbeFlow::ReflectionProbeFlow() = default; +ReflectionProbeFlow::~ReflectionProbeFlow() = default; + +bool ReflectionProbeFlow::initialize(const RenderFlowInfo &info) { + RenderFlow::initialize(info); + if (_stages.empty()) { + auto *probeStage = ccnew ReflectionProbeStage; + probeStage->initialize(ReflectionProbeStage::getInitializeInfo()); + _stages.emplace_back(probeStage); + } + return true; +} + +void ReflectionProbeFlow::activate(RenderPipeline *pipeline) { + RenderFlow::activate(pipeline); +} + +void ReflectionProbeFlow::render(scene::Camera *camera) { + CC_PROFILE(ReflectionProbeFlowRender); + const auto *sceneData = _pipeline->getPipelineSceneData(); + const auto probes = scene::ReflectionProbeManager::getInstance()->getAllProbes(); + for (auto * probe : probes) { + if (probe->needRender()) { + renderStage(camera, probe); + } + } +} + +void ReflectionProbeFlow::renderStage(scene::Camera *camera, scene::ReflectionProbe *probe) { + for (auto &stage : _stages) { + auto * framebuffer = probe->getRealtimePlanarTexture()->getWindow()->getFramebuffer(); + auto *reflectionProbeStage = static_cast(stage.get()); + reflectionProbeStage->setUsage(framebuffer, probe); + reflectionProbeStage->render(camera); + probe->updatePlanarTexture(camera->getScene()); + } +} + +void ReflectionProbeFlow::destroy() { + RenderFlow::destroy(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.h b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.h new file mode 100644 index 0000000000..8a6c40e8c4 --- /dev/null +++ b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeFlow.h @@ -0,0 +1,57 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "../RenderFlow.h" +namespace cc { +namespace scene { +class ReflectionProbe; +} +namespace pipeline { +class ForwardPipeline; + +class CC_DLL ReflectionProbeFlow : public RenderFlow { +public: + ReflectionProbeFlow(); + ~ReflectionProbeFlow() override; + + static const RenderFlowInfo &getInitializeInfo(); + + bool initialize(const RenderFlowInfo &info) override; + + void activate(RenderPipeline *pipeline) override; + + void render(scene::Camera *camera) override; + + void destroy() override; + +private: + void renderStage(scene::Camera *camera, scene::ReflectionProbe *probe); + + static RenderFlowInfo initInfo; +}; +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.cpp b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.cpp new file mode 100644 index 0000000000..be7ce6e013 --- /dev/null +++ b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "ReflectionProbeStage.h" +#include "../Define.h" +#include "../PipelineUBO.h" +#include "../ReflectionProbeBatchedQueue.h" +#include "../RenderPipeline.h" +#include "scene/ReflectionProbeManager.h" +#include "profiler/Profiler.h" +#include "scene/Camera.h" +#include "scene/ReflectionProbe.h" +namespace cc { +namespace pipeline { + +ReflectionProbeStage::ReflectionProbeStage() = default; +ReflectionProbeStage::~ReflectionProbeStage() = default; + +RenderStageInfo ReflectionProbeStage::initInfo = { + "ReflectionStage", + static_cast(ForwardStagePriority::FORWARD), + static_cast(RenderFlowTag::SCENE), + {}}; +const RenderStageInfo &ReflectionProbeStage::getInitializeInfo() { return ReflectionProbeStage::initInfo; } + +bool ReflectionProbeStage::initialize(const RenderStageInfo &info) { + RenderStage::initialize(info); + RenderQueueDesc descriptor = {true, RenderQueueSortMode::BACK_TO_FRONT, {"default"}}; + _renderQueueDescriptors.emplace_back(std::move(descriptor)); + + return true; +} + +void ReflectionProbeStage::activate(RenderPipeline *pipeline, RenderFlow *flow) { + RenderStage::activate(pipeline, flow); + + _reflectionProbeBatchedQueue = ccnew ReflectionProbeBatchedQueue(pipeline); +} + +void ReflectionProbeStage::render(scene::Camera *camera) { + CC_PROFILE(ReflectionProbeStage); + const auto *sceneData = _pipeline->getPipelineSceneData(); + + auto *cmdBuffer = _pipeline->getCommandBuffers()[0]; + + _reflectionProbeBatchedQueue->gatherRenderObjects(camera, cmdBuffer, _probe); + + _pipeline->getPipelineUBO()->updateCameraUBO(_probe->getCamera(), camera->getScene()); + + _renderArea.x = 0; + _renderArea.y = 0; + _renderArea.width = _probe->getRealtimePlanarTexture()->getWidth(); + _renderArea.height = _probe->getRealtimePlanarTexture()->getHeight(); + + _clearColors[0] = _probe->getCamera()->getClearColor(); + auto *renderPass = _framebuffer->getRenderPass(); + + cmdBuffer->beginRenderPass(renderPass, _framebuffer, _renderArea, + _clearColors, _probe->getCamera()->getClearDepth(), _probe->getCamera()->getClearStencil()); + + const ccstd::array globalOffsets = {_pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()}; + cmdBuffer->bindDescriptorSet(globalSet, _pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data()); + _reflectionProbeBatchedQueue->recordCommandBuffer(_device, renderPass, cmdBuffer); + + cmdBuffer->endRenderPass(); + + _pipeline->getPipelineUBO()->updateCameraUBO(camera); +} + +void ReflectionProbeStage::destroy() { + _framebuffer = nullptr; + + CC_SAFE_DELETE(_reflectionProbeBatchedQueue); + RenderStage::destroy(); +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.h b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.h new file mode 100644 index 0000000000..9cdb7d0791 --- /dev/null +++ b/native/cocos/renderer/pipeline/reflection-probe/ReflectionProbeStage.h @@ -0,0 +1,67 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once +#include "../RenderStage.h" + +namespace cc { +namespace scene { +class ReflectionProbe; +} +namespace pipeline { +class RenderQueue; +class ReflectionProbeBatchedQueue; + +class CC_DLL ReflectionProbeStage : public RenderStage { +public: + ReflectionProbeStage(); + ~ReflectionProbeStage() override; + + static const RenderStageInfo &getInitializeInfo(); + + bool initialize(const RenderStageInfo &info) override; + void destroy() override; + void render(scene::Camera *camera) override; + void activate(RenderPipeline *pipeline, RenderFlow *flow) override; + + inline void setFramebuffer(gfx::Framebuffer *framebuffer) { _framebuffer = framebuffer; } + inline void setUsage(gfx::Framebuffer *framebuffer, scene::ReflectionProbe *probe) { + _framebuffer = framebuffer; + _probe = probe; + } + +private: + static RenderStageInfo initInfo; + + gfx::Rect _renderArea; + gfx::Framebuffer *_framebuffer = nullptr; + + scene::ReflectionProbe *_probe = nullptr; + + ReflectionProbeBatchedQueue *_reflectionProbeBatchedQueue = nullptr; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/scene/Model.h b/native/cocos/scene/Model.h index a28f4f87be..147aea1982 100644 --- a/native/cocos/scene/Model.h +++ b/native/cocos/scene/Model.h @@ -160,7 +160,7 @@ public: inline void setBakeToReflectionProbe(bool val) { _bakeToReflectionProbe = val; } - inline bool getReflectionProbeType() const { return _reflectionProbeType; } + inline int32_t getReflectionProbeType() const { return _reflectionProbeType; } inline void setReflectionProbeType(int32_t val) { _reflectionProbeType = val; } diff --git a/native/cocos/scene/ReflectionProbe.cpp b/native/cocos/scene/ReflectionProbe.cpp new file mode 100644 index 0000000000..4baae255a2 --- /dev/null +++ b/native/cocos/scene/ReflectionProbe.cpp @@ -0,0 +1,175 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "scene/ReflectionProbe.h" +#include "Define.h" +#include "core/scene-graph/Scene.h" +#include "math/Quaternion.h" +#include "scene/ReflectionProbeManager.h" +namespace cc { +namespace scene { +ReflectionProbe::ReflectionProbe(int32_t id) { + _probeId = id; + scene::ReflectionProbeManager::getInstance()->registerProbe(this); +} + +void ReflectionProbe::initialize(Node* probeNode, Node* cameraNode) { + _node = probeNode; + _cameraNode = cameraNode; + const Vec3 pos = probeNode->getWorldPosition(); + _boundingBox = geometry::AABB::create(pos.x, pos.y, pos.z, _size.x, _size.y, _size.z); + + if (!_camera) { + _camera = Root::getInstance()->createCamera(); + scene::ICameraInfo info; + info.name = cameraNode->getName(); + info.node = cameraNode; + info.projection = CameraProjection::PERSPECTIVE; + info.window = Root::getInstance()->getTempWindow(); + info.priority = 0; + info.cameraType = CameraType::DEFAULT; + info.trackingType = TrackingType::NO_TRACKING; + _camera->initialize(info); + } + _camera->setViewportInOrientedSpace(Rect(0, 0, 1, 1)); + _camera->setFovAxis(CameraFOVAxis::VERTICAL); + _camera->setFov(static_cast(mathutils::toRadian(90.0))); + _camera->setOrthoHeight(10.0); + + _camera->setNearClip(1.0); + _camera->setFarClip(1000.0); + _camera->setClearColor(_backgroundColor); + _camera->setClearDepth(1.0); + _camera->setClearStencil(0.0); + _camera->setClearFlag(_clearFlag); + _camera->setVisibility(_visibility); + _camera->setAperture(CameraAperture::F16_0); + _camera->setShutter(CameraShutter::D125); + _camera->setIso(CameraISO::ISO100); + + RenderWindow* win = Root::getInstance()->getMainWindow(); + _realtimePlanarTexture = ccnew RenderTexture(); + IRenderTextureCreateInfo info; + info.name = "realtimePlanarTexture"; + info.height = win->getHeight(); + info.width = win->getWidth(); + _realtimePlanarTexture->initialize(info); +} + +void ReflectionProbe::syncCameraParams(const Camera* camera) { + _camera->setProjectionType(camera->getProjectionType()); + _camera->setOrthoHeight(camera->getOrthoHeight()); + _camera->setNearClip(camera->getNearClip()); + _camera->setFarClip(camera->getFarClip()); + _camera->setFov(camera->getFov()); + _camera->setVisibility(camera->getVisibility()); + _camera->setClearFlag(camera->getClearFlag()); + _camera->setClearColor(camera->getClearColor()); + _camera->setPriority(camera->getPriority() - 1); +} + +void ReflectionProbe::renderPlanarReflection(const Camera* sourceCamera) { + if (!sourceCamera) return; + syncCameraParams(sourceCamera); + transformReflectionCamera(sourceCamera); + _needRender = true; +} + +void ReflectionProbe::transformReflectionCamera(const Camera* sourceCamera) { + int32_t offset = Vec3::dot(_node->getWorldPosition(), Vec3::UNIT_Y); + _cameraWorldPos = reflect(sourceCamera->getNode()->getWorldPosition(), Vec3::UNIT_Y, offset); + _cameraNode->setWorldPosition(_cameraWorldPos); + + _forward = Vec3::FORWARD; + _forward.transformQuat(sourceCamera->getNode()->getWorldRotation()); + _forward = reflect(_forward, Vec3::UNIT_Y, 0); + _forward.normalize(); + _forward *= -1; + + _up = Vec3::UNIT_Y; + _up.transformQuat(sourceCamera->getNode()->getWorldRotation()); + _up = reflect(_up, Vec3::UNIT_Y, 0); + _up.normalize(); + + Quaternion::fromViewUp(_forward, _up, &_cameraWorldRotation); + _cameraNode->setWorldRotation(_cameraWorldRotation); + _camera->update(true); +} + +Vec3 ReflectionProbe::reflect(const Vec3& point, const Vec3& normal, int32_t offset) { + Vec3 n = normal; + n.normalize(); + float dist = Vec3::dot(n, point) - offset; + n = n * 2.0 * dist; + Vec3 mirrorPos = point; + mirrorPos.subtract(n); + return mirrorPos; +} + +void ReflectionProbe::updateBoundingBox() { + if (_node) { + auto pos = _node->getWorldPosition(); + _boundingBox = geometry::AABB::set(_boundingBox, pos.x, pos.y, pos.z, _size.x, _size.y, _size.z); + } +} + +void ReflectionProbe::updatePlanarTexture(const scene::RenderScene* scene) { + if (!scene) return; + for (const auto& model : scene->getModels()) { + // filter model by view visibility + auto useProbeType = static_cast(scene::ReflectionProbe::UseProbeType::PLANAR_REFLECTION); + if (model->isEnabled() && model->getReflectionProbeType() == useProbeType) { + const auto visibility = _camera->getVisibility(); + const auto* const node = model->getNode(); + if ((model->getNode() && ((visibility & node->getLayer()) == node->getLayer())) || + (visibility & static_cast(model->getVisFlags()))) { + const auto* modelWorldBounds = model->getWorldBounds(); + if (!modelWorldBounds) { + continue; + } + const auto *probeBoundingBox = getBoundingBox(); + if (modelWorldBounds->aabbAabb(*probeBoundingBox)) { + model->updateReflctionProbePlanarMap(_realtimePlanarTexture->getGFXTexture()); + } + } + } + } +} + +void ReflectionProbe::destroy() { + _needRender = false; + if (_camera) { + _camera->destroy(); + _camera = nullptr; + } + if (_realtimePlanarTexture) { + _realtimePlanarTexture->destroy(); + _realtimePlanarTexture = nullptr; + } + scene::ReflectionProbeManager::getInstance()->unRegisterProbe(this); +} + +} // namespace scene +} // namespace cc diff --git a/native/cocos/scene/ReflectionProbe.h b/native/cocos/scene/ReflectionProbe.h new file mode 100644 index 0000000000..0168349fd9 --- /dev/null +++ b/native/cocos/scene/ReflectionProbe.h @@ -0,0 +1,184 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#pragma once + +#include "base/Macros.h" +#include "base/Ptr.h" +#include "base/RefCounted.h" +#include "core/Root.h" +#include "core/assets/RenderTexture.h" +#include "core/assets/TextureCube.h" +#include "renderer/pipeline/Define.h" +#include "scene/Camera.h" +namespace cc { +namespace scene { +class ReflectionProbe final { +public: + explicit ReflectionProbe(int32_t id); + ~ReflectionProbe() = default; + void initialize(Node* probeNode, Node* cameraNode); + enum class ProbeType { + CUBE = 0, + PLANAR = 1, + }; + enum class UseProbeType { + NONE = 0, + BAKED_CUBEMAP = 1, + PLANAR_REFLECTION = 2, + }; + /** + * @en Set probe type,cube or planar. + * @zh 设置探针类型,cube或者planar + */ + inline void setProbeType(ProbeType type) { + _probeType = type; + } + inline ProbeType getProbeType() const { return _probeType; } + + inline void setResolution(int32_t resolution) { _resolution = resolution; } + inline int32_t getResolution() const { return _resolution; } + /** + * @en Clearing flags of the camera, specifies which part of the framebuffer will be actually cleared every frame. + * @zh 相机的缓冲清除标志位,指定帧缓冲的哪部分要每帧清除。 + */ + inline void setClearFlag(gfx::ClearFlagBit value) { _clearFlag = value; } + inline gfx::ClearFlagBit getClearFlag() const { return _clearFlag; } + + /** + * @en Clearing color of the camera. + * @zh 相机的颜色缓冲默认值。 + */ + inline void setBackgroundColor(gfx::Color& val) { _backgroundColor = val; } + inline const gfx::Color& getBackgroundColor() const { return _backgroundColor; } + + /** + * @en Visibility mask, declaring a set of node layers that will be visible to this camera. + * @zh 可见性掩码,声明在当前相机中可见的节点层级集合。 + */ + inline void setVisibility(int32_t val) { _visibility = val; } + inline int32_t getVisibility() const { return _visibility; } + + /** + * @en Gets or sets the size of the bouding box, in local space. + * @zh 获取或设置包围盒的大小。 + */ + inline void setBoudingSize(Vec3& value) { + _size = value; + const Vec3 pos = _node->getWorldPosition(); + geometry::AABB::set(_boundingBox, pos.x, pos.y, pos.z, _size.x, _size.y, _size.z); + } + inline const Vec3& getBoudingSize() const { return _size; } + /** + * @en The node of the probe. + * @zh probe绑定的节点 + */ + inline Node* getNode() { return _node; } + inline Camera* getCamera() { return _camera; } + inline bool needRender() const { return _needRender; } + inline const geometry::AABB* getBoundingBox() const { return _boundingBox; } + + inline void setCameraNode(Node* val) { _cameraNode = val; } + inline Node* getCameraNode()const { return _cameraNode; } + + inline RenderTexture* getRealtimePlanarTexture() const { return _realtimePlanarTexture; } + void updateBoundingBox(); + void syncCameraParams(const Camera* camera); + void transformReflectionCamera(const Camera* sourceCamera); + void renderPlanarReflection(const Camera* camera); + static Vec3 reflect(const Vec3& point, const Vec3& normal, int32_t offset); + + void updatePlanarTexture(const scene::RenderScene* scene); + + void destroy(); + +private: + IntrusivePtr _realtimePlanarTexture{nullptr}; + + int32_t _resolution = 512; + gfx::ClearFlagBit _clearFlag = gfx::ClearFlagBit::NONE; + gfx::Color _backgroundColor{1.0, 1.0, 1.0, 1.0}; + int32_t _visibility = 0; + ProbeType _probeType = ProbeType::CUBE; + IntrusivePtr _cubemap{nullptr}; + Vec3 _size; + /** + * @en Objects inside bouding box. + * @zh 包围盒范围内的物体 + */ + ccstd::vector _renderObjects; + + /** + * @en Render cubemap's camera + * @zh 渲染cubemap的相机 + */ + IntrusivePtr _camera{nullptr}; + + /** + * @en Unique id of probe. + * @zh probe的唯一id + */ + int32_t _probeId = 0; + + bool _needRender = false; + + IntrusivePtr _node; + + IntrusivePtr _cameraNode; + + /** + * @en The AABB bounding box and probe only render the objects inside the bounding box. + * @zh AABB包围盒,probe只渲染包围盒内的物体 + */ + IntrusivePtr _boundingBox{nullptr}; + + /** + * @en The position of the camera in world space. + * @zh 世界空间相机的位置 + */ + Vec3 _cameraWorldPos; + + /** + * @en The rotation of the camera in world space. + * @zh 世界空间相机的旋转 + */ + Quaternion _cameraWorldRotation; + + /** + * @en The forward direction vertor of the camera in world space. + * @zh 世界空间相机朝前的方向向量 + */ + Vec3 _forward; + /** + * @en The up direction vertor of the camera in world space. + * @zh 世界空间相机朝上的方向向量 + */ + Vec3 _up; + + CC_DISALLOW_COPY_MOVE_ASSIGN(ReflectionProbe); +}; + +} // namespace scene +} // namespace cc diff --git a/native/cocos/scene/ReflectionProbeManager.cpp b/native/cocos/scene/ReflectionProbeManager.cpp new file mode 100644 index 0000000000..d78530640a --- /dev/null +++ b/native/cocos/scene/ReflectionProbeManager.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "ReflectionProbeManager.h" +#include "scene/ReflectionProbe.h" +namespace cc { + +namespace scene { +namespace { +ReflectionProbeManager* instance = nullptr; +} + +ReflectionProbeManager* ReflectionProbeManager::getInstance() { + if (instance == nullptr) { + instance = new ReflectionProbeManager(); + } + return instance; +} + +ReflectionProbeManager::ReflectionProbeManager() = default; +void ReflectionProbeManager::registerProbe(scene::ReflectionProbe* probe) { + _probes.emplace_back(probe); +} +void ReflectionProbeManager::unRegisterProbe(scene::ReflectionProbe* probe) { + const auto iter = std::find(_probes.begin(), _probes.end(), probe); + _probes.erase(iter); +} + +} // namespace scene +} // namespace cc diff --git a/native/cocos/scene/ReflectionProbeManager.h b/native/cocos/scene/ReflectionProbeManager.h new file mode 100644 index 0000000000..cd2fb2b8df --- /dev/null +++ b/native/cocos/scene/ReflectionProbeManager.h @@ -0,0 +1,49 @@ +/**************************************************************************** + Copyright (c) 2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once +#include "base/Macros.h" +#include "base/Ptr.h" +#include "renderer/pipeline/Define.h" +#include "scene/Camera.h" +namespace cc { +namespace scene { +class ReflectionProbe; +// namespace scene +class ReflectionProbeManager final { +public: + static ReflectionProbeManager* getInstance(); + ReflectionProbeManager(); + ~ReflectionProbeManager() = default; + void registerProbe(scene::ReflectionProbe* probe); + void unRegisterProbe(scene::ReflectionProbe* probe); + const ccstd::vector& getAllProbes() const { return _probes; } + +private: + ccstd::vector _probes; +}; + +} // namespace scene +} // namespace cc diff --git a/native/tools/swig-config/scene.i b/native/tools/swig-config/scene.i index d9ea3560da..e1ed12b0e6 100644 --- a/native/tools/swig-config/scene.i +++ b/native/tools/swig-config/scene.i @@ -38,6 +38,7 @@ #include "3d/models/BakedSkinningModel.h" #include "renderer/core/ProgramLib.h" #include "scene/Octree.h" +#include "scene/ReflectionProbe.h" %} // Insert code at the beginning of generated source file (.cpp) @@ -521,6 +522,15 @@ using namespace cc; %attribute(cc::Scene, bool, autoReleaseAssets, isAutoReleaseAssets, setAutoReleaseAssets); +%attribute(cc::scene::ReflectionProbe, cc::scene::ReflectionProbe::ProbeType, probeType, getProbeType, setProbeType); +%attribute(cc::scene::ReflectionProbe, uint32_t, resolution, getResolution, setResolution); +%attribute(cc::scene::ReflectionProbe, cc::gfx::ClearFlagBit, clearFlag, getClearFlag, setClearFlag); +%attribute(cc::scene::ReflectionProbe, cc::gfx::Color&, backgroundColor, getBackgroundColor, setBackgroundColor); +%attribute(cc::scene::ReflectionProbe, uint32_t, visibility, getVisibility, setVisibility); +%attribute(cc::scene::ReflectionProbe, Vec3&, size, getBoudingSize, setBoudingSize); + + + // ----- Import Section ------ // Brief: Import header files which are depended by 'Include Section' // Note: @@ -609,6 +619,7 @@ using namespace cc; %include "scene/RenderWindow.h" %include "scene/Camera.h" %include "scene/Ambient.h" +%include "scene/ReflectionProbe.h" %include "renderer/core/PassInstance.h" %include "renderer/core/MaterialInstance.h" diff --git a/scripts/build-engine/tests/stats-query/cc.config.json b/scripts/build-engine/tests/stats-query/cc.config.json index 7402dc9c7d..02b5291577 100644 --- a/scripts/build-engine/tests/stats-query/cc.config.json +++ b/scripts/build-engine/tests/stats-query/cc.config.json @@ -66,6 +66,7 @@ "cocos/core/renderer/core/render-scene.ts": "cocos/core/renderer/core/render-scene.jsb.ts", "cocos/core/renderer/scene/submodel.ts": "cocos/core/renderer/scene/submodel.jsb.ts", "cocos/core/renderer/scene/index.ts": "cocos/core/renderer/scene/index.jsb.ts", + "cocos/core/renderer/scene/reflection-probe.ts": "cocos/core/renderer/scene/reflection-probe.jsb.ts", "cocos/core/gfx/index.ts": "cocos/core/gfx/index.jsb.ts", "cocos/core/gfx/pipeline-state.ts": "cocos/core/gfx/pipeline-state.jsb.ts", "cocos/spine/index.ts": "cocos/spine/index.jsb.ts",