diff --git a/cocos/3d/lights/directional-light-component.ts b/cocos/3d/lights/directional-light-component.ts index 530e44e069..1a036d5a26 100644 --- a/cocos/3d/lights/directional-light-component.ts +++ b/cocos/3d/lights/directional-light-component.ts @@ -109,6 +109,19 @@ export class DirectionalLight extends Light { } } + /** + * @en Turn off the function for directional light. + * @zh 方向光关闭该功能。 + * @engineInternal + */ + @visible(false) + set visibility (vis: number) { + this._visibility = vis; + } + get visibility (): number { + return this._visibility; + } + /** * @en Whether activate real time shadow. * @zh 是否启用实时阴影? diff --git a/cocos/3d/lights/light-component.ts b/cocos/3d/lights/light-component.ts index aaae2045df..4aa5c63117 100644 --- a/cocos/3d/lights/light-component.ts +++ b/cocos/3d/lights/light-component.ts @@ -28,6 +28,9 @@ import { Component } from '../../scene-graph/component'; import { Color, Vec3, Enum, cclegacy } from '../../core'; import { scene } from '../../render-scene'; import { Root } from '../../root'; +import { legacyCC } from '../../core/global-exports'; +import { CAMERA_DEFAULT_MASK } from '../../rendering/define'; +import { Layers } from '../../scene-graph/layers'; const _color_tmp = new Vec3(); @@ -136,6 +139,8 @@ export class Light extends Component { protected _colorTemperature = 6550; @serializable protected _staticSettings: StaticLightSettings = new StaticLightSettings(); + @serializable + protected _visibility = CAMERA_DEFAULT_MASK; protected _type = scene.LightType.UNKNOWN; protected _lightType: typeof scene.Light; @@ -231,6 +236,21 @@ export class Light extends Component { } } + /** + * @en Visibility mask of the light, declaring a set of node layers that will be visible to this light(Does not work with directional light). + * @zh 光照的可见性掩码,声明在当前光照中可见的节点层级集合(对方向光不生效)。 + */ + @tooltip('i18n:lights.visibility') + @displayOrder(255) + @type(Layers.BitMask) + set visibility (vis: number) { + this._visibility = vis; + if (this._light) { this._light.visibility = vis; } + } + get visibility (): number { + return this._visibility; + } + constructor () { super(); this._lightType = scene.Light; @@ -261,6 +281,7 @@ export class Light extends Component { this.colorTemperature = this._colorTemperature; this._light.node = this.node; this._light.baked = this.baked; + this._light.visibility = this.visibility; } protected _destroyLight () { diff --git a/cocos/render-scene/scene/light.ts b/cocos/render-scene/scene/light.ts index 30b47b96f6..7f8f33dd9b 100644 --- a/cocos/render-scene/scene/light.ts +++ b/cocos/render-scene/scene/light.ts @@ -27,6 +27,7 @@ import { Vec3 } from '../../core/math'; import { TransformBit } from '../../scene-graph/node-enum'; import { RenderScene } from '../core/render-scene'; import { Node } from '../../scene-graph'; +import { CAMERA_DEFAULT_MASK } from '../../rendering/define'; // Color temperature (in Kelvin) to RGB export function ColorTemperatureToRGB (rgb: Vec3, kelvin: number) { @@ -130,6 +131,18 @@ export class Light { return this._colorTempRGB; } + /** + * @en Visibility mask of the light, declaring a set of node layers that will be visible to this light. + * @zh 光照的可见性掩码,声明在当前光照中可见的节点层级集合。 + * @engineInternal + */ + set visibility (vis: number) { + this._visibility = vis; + } + get visibility (): number { + return this._visibility; + } + set node (n) { this._node = n; if (this._node) { @@ -191,6 +204,8 @@ export class Light { protected _type: LightType = LightType.UNKNOWN; + protected _visibility = CAMERA_DEFAULT_MASK; + public initialize () { this.color = new Vec3(1, 1, 1); this.colorTemperature = 6550.0; diff --git a/cocos/rendering/render-additive-light-queue.ts b/cocos/rendering/render-additive-light-queue.ts index b68b817eda..5f2207b829 100644 --- a/cocos/rendering/render-additive-light-queue.ts +++ b/cocos/rendering/render-additive-light-queue.ts @@ -89,24 +89,6 @@ function getLightPassIndices (subModels: SubModel[], lightPassIndices: number[]) return hasValidLightPass; } -function isInstancedOrBatched (model: Model) { - const subModels = model.subModels; - for (let m = 0; m < subModels.length; ++m) { - const passes = subModels[m].passes; - for (let p = 0; p < passes.length; ++p) { - const pass = passes[p]; - const batchingScheme = pass.batchingScheme; - if (batchingScheme === BatchingSchemes.INSTANCING) { - return true; - } - if (batchingScheme === BatchingSchemes.VB_MERGING) { - return true; - } - } - } - return false; -} - /** * @zh 叠加光照队列。 */ @@ -277,15 +259,14 @@ export class RenderAdditiveLightQueue { // light culling protected _lightCulling (model:Model, validPunctualLights: Light[]) { let isCulled = false; - const isNeedCulling = !isInstancedOrBatched(model); for (let l = 0; l < validPunctualLights.length; l++) { const light = validPunctualLights[l]; switch (light.type) { case LightType.SPHERE: - if (isNeedCulling) { isCulled = cullSphereLight(light as SphereLight, model); } + isCulled = cullSphereLight(light as SphereLight, model); break; case LightType.SPOT: - if (isNeedCulling) { isCulled = cullSpotLight(light as SpotLight, model); } + isCulled = cullSpotLight(light as SpotLight, model); break; default: } @@ -299,27 +280,41 @@ export class RenderAdditiveLightQueue { protected _addRenderQueue (pass: Pass, subModel: SubModel, model: Model, lightPassIdx: number) { const validPunctualLights = this._pipeline.pipelineSceneData.validPunctualLights; const { batchingScheme } = pass; - if (batchingScheme === BatchingSchemes.INSTANCING) { // instancing - const buffer = pass.getInstancedBuffer(); - buffer.merge(subModel, lightPassIdx); - buffer.dynamicOffsets[0] = this._lightBufferStride; - this._instancedQueue.queue.add(buffer); - } else if (batchingScheme === BatchingSchemes.VB_MERGING) { // vb-merging - const buffer = pass.getBatchedBuffer(); - buffer.merge(subModel, lightPassIdx, model); - buffer.dynamicOffsets[0] = this._lightBufferStride; - this._batchedQueue.queue.add(buffer); - } else { // standard draw - const lp = _lightPassPool.alloc(); + + let lp: IAdditiveLightPass | null = null; + if (batchingScheme === BatchingSchemes.NONE) { + lp = _lightPassPool.alloc(); lp.subModel = subModel; lp.passIdx = lightPassIdx; - for (let l = 0; l < _lightIndices.length; l++) { - const lightIdx = _lightIndices[l]; - const light = validPunctualLights[lightIdx]; - lp.lights.push(light); - lp.dynamicOffsets.push(this._lightBufferStride * lightIdx); + } + + for (let l = 0; l < _lightIndices.length; l++) { + const lightIdx = _lightIndices[l]; + const light = validPunctualLights[lightIdx]; + const visibility = light.visibility; + if (((visibility & model.node.layer) === model.node.layer)) { + switch (batchingScheme) { + case BatchingSchemes.INSTANCING: { + const buffer = pass.getInstancedBuffer(l); + buffer.merge(subModel, lightPassIdx); + buffer.dynamicOffsets[0] = this._lightBufferStride; + this._instancedQueue.queue.add(buffer); + } break; + case BatchingSchemes.VB_MERGING: { + const buffer = pass.getBatchedBuffer(l); + buffer.merge(subModel, lightPassIdx, model); + buffer.dynamicOffsets[0] = this._lightBufferStride; + this._batchedQueue.queue.add(buffer); + } break; + default: + lp!.lights.push(light); + lp!.dynamicOffsets.push(this._lightBufferStride * lightIdx); + } } - this._lightPasses.push(lp); + } + + if (batchingScheme === BatchingSchemes.NONE) { + this._lightPasses.push(lp!); } } diff --git a/cocos/rendering/render-shadow-map-batched-queue.ts b/cocos/rendering/render-shadow-map-batched-queue.ts index 9974df385d..f844b5c502 100644 --- a/cocos/rendering/render-shadow-map-batched-queue.ts +++ b/cocos/rendering/render-shadow-map-batched-queue.ts @@ -100,12 +100,14 @@ export class RenderShadowMapBatchedQueue { // eslint-disable-next-line no-case-declarations const spotLight = light as SpotLight; if (spotLight.shadowEnabled) { + const visibility = spotLight.visibility; const castShadowObjects = sceneData.csmLayers.castShadowObjects; for (let i = 0; i < castShadowObjects.length; i++) { const ro = castShadowObjects[i]; const model = ro.model; if (model.worldBounds) { - if (!intersect.aabbFrustum(model.worldBounds, spotLight.frustum)) { continue; } + if (((visibility & model.node.layer) !== model.node.layer) + || !intersect.aabbFrustum(model.worldBounds, spotLight.frustum)) { continue; } } this.add(model); diff --git a/editor/i18n/en/localization.js b/editor/i18n/en/localization.js index c6f6deaefc..5883153bbd 100755 --- a/editor/i18n/en/localization.js +++ b/editor/i18n/en/localization.js @@ -153,6 +153,7 @@ module.exports = { illuminance: 'Illuminance of the light', luminous_power: 'Luminous power of the light', luminance: 'Luminance of the light', + visibility: 'Visibility mask, declaring a set of node layers that will be visible to this valid punctual Light(Does not work with directional light)', term: 'The photometric term currently being used', size: 'Size of the light', range: 'Range of the light', diff --git a/editor/i18n/zh/localization.js b/editor/i18n/zh/localization.js index 34e687aeb5..ab6c12b8f8 100755 --- a/editor/i18n/zh/localization.js +++ b/editor/i18n/zh/localization.js @@ -150,6 +150,7 @@ module.exports = { illuminance: '光源强度', luminous_flux: '光通量', luminance: '光亮度', + visibility: '可见性掩码,声明在当前精确光源中可见的节点层级集合(对方向光不生效)', term: '当前使用的光度学计量单位', size: '光源大小', range: '光源范围', diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp index 38a7b319d3..d7b97ea411 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp @@ -196,51 +196,45 @@ bool RenderAdditiveLightQueue::cullSpotLight(const scene::SpotLight *light, cons return model->getWorldBounds() && (!model->getWorldBounds()->aabbAabb(light->getAABB()) || !model->getWorldBounds()->aabbFrustum(light->getFrustum())); } -bool RenderAdditiveLightQueue::isInstancedOrBatched(const scene::Model *model) { - const auto &subModels = model->getSubModels(); - const auto subModelCount = subModels.size(); - for (uint32_t subModelIdx = 0; subModelIdx < subModelCount; ++subModelIdx) { - const auto &subModel = subModels[subModelIdx]; - const auto &passes = subModel->getPasses(); - const auto passCount = passes.size(); - for (uint32_t passIdx = 0; passIdx < passCount; ++passIdx) { - const auto &pass = passes[passIdx]; - if (pass->getBatchingScheme() == scene::BatchingSchemes::INSTANCING) { - return true; - } - if (pass->getBatchingScheme() == scene::BatchingSchemes::VB_MERGING) { - return true; - } - } - } - return false; -} - void RenderAdditiveLightQueue::addRenderQueue(scene::SubModel *subModel, const scene::Model *model, scene::Pass *pass, uint32_t lightPassIdx) { - const auto batchingScheme = pass->getBatchingScheme(); const auto lightCount = _lightIndices.size(); - if (batchingScheme == scene::BatchingSchemes::INSTANCING) { // instancing - auto *buffer = pass->getInstancedBuffer(); - buffer->merge(subModel, lightPassIdx); - buffer->setDynamicOffset(0, _lightBufferStride); - _instancedQueue->add(buffer); - } else if (batchingScheme == scene::BatchingSchemes::VB_MERGING) { // vb-merging - auto *buffer = pass->getBatchedBuffer(); - buffer->merge(subModel, lightPassIdx, model); - buffer->setDynamicOffset(0, _lightBufferStride); - _batchedQueue->add(buffer); - } else { // standard draw - AdditiveLightPass lightPass; + const auto batchingScheme = pass->getBatchingScheme(); + + AdditiveLightPass lightPass; + if (batchingScheme == scene::BatchingSchemes::NONE) { lightPass.subModel = subModel; lightPass.pass = pass; lightPass.shader = subModel->getShader(lightPassIdx); lightPass.dynamicOffsets.resize(lightCount); - for (uint32_t i = 0; i < lightCount; ++i) { - const auto lightIdx = _lightIndices[i]; - const auto *light = _validPunctualLights[lightIdx]; - lightPass.lights.emplace_back(light); - lightPass.dynamicOffsets[i] = _lightBufferStride * lightIdx; + } + + for (uint32_t i = 0; i < lightCount; ++i) { + const auto lightIdx = _lightIndices[i]; + const auto *light = _validPunctualLights[lightIdx]; + const auto visibility = light->getVisibility(); + if ((visibility & model->getNode()->getLayer()) == model->getNode()->getLayer()) { + switch (batchingScheme) { + case scene::BatchingSchemes::INSTANCING: { + auto *buffer = pass->getInstancedBuffer(i); + buffer->merge(subModel, lightPassIdx); + buffer->setDynamicOffset(0, _lightBufferStride); + _instancedQueue->add(buffer); + } break; + case scene::BatchingSchemes::VB_MERGING: { + auto *buffer = pass->getBatchedBuffer(i); + buffer->merge(subModel, lightPassIdx, model); + buffer->setDynamicOffset(0, _lightBufferStride); + _batchedQueue->add(buffer); + } break; + case scene::BatchingSchemes::NONE: { + lightPass.lights.emplace_back(light); + lightPass.dynamicOffsets[i] = _lightBufferStride * lightIdx; + } break; + } } + } + + if (batchingScheme == scene::BatchingSchemes::NONE) { _lightPasses.emplace_back(std::move(lightPass)); } } @@ -443,19 +437,14 @@ bool RenderAdditiveLightQueue::getLightPassIndex(const scene::Model *model, ccst void RenderAdditiveLightQueue::lightCulling(const scene::Model *model) { bool isCulled = false; - const auto isNeedCulling = !isInstancedOrBatched(model); for (size_t i = 0; i < _validPunctualLights.size(); i++) { const auto *const light = _validPunctualLights[i]; switch (light->getType()) { case scene::LightType::SPHERE: - if (isNeedCulling) { - isCulled = cullSphereLight(static_cast(light), model); - } + isCulled = cullSphereLight(static_cast(light), model); break; case scene::LightType::SPOT: - if (isNeedCulling) { - isCulled = cullSpotLight(static_cast(light), model); - } + isCulled = cullSpotLight(static_cast(light), model); break; default: isCulled = false; diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h index d63863dc9e..173f09d9ba 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.h @@ -63,7 +63,6 @@ public: private: static bool cullSphereLight(const scene::SphereLight *light, const scene::Model *model); static bool cullSpotLight(const scene::SpotLight *light, const scene::Model *model); - static bool isInstancedOrBatched(const scene::Model *model); void clear(); void addRenderQueue(scene::SubModel *subModel, const scene::Model *model, scene::Pass *pass, uint32_t lightPassIdx); @@ -72,31 +71,38 @@ private: bool getLightPassIndex(const scene::Model *model, ccstd::vector *lightPassIndices) const; void lightCulling(const scene::Model *model); + uint32_t _lightBufferStride{0}; + uint32_t _lightBufferElementCount{0}; + uint32_t _lightBufferCount{16}; + uint32_t _phaseID{0}; + // weak reference RenderPipeline *_pipeline{nullptr}; - ccstd::vector> _sortedPSOCIArray; - // weak reference - ccstd::vector _validPunctualLights; - ccstd::vector _lightIndices; - ccstd::vector _lightPasses; - AdditiveLightPass _instancedLightPass; - AdditiveLightPass _batchedLightPass; - ccstd::vector _dynamicOffsets; - ccstd::vector _lightBufferData; // manage memory manually RenderInstancedQueue *_instancedQueue{nullptr}; // manage memory manually RenderBatchedQueue *_batchedQueue{nullptr}; + IntrusivePtr _lightBuffer; IntrusivePtr _firstLightBufferView; + float _lightMeterScale{10000.0F}; + + AdditiveLightPass _instancedLightPass; + AdditiveLightPass _batchedLightPass; + + ccstd::vector _dynamicOffsets; + ccstd::vector _lightIndices; + + ccstd::vector _lightBufferData; ccstd::array _shadowUBO{}; - uint32_t _lightBufferStride = 0; - uint32_t _lightBufferElementCount = 0; - uint32_t _lightBufferCount = 16; - float _lightMeterScale = 10000.0F; - uint32_t _phaseID = 0; + // weak reference + ccstd::vector _validPunctualLights; + + ccstd::vector _lightPasses; + + ccstd::vector> _sortedPSOCIArray; }; } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/ShadowMapBatchedQueue.cpp b/native/cocos/renderer/pipeline/ShadowMapBatchedQueue.cpp index 7af8e4f307..55512612db 100644 --- a/native/cocos/renderer/pipeline/ShadowMapBatchedQueue.cpp +++ b/native/cocos/renderer/pipeline/ShadowMapBatchedQueue.cpp @@ -34,7 +34,6 @@ #include "SceneCulling.h" #include "forward/ForwardPipeline.h" #include "gfx-base/GFXCommandBuffer.h" -#include "gfx-base/GFXDescriptorSet.h" #include "gfx-base/GFXDevice.h" #include "scene/Camera.h" #include "scene/DirectionalLight.h" @@ -84,10 +83,12 @@ void ShadowMapBatchedQueue::gatherLightPasses(const scene::Camera *camera, const const auto *spotLight = static_cast(light); const RenderObjectList &castShadowObjects = csmLayers->getCastShadowObjects(); if (spotLight->isShadowEnabled()) { + const auto visibility = spotLight->getVisibility(); geometry::AABB ab; for (const auto &ro : castShadowObjects) { const auto *model = ro.model; - if (!model->isEnabled() || !model->isCastShadow() || !model->getNode()) { + if ((visibility & model->getNode()->getLayer()) != model->getNode()->getLayer() + || !model->isEnabled() || !model->isCastShadow() || !model->getNode()) { continue; } if (model->getWorldBounds()) { @@ -98,15 +99,8 @@ void ShadowMapBatchedQueue::gatherLightPasses(const scene::Camera *camera, const } } } break; - - case scene::LightType::SPHERE: { - } break; - - case scene::LightType::UNKNOWN: { - } break; - - default: { - } break; + default: + break; } _instancedQueue->uploadBuffers(cmdBuffer); diff --git a/native/cocos/scene/Light.h b/native/cocos/scene/Light.h index e8347a9f74..67d251b39b 100644 --- a/native/cocos/scene/Light.h +++ b/native/cocos/scene/Light.h @@ -29,6 +29,7 @@ #include "base/RefCounted.h" #include "base/std/container/string.h" #include "math/Vec3.h" +#include "pipeline/Define.h" namespace cc { class Node; @@ -75,6 +76,9 @@ public: setColorTemperatureRGB(colorTemperatureToRGB(val)); } + inline uint32_t getVisibility() const { return _visibility; } + inline void setVisibility(uint32_t visibility) { _visibility = visibility; } + inline Node *getNode() const { return _node.get(); } void setNode(Node *node); @@ -95,15 +99,22 @@ public: protected: bool _useColorTemperature{false}; bool _baked{false}; - IntrusivePtr _node; - float _colorTemp{6550.F}; + LightType _type{LightType::UNKNOWN}; - ccstd::string _name; + + uint32_t _visibility = pipeline::CAMERA_DEFAULT_MASK; + + IntrusivePtr _node; RenderScene *_scene{nullptr}; + + float _colorTemp{6550.F}; + Vec3 _color{1, 1, 1}; Vec3 _colorTemperatureRGB; Vec3 _forward{0, 0, -1}; + ccstd::string _name; + private: CC_DISALLOW_COPY_MOVE_ASSIGN(Light); }; diff --git a/native/tools/swig-config/scene.i b/native/tools/swig-config/scene.i index 9894d3ac91..ac316a5f37 100644 --- a/native/tools/swig-config/scene.i +++ b/native/tools/swig-config/scene.i @@ -286,6 +286,7 @@ using namespace cc; %attribute(cc::scene::Light, cc::scene::LightType, type, getType, setType); %attribute(cc::scene::Light, ccstd::string&, name, getName, setName); %attribute(cc::scene::Light, cc::scene::RenderScene*, scene, getScene); +%attribute(cc::scene::Light, uint32_t, visibility, getVisibility, setVisibility); %attribute(cc::scene::DirectionalLight, cc::Vec3&, direction, getDirection, setDirection); %attribute(cc::scene::DirectionalLight, float, illuminance, getIlluminance, setIlluminance);