[Feature] add visibility for light component (#13131)

This commit is contained in:
troublemaker52025
2022-10-26 15:26:56 +08:00
committed by GitHub
parent a354281d7e
commit 4bb8dfd702
12 changed files with 163 additions and 114 deletions
@@ -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 是否启用实时阴影?
+21
View File
@@ -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 () {
+15
View File
@@ -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;
+34 -39
View File
@@ -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!);
}
}
@@ -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);
+1
View File
@@ -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',
+1
View File
@@ -150,6 +150,7 @@ module.exports = {
illuminance: '光源强度',
luminous_flux: '光通量',
luminance: '光亮度',
visibility: '可见性掩码,声明在当前精确光源中可见的节点层级集合(对方向光不生效)',
term: '当前使用的光度学计量单位',
size: '光源大小',
range: '光源范围',
@@ -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<const scene::SphereLight *>(light), model);
}
isCulled = cullSphereLight(static_cast<const scene::SphereLight *>(light), model);
break;
case scene::LightType::SPOT:
if (isNeedCulling) {
isCulled = cullSpotLight(static_cast<const scene::SpotLight *>(light), model);
}
isCulled = cullSpotLight(static_cast<const scene::SpotLight *>(light), model);
break;
default:
isCulled = false;
@@ -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<uint32_t> *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<ccstd::vector<uint32_t>> _sortedPSOCIArray;
// weak reference
ccstd::vector<const scene::Light *> _validPunctualLights;
ccstd::vector<uint32_t> _lightIndices;
ccstd::vector<AdditiveLightPass> _lightPasses;
AdditiveLightPass _instancedLightPass;
AdditiveLightPass _batchedLightPass;
ccstd::vector<uint32_t> _dynamicOffsets;
ccstd::vector<float> _lightBufferData;
// manage memory manually
RenderInstancedQueue *_instancedQueue{nullptr};
// manage memory manually
RenderBatchedQueue *_batchedQueue{nullptr};
IntrusivePtr<gfx::Buffer> _lightBuffer;
IntrusivePtr<gfx::Buffer> _firstLightBufferView;
float _lightMeterScale{10000.0F};
AdditiveLightPass _instancedLightPass;
AdditiveLightPass _batchedLightPass;
ccstd::vector<uint32_t> _dynamicOffsets;
ccstd::vector<uint32_t> _lightIndices;
ccstd::vector<float> _lightBufferData;
ccstd::array<float, UBOShadow::COUNT> _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<const scene::Light *> _validPunctualLights;
ccstd::vector<AdditiveLightPass> _lightPasses;
ccstd::vector<ccstd::vector<uint32_t>> _sortedPSOCIArray;
};
} // namespace pipeline
@@ -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<const scene::SpotLight *>(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);
+14 -3
View File
@@ -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> _node;
float _colorTemp{6550.F};
LightType _type{LightType::UNKNOWN};
ccstd::string _name;
uint32_t _visibility = pipeline::CAMERA_DEFAULT_MASK;
IntrusivePtr<Node> _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);
};
+1
View File
@@ -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);