Skip to content

Commit

Permalink
terrain multi material
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jan 1, 2025
1 parent 274a829 commit 2c9b647
Show file tree
Hide file tree
Showing 26 changed files with 695 additions and 96 deletions.
48 changes: 37 additions & 11 deletions application/lucre/sceneDescriptions/reserved0.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
}
}
]
},
}
],
"terrainMultiMaterial":
[
{
"filename": "application/lucre/terrainDescriptions/terrain1.json",
"instances":
Expand All @@ -36,15 +39,15 @@
{
"scale":
[
1, 1.5, 1
1.33093, 1.5, 1.54767
],
"rotation":
[
0, -0, 0
],
"translation":
[
-0.622098, -3.23777, -1.23215
-10.9892, -4.4496, 1.19157
]
}
}
Expand All @@ -53,6 +56,29 @@
],
"fastgltf files":
[
{
"filename": "application/lucre/models/external_3D_files/measured/2m.gltf",
"instances":
[
{
"transform":
{
"scale":
[
1, 1, 1
],
"rotation":
[
0, -0, 0
],
"translation":
[
0.323799, -2.10549, 0
]
}
}
]
},
{
"filename": "application/lucre/models/assets/Sponza/glTF/Sponza.gltf",
"instances":
Expand Down Expand Up @@ -135,11 +161,11 @@
],
"rotation":
[
0, 3.83972, 0
-3.14159, -0.698127, -3.14159
],
"translation":
[
4.74126, -0.785, 0
-2.85519, -2, -5.15315
]
}
},
Expand All @@ -152,11 +178,11 @@
],
"rotation":
[
0, 4.18879, 0
-3.14159, -1.0472, -3.14159
],
"translation":
[
3.64108, -0.785, 0
-3.38277, -2, -4.05522
]
}
},
Expand All @@ -165,15 +191,15 @@
{
"scale":
[
0.00999998, 0.01, 0.00999998
0.00999997, 0.01, 0.00999997
],
"rotation":
[
-3.14159, -0.349068, -3.14159
],
"translation":
[
4.37833, -0.784963, 0.141733
-1.48155, -2, -4.65439
]
}
},
Expand All @@ -190,7 +216,7 @@
],
"translation":
[
3.26095, -0.786382, -0.817254
-2.46564, -2, -6.26672
]
}
}
Expand All @@ -213,7 +239,7 @@
],
"translation":
[
-1.07286, -3.53, -1.23215
-1.38243, -3.53, -1.23215
]
}
}
Expand Down
7 changes: 4 additions & 3 deletions application/lucre/scenes/reserved0Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ namespace LucreApp
}

{
m_Terrain1 = m_Dictionary.Retrieve("SL::application/lucre/models/terrain/terrain1.glb::0::root");
// TLMM = terrain loader multi material
m_Terrain1 = m_Dictionary.Retrieve("TLMM::application/lucre/models/terrain/terrain1.glb::0::root");
if (m_Terrain1 != entt::null)
{
Water1Component water1Component{.m_Scale = {25.0f, 1.0f, 50.0f}, .m_Translation = {0.0f, 1.728f, 0.0f}};
m_Registry.emplace<Water1Component>(m_Terrain1, water1Component);
}

auto terrain = m_Dictionary.Retrieve("SL::application/lucre/models/terrain/terrain1.glb::0::Scene::terrain");
auto terrain = m_Dictionary.Retrieve("TLMM::application/lucre/models/terrain/terrain1.glb::0::Scene::terrain");
if (terrain != entt::null)
{
m_Registry.remove<PbrMaterialTag>(terrain);
Expand Down Expand Up @@ -314,7 +315,7 @@ namespace LucreApp
m_CameraController->SetZoomFactor(1.0f);
auto& cameraTransform = m_Registry.get<TransformComponent>(m_Camera);

cameraTransform.SetTranslation({4.027f, 0.817f, -10.658f});
cameraTransform.SetTranslation({-3.485f, 3.625f, -32.261f});
cameraTransform.SetRotation({-0.074769905f, 3.11448769f, 0.0f});

// global camera transform is not yet available
Expand Down
3 changes: 1 addition & 2 deletions application/lucre/terrainDescriptions/terrain1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"file format identifier": 1.2,
"description": "terrain 1",
"author": "Copyright (c) 2024 Engine Development Team",
"mesh": "application/lucre/models/terrain/terrain1.glb",
"controlTexture": "application/lucre/models/terrain/controlTexture.png"
"mesh": "application/lucre/models/terrain/terrain1.glb"
}
6 changes: 1 addition & 5 deletions engine/platform/Vulkan/VKmaterialDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ namespace GfxRenderEngine
}

VK_MaterialDescriptor::VK_MaterialDescriptor(Material::MaterialType materialType,
PbrMultiMaterial::PbrMultiMaterialTextures& multiTextures,
std::shared_ptr<Texture>& controlTexture)
PbrMultiMaterial::PbrMultiMaterialTextures& multiTextures)
: m_MaterialType{materialType}
{
switch (materialType)
Expand Down Expand Up @@ -146,16 +145,13 @@ namespace GfxRenderEngine
}
}

