Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use texture switch #440

Merged
merged 5 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Engine/BuiltInShaders/shaders/fs_PBR.sc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $input v_worldPos, v_normal, v_texcoord0, v_TBN
#include "../common/LightSource.sh"
#include "../common/Envirnoment.sh"

uniform vec4 u_emissiveColor;
uniform vec4 u_emissiveColorAndFactor;

vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir) {
vec3 diffuseBRDF = material.albedo * CD_INV_PI;
Expand Down Expand Up @@ -36,7 +36,7 @@ void main()
vec3 envColor = GetEnvironment(material, v_worldPos, viewDir, v_normal);

// Emissive
vec3 emiColor = material.emissive * u_emissiveColor.xyz;
roeas marked this conversation as resolved.
Show resolved Hide resolved
vec3 emiColor = material.emissive * u_emissiveColorAndFactor.xyz * vec3_splat(u_emissiveColorAndFactor.w);

// Fragment Color
gl_FragData[0] = vec4(dirColor + envColor + emiColor, 1.0);
Expand Down
154 changes: 66 additions & 88 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,18 @@
#include "Math/Transform.hpp"
#include "Path/Path.h"
#include "Rendering/RenderContext.h"
#include "Rendering/ShaderFeature.h"
#include "Resources/ResourceBuilder.h"
#include "Resources/ResourceLoader.h"
#include "Resources/ShaderBuilder.h"
#include "Scene/SceneDatabase.h"
T-rvw marked this conversation as resolved.
Show resolved Hide resolved

#include <algorithm>
#include <filesystem>
//#include <format>

namespace editor
{

namespace Detail
{

const std::unordered_map<cd::MaterialTextureType, engine::ShaderFeature> materialTextureTypeToShaderFeature
{
{ cd::MaterialTextureType::BaseColor, engine::ShaderFeature::ALBEDO_MAP },
{ cd::MaterialTextureType::Normal, engine::ShaderFeature::NORMAL_MAP },
{ cd::MaterialTextureType::Occlusion, engine::ShaderFeature::ORM_MAP },
{ cd::MaterialTextureType::Roughness, engine::ShaderFeature::ORM_MAP },
{ cd::MaterialTextureType::Metallic, engine::ShaderFeature::ORM_MAP },
{ cd::MaterialTextureType::Emissive, engine::ShaderFeature::EMISSIVE_MAP },
};

CD_FORCEINLINE bool IsMaterialTextureTypeValid(cd::MaterialTextureType type)
{
return materialTextureTypeToShaderFeature.find(type) != materialTextureTypeToShaderFeature.end();
}

} // namespace Detail

ECWorldConsumer::ECWorldConsumer(engine::SceneWorld* pSceneWorld, engine::RenderContext* pRenderContext) :
m_pSceneWorld(pSceneWorld), m_pRenderContext(pRenderContext)
{
Expand Down Expand Up @@ -259,108 +239,106 @@ void ECWorldConsumer::AddAnimation(engine::Entity entity, const cd::Animation& a
void ECWorldConsumer::AddMaterial(engine::Entity entity, const cd::Material* pMaterial, engine::MaterialType* pMaterialType, const cd::SceneDatabase* pSceneDatabase)
{
std::set<uint8_t> compiledTextureSlot;
std::map<std::string, const cd::Texture*> outputTexturePathToData;
std::vector<std::tuple<cd::MaterialTextureType, std::string, const cd::Texture*>> outputTypeToData;

engine::MaterialComponent& materialComponent = m_pSceneWorld->GetWorld()->CreateComponent<engine::MaterialComponent>(entity);
materialComponent.Init();
materialComponent.SetMaterialType(pMaterialType);
materialComponent.SetMaterialData(pMaterial);
materialComponent.ActivateShaderFeature(engine::GetSkyTypeShaderFeature(m_pSceneWorld->GetSkyComponent(m_pSceneWorld->GetSkyEntity())->GetSkyType()));

cd::Vec3f albedoColor(1.0f);
engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema();
if (pMaterial)
// Expected textures are ready to build. Add more optional texture data.
for (cd::MaterialTextureType optionalTextureType : pMaterialType->GetOptionalTextureTypes())
{
// Expected textures are ready to build. Add more optional texture data.
for (cd::MaterialTextureType optionalTextureType : pMaterialType->GetOptionalTextureTypes())
cd::TextureID textureID = pMaterial->GetTextureID(optionalTextureType);
if (!textureID.IsValid())
{
cd::TextureID textureID = pMaterial->GetTextureID(optionalTextureType);
if (!textureID.IsValid())
{
// TODO : Its ok to have a material factor instead of texture, remove factor case warning.
CD_WARN("Material {0} does not have optional texture type {1}!", pMaterial->GetName(),
nameof::nameof_enum(optionalTextureType));
continue;
}

std::optional<uint8_t> optTextureSlot = pMaterialType->GetTextureSlot(optionalTextureType);
if (!optTextureSlot.has_value())
{
CD_ERROR("Unknow texture {0} slot!", nameof::nameof_enum(optionalTextureType));
break;
}

if (Detail::IsMaterialTextureTypeValid(optionalTextureType))
{
materialComponent.ActivateShaderFeature(Detail::materialTextureTypeToShaderFeature.at(optionalTextureType));
}

uint8_t textureSlot = optTextureSlot.value();
const cd::Texture& optionalTexture = pSceneDatabase->GetTexture(textureID.Data());
std::string outputTexturePath = engine::Path::GetTextureOutputFilePath(optionalTexture.GetPath(), ".dds");
if (compiledTextureSlot.find(textureSlot) == compiledTextureSlot.end())
{
compiledTextureSlot.insert(textureSlot);
ResourceBuilder::Get().AddTextureBuildTask(optionalTexture.GetType(), optionalTexture.GetPath(), outputTexturePath.c_str());
outputTexturePathToData[cd::MoveTemp(outputTexturePath)] = &optionalTexture;
}
continue;
}

if (auto optMetallic = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Metallic, cd::MaterialProperty::Factor); optMetallic.has_value())
std::optional<uint8_t> optTextureSlot = pMaterialType->GetTextureSlot(optionalTextureType);
if (!optTextureSlot.has_value())
{
materialComponent.SetMetallicFactor(optMetallic.value());
CD_ERROR("Unknow texture {0} slot!", nameof::nameof_enum(optionalTextureType));
break;
}

if (auto optRoughness = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Roughness, cd::MaterialProperty::Factor); optRoughness.has_value())
uint8_t textureSlot = optTextureSlot.value();
const cd::Texture& optionalTexture = pSceneDatabase->GetTexture(textureID.Data());
std::string outputTexturePath = engine::Path::GetTextureOutputFilePath(optionalTexture.GetPath(), ".dds");
if (compiledTextureSlot.find(textureSlot) == compiledTextureSlot.end())
{
materialComponent.SetRoughnessFactor(optRoughness.value());
// TODO : Resource level
compiledTextureSlot.insert(textureSlot);
ResourceBuilder::Get().AddTextureBuildTask(optionalTextureType, optionalTexture.GetPath(), outputTexturePath.c_str());
}
outputTypeToData.emplace_back(optionalTextureType, cd::MoveTemp(outputTexturePath), &optionalTexture);
}

if (auto optTwoSided = pMaterial->GetBoolProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::TwoSided); optTwoSided.has_value())
{
materialComponent.SetTwoSided(optTwoSided.value());
}
// TODO : create material component before ResourceBuilder done.
// Assign a special color for loading resource status.

// TODO : Need pMaterial->GetVec3fProperty
// if (auto optOcclusion = pMaterial->GetVec3fProperty(cd::MaterialPropertyGroup::BaseColor, cd::MaterialProperty::Factor); optOcclusion.has_value())
// {
// materialComponent.SetFactor(cd::MaterialPropertyGroup::BaseColor, optOcclusion.value());
// }

if (auto optOcclusion = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Occlusion, cd::MaterialProperty::Factor); optOcclusion.has_value())
{
materialComponent.SetFactor(cd::MaterialPropertyGroup::Occlusion, optOcclusion.value());
}
if (auto optRoughness = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Roughness, cd::MaterialProperty::Factor); optRoughness.has_value())
{
materialComponent.SetFactor(cd::MaterialPropertyGroup::Metallic, optRoughness.value());
}
if (auto optMetallic = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::Metallic, cd::MaterialProperty::Factor); optMetallic.has_value())
{
materialComponent.SetFactor(cd::MaterialPropertyGroup::Metallic, optMetallic.value());
}

