- 修 void* 指针算术警告(Texture::InitFromDds) - Material:slot2 语义切为 metallicRoughness(glTF 约定 G=粗糙度 B=金属度)+ 新增材质因子参数 UBO(binding 4,per-slot ring, 创建时写死,metallic/roughness 因子乘 MR 贴图) - simple_shader.frag:Blinn-Phong → Cook-Torrance(GGX 分布 + Smith 几何 + Schlick Fresnel,k=(r+1)²/8 解析光源版);金属不参与 漫反射;环境光暂为常数(IBL 后续) - Model:读 aiTextureType_UNKNOWN(glTF MR 落点)+ AI_MATKEY_ METALLIC/ROUGHNESS_FACTOR(旧资产缺键退化为电介质 0/0.5); 材质缓存键含因子 - main.cpp:演示场景换回 backpack(旧资产走因子路径验证 PBR 全链路) - 旧 specular 贴图不再使用(语义被 MR 取代)
59 lines
2.0 KiB
C++
59 lines
2.0 KiB
C++
//
|
|
// Created by atdunbg on 2026/9/21.
|
|
//
|
|
|
|
#include <Engine/Renderer/Material.h>
|
|
#include <Engine/Vulkan/Device.h>
|
|
#include <Engine/Vulkan/MemoryAllocator.h>
|
|
#include <Engine/Vulkan/DescriptorManager.h>
|
|
#include <Engine/Vulkan/Texture.h>
|
|
#include <Engine/Vulkan/UniformBuffer.h>
|
|
#include <Engine/Core/FrameContext.h>
|
|
#include <Engine/Core/Log.h>
|
|
|
|
namespace Engine
|
|
{
|
|
Material::Material(Device& device, MemoryAllocator& allocator,
|
|
DescriptorManager& descriptorManager,
|
|
vk::DescriptorSetLayout layout,
|
|
const std::array<Texture*, kSlotCount>& textures,
|
|
const MaterialParams& params)
|
|
: m_Textures(textures)
|
|
{
|
|
m_DescriptorSet = descriptorManager.AllocateDescriptorSet(layout);
|
|
|
|
for (u32 slot = 0; slot < kSlotCount; ++slot) {
|
|
descriptorManager.UpdateTexture(
|
|
m_DescriptorSet, slot,
|
|
m_Textures[slot]->GetSampler(),
|
|
m_Textures[slot]->GetView());
|
|
}
|
|
|
|
// 材质参数:per-slot ring,创建时写死(参数静态,无每帧更新)
|
|
m_ParamsBuffer = CreateScope<UniformBuffer>(
|
|
device, allocator, sizeof(MaterialParams), kMaxFramesInFlight);
|
|
for (u32 slot = 0; slot < kMaxFramesInFlight; ++slot) {
|
|
m_ParamsBuffer->Update(slot, ¶ms, sizeof(params));
|
|
descriptorManager.UpdateUniformBuffer(
|
|
m_DescriptorSet, kParamsBinding,
|
|
m_ParamsBuffer->Get(slot), m_ParamsBuffer->GetSize());
|
|
}
|
|
}
|
|
|
|
|
|
Material::~Material() = default;
|
|
|
|
std::string Material::MakeCacheKey(const std::string paths[kSlotCount],
|
|
const MaterialParams& params)
|
|
{
|
|
std::string key;
|
|
for (u32 slot = 0; slot < kSlotCount; ++slot) {
|
|
key += paths[slot];
|
|
key += '\n';
|
|
}
|
|
key += "f:" + std::to_string(params.factors.x) +
|
|
"," + std::to_string(params.factors.y);
|
|
return key;
|
|
}
|
|
} // namespace Engine
|