Skip to content

Commit

Permalink
Support rendering single-sided mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Nov 5, 2024
1 parent a2902af commit a1634ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Engine/Asset/Shader/Lib/Material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Material GetMaterial(vec2 uv, vec3 normal, vec3 tangent, vec3 bitangent)
vec3 normalMap = SampleNormalTexture(uv);
material.normal = normalize(TBN * normalMap);
material.normal = normalize(material.normal * u_normalFactor);

if(!gl_FrontFacing)
{
material.normal = -material.normal;
}
}
if (u_useEmissiveTexture)
{
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Editor/Layer/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,8 @@ void ImGuiLayer::ShowDetails()

StartWithText("Reflectance");
ImGui::DragFloat("##Reflectance", &pMaterialResource->m_reflectance, 0.01f, 0.0f, 1.0f);
StartWithText("TwoSide");
ImGui::Checkbox("##TwoSide", &pMaterialResource->m_twoSide);
}
}
else
Expand Down
33 changes: 25 additions & 8 deletions Engine/Source/Editor/Layer/RendererLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ void RendererLayer::BasePass()
UploadMaterialPropertyGroup(pShader, pMaterialResource->m_occlusionPropertyGroup);
UploadMaterialPropertyGroup(pShader, pMaterialResource->m_roughnessPropertyGroup);
UploadMaterialPropertyGroup(pShader, pMaterialResource->m_metallicPropertyGroup);

pShader->UploadUniform(pMaterialResource->m_reflectanceLocation, pMaterialResource->m_reflectance);

// TODO: Cache current rendering state.
if (pMaterialResource->m_twoSide)
{
sl::RenderCore::Culling(sl::CullingMode::FrontNitherBack);
}
else
{
sl::RenderCore::Culling(sl::CullingMode::Back);
}

sl::RenderCore::Submit(pMeshResource->GetVertexArray(), pShader);
}

Expand All @@ -203,14 +213,12 @@ void RendererLayer::EntityIDPass()
continue;
}

auto *pShaderResource = sl::ResourceManager::GetShaderResource(rendering.m_optIDShaderResourceName.value());
auto *pMeshResource = sl::ResourceManager::GetMeshResource(rendering.m_optMeshResourceName.value());
if (!pShaderResource ||!pMeshResource)
{
continue;
}

if (!pShaderResource->IsReady() || !pMeshResource->IsReady())
auto *pMaterialResource = sl::ResourceManager::GetMaterialResource(rendering.m_optMaterialResourceName.value());
auto *pShaderResource = sl::ResourceManager::GetShaderResource(rendering.m_optIDShaderResourceName.value());
if (!pMeshResource || !pMeshResource->IsReady() ||
!pMaterialResource || !pMaterialResource->IsReady() ||
!pShaderResource || !pShaderResource->IsReady())
{
continue;
}
Expand All @@ -220,6 +228,15 @@ void RendererLayer::EntityIDPass()
pShader->UploadUniform(0, transform.GetTransform());
pShader->UploadUniform(1, (int)entity);

if (pMaterialResource->m_twoSide)
{
sl::RenderCore::Culling(sl::CullingMode::FrontNitherBack);
}
else
{
sl::RenderCore::Culling(sl::CullingMode::Back);
}

sl::RenderCore::Submit(pMeshResource->GetVertexArray(), pShader);
}

Expand Down

0 comments on commit a1634ac

Please sign in to comment.