if (auto optBlendMode = pMaterial->GetI32Property(cd::MaterialPropertyGroup::General, cd::MaterialProperty::BlendMode); optBlendMode.has_value())
if (auto optTwoSided = pMaterial->GetBoolProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::TwoSided); optTwoSided.has_value())
{
materialComponent.SetTwoSided(optTwoSided.value());
}
if (auto optBlendMode = pMaterial->GetI32Property(cd::MaterialPropertyGroup::General, cd::MaterialProperty::BlendMode); optBlendMode.has_value())
{
cd::BlendMode blendMode = static_cast<cd::BlendMode>(optBlendMode.value());
if (cd::BlendMode::Mask == blendMode)
{
cd::BlendMode blendMode = static_cast<cd::BlendMode>(optBlendMode.value());
if (cd::BlendMode::Mask == blendMode)
{
auto optAlphaTestValue = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::OpacityMaskClipValue);
assert(optAlphaTestValue.has_value());
auto optAlphaTestValue = pMaterial->GetFloatProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::OpacityMaskClipValue);
assert(optAlphaTestValue.has_value());

materialComponent.SetAlphaCutOff(optAlphaTestValue.value());
}

materialComponent.SetBlendMode(blendMode);
materialComponent.SetAlphaCutOff(optAlphaTestValue.value());
}
}
else
{
albedoColor = cd::Vec3f(0.2f);

materialComponent.SetBlendMode(blendMode);
}