auto& imageInfoCtrl = static_cast<VK_Texture*>(controlTexture.get())->GetDescriptorImageInfo();

VK_DescriptorWriter(GetMaterialDescriptorSetLayout(materialType))
.WriteImage(0, imageInfo0Vec)
.WriteImage(1, imageInfo1Vec)
.WriteImage(2, imageInfo2Vec)
.WriteImage(3, imageInfo3Vec)
.WriteImage(4, imageInfo4Vec)
.WriteImage(5, imageInfo5Vec)
.WriteImage(6, imageInfoCtrl)
.Build(m_DescriptorSet);
break;
}
Expand Down
5 changes: 2 additions & 3 deletions engine/platform/Vulkan/VKmaterialDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ namespace GfxRenderEngine
public:
VK_MaterialDescriptor(Material::MaterialType materialType, PbrMaterial::MaterialTextures& textures);
VK_MaterialDescriptor(Material::MaterialType materialType, std::shared_ptr<Cubemap> const& cubemap);

VK_MaterialDescriptor(Material::MaterialType materialType, PbrMultiMaterial::PbrMultiMaterialTextures& multiTextures,
std::shared_ptr<Texture>& controlTexture);
VK_MaterialDescriptor(Material::MaterialType materialType,
PbrMultiMaterial::PbrMultiMaterialTextures& multiTextures);

VK_MaterialDescriptor(VK_MaterialDescriptor const& other);
VK_MaterialDescriptor(std::shared_ptr<MaterialDescriptor> const& materialDescriptor);
Expand Down
19 changes: 17 additions & 2 deletions engine/platform/Vulkan/VKmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,24 @@ namespace GfxRenderEngine

void VK_Model::DrawPbrMulti(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout)
{
for (auto& submesh : m_SubmeshesPbr)
for (auto& submesh : m_SubmeshesPbrMulti)
{
BindDescriptors(frameInfo, pipelineLayout, submesh, true /*bind resources*/);

VK_MaterialDescriptor& materialDescriptor =
*static_cast<VK_MaterialDescriptor*>(submesh.m_Material->m_MaterialDescriptor.get());
const VkDescriptorSet& materialDescriptorSet = materialDescriptor.GetDescriptorSet();
const VkDescriptorSet& resourceDescriptorSet = submesh.m_ResourceDescriptor.GetDescriptorSet();
std::vector<VkDescriptorSet> descriptorSets = {frameInfo.m_GlobalDescriptorSet, materialDescriptorSet,
resourceDescriptorSet};
vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, // VkCommandBuffer commandBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint,
pipelineLayout, // VkPipelineLayout layout,
0, // uint32_t firstSet,
descriptorSets.size(), // uint32_t descriptorSetCount,
descriptorSets.data(), // const VkDescriptorSet* pDescriptorSets,
0, // uint32_t dynamicOffsetCount,
nullptr // const uint32_t* pDynamicOffsets);
);
PushConstantsPbrMulti(frameInfo, pipelineLayout, submesh);
DrawSubmesh(frameInfo.m_CommandBuffer, submesh);
}
Expand Down
3 changes: 1 addition & 2 deletions engine/platform/Vulkan/VKrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ namespace GfxRenderEngine
.AddBinding(4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT,
Material::NUM_MULTI_MATERIAL) // roughness map
.AddBinding(5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT,
Material::NUM_MULTI_MATERIAL) // metallic map
.AddBinding(6, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) // control texture
Material::NUM_MULTI_MATERIAL) // metallic map
.Build();

m_ResourceDescriptorSetLayouts[Rt::RtInstance] =
Expand Down
3 changes: 1 addition & 2 deletions engine/platform/Vulkan/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@
#define GLSL_HAS_EMISSIVE_COLOR (0x1 << 0x5)
#define GLSL_HAS_EMISSIVE_MAP (0x1 << 0x6)


#define GLSL_NUM_MULTI_MATERIAL 2
#define GLSL_NUM_MULTI_MATERIAL 4
3 changes: 0 additions & 3 deletions engine/platform/Vulkan/shaders/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ layout(push_constant) uniform Push
float m_Spare1; // padding
float m_Spare2; // padding
float m_Spare3; // padding

// byte 64 to 128
vec4 m_Spare4[4];
} push;

void main()
Expand Down
68 changes: 42 additions & 26 deletions engine/platform/Vulkan/shaders/pbrMultiMaterial.frag
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ layout(set = 1, binding = 2) uniform sampler2D roughnessMetallicMap[GLSL_NUM_MUL
layout(set = 1, binding = 3) uniform sampler2D emissiveMap[GLSL_NUM_MULTI_MATERIAL];
layout(set = 1, binding = 4) uniform sampler2D roughnessMap[GLSL_NUM_MULTI_MATERIAL];
layout(set = 1, binding = 5) uniform sampler2D metallicMap[GLSL_NUM_MULTI_MATERIAL];
layout(set = 1, binding = 6) uniform sampler2D controlMap;

layout(location = 0) in vec3 fragPosition;
layout(location = 1) in vec4 fragColor;
Expand Down Expand Up @@ -78,9 +77,6 @@ struct PbrMaterial
float m_Spare1; // padding
float m_Spare2; // padding
float m_Spare3; // padding

// byte 64 to 128
vec4 m_Spare4[4];
};

