diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index 46bec5096b..19921e9905 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -318,6 +318,11 @@ export class MeshRenderer extends ModelRenderer { @serializable protected _shadowNormalBias = 0; + @serializable + protected _reflectionProbeId = -1; + + protected _reflectionProbeDataMap: Texture2D | null = null; + // @serializable private _subMeshShapesWeights: number[][] = []; @@ -336,7 +341,7 @@ export class MeshRenderer extends ModelRenderer { set shadowBias (val) { this._shadowBias = val; this._updateShadowBias(); - this._onUpdateLocalShadowBias(); + this._onUpdateLocalShadowBiasAndProbeId(); } /** @@ -354,7 +359,7 @@ export class MeshRenderer extends ModelRenderer { set shadowNormalBias (val) { this._shadowNormalBias = val; this._updateShadowNormalBias(); - this._onUpdateLocalShadowBias(); + this._onUpdateLocalShadowBiasAndProbeId(); } /** @@ -541,7 +546,7 @@ export class MeshRenderer extends ModelRenderer { this._updateShadowNormalBias(); this._updateBakeToReflectionProbe(); this._updateUseReflectionProbe(); - this._onUpdateLocalShadowBias(); + this._onUpdateLocalShadowBiasAndProbeId(); this._updateUseLightProbe(); this._updateReceiveDirLight(); this._attachToScene(); @@ -700,6 +705,21 @@ export class MeshRenderer extends ModelRenderer { } } + public updateReflectionProbeDataMap (dataMap: Texture2D | null) { + this._reflectionProbeDataMap = dataMap; + if (this.model !== null) { + this.model.updateReflectionProbeDataMap(dataMap); + } + } + + public updateReflectionProbeId (probeId: number) { + this._reflectionProbeId = probeId; + if (this.model) { + this.model.reflectionProbeId = probeId; + } + this._onUpdateLocalShadowBiasAndProbeId(); + } + protected _updateReflectionProbeTexture () { if (this.model === null) return; if (this.bakeSettings.reflectionProbe === ReflectionProbeType.BAKED_CUBEMAP) { @@ -743,9 +763,10 @@ export class MeshRenderer extends ModelRenderer { this._updateUseLightProbe(); this._updateModelParams(); this._onUpdateLightingmap(); - this._onUpdateLocalShadowBias(); + this._onUpdateLocalShadowBiasAndProbeId(); this._updateUseReflectionProbe(); this._updateReceiveDirLight(); + this._onUpdateReflectionProbeDataMap(); } } @@ -839,17 +860,26 @@ export class MeshRenderer extends ModelRenderer { ]); } - protected _onUpdateLocalShadowBias () { + protected _onUpdateLocalShadowBiasAndProbeId () { if (this.model !== null) { this.model.updateLocalShadowBias(); + this.model.updateReflectionProbeId(); } - this.setInstancedAttribute('a_localShadowBias', [ + this.setInstancedAttribute('a_localShadowBiasAndProbeId', [ this._shadowBias, this._shadowNormalBias, + this._reflectionProbeId, + 0.0, ]); } + protected _onUpdateReflectionProbeDataMap () { + if (this.model !== null) { + this.model.updateReflectionProbeDataMap(this._reflectionProbeDataMap); + } + } + protected _onMaterialModified (idx: number, material: Material | null) { if (!this._model || !this._model.inited) { return; } this._onRebuildPSO(idx, material || this._getBuiltinMaterial()); @@ -860,8 +890,9 @@ export class MeshRenderer extends ModelRenderer { this._model.isDynamicBatching = this._isBatchingEnabled(); this._model.setSubModelMaterial(idx, material); this._onUpdateLightingmap(); - this._onUpdateLocalShadowBias(); + this._onUpdateLocalShadowBiasAndProbeId(); this._updateReflectionProbeTexture(); + this._onUpdateReflectionProbeDataMap(); } protected _onMeshChanged (old: Mesh | null) { @@ -932,6 +963,7 @@ export class MeshRenderer extends ModelRenderer { protected onReflectionProbeChanged () { this._updateUseReflectionProbe(); + this._onUpdateLocalShadowBiasAndProbeId(); if (this.bakeSettings.reflectionProbe === ReflectionProbeType.BAKED_CUBEMAP) { cclegacy.internal.reflectionProbeManager.updateUseCubeModels(this._model); if (!cclegacy.internal.reflectionProbeManager.getUsedReflectionProbe(this._model, ReflectionProbeType.BAKED_CUBEMAP)) { diff --git a/cocos/3d/reflection-probe/reflection-probe-component.ts b/cocos/3d/reflection-probe/reflection-probe-component.ts index c8f36de620..74ef5ad990 100644 --- a/cocos/3d/reflection-probe/reflection-probe-component.ts +++ b/cocos/3d/reflection-probe/reflection-probe-component.ts @@ -111,6 +111,7 @@ export class ReflectionProbe extends Component { this.probe.size = this._size; if (this.probe) { ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.updateProbeData(); } } @type(Vec3) @@ -292,6 +293,11 @@ export class ReflectionProbe extends Component { onEnable () { if (this._probe) { + const probe = ReflectionProbeManager.probeManager.getProbeById(this._probeId); + if (probe !== null && probe !== this._probe) { + this._probeId = ReflectionProbeManager.probeManager.getNewReflectionProbeId(); + this._probe.updateProbeId(this._probeId); + } ReflectionProbeManager.probeManager.register(this._probe); ReflectionProbeManager.probeManager.onUpdateProbes(true); this._probe.enable(); @@ -309,6 +315,7 @@ export class ReflectionProbe extends Component { this.probe.renderPlanarReflection(this.sourceCamera.camera); ReflectionProbeManager.probeManager.filterModelsForPlanarReflection(); } + ReflectionProbeManager.probeManager.updateProbeData(); } public onDestroy () { @@ -338,6 +345,7 @@ export class ReflectionProbe extends Component { } if (this.node.hasChangedFlags & TransformBit.POSITION) { ReflectionProbeManager.probeManager.onUpdateProbes(true); + ReflectionProbeManager.probeManager.updateProbeData(); } } if (this.probeType === ProbeType.PLANAR && this.sourceCamera) { diff --git a/cocos/render-scene/scene/model.ts b/cocos/render-scene/scene/model.ts index 8b2c081809..8ef1a06e0f 100644 --- a/cocos/render-scene/scene/model.ts +++ b/cocos/render-scene/scene/model.ts @@ -36,10 +36,11 @@ import { IMacroPatch } from '../core/pass'; import { Mat4, Vec3, Vec4, geometry, cclegacy, EPSILON } from '../../core'; import { Attribute, DescriptorSet, Device, Buffer, BufferInfo, BufferUsageBit, MemoryUsageBit, Filter, Address, SamplerInfo, deviceManager, Texture } from '../../gfx'; -import { UBOLocal, UBOSH, UBOWorldBound, UNIFORM_LIGHTMAP_TEXTURE_BINDING, UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING } from '../../rendering/define'; +import { UBOLocal, UBOSH, UBOWorldBound, UNIFORM_LIGHTMAP_TEXTURE_BINDING, UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING, UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING } from '../../rendering/define'; import { Root } from '../../root'; import { TextureCube } from '../../asset/assets'; import { ShadowType } from './shadows'; +import { ProbeType, ReflectionProbe } from './reflection-probe'; const m4_1 = new Mat4(); @@ -346,6 +347,18 @@ export class Model { this.onMacroPatchesStateChanged(); } + /** + * @en sets or gets reflection probe id + * @zh 设置或获取反射探针id。 + */ + get reflectionProbeId () { + return this._reflectionProbeId; + } + + set reflectionProbeId (val) { + this._reflectionProbeId = val; + } + /** * @en The type of the model * @zh 模型类型 @@ -495,6 +508,12 @@ export class Model { */ protected _shadowNormalBias = 0; + /** + * @en Reflect probe Id + * @zh 使用第几个反射探针 + */ + protected _reflectionProbeId = -1; + /** * @en Whether the model is enabled in the render scene so that it will be rendered * @zh 模型是否在渲染场景中启用并被渲染 @@ -928,9 +947,11 @@ export class Model { const subModels = this._subModels; for (let i = 0; i < subModels.length; i++) { const { descriptorSet } = subModels[i]; - descriptorSet.bindSampler(UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, reflectionSampler); - descriptorSet.bindTexture(UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, gfxTexture); - descriptorSet.update(); + if (descriptorSet) { + descriptorSet.bindSampler(UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, reflectionSampler); + descriptorSet.bindTexture(UNIFORM_REFLECTION_PROBE_CUBEMAP_BINDING, gfxTexture); + descriptorSet.update(); + } } } } @@ -959,9 +980,37 @@ export class Model { const subModels = this._subModels; for (let i = 0; i < subModels.length; i++) { const { descriptorSet } = subModels[i]; - descriptorSet.bindTexture(UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING, texture); - descriptorSet.bindSampler(UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING, sampler); - descriptorSet.update(); + if (descriptorSet) { + descriptorSet.bindTexture(UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING, texture); + descriptorSet.bindSampler(UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING, sampler); + descriptorSet.update(); + } + } + } + } + + /** + * @en Update the data map of the reflection probe + * @zh 更新反射探针的数据贴图 + * @param texture data map + */ + public updateReflectionProbeDataMap (texture: Texture2D | null) { + this._localDataUpdated = true; + this.onMacroPatchesStateChanged(); + + if (!texture) { + texture = builtinResMgr.get('empty-texture'); + } + const gfxTexture = texture.getGFXTexture(); + if (gfxTexture) { + const subModels = this._subModels; + for (let i = 0; i < subModels.length; i++) { + const { descriptorSet } = subModels[i]; + if (descriptorSet) { + descriptorSet.bindTexture(UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING, gfxTexture); + descriptorSet.bindSampler(UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING, texture.getGFXSampler()); + descriptorSet.update(); + } } } } @@ -974,8 +1023,44 @@ export class Model { const sv = this._localData; sv[UBOLocal.LOCAL_SHADOW_BIAS + 0] = this._shadowBias; sv[UBOLocal.LOCAL_SHADOW_BIAS + 1] = this._shadowNormalBias; - sv[UBOLocal.LOCAL_SHADOW_BIAS + 2] = 0; + this._localDataUpdated = true; + } + + /** + * @en Update the id of reflection probe + * @zh 更新物体使用哪个反射探针 + */ + public updateReflectionProbeId () { + const sv = this._localData; + sv[UBOLocal.LOCAL_SHADOW_BIAS + 2] = this._reflectionProbeId; sv[UBOLocal.LOCAL_SHADOW_BIAS + 3] = 0; + let probe: ReflectionProbe | null = null; + if (cclegacy.internal.reflectionProbeManager) { + probe = cclegacy.internal.reflectionProbeManager.getProbeById(this._reflectionProbeId); + } + if (probe) { + if (probe.probeType === ProbeType.PLANAR) { + sv[UBOLocal.REFLECTION_PROBE_DATA1] = probe.node.up.x; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 1] = probe.node.up.y; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 2] = probe.node.up.z; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 3] = 1.0; + + sv[UBOLocal.REFLECTION_PROBE_DATA2] = 1.0; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 1] = 0.0; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 2] = 0.0; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 3] = 1.0; + } else { + sv[UBOLocal.REFLECTION_PROBE_DATA1] = probe.node.worldPosition.x; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 1] = probe.node.worldPosition.y; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 2] = probe.node.worldPosition.z; + sv[UBOLocal.REFLECTION_PROBE_DATA1 + 3] = 0.0; + + sv[UBOLocal.REFLECTION_PROBE_DATA2] = probe.size.x; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 1] = probe.size.y; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 2] = probe.size.z; + sv[UBOLocal.REFLECTION_PROBE_DATA2 + 3] = probe.cubemap ? probe.cubemap.mipmapLevel : 1.0; + } + } this._localDataUpdated = true; } diff --git a/cocos/render-scene/scene/reflection-probe.ts b/cocos/render-scene/scene/reflection-probe.ts index c0f00e9ef9..d75e82d4c4 100644 --- a/cocos/render-scene/scene/reflection-probe.ts +++ b/cocos/render-scene/scene/reflection-probe.ts @@ -56,7 +56,7 @@ export class ReflectionProbe { public realtimePlanarTexture: RenderTexture | null = null; - protected _resolution = 512; + protected _resolution = 256; protected _clearFlag: number = ProbeClearFlag.SKYBOX; protected _backgroundColor = new Color(0, 0, 0, 255); protected _visibility = CAMERA_DEFAULT_MASK; @@ -329,6 +329,10 @@ export class ReflectionProbe { return this._probeId; } + public updateProbeId (id) { + this._probeId = id; + } + public renderArea (): Vec2 { if (this._probeType === ProbeType.PLANAR) { return new Vec2(this.realtimePlanarTexture!.width, this.realtimePlanarTexture!.height); diff --git a/cocos/rendering/define.ts b/cocos/rendering/define.ts index 2b426dbe3b..685b44a1d8 100644 --- a/cocos/rendering/define.ts +++ b/cocos/rendering/define.ts @@ -146,6 +146,7 @@ export enum ModelLocalBindings { SAMPLER_REFLECTION_PROBE_CUBE, SAMPLER_REFLECTION_PROBE_PLANAR, + SAMPLER_REFLECTION_PROBE_DATA_MAP, COUNT, } @@ -178,8 +179,9 @@ export class UBOGlobal { public static readonly TIME_OFFSET = 0; public static readonly SCREEN_SIZE_OFFSET = UBOGlobal.TIME_OFFSET + 4; public static readonly NATIVE_SIZE_OFFSET = UBOGlobal.SCREEN_SIZE_OFFSET + 4; + public static readonly PROBE_INFO_OFFSET = UBOGlobal.NATIVE_SIZE_OFFSET + 4; - public static readonly DEBUG_VIEW_MODE_OFFSET = UBOGlobal.NATIVE_SIZE_OFFSET + 4; + public static readonly DEBUG_VIEW_MODE_OFFSET = UBOGlobal.PROBE_INFO_OFFSET + 4; public static readonly DEBUG_VIEW_COMPOSITE_PACK_1_OFFSET = UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 4; public static readonly DEBUG_VIEW_COMPOSITE_PACK_2_OFFSET = UBOGlobal.DEBUG_VIEW_COMPOSITE_PACK_1_OFFSET + 4; public static readonly DEBUG_VIEW_COMPOSITE_PACK_3_OFFSET = UBOGlobal.DEBUG_VIEW_COMPOSITE_PACK_2_OFFSET + 4; @@ -195,6 +197,7 @@ export class UBOGlobal { new Uniform('cc_time', Type.FLOAT4, 1), new Uniform('cc_screenSize', Type.FLOAT4, 1), new Uniform('cc_nativeSize', Type.FLOAT4, 1), + new Uniform('cc_probeInfo', Type.FLOAT4, 1), new Uniform('cc_debug_view_mode', Type.FLOAT4, 1), new Uniform('cc_debug_view_composite_pack_1', Type.FLOAT4, 1), @@ -378,7 +381,9 @@ export class UBOLocal { public static readonly MAT_WORLD_IT_OFFSET = UBOLocal.MAT_WORLD_OFFSET + 16; public static readonly LIGHTINGMAP_UVPARAM = UBOLocal.MAT_WORLD_IT_OFFSET + 16; public static readonly LOCAL_SHADOW_BIAS = UBOLocal.LIGHTINGMAP_UVPARAM + 4; - public static readonly COUNT = UBOLocal.LOCAL_SHADOW_BIAS + 4; + public static readonly REFLECTION_PROBE_DATA1 = UBOLocal.LOCAL_SHADOW_BIAS + 4; + public static readonly REFLECTION_PROBE_DATA2 = UBOLocal.REFLECTION_PROBE_DATA1 + 4; + public static readonly COUNT = UBOLocal.REFLECTION_PROBE_DATA2 + 4; public static readonly SIZE = UBOLocal.COUNT * 4; public static readonly NAME = 'CCLocal'; @@ -389,6 +394,8 @@ export class UBOLocal { new Uniform('cc_matWorldIT', Type.MAT4, 1), new Uniform('cc_lightingMapUVParam', Type.FLOAT4, 1), new Uniform('cc_localShadowBias', Type.FLOAT4, 1), + new Uniform('cc_reflectionProbeData1', Type.FLOAT4, 1), + new Uniform('cc_reflectionProbeData2', Type.FLOAT4, 1), ], 1); } localDescriptorSetLayout.layouts[UBOLocal.NAME] = UBOLocal.LAYOUT; @@ -730,6 +737,18 @@ const UNIFORM_REFLECTION_PROBE_TEXTURE_LAYOUT = new UniformSamplerTexture(SetInd localDescriptorSetLayout.layouts[UNIFORM_REFLECTION_PROBE_TEXTURE_NAME] = UNIFORM_REFLECTION_PROBE_TEXTURE_LAYOUT; localDescriptorSetLayout.bindings[UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING] = UNIFORM_REFLECTION_PROBE_TEXTURE_DESCRIPTOR; +/** + * @en The sampler for reflection probe data map + * @zh 反射探针数据贴图采样器。 + */ +const UNIFORM_REFLECTION_PROBE_DATA_MAP_NAME = 'cc_reflectionProbeDataMap'; +export const UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING = ModelLocalBindings.SAMPLER_REFLECTION_PROBE_DATA_MAP; +const UNIFORM_REFLECTION_PROBE_DATA_MAP_DESCRIPTOR = new DescriptorSetLayoutBinding(UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING, DescriptorType.SAMPLER_TEXTURE, 1, ShaderStageFlagBit.FRAGMENT); +const UNIFORM_REFLECTION_PROBE_DATA_MAP_LAYOUT = new UniformSamplerTexture(SetIndex.LOCAL, UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING, + UNIFORM_REFLECTION_PROBE_DATA_MAP_NAME, Type.SAMPLER2D, 1); +localDescriptorSetLayout.layouts[UNIFORM_REFLECTION_PROBE_DATA_MAP_NAME] = UNIFORM_REFLECTION_PROBE_DATA_MAP_LAYOUT; +localDescriptorSetLayout.bindings[UNIFORM_REFLECTION_PROBE_DATA_MAP_BINDING] = UNIFORM_REFLECTION_PROBE_DATA_MAP_DESCRIPTOR; + export const CAMERA_DEFAULT_MASK = Layers.makeMaskExclude([Layers.BitMask.UI_2D, Layers.BitMask.GIZMOS, Layers.BitMask.EDITOR, Layers.BitMask.SCENE_GIZMO, Layers.BitMask.PROFILER]); diff --git a/cocos/rendering/pipeline-ubo.ts b/cocos/rendering/pipeline-ubo.ts index e7c6f23d6b..3994054d19 100644 --- a/cocos/rendering/pipeline-ubo.ts +++ b/cocos/rendering/pipeline-ubo.ts @@ -70,6 +70,9 @@ export class PipelineUBO { fv[UBOGlobal.NATIVE_SIZE_OFFSET + 2] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET]; fv[UBOGlobal.NATIVE_SIZE_OFFSET + 3] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1]; + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + fv[UBOGlobal.PROBE_INFO_OFFSET] = cclegacy.internal.reflectionProbeManager.getMaxProbeId() + 1; + const debugView = root.debugView; fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET] = debugView.singleMode as number; fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 1] = debugView.lightingWithAlbedo ? 1.0 : 0.0; diff --git a/cocos/rendering/reflection-probe-manager.ts b/cocos/rendering/reflection-probe-manager.ts index df69960125..a5d9ac31d9 100644 --- a/cocos/rendering/reflection-probe-manager.ts +++ b/cocos/rendering/reflection-probe-manager.ts @@ -23,8 +23,9 @@ THE SOFTWARE. */ -import { EDITOR } from 'internal:constants'; import { MeshRenderer, ReflectionProbeType } from '../3d/framework/mesh-renderer'; +import { ImageAsset, Texture2D } from '../asset/assets'; +import { PixelFormat } from '../asset/assets/asset-enum'; import { Vec3, geometry, cclegacy } from '../core'; import { director, Director } from '../game'; import { Texture } from '../gfx'; @@ -52,13 +53,11 @@ export class ReflectionProbeManager { * 场景中所有使用planar类型反射探针的模型 */ private _usePlanarModels = new Map(); - private _updateForRuntime = true; - + private _dataTexture: Texture2D | null = null; constructor () { director.on(Director.EVENT_BEFORE_UPDATE, this.onUpdateProbes, this); } - /** * @en Set and get whether to detect objects leaving or entering the reflection probe's bounding box at runtime. * @zh 设置和获取是否在运行时检测物体离开或者进入反射探针的包围盒。 @@ -75,8 +74,7 @@ export class ReflectionProbeManager { * @zh 刷新所有反射探针 */ public onUpdateProbes (forceUpdate = false) { - if (!this._updateForRuntime) return; - if (this._probes.length === 0) return; + if (!this._updateForRuntime || this._probes.length === 0) return; const scene = director.getScene(); if (!scene || !scene.renderScene) { return; @@ -114,11 +112,7 @@ export class ReflectionProbeManager { public clearPlanarReflectionMap (probe: ReflectionProbe) { for (const entry of this._usePlanarModels.entries()) { if (entry[1] === probe) { - const model = entry[0]; - const meshRender = model.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbePlanarMap(null); - } + this._updatePlanarMapOfModel(entry[0], null, null); } } } @@ -127,6 +121,7 @@ export class ReflectionProbeManager { const index = this._probes.indexOf(probe); if (index === -1) { this._probes.push(probe); + this.updateProbeData(); } } @@ -140,6 +135,7 @@ export class ReflectionProbeManager { break; } } + this.updateProbeData(); } public exists (probeId: number): boolean { @@ -168,6 +164,15 @@ export class ReflectionProbeManager { return this._probes; } + public getProbeById (probeId: number): ReflectionProbe | null { + for (let i = 0; i < this._probes.length; i++) { + if (this._probes[i].getProbeId() === probeId) { + return this._probes[i]; + } + } + return null; + } + public clearAll () { this._probes = []; } @@ -191,10 +196,7 @@ export class ReflectionProbeManager { if (!probe.cubemap) return; for (let i = 0; i < models.length; i++) { const model = models[i]; - const meshRender = model.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbeCubemap(probe.cubemap); - } + this._updateCubemapOfModel(model, probe); } probe.needRefresh = false; } @@ -208,11 +210,7 @@ export class ReflectionProbeManager { 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); - } + this._updatePlanarMapOfModel(models[i], texture, probe); } if (probe.previewPlane) { const meshRender = probe.previewPlane.getComponent(MeshRenderer); @@ -233,8 +231,7 @@ export class ReflectionProbeManager { for (let i = 0; i < this._probes.length; i++) { const probe = this._probes[i]; if (probe.probeType !== ProbeType.PLANAR) continue; - const meshRender = model.node.getComponent(MeshRenderer); - if ((model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) && meshRender) { + if (model.node.layer & REFLECTION_PROBE_DEFAULT_MASK) { model.updateWorldBound(); if (geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) { this._usePlanarModels.set(model, probe); @@ -242,10 +239,7 @@ export class ReflectionProbeManager { const old = this._usePlanarModels.get(model); if (old === probe) { this._usePlanarModels.delete(model); - const meshRender = model.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbePlanarMap(null); - } + this._updatePlanarMapOfModel(model, null, null); } } } @@ -274,10 +268,7 @@ export class ReflectionProbeManager { const nearest = this._getNearestProbe(model); if (!nearest) { //not in the range of any probe,set default texture for the model - const meshRender = model.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbeCubemap(null); - } + this._updateCubemapOfModel(model, null); this._useCubeModels.delete(model); } else if (this._useCubeModels.has(model)) { const old = this._useCubeModels.get(model); @@ -325,6 +316,100 @@ export class ReflectionProbeManager { } } + /** + * @en Update reflection probe data of model bind. + * @zh 更新模型绑定的反射探针数据。 + */ + public updateProbeData () { + if (this._probes.length === 0) return; + const maxId = this.getMaxProbeId(); + const height = maxId + 1; + const dataWidth = 3; + if (this._dataTexture) { + this._dataTexture.destroy(); + } + + const buffer = new Float32Array(4 * dataWidth * height); + let bufferOffset = 0; + for (let i = 0; i <= maxId; i++) { + const probe = this.getProbeById(i); + if (!probe) { + bufferOffset += 4 * dataWidth; + continue; + } + if (probe.probeType === ProbeType.CUBE) { + //world pos + buffer[bufferOffset] = probe.node.worldPosition.x; + buffer[bufferOffset + 1] = probe.node.worldPosition.y; + buffer[bufferOffset + 2] = probe.node.worldPosition.z; + buffer[bufferOffset + 3] = 0.0; + + buffer[bufferOffset + 4] = probe.size.x; + buffer[bufferOffset + 5] = probe.size.y; + buffer[bufferOffset + 6] = probe.size.z; + buffer[bufferOffset + 7] = 0.0; + buffer[bufferOffset + 8] = probe.cubemap ? probe.cubemap.mipmapLevel : 1.0; + } else { + //plane.xyz; + buffer[bufferOffset] = probe.node.up.x; + buffer[bufferOffset + 1] = probe.node.up.y; + buffer[bufferOffset + 2] = probe.node.up.z; + buffer[bufferOffset + 3] = 1.0; + //plane.w; + buffer[bufferOffset + 4] = 1.0; + //planarReflectionDepthScale + buffer[bufferOffset + 5] = 1.0; + buffer[bufferOffset + 6] = 0.0; + buffer[bufferOffset + 7] = 0.0; + //mipCount; + buffer[bufferOffset + 8] = 1.0; + } + bufferOffset += 4 * dataWidth; + } + const updateView = new Uint8Array(buffer.buffer); + const image = new ImageAsset({ + _data: updateView, + _compressed: false, + width: dataWidth * 4, + height, + format: PixelFormat.RGBA8888, + }); + + this._dataTexture = new Texture2D(); + this._dataTexture.setFilters(Texture2D.Filter.NONE, Texture2D.Filter.NONE); + this._dataTexture.setMipFilter(Texture2D.Filter.NONE); + this._dataTexture.setWrapMode(Texture2D.WrapMode.CLAMP_TO_EDGE, Texture2D.WrapMode.CLAMP_TO_EDGE, Texture2D.WrapMode.CLAMP_TO_EDGE); + this._dataTexture.image = image; + + this._dataTexture.uploadData(updateView); + + for (let i = 0; i < this._probes.length; i++) { + const probe = this._probes[i]; + const models = this._getModelsByProbe(probe); + for (let j = 0; j < models.length; j++) { + const meshRender = models[j].node.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateReflectionProbeDataMap(this._dataTexture); + meshRender.updateReflectionProbeId(probe.getProbeId()); + } + } + } + } + + /** + * @en get max value of probe id. + * @zh 获取反射探针id的最大值。 + */ + public getMaxProbeId () { + if (this._probes.length === 0) { + return -1; + } + if (this._probes.length === 1) { + return this._probes[0].getProbeId(); + } + this._probes.sort((a: ReflectionProbe, b: ReflectionProbe) => a.getProbeId() - b.getProbeId()); + return this._probes[this._probes.length - 1].getProbeId(); + } /** * @en Get the reflection probe used by the model. * @zh 获取模型使用的反射探针。 @@ -350,26 +435,42 @@ export class ReflectionProbeManager { * @param model select the probe for this model */ private _getNearestProbe (model: Model): ReflectionProbe | null { + if (!model.node || !model.worldBounds || this._probes.length === 0) return null; + + let nearestProbe: ReflectionProbe | null = null; + let minDistance = Infinity; + + for (const probe of this._probes) { + if (probe.probeType !== ProbeType.CUBE || !probe.validate() || !geometry.intersect.aabbWithAABB(model.worldBounds, probe.boundingBox!)) { + continue; + } + + const distance = Vec3.distance(model.node.worldPosition, probe.node.worldPosition); + if (distance < minDistance) { + minDistance = distance; + nearestProbe = probe; + } + } + + return nearestProbe; + } + + private _getBlendProbe (model: Model): ReflectionProbe | null { if (this._probes.length === 0) return null; if (!model.node || !model.worldBounds) return null; - let distance = 0; - let idx = -1; - let find = false; + const temp: ReflectionProbe[] = []; for (let i = 0; i < this._probes.length; i++) { if (this._probes[i].probeType !== ProbeType.CUBE || !this._probes[i].validate() || !geometry.intersect.aabbWithAABB(model.worldBounds, this._probes[i].boundingBox!)) { continue; - } else if (!find) { - find = true; - distance = Vec3.distance(model.node.worldPosition, this._probes[i].node.worldPosition); - idx = i; - } - const d = Vec3.distance(model.node.worldPosition, this._probes[i].node.worldPosition); - if (d < distance) { - distance = d; - idx = i; } + temp.push(this._probes[i]); } - return find ? this._probes[idx] : null; + temp.sort((a: ReflectionProbe, b: ReflectionProbe) => { + const aDistance = Vec3.distance(model.node.worldPosition, a.node.worldPosition); + const bDistance = Vec3.distance(model.node.worldPosition, b.node.worldPosition); + return bDistance - aDistance; + }); + return temp.length > 1 ? temp[1] : null; } private _getModelsByProbe (probe: ReflectionProbe) { @@ -398,10 +499,31 @@ export class ReflectionProbeManager { const p = this._usePlanarModels.get(key); if (p !== undefined && p === probe) { this._usePlanarModels.delete(key); - const meshRender = key.node.getComponent(MeshRenderer); - if (meshRender) { - meshRender.updateProbePlanarMap(null); - } + this.updateUsePlanarModels(key); + } + } + } + + private _updateCubemapOfModel (model: Model, probe: ReflectionProbe | null) { + if (!model.node) { + return; + } + const meshRender = model.node.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateProbeCubemap(probe ? probe.cubemap : null); + meshRender.updateReflectionProbeId(probe ? probe.getProbeId() : -1); + if (probe) { + meshRender.updateReflectionProbeDataMap(this._dataTexture); + } + } + } + private _updatePlanarMapOfModel (model: Model, texture: Texture | null, probe: ReflectionProbe | null) { + const meshRender = model.node.getComponent(MeshRenderer); + if (meshRender) { + meshRender.updateProbePlanarMap(texture); + meshRender.updateReflectionProbeId(probe ? probe.getProbeId() : -1); + if (probe) { + meshRender.updateReflectionProbeDataMap(this._dataTexture); } } } diff --git a/editor/assets/chunks/builtin/functionalities/probe.chunk b/editor/assets/chunks/builtin/functionalities/probe.chunk new file mode 100644 index 0000000000..11b2fc9e5a --- /dev/null +++ b/editor/assets/chunks/builtin/functionalities/probe.chunk @@ -0,0 +1,45 @@ +#include +vec4 GetTexData(sampler2D dataMap, float dataMapWidth, float x, float uv_y) +{ + return vec4( + decode32(texture(dataMap, vec2(((x + 0.5)/dataMapWidth), uv_y))), + decode32(texture(dataMap, vec2(((x + 1.5)/dataMapWidth), uv_y))), + decode32(texture(dataMap, vec2(((x + 2.5)/dataMapWidth), uv_y))), + decode32(texture(dataMap, vec2(((x + 3.5)/dataMapWidth), uv_y))) + ); +} +void GetPlanarReflectionProbeData(out vec4 plane, out float planarReflectionDepthScale, out float mipCount, float probeId) +{ + #if USE_INSTANCING + float uv_y = (probeId + 0.5) / cc_probeInfo.x; //align to texel center + float dataMapWidth = 12.0; + vec4 texData1 = GetTexData(cc_reflectionProbeDataMap, dataMapWidth, 0.0, uv_y); + vec4 texData2 = GetTexData(cc_reflectionProbeDataMap, dataMapWidth, 4.0, uv_y); + plane.xyz = texData1.xyz; + plane.w = texData2.x; + planarReflectionDepthScale = texData2.y; + mipCount = texData2.z; + #else + plane = cc_reflectionProbeData1; + planarReflectionDepthScale = cc_reflectionProbeData2.x; + mipCount = cc_reflectionProbeData2.w; + #endif +} + +void GetCubeReflectionProbeData(out vec3 centerPos, out vec3 boxHalfSize, out float mipCount, float probeId) +{ + #if USE_INSTANCING + float uv_y = (probeId + 0.5) / cc_probeInfo.x; //align to texel center + float dataMapWidth = 12.0; + vec4 texData1 = GetTexData(cc_reflectionProbeDataMap, dataMapWidth, 0.0, uv_y); + vec4 texData2 = GetTexData(cc_reflectionProbeDataMap, dataMapWidth, 4.0, uv_y); + vec4 texData3 = GetTexData(cc_reflectionProbeDataMap, dataMapWidth, 8.0, uv_y); + centerPos = texData1.xyz; + boxHalfSize = texData2.xyz; + mipCount = texData3.x; + #else + centerPos = cc_reflectionProbeData1.xyz; + boxHalfSize = cc_reflectionProbeData2.xyz; + mipCount = cc_reflectionProbeData2.w; + #endif +} diff --git a/editor/assets/chunks/builtin/functionalities/probe.chunk.meta b/editor/assets/chunks/builtin/functionalities/probe.chunk.meta new file mode 100644 index 0000000000..b1a48df9be --- /dev/null +++ b/editor/assets/chunks/builtin/functionalities/probe.chunk.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.0.7", + "importer": "effect-header", + "imported": true, + "uuid": "92ac3587-8a8c-4405-87b0-5cf026196c64", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/editor/assets/chunks/builtin/uniforms/cc-global.chunk b/editor/assets/chunks/builtin/uniforms/cc-global.chunk index 8e815860f1..0962160ff6 100644 --- a/editor/assets/chunks/builtin/uniforms/cc-global.chunk +++ b/editor/assets/chunks/builtin/uniforms/cc-global.chunk @@ -7,6 +7,7 @@ layout(set = 0, binding = 0) uniform CCGlobal { highp vec4 cc_time; // x: global time since started in seconds, y: delta time for current frame, z: total frames since started, w: fractinal part of x, better precious, should be used with uv animation mediump vec4 cc_screenSize; // xy: screen size, zw: inverse screen size mediump vec4 cc_nativeSize; // xy: shading size, zw: inverse shading size + mediump vec4 cc_probeInfo; // x: reflection probe count mediump vec4 cc_debug_view_mode; // x: single mode, y: lighting with albedo, z: csm layer coloration w: unused mediump vec4 cc_debug_view_composite_pack_1; // x: dir diffuse, y: dir specular, z: env diffuse, w: env specular diff --git a/editor/assets/chunks/builtin/uniforms/cc-local.chunk b/editor/assets/chunks/builtin/uniforms/cc-local.chunk index a2bfe24071..3e3e763988 100644 --- a/editor/assets/chunks/builtin/uniforms/cc-local.chunk +++ b/editor/assets/chunks/builtin/uniforms/cc-local.chunk @@ -5,5 +5,7 @@ layout(set = 2, binding = 0) uniform CCLocal { highp mat4 cc_matWorld; highp mat4 cc_matWorldIT; highp vec4 cc_lightingMapUVParam; - highp vec4 cc_localShadowBias; // x:shadow bias, y:shadow normal bias. + highp vec4 cc_localShadowBias; // x:shadow bias, y:shadow normal bias, z:unused, w:visibility for dirlight + highp vec4 cc_reflectionProbeData1; // xyzw: plane or xyz: cube center + highp vec4 cc_reflectionProbeData2; // x: planar reflection depth scale or xyz: cube box half size, w: mipCount }; diff --git a/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk b/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk index 00975fe494..52e50b9761 100644 --- a/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk +++ b/editor/assets/chunks/builtin/uniforms/cc-reflection-probe.chunk @@ -5,3 +5,6 @@ layout(set = 2, binding = 15) uniform samplerCube cc_reflectionProbeCubemap; #pragma builtin(local) layout(set = 2, binding = 16) uniform sampler2D cc_reflectionProbePlanarMap; + +#pragma builtin(local) +layout(set = 2, binding = 17) uniform sampler2D cc_reflectionProbeDataMap; diff --git a/editor/assets/chunks/common/lighting/functions.chunk b/editor/assets/chunks/common/lighting/functions.chunk index 025ab31d3b..18f478f2bb 100644 --- a/editor/assets/chunks/common/lighting/functions.chunk +++ b/editor/assets/chunks/common/lighting/functions.chunk @@ -47,3 +47,19 @@ vec3 CalculatePlanarReflectPositionOnPlane(vec3 N, vec3 V, vec3 worldPos, vec4 p float y = distCameraToPlane / max(EPSILON_LOWP, dot(planeN, virtualCameraToReflectedPoint)); return virtualCameraPos + y * virtualCameraToReflectedPoint; } + + + +// fix cubemap direction with box projection +// return unnormalized vector and weight for exceeding +vec4 CalculateBoxProjectedDirection(vec3 R, vec3 worldPos, vec3 cubeCenterPos, vec3 cubeBoxHalfSize) +{ + // point W is the worldPos in the space origin align with cube center + vec3 W = worldPos - cubeCenterPos; + // find point P which intersected with cube box border from W alone R + vec3 projectedLength = (sign(R) * cubeBoxHalfSize - W) / (R + vec3(EPSILON)); + float len = min(min(projectedLength.x, projectedLength.y), projectedLength.z); + vec3 P = W + len * R; + float weight = len < 0.0 ? 0.0 : 1.0; + return vec4(P, weight); +} \ No newline at end of file diff --git a/editor/assets/chunks/legacy/decode-base.chunk b/editor/assets/chunks/legacy/decode-base.chunk index a1c18c1af2..4592cba78e 100644 --- a/editor/assets/chunks/legacy/decode-base.chunk +++ b/editor/assets/chunks/legacy/decode-base.chunk @@ -39,8 +39,8 @@ layout(location = 3) in vec4 a_tangent; #if CC_USE_LIGHTMAP in vec4 a_lightingMapUVParam; #endif - #if CC_RECEIVE_SHADOW - in vec2 a_localShadowBias; // x:shadow bias, y:shadow normal bias. + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + in vec4 a_localShadowBiasAndProbeId; // x:shadow bias, y:shadow normal bias, z: reflection probe id, w: reserved for blend reflection probe id #endif #if CC_USE_LIGHT_PROBE in vec4 a_sh_linear_const_r; diff --git a/editor/assets/chunks/legacy/shading-standard-base.chunk b/editor/assets/chunks/legacy/shading-standard-base.chunk index e2130729c1..753ee54e8a 100644 --- a/editor/assets/chunks/legacy/shading-standard-base.chunk +++ b/editor/assets/chunks/legacy/shading-standard-base.chunk @@ -7,9 +7,9 @@ #include #include #include +#include +#include #if CC_USE_IBL - #include - #if CC_USE_DIFFUSEMAP #include #endif @@ -17,6 +17,7 @@ #if CC_USE_REFLECTION_PROBE #include + #include #endif #if CC_USE_LIGHT_PROBE #include @@ -101,6 +102,9 @@ struct StandardSurface { #if CC_RECEIVE_SHADOW vec2 shadowBias; #endif + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + float reflectionProbeId; + #endif }; vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) { @@ -203,10 +207,29 @@ vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) { vec4 probe = vec4(0.0); vec3 R = normalize(reflect(-V, N)); #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_CUBE - probe = fragTextureLod(cc_reflectionProbeCubemap, R, s.roughness * (cc_ambientGround.w - 1.0)); + if(s.reflectionProbeId < 0.0){ + probe = fragTextureLod(cc_reflectionProbeCubemap, R, s.roughness * (cc_ambientGround.w - 1.0)); + }else{ + vec3 centerPos, boxHalfSize; + float mipCount; + GetCubeReflectionProbeData(centerPos, boxHalfSize, mipCount, s.reflectionProbeId); + vec4 fixedR = CalculateBoxProjectedDirection(R, position, centerPos, boxHalfSize); + probe = mix(fragTextureLod(cc_environment, R, s.roughness * (cc_ambientGround.w - 1.0)), + fragTextureLod(cc_reflectionProbeCubemap, fixedR.xyz, s.roughness * (mipCount - 1.0)), fixedR.w); + } #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_PLANAR - vec2 screenUV = GetPlanarReflectScreenUV(s.position, cc_matViewProj, cc_cameraPos.w, V, R); - probe = texture(cc_reflectionProbePlanarMap, screenUV); + if(s.reflectionProbeId < 0.0){ + vec2 screenUV = GetPlanarReflectScreenUV(s.position, cc_matViewProj, cc_cameraPos.w, V, R); + probe = fragTextureLod(cc_reflectionProbePlanarMap, screenUV, 1.0); + }else{ + vec4 plane; + float planarReflectionDepthScale, mipCount; + GetPlanarReflectionProbeData(plane, planarReflectionDepthScale, mipCount, s.reflectionProbeId); + R = normalize(CalculateReflectDirection(N, V, max(abs(dot(N, V)), 0.0))); + vec3 worldPosOffset = CalculatePlanarReflectPositionOnPlane(N, V, s.position, plane, cc_cameraPos.xyz, planarReflectionDepthScale); + vec2 screenUV = GetPlanarReflectScreenUV(worldPosOffset, cc_matViewProj, cc_cameraPos.w, V, R); + probe = fragTextureLod(cc_reflectionProbePlanarMap, screenUV, mipCount); + } #endif env = unpackRGBE(probe); #endif diff --git a/editor/assets/chunks/legacy/shadow-map-vs.chunk b/editor/assets/chunks/legacy/shadow-map-vs.chunk index fa32af28a3..85c5209302 100644 --- a/editor/assets/chunks/legacy/shadow-map-vs.chunk +++ b/editor/assets/chunks/legacy/shadow-map-vs.chunk @@ -12,7 +12,7 @@ out highp vec4 v_shadowPos; vec2 CCGetShadowBias() { #if USE_INSTANCING - return vec2(a_localShadowBias.x + cc_shadowWHPBInfo.w, a_localShadowBias.y + cc_shadowLPNNInfo.z); + return vec2(a_localShadowBiasAndProbeId.x + cc_shadowWHPBInfo.w, a_localShadowBiasAndProbeId.y + cc_shadowLPNNInfo.z); #else return vec2(cc_localShadowBias.x + cc_shadowWHPBInfo.w, cc_localShadowBias.y + cc_shadowLPNNInfo.z); #endif diff --git a/editor/assets/chunks/lighting-models/model-functions/standard-common.chunk b/editor/assets/chunks/lighting-models/model-functions/standard-common.chunk index 75007da34e..0d7ac87701 100644 --- a/editor/assets/chunks/lighting-models/model-functions/standard-common.chunk +++ b/editor/assets/chunks/lighting-models/model-functions/standard-common.chunk @@ -88,6 +88,33 @@ vec3 SampleEnvironmentSpecular(samplerCube tex, in LightingIntermediateData ligh return envSpec; } +vec3 SampleEnvironmentSpecular(samplerCube tex, in LightingIntermediateData lightingData, float mipCount, vec3 worldPos, vec3 cubeCenterPos, vec3 boxHalfSize) +{ + vec3 envSpec = vec3(0.0); + float roughness = lightingData.specularParam; + #if CC_SURFACES_LIGHTING_ANISOTROPIC && !CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT + vec3 R = GetAnisotropicReflect(roughness, lightingData.anisotropyShape, lightingData.V, lightingData.N, lightingData.T, lightingData.B); + #else + vec3 R = CalculateReflectDirection(lightingData.N, lightingData.V, lightingData.NoV); + #endif + + vec4 fixedR = CalculateBoxProjectedDirection(R, worldPos, cubeCenterPos, boxHalfSize); + R = fixedR.xyz; + + vec3 envmap = SampleEnvironmentSpecular(cc_environment, lightingData, cc_ambientGround.w).xyz; + #if CC_SURFACES_LIGHTING_ANISOTROPIC && CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT + envSpec = EnvAnisotropicReflection(tex, fixedR.xyz, roughness, mipCount, lightingData.anisotropyShape, lightingData.V, lightingData.N, lightingData.T, lightingData.B); + envSpec = mix(envmap, envSpec, fixedR.w); + #else + #if CC_SURFACES_USE_REFLECTION_DENOISE && !CC_IBL_CONVOLUTED + envSpec = EnvReflectionWithMipFiltering(normalize(R), roughness, mipCount, 0.6); + #else + envSpec = mix(envmap, EnvReflection(tex, R, roughness, mipCount), fixedR.w); + #endif + #endif + return envSpec; +} + vec3 CalculateEnvironmentDiffuse(in LightingIntermediateData lightingData, float lightIntensity) { @@ -123,20 +150,31 @@ vec3 CalculateEnvironmentSpecular(in LightingIntermediateData lightingData, floa vec3 envSpec = vec3(0.0); #if CC_USE_REFLECTION_PROBE - #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_CUBE - envSpec = SampleEnvironmentSpecular(cc_reflectionProbeCubemap, lightingData, cc_ambientGround.w); - #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_PLANAR - vec3 R = normalize(CalculateReflectDirection(lightingData.N, lightingData.V, lightingData.NoV)); vec3 worldPos; HIGHP_VALUE_FROM_STRUCT_DEFINED(worldPos, lightingData.worldPosition); - // todo: - // vec4 cc_planarReflectionPlane = vec4(-1.0, 0.0, 0.0, 0.0); - // float cc_planarReflectionDepthScale = 1.0; - // vec3 bumpedWorldPos = CalculatePlanarReflectPositionOnPlane(lightingData.N, lightingData.V, worldPos, cc_planarReflectionPlane, cc_cameraPos.xyz, cc_planarReflectionDepthScale); - vec3 bumpedWorldPos = worldPos; - vec2 screenUV = GetPlanarReflectScreenUV(bumpedWorldPos, cc_matViewProj, cc_cameraPos.w, lightingData.V, R); - vec4 rgbe = fragTextureLod(cc_reflectionProbePlanarMap, screenUV, cc_ambientGround.w); - envSpec = unpackRGBE(rgbe); + + #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_CUBE + if(FSInput_reflectionProbeId < 0.0){ + envSpec = SampleEnvironmentSpecular(cc_reflectionProbeCubemap, lightingData, cc_ambientGround.w); + }else{ + vec3 centerPos, boxHalfSize; + float mipCount; + GetCubeReflectionProbeData(centerPos, boxHalfSize, mipCount, FSInput_reflectionProbeId); + envSpec = SampleEnvironmentSpecular(cc_reflectionProbeCubemap, lightingData, mipCount, worldPos, centerPos, boxHalfSize); + } + #elif CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_PLANAR + vec3 R = normalize(CalculateReflectDirection(lightingData.N, lightingData.V, lightingData.NoV)); + if(FSInput_reflectionProbeId < 0.0){ + vec2 screenUV = GetPlanarReflectScreenUV(worldPos, cc_matViewProj, cc_cameraPos.w, lightingData.V, R); + envSpec = unpackRGBE(fragTextureLod(cc_reflectionProbePlanarMap, screenUV, 1.0)).xyz; + }else{ + vec4 plane; + float planarReflectionDepthScale, mipCount; + GetPlanarReflectionProbeData(plane, planarReflectionDepthScale, mipCount, FSInput_reflectionProbeId); + vec3 worldPosOffset = CalculatePlanarReflectPositionOnPlane(lightingData.N, lightingData.V, worldPos, plane, cc_cameraPos.xyz, planarReflectionDepthScale); + vec2 screenUV = GetPlanarReflectScreenUV(worldPosOffset, cc_matViewProj, cc_cameraPos.w, lightingData.V, R); + envSpec = unpackRGBE(fragTextureLod(cc_reflectionProbePlanarMap, screenUV, mipCount)).xyz; + } #endif #elif CC_USE_IBL envSpec = SampleEnvironmentSpecular(cc_environment, lightingData, cc_ambientGround.w); diff --git a/editor/assets/chunks/shading-entries/data-structures/fs-input.chunk b/editor/assets/chunks/shading-entries/data-structures/fs-input.chunk index 2dc042f93a..72b184a529 100644 --- a/editor/assets/chunks/shading-entries/data-structures/fs-input.chunk +++ b/editor/assets/chunks/shading-entries/data-structures/fs-input.chunk @@ -46,7 +46,10 @@ void CCSurfacesGetFragmentInput(out SurfacesStandardFragmentInput fsInput) #endif #if CC_RECEIVE_SHADOW - #define FSInput_shadowBias v_shadowBias + #define FSInput_shadowBias v_shadowBiasAndProbeId.xy +#endif +#if CC_USE_REFLECTION_PROBE + #define FSInput_reflectionProbeId v_shadowBiasAndProbeId.z #endif #if CC_USE_FOG != CC_FOG_NONE && !CC_USE_ACCURATE_FOG diff --git a/editor/assets/chunks/shading-entries/data-structures/vs-fs.chunk b/editor/assets/chunks/shading-entries/data-structures/vs-fs.chunk index da977f16bd..0f9aadd732 100644 --- a/editor/assets/chunks/shading-entries/data-structures/vs-fs.chunk +++ b/editor/assets/chunks/shading-entries/data-structures/vs-fs.chunk @@ -19,8 +19,8 @@ CC_SURFACES_VARING_MODIFIER vec2 v_uv; CC_SURFACES_VARING_MODIFIER mediump vec3 v_luv; #endif -#if CC_RECEIVE_SHADOW - CC_SURFACES_VARING_MODIFIER mediump vec2 v_shadowBias; +#if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + CC_SURFACES_VARING_MODIFIER mediump vec4 v_shadowBiasAndProbeId; #endif #if CC_USE_FOG != CC_FOG_NONE && !CC_USE_ACCURATE_FOG diff --git a/editor/assets/chunks/shading-entries/data-structures/vs-input.chunk b/editor/assets/chunks/shading-entries/data-structures/vs-input.chunk index 102a0c5bf2..21f540250d 100644 --- a/editor/assets/chunks/shading-entries/data-structures/vs-input.chunk +++ b/editor/assets/chunks/shading-entries/data-structures/vs-input.chunk @@ -38,8 +38,8 @@ layout(location = 2) in vec2 a_texCoord; #if CC_USE_LIGHTMAP in vec4 a_lightingMapUVParam; #endif - #if CC_RECEIVE_SHADOW - in vec2 a_localShadowBias; // x:shadow bias, y:shadow normal bias. + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + in vec4 a_localShadowBiasAndProbeId; // x:shadow bias, y:shadow normal bias, z: reflection probe id, w: reserved for blend reflection probe id #endif #if CC_USE_LIGHT_PROBE in vec4 a_sh_linear_const_r; @@ -48,7 +48,6 @@ layout(location = 2) in vec2 a_texCoord; #endif #endif - #if CC_USE_MORPH #if __VERSION__ < 450 in float a_vertexId; diff --git a/editor/assets/chunks/shading-entries/data-structures/vs-intermediate.chunk b/editor/assets/chunks/shading-entries/data-structures/vs-intermediate.chunk index c2520e3f13..c97b12c37d 100644 --- a/editor/assets/chunks/shading-entries/data-structures/vs-intermediate.chunk +++ b/editor/assets/chunks/shading-entries/data-structures/vs-intermediate.chunk @@ -26,8 +26,8 @@ struct SurfacesStandardVertexIntermediate #endif //transfered -#if CC_RECEIVE_SHADOW - vec2 shadowBias; +#if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + vec4 shadowBiasAndProbeId; #endif #if CC_USE_FOG != CC_FOG_NONE && !CC_USE_ACCURATE_FOG diff --git a/editor/assets/chunks/shading-entries/data-structures/vs-output.chunk b/editor/assets/chunks/shading-entries/data-structures/vs-output.chunk index f18c6573f7..ea9593b4da 100644 --- a/editor/assets/chunks/shading-entries/data-structures/vs-output.chunk +++ b/editor/assets/chunks/shading-entries/data-structures/vs-output.chunk @@ -31,7 +31,10 @@ #endif #if CC_RECEIVE_SHADOW - #define VSOutput_shadowBias v_shadowBias + #define VSOutput_shadowBias v_shadowBiasAndProbeId.xy +#endif +#if CC_USE_REFLECTION_PROBE + #define VSOutput_reflectionProbeId v_shadowBiasAndProbeId.z #endif #if CC_USE_FOG != CC_FOG_NONE && !CC_USE_ACCURATE_FOG diff --git a/editor/assets/chunks/surfaces/includes/common-fs.chunk b/editor/assets/chunks/surfaces/includes/common-fs.chunk index 2104c887ca..e99bf8f895 100644 --- a/editor/assets/chunks/surfaces/includes/common-fs.chunk +++ b/editor/assets/chunks/surfaces/includes/common-fs.chunk @@ -32,8 +32,9 @@ #if CC_SUPPORT_CASCADED_SHADOW_MAP #include #endif + +#include #if CC_USE_IBL - #include #if CC_USE_DIFFUSEMAP #include #endif @@ -61,6 +62,9 @@ #if CC_USE_LIGHT_PROBE #include #endif +#if CC_USE_REFLECTION_PROBE + #include +#endif // Uniform should depend on system macro, not surface macro #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD diff --git a/editor/assets/chunks/surfaces/module-functions/common-vs.chunk b/editor/assets/chunks/surfaces/module-functions/common-vs.chunk index 0fa9152566..21b4bff405 100644 --- a/editor/assets/chunks/surfaces/module-functions/common-vs.chunk +++ b/editor/assets/chunks/surfaces/module-functions/common-vs.chunk @@ -45,7 +45,11 @@ void CCSurfacesVertexOutput(in SurfacesStandardVertexIntermediate In) #endif #if CC_RECEIVE_SHADOW - VSOutput_shadowBias = In.shadowBias; + VSOutput_shadowBias = In.shadowBiasAndProbeId.xy; +#endif + +#if CC_USE_REFLECTION_PROBE + VSOutput_reflectionProbeId = In.shadowBiasAndProbeId.z; #endif #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD @@ -121,12 +125,23 @@ void CCSurfacesVertexTransferFog(inout SurfacesStandardVertexIntermediate In) void CCSurfacesVertexTransferShadow(inout SurfacesStandardVertexIntermediate In) { + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + In.shadowBiasAndProbeId = vec4(0.0); + #endif #if CC_RECEIVE_SHADOW - In.shadowBias = vec2(cc_shadowWHPBInfo.w, cc_shadowLPNNInfo.z); + In.shadowBiasAndProbeId.xy = vec2(cc_shadowWHPBInfo.w, cc_shadowLPNNInfo.z); #if USE_INSTANCING - In.shadowBias += a_localShadowBias.xy; + In.shadowBiasAndProbeId.xy += a_localShadowBiasAndProbeId.xy; #else - In.shadowBias += cc_localShadowBias.xy; + In.shadowBiasAndProbeId.xy += cc_localShadowBias.xy; + #endif + #endif + + #if CC_USE_REFLECTION_PROBE + #if USE_INSTANCING + In.shadowBiasAndProbeId.zw = a_localShadowBiasAndProbeId.zw; + #else + In.shadowBiasAndProbeId.zw = cc_localShadowBias.zw; #endif #endif } diff --git a/editor/assets/effects/legacy/standard.effect b/editor/assets/effects/legacy/standard.effect index e6b9bf11d5..033c19442d 100644 --- a/editor/assets/effects/legacy/standard.effect +++ b/editor/assets/effects/legacy/standard.effect @@ -107,8 +107,8 @@ CCProgram standard-vs %{ out mediump vec2 v_uv1; #endif - #if CC_RECEIVE_SHADOW - out mediump vec2 v_shadowBias; + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + out mediump vec4 v_shadowBiasAndProbeId; #endif #if USE_NORMAL_MAP @@ -136,7 +136,14 @@ CCProgram standard-vs %{ v_normal = normalize((matWorldIT * vec4(In.normal, 0.0)).xyz); #if CC_RECEIVE_SHADOW - v_shadowBias = CCGetShadowBias(); + v_shadowBiasAndProbeId.xy = CCGetShadowBias(); + #endif + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + #if USE_INSTANCING + v_shadowBiasAndProbeId.zw = a_localShadowBiasAndProbeId.zw; + #else + v_shadowBiasAndProbeId.zw = cc_localShadowBias.zw; + #endif #endif #if USE_TWOSIDE @@ -193,8 +200,8 @@ CCProgram standard-fs %{ #endif in mediump vec3 v_normal; - #if CC_RECEIVE_SHADOW - in mediump vec2 v_shadowBias; + #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE + in mediump vec4 v_shadowBiasAndProbeId; #endif #if USE_VERTEX_COLOR @@ -255,8 +262,12 @@ CCProgram standard-fs %{ s.normal = v_normal; #if CC_RECEIVE_SHADOW - s.shadowBias = v_shadowBias; + s.shadowBias = v_shadowBiasAndProbeId.xy; #endif + #if CC_USE_REFLECTION_PROBE + s.reflectionProbeId = v_shadowBiasAndProbeId.z; + #endif + #if USE_NORMAL_MAP vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5); diff --git a/native/cocos/renderer/gfx-base/GFXBuffer.cpp b/native/cocos/renderer/gfx-base/GFXBuffer.cpp index cb54118d19..4a6d3c13a3 100644 --- a/native/cocos/renderer/gfx-base/GFXBuffer.cpp +++ b/native/cocos/renderer/gfx-base/GFXBuffer.cpp @@ -84,7 +84,7 @@ void Buffer::resize(uint32_t size) { void Buffer::write(const uint8_t *value, uint32_t offset, uint32_t size) const { CC_ASSERT(hasFlag(_flags, BufferFlagBit::ENABLE_STAGING_WRITE)); uint8_t *dst = getStagingAddress(); - if (dst == nullptr || offset + size >= _size) { + if (dst == nullptr || offset + size > _size) { return; } memcpy(dst + offset, value, size); diff --git a/native/cocos/renderer/pipeline/Define.cpp b/native/cocos/renderer/pipeline/Define.cpp index 8b0c81ca14..4e363b5280 100644 --- a/native/cocos/renderer/pipeline/Define.cpp +++ b/native/cocos/renderer/pipeline/Define.cpp @@ -70,6 +70,7 @@ const gfx::UniformBlock UBOGlobal::LAYOUT = { {"cc_time", gfx::Type::FLOAT4, 1}, {"cc_screenSize", gfx::Type::FLOAT4, 1}, {"cc_nativeSize", gfx::Type::FLOAT4, 1}, + {"cc_probeInfo", gfx::Type::FLOAT4, 1}, {"cc_debug_view_mode", gfx::Type::FLOAT4, 1}, {"cc_debug_view_composite_pack_1", gfx::Type::FLOAT4, 1}, @@ -191,7 +192,7 @@ const gfx::DescriptorSetLayoutBinding UBOLocal::DESCRIPTOR = { UBOLocal::BINDING, gfx::DescriptorType::UNIFORM_BUFFER, 1, - gfx::ShaderStageFlagBit::VERTEX | gfx::ShaderStageFlagBit::COMPUTE, + gfx::ShaderStageFlagBit::VERTEX | gfx::ShaderStageFlagBit::FRAGMENT | gfx::ShaderStageFlagBit::COMPUTE, {}, }; const gfx::UniformBlock UBOLocal::LAYOUT = { @@ -203,6 +204,8 @@ const gfx::UniformBlock UBOLocal::LAYOUT = { {"cc_matWorldIT", gfx::Type::MAT4, 1}, {"cc_lightingMapUVParam", gfx::Type::FLOAT4, 1}, {"cc_localShadowBias", gfx::Type::FLOAT4, 1}, + {"cc_reflectionProbeData1", gfx::Type::FLOAT4, 1}, + {"cc_reflectionProbeData2", gfx::Type::FLOAT4, 1}, }, 1, }; @@ -212,7 +215,7 @@ const gfx::DescriptorSetLayoutBinding UBOWorldBound::DESCRIPTOR = { UBOWorldBound::BINDING, gfx::DescriptorType::UNIFORM_BUFFER, 1, - gfx::ShaderStageFlagBit::VERTEX | gfx::ShaderStageFlagBit::COMPUTE, + gfx::ShaderStageFlagBit::VERTEX | gfx::ShaderStageFlagBit::FRAGMENT | gfx::ShaderStageFlagBit::COMPUTE, {}, }; const gfx::UniformBlock UBOWorldBound::LAYOUT = { @@ -611,6 +614,23 @@ const gfx::UniformSamplerTexture REFLECTIONPROBEPLANARMAP::LAYOUT = { 1, }; +const ccstd::string REFLECTIONPROBEDATAMAP::NAME = "cc_reflectionProbeDataMap"; +const gfx::DescriptorSetLayoutBinding REFLECTIONPROBEDATAMAP::DESCRIPTOR = { + REFLECTIONPROBEDATAMAP::BINDING, + gfx::DescriptorType::SAMPLER_TEXTURE, + 1, + gfx::ShaderStageFlagBit::FRAGMENT, + {}, +}; +const gfx::UniformSamplerTexture REFLECTIONPROBEDATAMAP::LAYOUT = { + localSet, + REFLECTIONPROBEDATAMAP::BINDING, + REFLECTIONPROBEDATAMAP::NAME, + gfx::Type::SAMPLER2D, + 1, +}; + + uint32_t skyboxFlag = static_cast(gfx::ClearFlagBit::STENCIL) << 1; uint32_t nextPow2(uint32_t val) { diff --git a/native/cocos/renderer/pipeline/Define.h b/native/cocos/renderer/pipeline/Define.h index 87d905eb23..ffea23d764 100644 --- a/native/cocos/renderer/pipeline/Define.h +++ b/native/cocos/renderer/pipeline/Define.h @@ -261,6 +261,7 @@ enum class CC_DLL ModelLocalBindings { SAMPLER_REFLECTION_PROBE_CUBE, SAMPLER_REFLECTION_PROBE_PLANAR, + SAMPLER_REFLECTION_PROBE_DATA_MAP, COUNT, }; @@ -296,7 +297,9 @@ struct CC_DLL UBOLocal { static constexpr uint32_t MAT_WORLD_IT_OFFSET = UBOLocal::MAT_WORLD_OFFSET + 16; static constexpr uint32_t LIGHTINGMAP_UVPARAM = UBOLocal::MAT_WORLD_IT_OFFSET + 16; static constexpr uint32_t LOCAL_SHADOW_BIAS = UBOLocal::LIGHTINGMAP_UVPARAM + 4; - static constexpr uint32_t COUNT = UBOLocal::LOCAL_SHADOW_BIAS + 4; + static constexpr uint32_t REFLECTION_PROBE_DATA1 = UBOLocal::LOCAL_SHADOW_BIAS + 4; + static constexpr uint32_t REFLECTION_PROBE_DATA2 = UBOLocal::REFLECTION_PROBE_DATA1 + 4; + static constexpr uint32_t COUNT = UBOLocal::REFLECTION_PROBE_DATA2 + 4; static constexpr uint32_t SIZE = UBOLocal::COUNT * 4; static constexpr uint32_t BINDING = static_cast(ModelLocalBindings::UBO_LOCAL); static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; @@ -442,8 +445,9 @@ struct CC_DLL UBOGlobal { static constexpr uint32_t TIME_OFFSET = 0; static constexpr uint32_t SCREEN_SIZE_OFFSET = UBOGlobal::TIME_OFFSET + 4; static constexpr uint32_t NATIVE_SIZE_OFFSET = UBOGlobal::SCREEN_SIZE_OFFSET + 4; + static constexpr uint32_t PROBE_INFO_OFFSET = UBOGlobal::NATIVE_SIZE_OFFSET + 4; - static constexpr uint32_t DEBUG_VIEW_MODE_OFFSET = UBOGlobal::NATIVE_SIZE_OFFSET + 4; + static constexpr uint32_t DEBUG_VIEW_MODE_OFFSET = UBOGlobal::PROBE_INFO_OFFSET + 4; static constexpr uint32_t DEBUG_VIEW_COMPOSITE_PACK_1_OFFSET = UBOGlobal::DEBUG_VIEW_MODE_OFFSET + 4; static constexpr uint32_t DEBUG_VIEW_COMPOSITE_PACK_2_OFFSET = UBOGlobal::DEBUG_VIEW_COMPOSITE_PACK_1_OFFSET + 4; static constexpr uint32_t DEBUG_VIEW_COMPOSITE_PACK_3_OFFSET = UBOGlobal::DEBUG_VIEW_COMPOSITE_PACK_2_OFFSET + 4; @@ -664,6 +668,13 @@ struct CC_DLL REFLECTIONPROBEPLANARMAP { static const ccstd::string NAME; }; +struct CC_DLL REFLECTIONPROBEDATAMAP { + static constexpr uint32_t BINDING = static_cast(ModelLocalBindings::SAMPLER_REFLECTION_PROBE_DATA_MAP); + static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; + static const gfx::UniformSamplerTexture LAYOUT; + static const ccstd::string NAME; +}; + static constexpr uint32_t CLUSTER_LIGHT_BINDING = 4; static constexpr uint32_t CLUSTER_LIGHT_INDEX_BINDING = 5; static constexpr uint32_t CLUSTER_LIGHT_GRID_BINDING = 6; diff --git a/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp index 4071c07fe3..9825385afd 100644 --- a/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp +++ b/native/cocos/renderer/pipeline/GlobalDescriptorSetManager.cpp @@ -235,6 +235,8 @@ void GlobalDSManager::setDescriptorSetLayout() { localDescriptorSetLayout.bindings[REFLECTIONPROBECUBEMAP::BINDING] = REFLECTIONPROBECUBEMAP::DESCRIPTOR; localDescriptorSetLayout.samplers[REFLECTIONPROBEPLANARMAP::NAME] = REFLECTIONPROBEPLANARMAP::LAYOUT; localDescriptorSetLayout.bindings[REFLECTIONPROBEPLANARMAP::BINDING] = REFLECTIONPROBEPLANARMAP::DESCRIPTOR; + localDescriptorSetLayout.samplers[REFLECTIONPROBEDATAMAP::NAME] = REFLECTIONPROBEDATAMAP::LAYOUT; + localDescriptorSetLayout.bindings[REFLECTIONPROBEDATAMAP::BINDING] = REFLECTIONPROBEDATAMAP::DESCRIPTOR; } } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index 12d18a2f95..be3d7dd8c0 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -38,6 +38,7 @@ #include "scene/Shadow.h" #include "scene/Skybox.h" #include "scene/SpotLight.h" +#include "scene/ReflectionProbeManager.h" namespace cc { @@ -78,6 +79,8 @@ void PipelineUBO::updateGlobalUBOView(const scene::Camera *camera, ccstd::array< uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET + 2] = 1.0F / uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET]; uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET + 3] = 1.0F / uboGlobalView[UBOGlobal::NATIVE_SIZE_OFFSET + 1]; + uboGlobalView[UBOGlobal::PROBE_INFO_OFFSET + 0] = scene::ReflectionProbeManager::getInstance()->getMaxProbeId() + 1; + auto *debugView = root->getDebugView(); uboGlobalView[UBOGlobal::DEBUG_VIEW_MODE_OFFSET] = static_cast(debugView->getSingleMode()); uboGlobalView[UBOGlobal::DEBUG_VIEW_MODE_OFFSET + 1] = debugView->isLightingWithAlbedo() ? 1.0F : 0.0F; diff --git a/native/cocos/scene/Model.cpp b/native/cocos/scene/Model.cpp index 816d0db8e9..0b5a62d0b8 100644 --- a/native/cocos/scene/Model.cpp +++ b/native/cocos/scene/Model.cpp @@ -40,6 +40,8 @@ #include "scene/Pass.h" #include "scene/RenderScene.h" #include "scene/SubModel.h" +#include "scene/ReflectionProbeManager.h" +#include "scene/ReflectionProbe.h" namespace { const cc::gfx::SamplerInfo LIGHTMAP_SAMPLER_HASH{ @@ -203,6 +205,21 @@ void Model::updateUBOs(uint32_t stamp) { _localBuffer->write(mat4, sizeof(float) * pipeline::UBOLocal::MAT_WORLD_IT_OFFSET); _localBuffer->write(_lightmapUVParam, sizeof(float) * pipeline::UBOLocal::LIGHTINGMAP_UVPARAM); _localBuffer->write(_shadowBias, sizeof(float) * (pipeline::UBOLocal::LOCAL_SHADOW_BIAS)); + + auto * probe = scene::ReflectionProbeManager::getInstance()->getReflectionProbeById(_reflectionProbeId); + if (probe) { + if (probe->getProbeType() == scene::ReflectionProbe::ProbeType::PLANAR) { + const Vec4 plane = {probe->getNode()->getUp().x, probe->getNode()->getUp().y, probe->getNode()->getUp().z, 1.F}; + _localBuffer->write(plane, sizeof(float) * (pipeline::UBOLocal::REFLECTION_PROBE_DATA1)); + const Vec4 depthScale = {1.F, 0.F, 0.F, 1.F}; + _localBuffer->write(depthScale, sizeof(float) * (pipeline::UBOLocal::REFLECTION_PROBE_DATA2)); + } else { + const Vec4 pos = {probe->getNode()->getWorldPosition().x, probe->getNode()->getWorldPosition().y, probe->getNode()->getWorldPosition().z, 0.F}; + _localBuffer->write(pos, sizeof(float) * (pipeline::UBOLocal::REFLECTION_PROBE_DATA1)); + const Vec4 boxSize = {probe->getBoudingSize().x, probe->getBoudingSize().y, probe->getBoudingSize().z, static_cast(probe->getCubeMap() ? probe->getCubeMap()->mipmapLevel() : 1)}; + _localBuffer->write(boxSize, sizeof(float) * (pipeline::UBOLocal::REFLECTION_PROBE_DATA2)); + } + } _localBuffer->update(); const bool enableOcclusionQuery = Root::getInstance()->getPipeline()->isOcclusionQueryEnabled(); @@ -616,6 +633,27 @@ void Model::updateReflectionProbePlanarMap(gfx::Texture *texture) { } } +void Model::updateReflectionProbeDataMap(Texture2D *texture) { + _localDataUpdated = true; + + if (!texture) { + texture = BuiltinResMgr::getInstance()->get(ccstd::string("empty-texture")); + } + gfx::Texture *gfxTexture = texture->getGFXTexture(); + if (gfxTexture) { + for (SubModel *subModel : _subModels) { + gfx::DescriptorSet *descriptorSet = subModel->getDescriptorSet(); + descriptorSet->bindTexture(pipeline::REFLECTIONPROBEDATAMAP::BINDING, gfxTexture); + descriptorSet->bindSampler(pipeline::REFLECTIONPROBEDATAMAP::BINDING, texture->getGFXSampler()); + descriptorSet->update(); + } + } +} + +void Model::updateReflectionProbeId() { + _localDataUpdated = true; +} + void Model::setInstancedAttribute(const ccstd::string &name, const float *value, uint32_t byteLength) { for (const auto &subModel : _subModels) { subModel->setInstancedAttribute(name, value, byteLength); diff --git a/native/cocos/scene/Model.h b/native/cocos/scene/Model.h index 0b885785b3..de8c7ebb0d 100644 --- a/native/cocos/scene/Model.h +++ b/native/cocos/scene/Model.h @@ -115,6 +115,8 @@ public: void updateLocalShadowBias(); void updateReflectionProbeCubemap(TextureCube *texture); void updateReflectionProbePlanarMap(gfx::Texture *texture); + void updateReflectionProbeId(); + void updateReflectionProbeDataMap(Texture2D *texture); inline void attachToScene(RenderScene *scene) { _scene = scene; @@ -161,6 +163,11 @@ public: } inline int32_t getReflectionProbeType() const { return _reflectionProbeType; } void setReflectionProbeType(int32_t val); + inline int32_t getReflectionProbeId() const { return _reflectionProbeId; } + inline void setReflectionProbeId(int32_t reflectionProbeId) { + _reflectionProbeId = reflectionProbeId; + _shadowBias.z = reflectionProbeId; + } inline int32_t getTetrahedronIndex() const { return _tetrahedronIndex; } inline void setTetrahedronIndex(int32_t index) { _tetrahedronIndex = index; } inline bool showTetrahedron() const { return isLightProbeAvailable(); } @@ -219,6 +226,7 @@ protected: uint32_t _descriptorSetCount{1}; uint32_t _priority{0}; uint32_t _updateStamp{0}; + int32_t _reflectionProbeId{-1}; OctreeNode *_octreeNode{nullptr}; RenderScene *_scene{nullptr}; diff --git a/native/cocos/scene/ReflectionProbeManager.cpp b/native/cocos/scene/ReflectionProbeManager.cpp index 3cc4ebadc5..22e8d3682e 100644 --- a/native/cocos/scene/ReflectionProbeManager.cpp +++ b/native/cocos/scene/ReflectionProbeManager.cpp @@ -49,5 +49,28 @@ void ReflectionProbeManager::unRegisterProbe(scene::ReflectionProbe* probe) { } } +ReflectionProbe* ReflectionProbeManager::getReflectionProbeById(int32_t probeId) { + for (const auto& probe : _probes) { + if (probe->getProbeId() == probeId) { + return probe; + } + } + return nullptr; +} + +int32_t ReflectionProbeManager::getMaxProbeId() { + if (_probes.empty()) { + return -1; + } + if (_probes.size() == 1) { + return _probes[0]->getProbeId(); + } + + std::sort(_probes.begin(), _probes.end(), [](const ReflectionProbe* p1, const ReflectionProbe* p2) { + return p1->getProbeId() < p2->getProbeId(); + }); + return _probes[_probes.size() - 1]->getProbeId(); +} + } // namespace scene } // namespace cc diff --git a/native/cocos/scene/ReflectionProbeManager.h b/native/cocos/scene/ReflectionProbeManager.h index e6827b9e8c..88c8ba080b 100644 --- a/native/cocos/scene/ReflectionProbeManager.h +++ b/native/cocos/scene/ReflectionProbeManager.h @@ -39,8 +39,9 @@ public: void registerProbe(scene::ReflectionProbe* probe); void unRegisterProbe(scene::ReflectionProbe* probe); const ccstd::vector& getAllProbes() const { return _probes; } - -private: + ReflectionProbe* getReflectionProbeById(int32_t probeId); + int32_t getMaxProbeId(); +private: ccstd::vector _probes; }; diff --git a/native/tools/swig-config/scene.i b/native/tools/swig-config/scene.i index 05f14b1085..4e2cff567c 100644 --- a/native/tools/swig-config/scene.i +++ b/native/tools/swig-config/scene.i @@ -455,6 +455,7 @@ using namespace cc; %attribute(cc::scene::Model, bool, bakeToReflectionProbe, getBakeToReflectionProbe, setBakeToReflectionProbe); %attribute(cc::scene::Model, uint32_t, reflectionProbeType, getReflectionProbeType, setReflectionProbeType); %attribute(cc::scene::Model, bool, receiveDirLight, isReceiveDirLight, setReceiveDirLight); +%attribute(cc::scene::Model, int32_t, reflectionProbeId, getReflectionProbeId, setReflectionProbeId); %attribute(cc::scene::SubModel, std::shared_ptr>> &, passes, getPasses, setPasses); %attribute(cc::scene::SubModel, ccstd::vector> &, shaders, getShaders, setShaders);