implement pbr

This commit is contained in:
lightling 2022-04-09 21:01:08 -04:00
parent 62a27191aa
commit 0517fe07dd
Signed by: lightling
GPG key ID: 016F11E0AA296B67
4 changed files with 149 additions and 71 deletions

View file

@ -74,14 +74,20 @@ void Game::LoadShadersAndMaterials()
{ {
vertexShader = std::make_shared<SimpleVertexShader>(device, context, GetFullPathTo_Wide(L"VertexShader.cso").c_str()); vertexShader = std::make_shared<SimpleVertexShader>(device, context, GetFullPathTo_Wide(L"VertexShader.cso").c_str());
pixelShader = std::make_shared<SimplePixelShader>(device, context, GetFullPathTo_Wide(L"SimplePixelShader.cso").c_str()); pixelShader = std::make_shared<SimplePixelShader>(device, context, GetFullPathTo_Wide(L"SimplePixelShader.cso").c_str());
vertexShaderPBR = std::make_shared<SimpleVertexShader>(device, context, GetFullPathTo_Wide(L"SimpleVertexPBR.cso").c_str());
pixelShaderPBR = std::make_shared<SimplePixelShader>(device, context, GetFullPathTo_Wide(L"SimplePixelPBR.cso").c_str());
XMFLOAT3 white = XMFLOAT3(1.0f, 1.0f, 1.0f); XMFLOAT3 white = XMFLOAT3(1.0f, 1.0f, 1.0f);
materials = { materials = {
std::make_shared<Material>(white, 0, vertexShader, pixelShader), std::make_shared<Material>(false, white, 0, vertexShader, pixelShader),
std::make_shared<Material>(white, 0, vertexShader, pixelShader), std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(white, 0, vertexShader, pixelShader), std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(white, 0, vertexShader, pixelShader), std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
std::make_shared<Material>(true, white, 0, vertexShaderPBR, pixelShaderPBR),
}; };
} }
@ -118,25 +124,46 @@ void Game::LoadTextures()
materials[0]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-deepfloor_emissive.png", TEXTYPE_EMISSIVE, device.Get(), context.Get()); materials[0]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-deepfloor_emissive.png", TEXTYPE_EMISSIVE, device.Get(), context.Get());
materials[1]->PushSampler("BasicSampler", sampler); materials[1]->PushSampler("BasicSampler", sampler);
materials[1]->PushTexture(TEXTYPE_REFLECTION, demoCubemap); materials[1]->LoadTexture(L"Assets/Textures/PBR/bronze_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[1]->hasReflectionMap = true; materials[1]->LoadTexture(L"Assets/Textures/PBR/bronze_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[1]->LoadTexture(L"Assets/Textures/WithNormals/cobblestone.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); materials[1]->LoadTexture(L"Assets/Textures/PBR/bronze_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[1]->LoadTexture(L"Assets/Textures/WithNormals/cobblestone_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); materials[1]->LoadTexture(L"Assets/Textures/PBR/bronze_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[1]->LoadTexture(L"Assets/Textures/WithNormals/cobblestone_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[2]->PushSampler("BasicSampler", sampler); materials[2]->PushSampler("BasicSampler", sampler);
materials[2]->PushTexture(TEXTYPE_REFLECTION, demoCubemap); materials[2]->LoadTexture(L"Assets/Textures/PBR/cobblestone_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[2]->hasReflectionMap = true; materials[2]->LoadTexture(L"Assets/Textures/PBR/cobblestone_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[2]->LoadTexture(L"Assets/Textures/WithNormals/rock.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); materials[2]->LoadTexture(L"Assets/Textures/PBR/cobblestone_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[2]->LoadTexture(L"Assets/Textures/WithNormals/rock_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); materials[2]->LoadTexture(L"Assets/Textures/PBR/cobblestone_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[2]->LoadTexture(L"Assets/Textures/WithNormals/rock_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[3]->PushSampler("BasicSampler", sampler); materials[3]->PushSampler("BasicSampler", sampler);
materials[3]->PushTexture(TEXTYPE_REFLECTION, demoCubemap); materials[3]->LoadTexture(L"Assets/Textures/PBR/floor_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[3]->hasReflectionMap = true; materials[3]->LoadTexture(L"Assets/Textures/PBR/floor_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[3]->LoadTexture(L"Assets/Textures/WithNormals/cushion.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); materials[3]->LoadTexture(L"Assets/Textures/PBR/floor_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[3]->LoadTexture(L"Assets/Textures/WithNormals/cushion_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); materials[3]->LoadTexture(L"Assets/Textures/PBR/floor_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[3]->LoadTexture(L"Assets/Textures/WithNormals/cushion_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[4]->PushSampler("BasicSampler", sampler);
materials[4]->LoadTexture(L"Assets/Textures/PBR/paint_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[4]->LoadTexture(L"Assets/Textures/PBR/paint_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[4]->LoadTexture(L"Assets/Textures/PBR/paint_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[4]->LoadTexture(L"Assets/Textures/PBR/paint_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[5]->PushSampler("BasicSampler", sampler);
materials[5]->LoadTexture(L"Assets/Textures/PBR/rough_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[5]->LoadTexture(L"Assets/Textures/PBR/rough_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[5]->LoadTexture(L"Assets/Textures/PBR/rough_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[5]->LoadTexture(L"Assets/Textures/PBR/rough_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[6]->PushSampler("BasicSampler", sampler);
materials[6]->LoadTexture(L"Assets/Textures/PBR/scratched_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[6]->LoadTexture(L"Assets/Textures/PBR/scratched_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[6]->LoadTexture(L"Assets/Textures/PBR/scratched_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[6]->LoadTexture(L"Assets/Textures/PBR/scratched_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
materials[7]->PushSampler("BasicSampler", sampler);
materials[7]->LoadTexture(L"Assets/Textures/PBR/wood_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get());
materials[7]->LoadTexture(L"Assets/Textures/PBR/wood_metal.png", TEXTYPE_METALNESS, device.Get(), context.Get());
materials[7]->LoadTexture(L"Assets/Textures/PBR/wood_roughness.png", TEXTYPE_ROUGHNESS, device.Get(), context.Get());
materials[7]->LoadTexture(L"Assets/Textures/PBR/wood_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get());
} }
// -------------------------------------------------------- // --------------------------------------------------------
@ -147,13 +174,13 @@ void Game::LoadLighting()
ambient = XMFLOAT3(0.1f, 0.1f, 0.15f); ambient = XMFLOAT3(0.1f, 0.1f, 0.15f);
lights = { lights = {
Light::Directional(XMFLOAT3(1, 0.5f, 0.5f), XMFLOAT3(1, 1, 1), 0.75f), Light::Directional(XMFLOAT3(1, 0.5f, 0.5f), XMFLOAT3(1, 1, 1), 2.00f),
Light::Directional(XMFLOAT3(-0.25f, -1, 0.75f), XMFLOAT3(1, 1, 1), 0.75f), Light::Directional(XMFLOAT3(-0.25f, -1, 0.75f), XMFLOAT3(1, 1, 1), 0.25f),
Light::Directional(XMFLOAT3(-1, 1, -0.5f), XMFLOAT3(1, 1, 1), 0.75f), Light::Directional(XMFLOAT3(-1, 1, -0.5f), XMFLOAT3(1, 1, 1), 0.25f),
Light::Point(XMFLOAT3(-1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.5f, 10), Light::Point(XMFLOAT3(-1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.25f, 10),
Light::Point(XMFLOAT3(1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.25f, 10), Light::Point(XMFLOAT3(1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.25f, 10),
Light::Point(XMFLOAT3(0, 2, 0), XMFLOAT3(1, 0, 0), 0.25f, 10), Light::Point(XMFLOAT3(0, 2, 0), XMFLOAT3(1, 0, 0), 0.25f, 10),
Light::Point(XMFLOAT3(-27.5f, 0, 0), XMFLOAT3(1, 1, 0.5f), 5, 20), Light::Point(XMFLOAT3(-27.5f, 0, 0), XMFLOAT3(1, 1, 0.5f), 0.25f, 20),
}; };
} }
@ -187,21 +214,19 @@ void Game::CreateBasicGeometry()
}; };
entities = { entities = {
std::make_shared<Entity>(materials[1], shapes[0]), std::make_shared<Entity>(materials[1], shapes[3]),
std::make_shared<Entity>(materials[2], shapes[1]),
std::make_shared<Entity>(materials[3], shapes[2]),
std::make_shared<Entity>(materials[2], shapes[3]), std::make_shared<Entity>(materials[2], shapes[3]),
std::make_shared<Entity>(materials[1], shapes[4]), std::make_shared<Entity>(materials[3], shapes[3]),
std::make_shared<Entity>(materials[0], shapes[5]), std::make_shared<Entity>(materials[4], shapes[3]),
std::make_shared<Entity>(materials[0], shapes[6]), std::make_shared<Entity>(materials[5], shapes[3]),
std::make_shared<Entity>(materials[6], shapes[3]),
std::make_shared<Entity>(materials[7], shapes[3]),
std::make_shared<Entity>(materials[0], shapes[3]),
}; };
entities[6]->GetMaterial()->SetEmitAmount(0.75f);
for (int i = 0; i < entities.size(); ++i) for (int i = 0; i < entities.size(); ++i)
{ {
entities[i]->GetTransform()->SetPosition((-(int)(entities.size() / 2) + i) * 5, 0, 0); entities[i]->GetTransform()->SetPosition((-(int)(entities.size() / 2) + i) * 5, 0, 0);
entities[i]->GetMaterial()->SetRoughness(0.60f);
} }
skybox = std::make_shared<Sky>( skybox = std::make_shared<Sky>(

2
Game.h
View file

@ -47,6 +47,8 @@ private:
// Shaders and shader-related constructs // Shaders and shader-related constructs
std::shared_ptr<SimplePixelShader> pixelShader; std::shared_ptr<SimplePixelShader> pixelShader;
std::shared_ptr<SimpleVertexShader> vertexShader; std::shared_ptr<SimpleVertexShader> vertexShader;
std::shared_ptr<SimplePixelShader> pixelShaderPBR;
std::shared_ptr<SimpleVertexShader> vertexShaderPBR;
// A2 shapes // A2 shapes
std::vector<std::shared_ptr<Mesh>> shapes; std::vector<std::shared_ptr<Mesh>> shapes;

View file

@ -1,11 +1,13 @@
#include "Material.h" #include "Material.h"
Material::Material( Material::Material(
bool _pbr,
DirectX::XMFLOAT3 _tint, DirectX::XMFLOAT3 _tint,
float _roughness, float _roughness,
std::shared_ptr<SimpleVertexShader> _vertexShader, std::shared_ptr<SimpleVertexShader> _vertexShader,
std::shared_ptr<SimplePixelShader> _pixelShader) std::shared_ptr<SimplePixelShader> _pixelShader)
{ {
pbr = _pbr;
tint = _tint; tint = _tint;
roughness = _roughness; roughness = _roughness;
vertexShader = _vertexShader; vertexShader = _vertexShader;
@ -25,37 +27,8 @@ Material::~Material()
void Material::Activate(Transform* _transform, std::shared_ptr<Camera> _camera, DirectX::XMFLOAT3 _ambient, std::vector<Light> _lights) void Material::Activate(Transform* _transform, std::shared_ptr<Camera> _camera, DirectX::XMFLOAT3 _ambient, std::vector<Light> _lights)
{ {
vertexShader->SetMatrix4x4("world", _transform->GetWorldMatrix()); if (pbr) ActivatePBR(_transform, _camera, _ambient, _lights);
vertexShader->SetMatrix4x4("worldInvTranspose", _transform->GetWorldMatrixInverseTranspose()); else ActivateStandard(_transform, _camera, _ambient, _lights);
vertexShader->SetMatrix4x4("view", _camera->GetViewMatrix());
vertexShader->SetMatrix4x4("projection", _camera->GetProjectionMatrix());
vertexShader->CopyAllBufferData();
vertexShader->SetShader();
pixelShader->SetFloat3("cameraPosition", _camera->GetTransform()->GetPosition());
pixelShader->SetFloat("roughness", GetRoughness());
pixelShader->SetFloat2("scale", GetUVScale());
pixelShader->SetFloat2("offset", GetUVOffset());
pixelShader->SetFloat3("ambient", _ambient);
pixelShader->SetFloat("emitAmount", GetEmitAmount());
pixelShader->SetFloat3("tint", GetTint());
pixelShader->SetFloat("lightCount", (int)_lights.size());
pixelShader->SetInt("hasEmissiveMap", (int)hasEmissiveMap);
pixelShader->SetInt("hasSpecularMap", (int)hasSpecularMap);
pixelShader->SetInt("hasNormalMap", (int)hasNormalMap);
pixelShader->SetInt("hasReflectionMap", (int)hasReflectionMap);
pixelShader->SetData("lights", &_lights[0], sizeof(Light) * (int)_lights.size());
pixelShader->CopyAllBufferData();
pixelShader->SetShader();
for (auto& t : textures)
{
pixelShader->SetShaderResourceView(t.first.c_str(), t.second.Get());
}
for (auto& s : samplers)
{
pixelShader->SetSamplerState(s.first.c_str(), s.second.Get());
}
} }
DirectX::XMFLOAT3 Material::GetTint() DirectX::XMFLOAT3 Material::GetTint()
@ -160,3 +133,65 @@ void Material::PushTexture(std::string _name, Microsoft::WRL::ComPtr<ID3D11Shade
{ {
textures.insert({ _name, _texture }); textures.insert({ _name, _texture });
} }
void Material::ActivateStandard(Transform* _transform, std::shared_ptr<Camera> _camera, DirectX::XMFLOAT3 _ambient, std::vector<Light> _lights)
{
vertexShader->SetMatrix4x4("world", _transform->GetWorldMatrix());
vertexShader->SetMatrix4x4("worldInvTranspose", _transform->GetWorldMatrixInverseTranspose());
vertexShader->SetMatrix4x4("view", _camera->GetViewMatrix());
vertexShader->SetMatrix4x4("projection", _camera->GetProjectionMatrix());
vertexShader->CopyAllBufferData();
vertexShader->SetShader();
pixelShader->SetFloat3("cameraPosition", _camera->GetTransform()->GetPosition());
pixelShader->SetFloat("roughness", GetRoughness());
pixelShader->SetFloat2("scale", GetUVScale());
pixelShader->SetFloat2("offset", GetUVOffset());
pixelShader->SetFloat3("ambient", _ambient);
pixelShader->SetFloat("emitAmount", GetEmitAmount());
pixelShader->SetFloat3("tint", GetTint());
pixelShader->SetFloat("lightCount", (int)_lights.size());
pixelShader->SetInt("hasEmissiveMap", (int)hasEmissiveMap);
pixelShader->SetInt("hasSpecularMap", (int)hasSpecularMap);
pixelShader->SetInt("hasNormalMap", (int)hasNormalMap);
pixelShader->SetInt("hasReflectionMap", (int)hasReflectionMap);
pixelShader->SetData("lights", &_lights[0], sizeof(Light) * (int)_lights.size());
pixelShader->CopyAllBufferData();
pixelShader->SetShader();
for (auto& t : textures)
{
pixelShader->SetShaderResourceView(t.first.c_str(), t.second.Get());
}
for (auto& s : samplers)
{
pixelShader->SetSamplerState(s.first.c_str(), s.second.Get());
}
}
void Material::ActivatePBR(Transform* _transform, std::shared_ptr<Camera> _camera, DirectX::XMFLOAT3 _ambient, std::vector<Light> _lights)
{
vertexShader->SetMatrix4x4("world", _transform->GetWorldMatrix());
vertexShader->SetMatrix4x4("worldInvTranspose", _transform->GetWorldMatrixInverseTranspose());
vertexShader->SetMatrix4x4("view", _camera->GetViewMatrix());
vertexShader->SetMatrix4x4("projection", _camera->GetProjectionMatrix());
vertexShader->CopyAllBufferData();
vertexShader->SetShader();
pixelShader->SetFloat2("scale", GetUVScale());
pixelShader->SetFloat2("offset", GetUVOffset());
pixelShader->SetFloat3("cameraPosition", _camera->GetTransform()->GetPosition());
pixelShader->SetFloat("lightCount", (int)_lights.size());
pixelShader->SetData("lights", &_lights[0], sizeof(Light) * (int)_lights.size());
pixelShader->CopyAllBufferData();
pixelShader->SetShader();
for (auto& t : textures)
{
pixelShader->SetShaderResourceView(t.first.c_str(), t.second.Get());
}
for (auto& s : samplers)
{
pixelShader->SetSamplerState(s.first.c_str(), s.second.Get());
}
}

View file

@ -2,6 +2,7 @@
#include <DirectXMath.h> #include <DirectXMath.h>
#include <memory> #include <memory>
#include <functional>
#include "DXCore.h" #include "DXCore.h"
#include "SimpleShader.h" #include "SimpleShader.h"
#include "Transform.h" #include "Transform.h"
@ -14,11 +15,14 @@ constexpr auto TEXTYPE_NORMAL = "Normal";
constexpr auto TEXTYPE_EMISSIVE = "Emissive"; constexpr auto TEXTYPE_EMISSIVE = "Emissive";
constexpr auto TEXTYPE_SPECULAR = "Specular"; constexpr auto TEXTYPE_SPECULAR = "Specular";
constexpr auto TEXTYPE_REFLECTION = "Reflection"; constexpr auto TEXTYPE_REFLECTION = "Reflection";
constexpr auto TEXTYPE_ROUGHNESS = "Roughness";
constexpr auto TEXTYPE_METALNESS = "Metalness";
class Material class Material
{ {
public: public:
Material( Material(
bool _pbr,
DirectX::XMFLOAT3 _tint, DirectX::XMFLOAT3 _tint,
float _roughness, float _roughness,
std::shared_ptr<SimpleVertexShader> _vertexShader, std::shared_ptr<SimpleVertexShader> _vertexShader,
@ -56,6 +60,18 @@ public:
bool hasNormalMap; bool hasNormalMap;
bool hasReflectionMap; bool hasReflectionMap;
private: private:
void ActivateStandard(
Transform* _transform,
std::shared_ptr<Camera> _camera,
DirectX::XMFLOAT3 _ambient,
std::vector<Light> _lights);
void ActivatePBR(
Transform* _transform,
std::shared_ptr<Camera> _camera,
DirectX::XMFLOAT3 _ambient,
std::vector<Light> _lights);
bool pbr;
DirectX::XMFLOAT3 tint; DirectX::XMFLOAT3 tint;
float roughness; float roughness;
float emitAmount; float emitAmount;