feat: 多光源——点光源类型(距离衰减,位置来自 Transform)
- LightComponent 加 Type(Directional/Point)+ radius;点光位置取 实体 Transform(固有组件首次实际受益) - LightData shader 契约扩为 4×vec4(color/direction/position/params, 每灯 64B,未用字段置零);UBO 688→816B - 光照循环按类型分支:方向光无衰减;点光 L 指向光源 + 平方距离衰减, 衰减半径外跳过;CSM 阴影只来自第 0 盏方向光 - main.cpp:SunTemple 场景加暖橙火坑光 + 冷蓝填充光(点光); KeyLight/FillLight 改 designated initializer
This commit is contained in:
@@ -168,24 +168,39 @@ namespace Engine
|
||||
|
||||
ubo.ambientColor = scene.GetSettings().ambientColor;
|
||||
|
||||
// ---- 2. 灯光(第一个灯作为阴影光源)----
|
||||
// ---- 2. 灯光(第一个方向光作为 CSM 阴影光源;点光位置来自 Transform)----
|
||||
u32 lightCount = 0;
|
||||
glm::vec3 shadowDir{0.0f, -1.0f, 0.0f}; // 无灯兜底:头顶正射
|
||||
scene.Each<LightComponent>([&](Entity, LightComponent& light) {
|
||||
scene.Each<TransformComponent, LightComponent>(
|
||||
[&](Entity, TransformComponent& t, LightComponent& light) {
|
||||
if (lightCount >= kMaxLights) return;
|
||||
|
||||
glm::vec3 dir = glm::length(light.direction) > 0.0f
|
||||
? glm::normalize(light.direction)
|
||||
: glm::vec3(0.0f);
|
||||
ubo.lights[lightCount].direction = glm::vec4(dir, 0.0f);
|
||||
ubo.lights[lightCount].color = light.color;
|
||||
auto& dst = ubo.lights[lightCount];
|
||||
dst.color = light.color;
|
||||
dst.params = glm::vec4(
|
||||
light.type == LightComponent::Type::Point ? 1.0f : 0.0f,
|
||||
light.radius, 0.0f, 0.0f);
|
||||
|
||||
if (lightCount == 0) shadowDir = dir;
|
||||
if (light.type == LightComponent::Type::Point) {
|
||||
dst.position = glm::vec4(t.GetPosition(), 1.0f);
|
||||
dst.direction = glm::vec4(0.0f);
|
||||
} else {
|
||||
glm::vec3 dir = glm::length(light.direction) > 0.0f
|
||||
? glm::normalize(light.direction) : glm::vec3(0.0f);
|
||||
dst.direction = glm::vec4(dir, 0.0f);
|
||||
dst.position = glm::vec4(0.0f);
|
||||
}
|
||||
|
||||
// CSM 阴影光源 = 第一个方向光(点光不做级联阴影)
|
||||
if (lightCount == 0 &&
|
||||
light.type == LightComponent::Type::Directional) {
|
||||
shadowDir = glm::length(light.direction) > 0.0f
|
||||
? glm::normalize(light.direction) : shadowDir;
|
||||
}
|
||||
++lightCount;
|
||||
});
|
||||
for (u32 i = lightCount; i < kMaxLights; ++i) {
|
||||
ubo.lights[i].direction = glm::vec4(0.0f);
|
||||
ubo.lights[i].color = glm::vec4(0.0f);
|
||||
ubo.lights[i] = {};
|
||||
}
|
||||
ComputeCascades(shadowDir);
|
||||
ubo.cameraPos = glm::vec4(camTransform->GetPosition(), 1.0f);
|
||||
|
||||
Reference in New Issue
Block a user