From 0f8d8cd3d905dc998a4fb71ebb9fc97276bfd925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=85=B5?= <49358166+xubing0906@users.noreply.github.com> Date: Tue, 22 Nov 2022 10:16:55 +0800 Subject: [PATCH] reflection probe support instancing and moving update (#13426) --- cocos/3d/framework/mesh-renderer.ts | 60 +++------ cocos/core/math/color.ts | 47 +++++++ cocos/misc/reflection-probe-component.ts | 60 ++++++--- cocos/render-scene/scene/model.ts | 21 ++- cocos/render-scene/scene/reflection-probe.ts | 47 +++++-- cocos/render-scene/scene/submodel.ts | 12 ++ cocos/rendering/instanced-buffer.ts | 21 ++- cocos/rendering/reflection-probe-manager.ts | 120 ++++++++++++++---- .../reflection-probe-stage.ts | 16 ++- .../render-reflection-probe-queue.ts | 94 +++++++++++--- cocos/scene-graph/scene.ts | 14 -- .../uniforms/cc-reflection-probe.chunk | 2 - .../chunks/common/math/coordinates.chunk | 2 +- .../chunks/common/texture/cubemap.chunk | 2 +- .../chunks/legacy/shading-standard-base.chunk | 16 +-- .../model-functions/standard.chunk | 6 +- .../chunks/surfaces/includes/common-fs.chunk | 2 +- 17 files changed, 381 insertions(+), 161 deletions(-) diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index 04844b85f2..253f6fa57f 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -42,7 +42,6 @@ import { settings, Settings } from '../../core/settings'; const { property, ccclass, help, executeInEditMode, executionOrder, menu, tooltip, visible, type, formerlySerializedAs, serializable, editable, disallowAnimation } = _decorator; -const USE_REFLECTION_PROBE = 'USE_REFLECTION_PROBE'; /** * @en Shadow projection mode. * @zh 阴影投射方式。 @@ -418,6 +417,7 @@ export class MeshRenderer extends ModelRenderer { this._updateCastShadow(); this._updateReceiveShadow(); this._updateUseLightProbe(); + this._updateUseReflectionProbe(); } /** @@ -483,7 +483,7 @@ export class MeshRenderer extends ModelRenderer { this._updateShadowNormalBias(); this._updateUseLightProbe(); this._updateBakeToReflectionProbe(); - this._updateReflectionProbeRenderInfo(); + this._updateUseReflectionProbe(); } // Redo, Undo, Prefab restore, etc. @@ -498,7 +498,7 @@ export class MeshRenderer extends ModelRenderer { this._updateShadowNormalBias(); this._updateUseLightProbe(); this._updateBakeToReflectionProbe(); - this._updateReflectionProbeRenderInfo(); + this._updateUseReflectionProbe(); } public onEnable () { @@ -516,7 +516,7 @@ export class MeshRenderer extends ModelRenderer { this._updateShadowBias(); this._updateShadowNormalBias(); this._updateBakeToReflectionProbe(); - this._updateReflectionProbeRenderInfo(); + this._updateUseReflectionProbe(); this._onUpdateLocalShadowBias(); this._updateUseLightProbe(); this._attachToScene(); @@ -653,22 +653,27 @@ export class MeshRenderer extends ModelRenderer { public updateProbeCubemap (cubeMap: TextureCube | null) { this.bakeSettings._probeCubemap = cubeMap; if (this.model !== null) { - this.model.updateReflctionProbeCubemap(this.bakeSettings._probeCubemap!); + this.model.updateReflctionProbeCubemap(this.bakeSettings._probeCubemap); } } public updateProbePlanarMap (planarMap: Texture | null) { this.bakeSettings._probePlanarmap = planarMap; if (this.model !== null) { - this.model.updateReflctionProbePlanarMap(this.bakeSettings._probePlanarmap!); + this.model.updateReflctionProbePlanarMap(this.bakeSettings._probePlanarmap); } } - protected _onUpdateReflectionProbeTexture () { + protected _updateReflectionProbeTexture () { if (this.model === null) return; if (this.bakeSettings.reflectionProbe === ReflectionProbeType.BAKED_CUBEMAP) { - this.model.updateReflctionProbeCubemap(this.bakeSettings._probeCubemap!); + this.model.updateReflctionProbeCubemap(this.bakeSettings._probeCubemap); + this.model.updateReflctionProbePlanarMap(null); } else if (this.bakeSettings.reflectionProbe === ReflectionProbeType.PLANAR_REFLECTION) { - this.model.updateReflctionProbePlanarMap(this.bakeSettings._probePlanarmap!); + this.model.updateReflctionProbePlanarMap(this.bakeSettings._probePlanarmap); + this.model.updateReflctionProbeCubemap(null); + } else { + this.model.updateReflctionProbeCubemap(null); + this.model.updateReflctionProbePlanarMap(null); } } @@ -698,7 +703,7 @@ export class MeshRenderer extends ModelRenderer { this._updateModelParams(); this._onUpdateLightingmap(); this._onUpdateLocalShadowBias(); - this._onUpdateReflectionProbeTexture(); + this._updateUseReflectionProbe(); } } @@ -796,7 +801,7 @@ export class MeshRenderer extends ModelRenderer { this._model.setSubModelMaterial(idx, material); this._onUpdateLightingmap(); this._onUpdateLocalShadowBias(); - this._updateReflectionProbeRenderInfo(); + this._updateReflectionProbeTexture(); } protected _onMeshChanged (old: Mesh | null) { @@ -861,7 +866,7 @@ export class MeshRenderer extends ModelRenderer { } protected onReflectionProbeChanged () { - this._updateReflectionProbe(); + this._updateUseReflectionProbe(); } protected onBakeToReflectionProbeChanged () { @@ -890,19 +895,10 @@ export class MeshRenderer extends ModelRenderer { return false; } - protected _updateReflectionProbe () { - for (let i = 0; i < this._materials.length; i++) { - const mat = this.getMaterialInstance(i)!; - mat.recompileShaders({ USE_REFLECTION_PROBE: this.bakeSettings.reflectionProbe }); - } - if (this._model) { - this._model.reflectionProbeType = this.bakeSettings.reflectionProbe; - if (this.bakeSettings.reflectionProbe === ReflectionProbeType.BAKED_CUBEMAP) { - this._model.updateReflctionProbeCubemap(this.bakeSettings._probeCubemap!); - } else if (this.bakeSettings.reflectionProbe === ReflectionProbeType.PLANAR_REFLECTION) { - this._model.updateReflctionProbePlanarMap(this.bakeSettings._probePlanarmap!); - } - } + protected _updateUseReflectionProbe () { + if (!this._model) return; + this._model.reflectionProbeType = this.bakeSettings.reflectionProbe; + this._updateReflectionProbeTexture(); } protected _updateBakeToReflectionProbe () { @@ -910,20 +906,6 @@ export class MeshRenderer extends ModelRenderer { this._model.bakeToReflectionProbe = this.bakeSettings.bakeToReflectionProbe; } - protected _updateReflectionProbeRenderInfo () { - if (!this._model) { return; } - if (this.bakeSettings.reflectionProbe !== ReflectionProbeType.NONE) { - for (let i = 0; i < this._materials.length; i++) { - const mat = this.getMaterialInstance(i); - if (mat) { - mat.recompileShaders({ USE_REFLECTION_PROBE: this.bakeSettings.reflectionProbe }); - } - } - } - this._model.reflectionProbeType = this.bakeSettings.reflectionProbe; - this._onUpdateReflectionProbeTexture(); - } - private _watchMorphInMesh () { if (this._morphInstance) { this._morphInstance.destroy(); diff --git a/cocos/core/math/color.ts b/cocos/core/math/color.ts index 81324a4f0e..4866cab06f 100644 --- a/cocos/core/math/color.ts +++ b/cocos/core/math/color.ts @@ -31,6 +31,7 @@ import { clamp, EPSILON } from './utils'; import { legacyCC } from '../global-exports'; import { assertIsTrue } from '../data/utils/asserts'; import { Vec4 } from './vec4'; +import { Vec3 } from './vec3'; const toFloat = 1 / 255; @@ -752,3 +753,49 @@ export function linearToSrgb8Bit (x: number): number { // use table for more consistent conversion between uint8 and float, offline processes only. let SRGB_8BIT_TO_LINEAR: Array = []; for (let i = 0; i < 256; i++) { SRGB_8BIT_TO_LINEAR.push(srgbToLinear(i / 255.0)); } + +export function clampVec3 (val: Vec3, min: Vec3, max: Vec3) { + if (min > max) { + const temp = min; + min = max; + max = temp; + } + return val < min ? min : val > max ? max : val; +} + +export function floorVec3 (val: Vec3) { + const temp = val.clone(); + temp.x = Math.floor(val.x); + temp.y = Math.floor(val.y); + temp.z = Math.floor(val.z); + return temp; +} + +export function stepVec3 (a: Vec3, b: Vec3) { + if (a < b) { + return b; + } else { + return a; + } +} + +/** + * @en Three channel rgb color pack into four channel rbge format. + * @zh 三通道rgb颜色pack成四通道rbge格式 + * @param rgb Vec3 + */ +export function packRGBE (rgb: Vec3) { + const maxComp = Math.max(Math.max(rgb.x, rgb.y), rgb.z); + let e = 128.0; + if (maxComp > 0.0001) { + e = Math.log(maxComp) / Math.log(1.1); + e = Math.ceil(e); + e = clamp(e + 128.0, 0.0, 255.0); + } + // eslint-disable-next-line no-restricted-properties + const sc = 1.0 / Math.pow(1.1, e - 128.0); + const encode = clampVec3(rgb.multiplyScalar(sc), new Vec3(0.0, 0.0, 0.0), new Vec3(1.0, 1.0, 1.0)); + encode.multiplyScalar(255.0); + const encode_rounded = floorVec3(encode).add(stepVec3(encode.subtract(floorVec3(encode)), new Vec3(0.5, 0.5, 0.5))); + return new Vec4(encode_rounded.x / 255.0, encode_rounded.y / 255.0, encode_rounded.z / 255.0, e / 255.0); +} diff --git a/cocos/misc/reflection-probe-component.ts b/cocos/misc/reflection-probe-component.ts index b2a40be57d..925fbaf3f6 100644 --- a/cocos/misc/reflection-probe-component.ts +++ b/cocos/misc/reflection-probe-component.ts @@ -94,6 +94,9 @@ export class ReflectionProbe extends Component { protected _probe: scene.ReflectionProbe | null = null; + protected _previewSphere: Node | null = null; + protected _previewPlane: Node | null = null; + /** * @en * Gets or sets the size of the box @@ -129,6 +132,7 @@ export class ReflectionProbe extends Component { if (EDITOR) { this._objFlags |= CCObject.Flags.IsRotationLocked; } + ReflectionProbeManager.probeManager.updatePlanarMap(this.probe, null); } else { if (lastSizeIsNoExist) { this._size.set(ReflectionProbe.DEFAULT_PLANER_SIZE); @@ -146,6 +150,7 @@ export class ReflectionProbe extends Component { this._size.set(this._lastSize); } this._lastSize.set(lastSize); + this.size = this._size; } } get probeType () { @@ -229,7 +234,7 @@ export class ReflectionProbe extends Component { return this._sourceCamera!; } - set cubemap (val: TextureCube) { + set cubemap (val: TextureCube | null) { this._cubemap = val; this.probe.cubemap = val; } @@ -238,6 +243,26 @@ export class ReflectionProbe extends Component { return this._probe!; } + set previewSphere (val: Node) { + this._previewSphere = val; + this.probe.previewSphere = val; + if (this._previewSphere) { + ReflectionProbeManager.probeManager.updatePreviewSphere(this.probe); + } + } + + get previewSphere () { + return this._previewSphere!; + } + + set previewPlane (val: Node) { + this._previewPlane = val; + } + + get previewPlane () { + return this._previewPlane!; + } + public onLoad () { if (EDITOR || this.probeType === ProbeType.PLANAR) { this._createProbe(); @@ -257,6 +282,7 @@ export class ReflectionProbe extends Component { public start () { if (this._sourceCamera && this.probeType === ProbeType.PLANAR) { + ReflectionProbeManager.probeManager.updateUsePlanarModels(this.probe); this.probe.renderPlanarReflection(this.sourceCamera.camera); } } @@ -268,15 +294,9 @@ export class ReflectionProbe extends Component { } public update (dt: number) { - if (this.probeType === ProbeType.CUBE) { - if (EDITOR) { - if (this.node.hasChangedFlags) { - this.probe.updateBoundingBox(); - ReflectionProbeManager.probeManager.updateModes(this.probe); - } - } - } else { - if (EDITOR) { + if (!this.probe) return; + if (EDITOR) { + if (this.probeType === ProbeType.PLANAR) { const cameraLst: scene.Camera[] | undefined = this.node.scene.renderScene?.cameras; if (cameraLst !== undefined) { for (let i = 0; i < cameraLst.length; ++i) { @@ -288,22 +308,24 @@ export class ReflectionProbe extends Component { } } } - if (this.node.hasChangedFlags) { - this.probe.updateBoundingBox(); - ReflectionProbeManager.probeManager.updateModes(this.probe); - } + this.probe.updateBoundingBox(); + ReflectionProbeManager.probeManager.updateUseCubeModels(this.probe); + ReflectionProbeManager.probeManager.updateUsePlanarModels(this.probe); } } - public onFocusInEditor () { - if (this.probeType === ProbeType.CUBE) { - ReflectionProbeManager.probeManager.updateModes(this.probe); - } + /** + * @en Clear the baked cubemap. + * @zh 清除烘焙的cubemap + */ + public refresh () { + this.cubemap = null; + ReflectionProbeManager.probeManager.updateBakedCubemap(this.probe); } private _createProbe () { if (this._probeId === -1 || ReflectionProbeManager.probeManager.exists(this._probeId)) { - this._probeId = this.node.scene.getNewReflectionProbeId(); + this._probeId = ReflectionProbeManager.probeManager.getNewReflectionProbeId(); } this._probe = new scene.ReflectionProbe(this._probeId); if (this._probe) { diff --git a/cocos/render-scene/scene/model.ts b/cocos/render-scene/scene/model.ts index a07880771c..8940e642b5 100644 --- a/cocos/render-scene/scene/model.ts +++ b/cocos/render-scene/scene/model.ts @@ -58,7 +58,7 @@ const stationaryLightMapPatches: IMacroPatch[] = [ const lightProbePatches: IMacroPatch[] = [ { name: 'CC_USE_LIGHT_PROBE', value: true }, ]; - +const CC_USE_REFLECTION_PROBE = 'CC_USE_REFLECTION_PROBE'; export enum ModelType { DEFAULT, SKINNING, @@ -322,6 +322,11 @@ export class Model { set reflectionProbeType (val) { this._reflectionProbeType = val; + const subModels = this._subModels; + for (let i = 0; i < subModels.length; i++) { + subModels[i].useReflectionProbeType = val; + } + this.onMacroPatchesStateChanged(); } /** @@ -671,7 +676,7 @@ export class Model { return true; } - private updateSHBuffer() { + private updateSHBuffer () { if (!this._localSHData) { return; } @@ -697,7 +702,7 @@ export class Model { * @en Clear the model's SH ubo * @zh 清除模型的球谐 ubo */ - public clearSHUBOs () { + public clearSHUBOs () { if (!this._localSHData) { return; } @@ -707,7 +712,7 @@ export class Model { } this.updateSHBuffer(); - } + } /** * @en Update the model's SH ubo @@ -883,7 +888,7 @@ export class Model { * @zh 更新反射探针的立方体贴图 * @param texture probe cubemap */ - public updateReflctionProbeCubemap (texture: TextureCube) { + public updateReflctionProbeCubemap (texture: TextureCube | null) { this._localDataUpdated = true; this.onMacroPatchesStateChanged(); @@ -909,7 +914,7 @@ export class Model { * @zh 更新反射探针的平面反射贴图 * @param texture planar relflection map */ - public updateReflctionProbePlanarMap (texture: Texture) { + public updateReflctionProbePlanarMap (texture: Texture | null) { this._localDataUpdated = true; this.onMacroPatchesStateChanged(); @@ -967,6 +972,10 @@ export class Model { if (this._useLightProbe) { patches = patches ? patches.concat(lightProbePatches) : lightProbePatches; } + const reflectionProbePatches: IMacroPatch[] = [ + { name: CC_USE_REFLECTION_PROBE, value: this._reflectionProbeType }, + ]; + patches = patches ? patches.concat(reflectionProbePatches) : reflectionProbePatches; return patches; } diff --git a/cocos/render-scene/scene/reflection-probe.ts b/cocos/render-scene/scene/reflection-probe.ts index 883ca7584a..075d1e5b12 100644 --- a/cocos/render-scene/scene/reflection-probe.ts +++ b/cocos/render-scene/scene/reflection-probe.ts @@ -114,6 +114,12 @@ export class ReflectionProbe { */ private _up = new Vec3(); + /** + * @en Reflection probe cube pattern preview sphere + * @zh 反射探针cube模式的预览小球 + */ + protected _previewSphere: Node | null = null; + /** * @en Set probe type,cube or planar. * @zh 设置探针类型,cube或者planar @@ -191,7 +197,7 @@ export class ReflectionProbe { return this._size; } - set cubemap (val: TextureCube) { + set cubemap (val: TextureCube | null) { this._cubemap = val; } @@ -241,6 +247,18 @@ export class ReflectionProbe { return this._cameraNode!; } + /** + * @en Reflection probe cube mode preview sphere + * @zh 反射探针cube模式的预览小球 + */ + set previewSphere (val: Node) { + this._previewSphere = val; + } + + get previewSphere () { + return this._previewSphere!; + } + constructor (id: number) { this._probeId = id; } @@ -333,21 +351,9 @@ export class ReflectionProbe { this.camera.update(true); } - public _syncCameraParams (camera: Camera) { - 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.visibility = camera.visibility; - this.camera.clearFlag = camera.clearFlag; - this.camera.clearColor = camera.clearColor; - this.camera.priority = camera.priority - 1; - this.camera.resize(camera.width, camera.height); - } - public updateBoundingBox () { if (this.node) { + this.node.updateWorldTransform(); const pos = this.node.getWorldPosition(); geometry.AABB.set(this._boundingBox!, pos.x, pos.y, pos.z, this._size.x, this._size.y, this._size.z); } @@ -364,6 +370,19 @@ export class ReflectionProbe { return false; } + private _syncCameraParams (camera: Camera) { + 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.visibility = camera.visibility; + this.camera.clearFlag = camera.clearFlag; + this.camera.clearColor = camera.clearColor; + this.camera.priority = camera.priority - 1; + this.camera.resize(camera.width, camera.height); + } + private _createCamera (cameraNode:Node) { const root = cclegacy.director.root; if (!this._camera) { diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index 3cb51a1f67..30a12e526b 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -64,6 +64,7 @@ export class SubModel { protected _instancedAttributeBlock: IInstancedAttributeBlock = { buffer: null!, views: [], attributes: [] }; protected _instancedWorldMatrixIndex = -1; protected _instancedSHIndex = -1; + protected _useReflectionProbeType = 0; /** * @en @@ -213,6 +214,17 @@ export class SubModel { return this._instancedSHIndex; } + /** + * @en Gets or sets the type of reflection probe, Used to process instance + * @zh 获取或设置使用反射探针的类型,用于处理instance + */ + set useReflectionProbeType (val) { + this._useReflectionProbeType = val; + } + get useReflectionProbeType () { + return this._useReflectionProbeType; + } + /** * @en * init sub model diff --git a/cocos/rendering/instanced-buffer.ts b/cocos/rendering/instanced-buffer.ts index 193f02732f..8a2628ca70 100644 --- a/cocos/rendering/instanced-buffer.ts +++ b/cocos/rendering/instanced-buffer.ts @@ -25,7 +25,7 @@ import { Pass } from '../render-scene'; import { IInstancedAttributeBlock, SubModel } from '../render-scene/scene'; -import { UNIFORM_LIGHTMAP_TEXTURE_BINDING } from './define'; +import { UNIFORM_LIGHTMAP_TEXTURE_BINDING, UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING } from './define'; import { BufferUsageBit, MemoryUsageBit, Device, Texture, InputAssembler, InputAssemblerInfo, Attribute, Buffer, BufferInfo, CommandBuffer, Shader, DescriptorSet } from '../gfx'; @@ -39,6 +39,9 @@ export interface IInstancedItem { shader: Shader | null; descriptorSet: DescriptorSet; lightingMap: Texture; + reflectionProbeCubemap: Texture; + reflectionProbePlanarMap: Texture; + useReflectionProbeType: number; } const INITIAL_CAPACITY = 32; @@ -71,6 +74,9 @@ export class InstancedBuffer { if (!stride) { return; } // we assume per-instance attributes are always present const sourceIA = subModel.inputAssembler; const lightingMap = subModel.descriptorSet.getTexture(UNIFORM_LIGHTMAP_TEXTURE_BINDING); + const reflectionProbeCubemap = subModel.descriptorSet.getTexture(UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING); + const reflectionProbePlanarMap = subModel.descriptorSet.getTexture(UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING); + const useReflectionProbeType = subModel.useReflectionProbeType; let shader = shaderImplant; if (!shader) { shader = subModel.shaders[passIdx]; @@ -85,6 +91,17 @@ export class InstancedBuffer { continue; } + if (instance.useReflectionProbeType !== useReflectionProbeType) { + continue; + } else { + if (instance.reflectionProbeCubemap.objectID !== reflectionProbeCubemap.objectID) { + continue; + } + if (instance.reflectionProbePlanarMap.objectID !== reflectionProbePlanarMap.objectID) { + continue; + } + } + if (instance.stride !== stride) { // we allow this considering both baked and non-baked // skinning models may be present in the same buffer @@ -127,7 +144,7 @@ export class InstancedBuffer { vertexBuffers.push(vb); const iaInfo = new InputAssemblerInfo(attributes, vertexBuffers, indexBuffer); const ia = this._device.createInputAssembler(iaInfo); - this.instances.push({ count: 1, capacity: INITIAL_CAPACITY, vb, data, ia, stride, shader, descriptorSet, lightingMap }); + this.instances.push({ count: 1, capacity: INITIAL_CAPACITY, vb, data, ia, stride, shader, descriptorSet, lightingMap, reflectionProbeCubemap, reflectionProbePlanarMap, useReflectionProbeType }); this.hasPendingModels = true; } diff --git a/cocos/rendering/reflection-probe-manager.ts b/cocos/rendering/reflection-probe-manager.ts index c0be177484..4f2da06343 100644 --- a/cocos/rendering/reflection-probe-manager.ts +++ b/cocos/rendering/reflection-probe-manager.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-len */ /* Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. @@ -23,24 +24,33 @@ THE SOFTWARE. */ -import { MeshRenderer } from '../3d/framework/mesh-renderer'; +import { MeshRenderer, ReflectionProbeType } from '../3d/framework/mesh-renderer'; import { Vec3, geometry } from '../core'; import { Texture } from '../gfx'; import { Camera, Model } from '../render-scene/scene'; -import { ReflectionProbe } from '../render-scene/scene/reflection-probe'; -import { CAMERA_DEFAULT_MASK } from './define'; +import { ProbeType, ReflectionProbe } from '../render-scene/scene/reflection-probe'; +import { Layers } from '../scene-graph/layers'; -const SPHERE_NODE_NAME = 'Reflection Probe Sphere'; +const REFLECTION_PROBE_DEFAULT_MASK = Layers.makeMaskExclude([Layers.BitMask.UI_2D, Layers.BitMask.UI_3D, Layers.BitMask.GIZMOS, Layers.BitMask.EDITOR, + Layers.BitMask.SCENE_GIZMO, Layers.BitMask.PROFILER]); export class ReflectionProbeManager { public static probeManager: ReflectionProbeManager; private _probes: ReflectionProbe[] = []; /** * @en - * All models in the scene that use reflection probes. + * All models in the scene that use cube type reflection probe. * @zh - * 场景中所有使用反射探针的模型 + * 场景中所有使用cube类型反射探针的模型 */ - private _models = new Map(); + private _useCubeModels = new Map(); + + /** + * @en + * All models in the scene that use planar type reflection probe. + * @zh + * 场景中所有使用planar类型反射探针的模型 + */ + private _usePlanarModels = new Map(); public register (probe: ReflectionProbe) { const index = this._probes.indexOf(probe); @@ -71,6 +81,18 @@ export class ReflectionProbeManager { return false; } + public getNewReflectionProbeId () { + let probeId = 0; + // eslint-disable-next-line no-constant-condition + while (true) { + if (this.exists(probeId)) { + probeId++; + } else { + return probeId; + } + } + } + public getProbes (): ReflectionProbe[] { return this._probes; } @@ -112,50 +134,80 @@ export class ReflectionProbeManager { * @param probe update the texture for this probe */ public updatePlanarMap (probe: ReflectionProbe, texture: Texture | null) { + if (!probe.node || !probe.node.scene) return; + const models = this._getModelsByProbe(probe); + for (let i = 0; i < models.length; i++) { + const model = models[i]; + const meshRender = model.node.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateProbePlanarMap(texture); + } + } + } + + /** + * @en Update objects using reflection probe for planar reflection. + * @zh 更新使用反射探针进行平面反射的物体。 + * @param probe update the object for this probe + */ + public updateUsePlanarModels (probe: ReflectionProbe) { if (!probe.node || !probe.node.scene) return; const scene = probe.node.scene.renderScene; const models = scene!.models; for (let i = 0; i < models.length; i++) { const model = models[i]; - if (!model.node || !model.worldBounds) continue; - if ((model.node.layer & CAMERA_DEFAULT_MASK) && geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) { - const meshRender = model.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbePlanarMap(texture); + if (!model.node || !model.worldBounds + || model.reflectionProbeType !== ReflectionProbeType.PLANAR_REFLECTION + || probe.probeType !== ProbeType.PLANAR) continue; + model.updateWorldBound(); + const meshRender = model.node.getComponent(MeshRenderer); + if ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) && meshRender) { + if (geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) { + this._usePlanarModels.set(model, probe); + } else if (this._usePlanarModels.has(model)) { + const old = this._usePlanarModels.get(model); + if (old === probe) { + this._usePlanarModels.delete(model); + const meshRender = model.node.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateProbePlanarMap(null); + } + } } } } } /** - * @en This function is called when the transform of the reflection probe changes, to update the object using this reflection probe. - * @zh 反射探针的transform变化时,会调用此函数来更新使用此反射探针的物体。 + * @en Update objects using reflection probe for bake cubemap. + * @zh 更新使用反射探针烘焙cubemap的物体。 * @param probe update the object for this probe */ - public updateModes (probe: ReflectionProbe) { + public updateUseCubeModels (probe: ReflectionProbe) { if (!probe.node || !probe.node.scene) return; const scene = probe.node.scene.renderScene; if (!scene) return; const models = scene.models; for (let i = 0; i < models.length; i++) { const model = models[i]; - if (model.node && model.worldBounds && ((model.node.layer & CAMERA_DEFAULT_MASK)) || model.node.name === SPHERE_NODE_NAME) { + model.updateWorldBound(); + if (model.node && model.worldBounds && ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK))) { const nearest = this._getNearestProbe(model); if (!nearest) { const meshRender = model.node.getComponent(MeshRenderer); if (meshRender) { meshRender.updateProbeCubemap(null); } - this._models.delete(model); + this._useCubeModels.delete(model); continue; } - if (this._models.has(model)) { - const old = this._models.get(model); + if (this._useCubeModels.has(model)) { + const old = this._useCubeModels.get(model); if (old === nearest) { continue; } } - this._models.set(model, nearest); + this._useCubeModels.set(model, nearest); nearest.needRefresh = true; } } @@ -167,6 +219,18 @@ export class ReflectionProbeManager { } } + /** + * @en Update the preview sphere of the Reflection Probe cube mode. + * @zh 更新反射探针cube模式的预览球 + */ + public updatePreviewSphere (probe: ReflectionProbe) { + if (!probe || !probe.previewSphere) return; + const meshRender = probe.previewSphere.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateProbeCubemap(probe.cubemap); + } + } + /** * @en * select the probe with the nearest distance. @@ -185,10 +249,10 @@ export class ReflectionProbeManager { continue; } else if (!find) { find = true; - distance = Vec3.distance(model.node.position, this._probes[i].node.position); + distance = Vec3.distance(model.node.worldPosition, this._probes[i].node.worldPosition); idx = i; } - const d = Vec3.distance(model.node.position, this._probes[i].node.position); + const d = Vec3.distance(model.node.worldPosition, this._probes[i].node.worldPosition); if (d < distance) { distance = d; idx = i; @@ -199,7 +263,11 @@ export class ReflectionProbeManager { private _getModelsByProbe (probe: ReflectionProbe) { const models: Model[] = []; - for (const entry of this._models.entries()) { + let useModels = this._useCubeModels; + if (probe.probeType === ProbeType.PLANAR) { + useModels = this._usePlanarModels; + } + for (const entry of useModels.entries()) { if (entry[1] === probe) { models.push(entry[0]); } @@ -208,10 +276,10 @@ export class ReflectionProbeManager { } private _removeDependentModels (probe: ReflectionProbe) { - for (const key of this._models.keys()) { - const p = this._models.get(key); + for (const key of this._useCubeModels.keys()) { + const p = this._useCubeModels.get(key); if (p !== undefined && p === probe) { - this._models.delete(key); + this._useCubeModels.delete(key); } } } diff --git a/cocos/rendering/reflection-probe/reflection-probe-stage.ts b/cocos/rendering/reflection-probe/reflection-probe-stage.ts index 34b7587ecb..d9776ae553 100644 --- a/cocos/rendering/reflection-probe/reflection-probe-stage.ts +++ b/cocos/rendering/reflection-probe/reflection-probe-stage.ts @@ -30,8 +30,10 @@ import { ForwardStagePriority } from '../enum'; import { ForwardPipeline } from '../forward/forward-pipeline'; import { SetIndex } from '../define'; import { ReflectionProbeFlow } from './reflection-probe-flow'; -import { Camera, ReflectionProbe } from '../../render-scene/scene'; +import { Camera, ProbeType, ReflectionProbe } from '../../render-scene/scene'; import { RenderReflectionProbeQueue } from '../render-reflection-probe-queue'; +import { Vec3 } from '../../core'; +import { packRGBE } from '../../core/math/color'; const colors: Color[] = [new Color(1, 1, 1, 1)]; @@ -55,6 +57,7 @@ export class ReflectionProbeStage extends RenderStage { private _renderArea = new Rect(); private _probe: ReflectionProbe | null = null; private _probeRenderQueue!: RenderReflectionProbeQueue; + private _rgbeColor = new Vec3(); /** * @en Sets the probe info @@ -107,9 +110,14 @@ export class ReflectionProbeStage extends RenderStage { const renderPass = this._frameBuffer!.renderPass; if (this._probe!.camera.clearFlag & ClearFlagBit.COLOR) { - colors[0].x = this._probe!.camera.clearColor.x; - colors[0].y = this._probe!.camera.clearColor.y; - colors[0].z = this._probe!.camera.clearColor.z; + this._rgbeColor.x = this._probe!.camera.clearColor.x; + this._rgbeColor.y = this._probe!.camera.clearColor.y; + this._rgbeColor.z = this._probe!.camera.clearColor.z; + const rgbe = packRGBE(this._rgbeColor); + colors[0].x = rgbe.x; + colors[0].y = rgbe.y; + colors[0].z = rgbe.z; + colors[0].w = rgbe.w; } const device = pipeline.device; cmdBuff.beginRenderPass(renderPass, this._frameBuffer!, this._renderArea, diff --git a/cocos/rendering/render-reflection-probe-queue.ts b/cocos/rendering/render-reflection-probe-queue.ts index 4fb54eeebe..8568ad7b2b 100644 --- a/cocos/rendering/render-reflection-probe-queue.ts +++ b/cocos/rendering/render-reflection-probe-queue.ts @@ -28,11 +28,19 @@ import { SetIndex } from './define'; import { Device, RenderPass, Shader, CommandBuffer } from '../gfx'; import { getPhaseID } from './pass-phase'; import { PipelineStateManager } from './pipeline-state-manager'; -import { Pass } from '../render-scene/core/pass'; +import { Pass, BatchingSchemes } from '../render-scene/core/pass'; import { Model } from '../render-scene/scene/model'; -import { ReflectionProbe, SKYBOX_FLAG } from '../render-scene/scene'; +import { ProbeType, ReflectionProbe, SKYBOX_FLAG } from '../render-scene/scene'; import { PipelineRuntime } from './custom/pipeline'; -import { RenderScene } from '../render-scene'; +import { IMacroPatch, RenderScene } from '../render-scene'; +import { RenderInstancedQueue } from './render-instanced-queue'; +import { RenderBatchedQueue } from './render-batched-queue'; +import { geometry } from '../core'; +import { Layers } from '../scene-graph/layers'; + +// eslint-disable-next-line max-len +const REFLECTION_PROBE_DEFAULT_MASK = Layers.makeMaskExclude([Layers.BitMask.UI_2D, Layers.BitMask.UI_3D, Layers.BitMask.GIZMOS, Layers.BitMask.EDITOR, + Layers.BitMask.SCENE_GIZMO, Layers.BitMask.PROFILER]); const CC_USE_RGBE_OUTPUT = 'CC_USE_RGBE_OUTPUT'; const _phaseID = getPhaseID('default'); @@ -66,11 +74,15 @@ export class RenderReflectionProbeQueue { private _subModelsArray: SubModel[] = []; private _passArray: Pass[] = []; private _shaderArray: Shader[] = []; + private _rgbeSubModelsArray:SubModel[]=[] + private _instancedQueue: RenderInstancedQueue; + private _batchedQueue: RenderBatchedQueue; public constructor (pipeline: PipelineRuntime) { this._pipeline = pipeline; + this._instancedQueue = new RenderInstancedQueue(); + this._batchedQueue = new RenderBatchedQueue(); } - //TODO The cmdBuff parameter is added to do the instance public gatherRenderObjects (probe: ReflectionProbe, scene:RenderScene, cmdBuff: CommandBuffer) { this.clear(); const sceneData = this._pipeline.pipelineSceneData; @@ -86,21 +98,29 @@ export class RenderReflectionProbeQueue { for (let i = 0; i < models.length; i++) { const model = models[i]; // filter model by view visibility - if (model.enabled) { - if (model.node && ((visibility & model.node.layer) === model.node.layer) - || (visibility & model.visFlags)) { - if (model.bakeToReflectionProbe) { + if (model.enabled && model.node && model.bakeToReflectionProbe) { + if (probe.probeType === ProbeType.CUBE) { + if ((((visibility & model.node.layer) === model.node.layer) || (visibility & model.visFlags)) + && geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) { this.add(model); } + } else if (((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) === model.node.layer) + || (REFLECTION_PROBE_DEFAULT_MASK & model.visFlags)) { + this.add(model); } } } + this._instancedQueue.uploadBuffers(cmdBuff); + this._batchedQueue.uploadBuffers(cmdBuff); } public clear () { this._subModelsArray.length = 0; this._shaderArray.length = 0; this._passArray.length = 0; + this._instancedQueue.clear(); + this._batchedQueue.clear(); + this._rgbeSubModelsArray.length = 0; } public add (model: Model) { @@ -115,15 +135,34 @@ export class RenderReflectionProbeQueue { bUseReflectPass = false; } if (passIdx < 0) { continue; } + const pass = subModel.passes[passIdx]; + const batchingScheme = pass.batchingScheme; + if (!bUseReflectPass) { - pass.defines[CC_USE_RGBE_OUTPUT] = true; - subModel.onPipelineStateChanged(); + let patches: IMacroPatch[] | null = subModel.patches; + const useRGBEPatchs: IMacroPatch[] = [ + { name: CC_USE_RGBE_OUTPUT, value: true }, + ]; + patches = patches ? patches.concat(useRGBEPatchs) : useRGBEPatchs; + subModel.onMacroPatchesStateChanged(patches); + this._rgbeSubModelsArray.push(subModel); + } + + if (batchingScheme === BatchingSchemes.INSTANCING) { // instancing + const buffer = pass.getInstancedBuffer(); + buffer.merge(subModel, passIdx); + this._instancedQueue.queue.add(buffer); + } else if (pass.batchingScheme === BatchingSchemes.VB_MERGING) { // vb-merging + const buffer = pass.getBatchedBuffer(); + buffer.merge(subModel, passIdx, model); + this._batchedQueue.queue.add(buffer); + } else { + const shader = subModel.shaders[passIdx]; + this._subModelsArray.push(subModel); + if (shader) this._shaderArray.push(shader); + this._passArray.push(pass); } - const shader = subModel.shaders[passIdx]; - this._subModelsArray.push(subModel); - if (shader) this._shaderArray.push(shader); - this._passArray.push(pass); } } @@ -132,6 +171,9 @@ export class RenderReflectionProbeQueue { * record CommandBuffer */ public recordCommandBuffer (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer) { + this._instancedQueue.recordCommandBuffer(device, renderPass, cmdBuff); + this._batchedQueue.recordCommandBuffer(device, renderPass, cmdBuff); + for (let i = 0; i < this._subModelsArray.length; ++i) { const subModel = this._subModelsArray[i]; const shader = this._shaderArray[i]; @@ -146,14 +188,24 @@ export class RenderReflectionProbeQueue { cmdBuff.bindInputAssembler(ia); cmdBuff.draw(ia); } - this.resetMacro(); + this.resetRGBEMacro(); + this._instancedQueue.clear(); + this._batchedQueue.clear(); } - public resetMacro () { - for (let i = 0; i < this._subModelsArray.length; ++i) { - const subModel = this._subModelsArray[i]; - const pass = this._passArray[i]; - pass.defines[CC_USE_RGBE_OUTPUT] = false; - subModel.onPipelineStateChanged(); + public resetRGBEMacro () { + for (let i = 0; i < this._rgbeSubModelsArray.length; i++) { + const subModel = this._rgbeSubModelsArray[i]; + // eslint-disable-next-line prefer-const + let patches: IMacroPatch[] | null = subModel.patches; + if (!patches) continue; + for (let j = 0; j < patches.length; j++) { + const patch = patches[j]; + if (patch.name === CC_USE_RGBE_OUTPUT) { + patches.splice(j, 1); + break; + } + } + subModel.onMacroPatchesStateChanged(patches); } } } diff --git a/cocos/scene-graph/scene.ts b/cocos/scene-graph/scene.ts index 8a41ad1047..5882ae9205 100644 --- a/cocos/scene-graph/scene.ts +++ b/cocos/scene-graph/scene.ts @@ -74,20 +74,6 @@ export class Scene extends Node { @serializable public _globals = new SceneGlobals(); - /** - * @en The id of the reflection probe is not duplicated. - * @zh 反射探针的id,同一个场景id不会重复 - */ - @serializable - private _reflectionProbeId = 0; - /** - * @en When a new reflection probe is added to the scene, a probe id needs to be generated. - * @zh 场景添加新的反射探针时,需要生成一个探针id - */ - public getNewReflectionProbeId (): number { - return this._reflectionProbeId++; - } - public dependAssets = null; // cache all depend assets for auto release protected _renderScene: RenderScene | null = null; diff --git a/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk b/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk index e189a8501d..c7911447b2 100644 --- a/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk +++ b/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk @@ -1,6 +1,4 @@ // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. - -#pragma define-meta USE_REFLECTION_PROBE range([0, 2]) #pragma define REFLECTION_PROBE_NONE 0 #pragma define REFLECTION_PROBE_CUBE 1 #pragma define REFLECTION_PROBE_PLANAR 2 diff --git a/editor/assets/chunks/common/math/coordinates.chunk b/editor/assets/chunks/common/math/coordinates.chunk index 8210dcda90..312d81db6b 100644 --- a/editor/assets/chunks/common/math/coordinates.chunk +++ b/editor/assets/chunks/common/math/coordinates.chunk @@ -7,7 +7,7 @@ vec2 GetScreenUV(vec3 worldPos, mat4 matViewProj, float flipNDCSign) { vec4 clipPos = matViewProj * vec4(worldPos, 1.0); vec2 screenUV = clipPos.xy / clipPos.w * 0.5 + 0.5; - screenUV = vec2(1.0 - screenUV.x, 1.0 - screenUV.y); + screenUV = vec2(1.0 - screenUV.x, screenUV.y); CC_HANDLE_NDC_SAMPLE_FLIP(screenUV, flipNDCSign); return screenUV; } diff --git a/editor/assets/chunks/common/texture/cubemap.chunk b/editor/assets/chunks/common/texture/cubemap.chunk index b796aee5dc..ced33dc6ec 100644 --- a/editor/assets/chunks/common/texture/cubemap.chunk +++ b/editor/assets/chunks/common/texture/cubemap.chunk @@ -39,7 +39,7 @@ vec3 rotationDir = RotationVecFromAxisY(R.xyz, cc_surfaceTransform.z, cc_surfaceTransform.w); vec4 envmap = fragTextureLod(tex, rotationDir, roughness * (mipCount - 1.0)); - #if CC_USE_IBL == IBL_RGBE || USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE + #if CC_USE_IBL == IBL_RGBE || CC_USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE return unpackRGBE(envmap); #else return SRGBToLinear(envmap.rgb); diff --git a/editor/assets/chunks/legacy/shading-standard-base.chunk b/editor/assets/chunks/legacy/shading-standard-base.chunk index 9d32ecba52..8f127c51d7 100644 --- a/editor/assets/chunks/legacy/shading-standard-base.chunk +++ b/editor/assets/chunks/legacy/shading-standard-base.chunk @@ -16,7 +16,7 @@ #endif #endif -#if USE_REFLECTION_PROBE +#if CC_USE_REFLECTION_PROBE #include #endif #if CC_USE_LIGHT_PROBE @@ -55,10 +55,10 @@ vec3 BRDFApprox (vec3 specular, float roughness, float NoV) { float mipBias = mix(0.0, 5.0, clamp(delta, 0.0, 1.0)); - #if USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE + #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE vec4 biased = fragTextureLod(cc_reflectionProbeCubemap, R, mip + mipBias); vec4 filtered = texture(cc_reflectionProbeCubemap, R); - #elif USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR + #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR vec4 biased = fragTextureLod(cc_reflectionProbePlanarMap, screenUV, mip + mipBias); vec4 filtered = texture(cc_reflectionProbePlanarMap, screenUV); #else @@ -66,7 +66,7 @@ vec3 BRDFApprox (vec3 specular, float roughness, float NoV) { vec4 filtered = texture(cc_environment, R); #endif - #if CC_USE_IBL == IBL_RGBE || USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE + #if CC_USE_IBL == IBL_RGBE || CC_USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE biased.rgb = unpackRGBE(biased); filtered.rgb = unpackRGBE(filtered); #else @@ -189,7 +189,7 @@ vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) { #else vec4 envmap = fragTextureLod(cc_environment, rotationDir, s.roughness * (cc_ambientGround.w - 1.0)); - #if CC_USE_IBL == IBL_RGBE || USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE + #if CC_USE_IBL == IBL_RGBE || CC_USE_REFLECTION_PROBE != REFLECTION_PROBE_NONE env = unpackRGBE(envmap); #else env = SRGBToLinear(envmap.rgb); @@ -198,10 +198,10 @@ vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) { #endif - #if USE_REFLECTION_PROBE - #if USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE + #if CC_USE_REFLECTION_PROBE + #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE vec4 probe = fragTextureLod(cc_reflectionProbeCubemap, R, s.roughness * (cc_ambientGround.w - 1.0)); - #elif USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR + #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR vec2 screenUV = GetScreenUV(s.position, cc_matViewProj, cc_cameraPos.w); vec4 probe = texture(cc_reflectionProbePlanarMap, screenUV); #endif diff --git a/editor/assets/chunks/lighting-models/model-functions/standard.chunk b/editor/assets/chunks/lighting-models/model-functions/standard.chunk index 5652a92bab..27d8f7f4b1 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard.chunk @@ -132,10 +132,10 @@ vec3 CalculateEnvironmentSpecular(in LightingIntermediateData lightingData, floa envSpec = SampleEnvironmentSpecular(cc_environment, lightingData, cc_ambientGround.w); #endif -#if USE_REFLECTION_PROBE - #if USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE +#if CC_USE_REFLECTION_PROBE + #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_CUBE envSpec = SampleEnvironmentSpecular(cc_reflectionProbeCubemap, lightingData, cc_ambientGround.w); - #elif USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR + #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_PLANAR vec2 screenUV = GetScreenUV(lightingData.worldPosition, cc_matViewProj, cc_cameraPos.w); envSpec = fragTextureLod(cc_reflectionProbePlanarMap, screenUV, cc_ambientGround.w).xyz; #endif diff --git a/editor/assets/chunks/surfaces/includes/common-fs.chunk b/editor/assets/chunks/surfaces/includes/common-fs.chunk index e5d60733bc..2104c887ca 100644 --- a/editor/assets/chunks/surfaces/includes/common-fs.chunk +++ b/editor/assets/chunks/surfaces/includes/common-fs.chunk @@ -38,7 +38,7 @@ #include #endif #endif -#if USE_REFLECTION_PROBE +#if CC_USE_REFLECTION_PROBE #include #endif