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:
@@ -39,7 +39,7 @@ namespace Engine {
|
||||
|
||||
class Application {
|
||||
public:
|
||||
Application(std::string title, u32 width, u32 height);
|
||||
Application(const std::string& title, u32 width, u32 height);
|
||||
virtual ~Application();
|
||||
|
||||
Application(const Application&) = delete;
|
||||
|
||||
@@ -89,10 +89,16 @@ namespace Engine::ECS {
|
||||
bool primary = false;
|
||||
};
|
||||
|
||||
// 方向光
|
||||
// 灯光。类型:
|
||||
// Directional——方向即照射方向(无位置,阴影光源用第 0 盏方向光)
|
||||
// Point——位置取实体 Transform(固有的),向四周发光 + 距离衰减
|
||||
struct LightComponent {
|
||||
glm::vec3 direction{0.0f, -1.0f, 0.0f};
|
||||
glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
enum class Type : u8 { Directional = 0, Point = 1 };
|
||||
|
||||
Type type = Type::Directional;
|
||||
glm::vec3 direction{0.0f, -1.0f, 0.0f}; // 仅方向光
|
||||
glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f}; // rgb = 颜色,a = 强度
|
||||
f32 radius = 10.0f; // 仅点光:衰减半径
|
||||
};
|
||||
|
||||
} // namespace Engine::ECS
|
||||
|
||||
@@ -37,10 +37,13 @@ namespace Engine {
|
||||
constexpr u32 kMaxCascades = 3; // CSM 级联数
|
||||
constexpr u32 kCascadeRes = 1024; // 每级联阴影图边长
|
||||
|
||||
// 镜像 shader 侧 UBO 布局的灯光项
|
||||
// 镜像 shader 侧 UBO 布局的灯光项(std140,每灯 64B)。
|
||||
// 字段未用部分置零——shader 按类型分支读取
|
||||
struct LightData {
|
||||
glm::vec4 direction; // xyz = 方向(已归一),w 未用
|
||||
glm::vec4 color; // rgb = 颜色,a = 强度
|
||||
glm::vec4 color; // rgb = 颜色,a = 强度
|
||||
glm::vec4 direction; // 方向光:xyz = 方向(已归一);点光未用
|
||||
glm::vec4 position; // 点光:xyz = 世界位置(来自实体 Transform)
|
||||
glm::vec4 params; // x = 类型(0=方向,1=点),y = 衰减半径,z/w 未用
|
||||
};
|
||||
|
||||
// 镜像 simple_shader 的 UBO 布局(std140 语义由 shader 侧对齐)。
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Engine {
|
||||
vk::DeviceSize GetSize() const { return m_Size; }
|
||||
|
||||
// 只有 HOST_VISIBLE 的 Buffer 能调
|
||||
void Upload(const void* data, vk::DeviceSize size, vk::DeviceSize offset = 0);
|
||||
void Upload(const void* data, vk::DeviceSize size, vk::DeviceSize offset = 0) const;
|
||||
|
||||
private:
|
||||
Device& m_Device;
|
||||
|
||||
@@ -54,15 +54,15 @@ namespace Engine {
|
||||
|
||||
// 创建 + 分配 + 绑定一步完成(VMA 惯用法)
|
||||
Allocation CreateBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage,
|
||||
vk::MemoryPropertyFlags props);
|
||||
vk::MemoryPropertyFlags props) const;
|
||||
Allocation CreateImage(const vk::ImageCreateInfo& imageInfo,
|
||||
vk::MemoryPropertyFlags props);
|
||||
vk::MemoryPropertyFlags props) const;
|
||||
|
||||
// 销毁资源并释放分配(按 alloc.Buffer/Image 判断类型),随后 alloc 归零
|
||||
void Destroy(Allocation& alloc);
|
||||
void Destroy(Allocation& alloc) const;
|
||||
|
||||
// HOST_VISIBLE 非 coherent 写入后冲刷(VMA 内部对 coherent 自动跳过)
|
||||
void Flush(const Allocation& alloc);
|
||||
void Flush(const Allocation& alloc) const;
|
||||
|
||||
void PrintStats() const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user