// TODO : ResourceBuilder will move to EditorApp::Update in the future.
// Now let's wait all resource build tasks done here.
ResourceBuilder::Get().Update();

// TODO : create material component before ResourceBuilder done.
// Assign a special color for loading resource status.
materialComponent.SetAlbedoColor(cd::MoveTemp(albedoColor));

// Textures.
for (const auto& [outputTextureFilePath, pTextureData] : outputTexturePathToData)
for (const auto& [type, path, pTexture] : outputTypeToData)
{
auto textureFileBlob = engine::ResourceLoader::LoadFile(outputTextureFilePath.c_str());
if(!textureFileBlob.empty())
auto textureFileBlob = engine::ResourceLoader::LoadFile(path.c_str());
if (!textureFileBlob.empty())
{
materialComponent.AddTextureFileBlob(pTextureData->GetType(), pMaterial, *pTextureData, cd::MoveTemp(textureFileBlob));
// TODO : Store TextureFileBlob multiple times, a temporary solution here.
// Should use something like TextureResource to avoid duplicate storage.
materialComponent.AddTextureFileBlob(type, pMaterial, *pTexture, cd::MoveTemp(textureFileBlob));
if (auto pPropertyGroup = materialComponent.GetPropertyGroup(type); pPropertyGroup)
{
pPropertyGroup->useTexture = true;
materialComponent.ActivateShaderFeature(engine::MaterialTextureTypeToShaderFeature.at(type));
}
}
}

materialComponent.Build();
}
/**/

void ECWorldConsumer::AddMorphs(engine::Entity entity, const std::vector<cd::Morph>& morphs, const cd::Mesh* pMesh)
{
engine::World* pWorld = m_pSceneWorld->GetWorld();
Expand Down
1 change: 1 addition & 0 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ void EditorApp::UpdateMaterials()
const std::string& programName = pMaterialComponent->GetShaderProgramName();
const std::string& featuresCombine = pMaterialComponent->GetFeaturesCombine();


// New shader feature added, need to compile new variants.
m_pRenderContext->CheckShaderProgram(entity, programName, featuresCombine);

Expand Down
12 changes: 6 additions & 6 deletions Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,13 @@ void AssetBrowser::ImportJson(const char* pFilePath)
float metallic = material["float@_Metallic"];
if (engine::MaterialComponent* pMaterialComponent = mapMaterialNameToMaterialData[name])
{
pMaterialComponent->SetAlbedoColor(GetVec3fFormString(color) / 255.0f);
pMaterialComponent->SetMetallicFactor(metallic);
pMaterialComponent->SetRoughnessFactor(1.0f - roughness);
if (pMaterialComponent->GetTextureInfo(cd::MaterialPropertyGroup::BaseColor))
pMaterialComponent->SetFactor(cd::MaterialPropertyGroup::BaseColor, GetVec3fFormString(color) / 255.0f);
pMaterialComponent->SetFactor(cd::MaterialPropertyGroup::Metallic, metallic);
pMaterialComponent->SetFactor(cd::MaterialPropertyGroup::Roughness, roughness);
if (auto pPropertyGroup = pMaterialComponent->GetPropertyGroup(cd::MaterialPropertyGroup::BaseColor); pPropertyGroup)
{
pMaterialComponent->GetTextureInfo(cd::MaterialPropertyGroup::BaseColor)->SetUVOffset(GetVec2fFormString(UVOffset));
pMaterialComponent->GetTextureInfo(cd::MaterialPropertyGroup::BaseColor)->SetUVScale(GetVec2fFormString(UVScale));
pPropertyGroup->textureInfo.SetUVOffset(GetVec2fFormString(UVOffset));
pPropertyGroup->textureInfo.SetUVScale(GetVec2fFormString(UVScale));
}
}
else
Expand Down
2 changes: 0 additions & 2 deletions Engine/Source/Editor/UILayers/EntityList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)
auto& materialComponent = pWorld->CreateComponent<engine::MaterialComponent>(entity);
materialComponent.Init();
materialComponent.SetMaterialType(pMaterialType);
materialComponent.SetAlbedoColor(cd::Vec3f(0.2f));
materialComponent.ActivateShaderFeature(engine::GetSkyTypeShaderFeature(pSceneWorld->GetSkyComponent(pSceneWorld->GetSkyEntity())->GetSkyType()));
materialComponent.Build();

Expand Down Expand Up @@ -126,7 +125,6 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)
auto& materialComponent = pWorld->CreateComponent<engine::MaterialComponent>(entity);
materialComponent.Init();
materialComponent.SetMaterialType(pTerrainMaterialType);
materialComponent.SetAlbedoColor(cd::Vec3f(0.2f));
materialComponent.SetTwoSided(true);
materialComponent.ActivateShaderFeature(engine::GetSkyTypeShaderFeature(pSceneWorld->GetSkyComponent(pSceneWorld->GetSkyEntity())->GetSkyType()));
materialComponent.Build();
Expand Down
Loading
Loading