layout(set = 0, binding = 0) uniform GlobalUniformBuffer
Expand All @@ -103,54 +99,47 @@ layout(push_constant) uniform Push

void main()
{
float contribution[4];
vec4 controlMapSample = texture(controlMap, fragUV);
contribution[0] = controlMapSample.r;
contribution[1] = controlMapSample.g;
contribution[2] = controlMapSample.b;
contribution[3] = controlMapSample.a;

// position
outPosition = vec4(fragPosition, 1.0);
outColor = vec4(0.0, 0.0, 0.0, 0.0);
outNormal = vec4(0.0, 0.0, 1.0, 1.0);


// normal
vec3 N = normalize(fragNormal);
vec3 T = normalize(fragTangent);
// Gram Schmidt
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T);
mat3 TBN = mat3(T, B, N);


vec4 col[GLSL_NUM_MULTI_MATERIAL];
vec4 normal[GLSL_NUM_MULTI_MATERIAL];
vec4 material[GLSL_NUM_MULTI_MATERIAL];
vec4 emissive[GLSL_NUM_MULTI_MATERIAL];


for (int i = 0; i < GLSL_NUM_MULTI_MATERIAL; ++i)
{
// color
vec4 col;
if (bool(push.m_PbrMaterial[i].m_Features & GLSL_HAS_DIFFUSE_MAP))
{
col = texture(diffuseMap[i], fragUV) * push.m_PbrMaterial[i].m_DiffuseColor;
col[i] = texture(diffuseMap[i], fragUV) * push.m_PbrMaterial[i].m_DiffuseColor;
}
else
{
col = vec4(fragColor.r, fragColor.g, fragColor.b, fragColor.a);
col[i] = vec4(fragColor.r, fragColor.g, fragColor.b, fragColor.a);
}
outColor = outColor + col * contribution[i];

// normal
vec4 normal;
float normalMapIntensity = push.m_PbrMaterial[i].m_NormalMapIntensity;
if (bool(push.m_PbrMaterial[i].m_Features & GLSL_HAS_NORMAL_MAP))
{
vec3 normalTangentSpace = texture(normalMap[i],fragUV).xyz * 2 - vec3(1.0, 1.0, 1.0);
normalTangentSpace = mix(vec3(0.0, 0.0, 1.0), normalTangentSpace, normalMapIntensity);
normal = vec4(normalize(TBN * normalTangentSpace), 1.0);
normal[i] = vec4(normalize(TBN * normalTangentSpace), 1.0);
}
else
{
normal = vec4(N, 1.0);
normal[i] = vec4(N, 1.0);
}
outNormal = mix(outNormal, normal, contribution[i]);

// roughness, metallic
float roughness;
Expand Down Expand Up @@ -180,18 +169,45 @@ void main()
metallic = push.m_PbrMaterial[i].m_Metallic;
}
}
outMaterial = vec4(normalMapIntensity, roughness, metallic, 0.0);
material[i] = vec4(normalMapIntensity, roughness, metallic, 0.0);

// emissive material
vec4 emissiveColor = vec4(push.m_PbrMaterial[i].m_EmissiveColor.r, push.m_PbrMaterial[i].m_EmissiveColor.g, push.m_PbrMaterial[i].m_EmissiveColor.b, 1.0);
if (bool(push.m_PbrMaterial[i].m_Features & GLSL_HAS_EMISSIVE_MAP))
{
vec4 fragEmissiveColor = texture(emissiveMap[i], fragUV);
outEmissive = fragEmissiveColor * emissiveColor * push.m_PbrMaterial[i].m_EmissiveStrength;
emissive[i] = fragEmissiveColor * emissiveColor * push.m_PbrMaterial[i].m_EmissiveStrength;
}
else
{
outEmissive = emissiveColor * push.m_PbrMaterial[i].m_EmissiveStrength;
emissive[i] = emissiveColor * push.m_PbrMaterial[i].m_EmissiveStrength;
}
}

float vertical;
vertical = smoothstep(0.5, 0.6, N.y);

float altitude;
altitude = smoothstep(8.7, 9.0, fragPosition.y);


float lowness;
lowness = smoothstep(1.3, 1.6, fragPosition.y);

// col
outColor = mix(col[0], col[3], altitude);
outColor = mix(col[2], outColor, lowness);
outColor = mix(col[1], outColor, vertical);

// normal
outNormal = mix(normal[1], normal[0], vertical);
outNormal = mix(outNormal, normal[3], altitude);

// material
outMaterial = mix(material[1], material[0], vertical);
outMaterial = mix(outMaterial, material[3], altitude);

// emissive
outEmissive = mix(emissive[1], emissive[0], vertical);
outEmissive = mix(outEmissive, emissive[3], altitude);
}
Loading

0 comments on commit 2c9b647

Please sign in to comment.