Skip to content

Commit 043afd4

Browse files
PBR Renderer: use special value to pack zero-length normals
1 parent 7bfa360 commit 043afd4

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Hydrogent/src/HnMeshUtils.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ pxr::VtValue HnMeshUtils::PackVertexNormals(const pxr::VtValue& Normals) const
411411
Uint32* pPackedNormals = reinterpret_cast<Uint32*>(PackedNormals.data());
412412
for (size_t i = 0; i < NormalsArray.size(); ++i)
413413
{
414-
pPackedNormals[i] = PBR_Renderer::PackVertexNormal(normalize(ToFloat3(NormalsArray[i])));
414+
float3 Normal = ToFloat3(NormalsArray[i]);
415+
float Length = length(Normal);
416+
if (Length != 0)
417+
Normal /= Length;
418+
419+
pPackedNormals[i] = PBR_Renderer::PackVertexNormal(Normal);
415420
}
416421
return pxr::VtValue::Take(PackedNormals);
417422
}

PBR/interface/PBR_Renderer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,9 @@ inline void PBR_Renderer::ProcessTexturAttribs(PBR_Renderer::PSO_FLAGS PSOFlags,
938938

939939
inline Uint32 PBR_Renderer::PackVertexNormal(const float3& Normal)
940940
{
941+
if (Normal == float3{})
942+
return ~0u;
943+
941944
Uint32 x = static_cast<Uint32>(clamp((Normal.x + 1.f) * 32767.f, 0.f, 65535.f));
942945
Uint32 y = static_cast<Uint32>(clamp((Normal.y + 1.f) * 16383.f, 0.f, 32767.f));
943946
Uint32 z = Normal.z >= 0 ? 0 : 1;

Shaders/PBR/private/RenderPBR.vsh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ float4 GetVertexColor(float4 Color)
107107
// Reverse of PBR_Renderer::PackVertexNormal()
108108
float3 GetNormal(in uint PackedNormal)
109109
{
110-
float3 Normal;
111-
Normal.x = float( PackedNormal & 0xFFFFu) / 32767.0 - 1.0;
112-
Normal.y = float((PackedNormal >> 16u) & 0x7FFFu) / 16383.0 - 1.0;
113-
Normal.z = sqrt(max(1.0 - dot(Normal.xy, Normal.xy), 0.0));
114-
Normal.z *= (PackedNormal & 0x80000000u) != 0u ? -1.0 : 1.0;
110+
float3 Normal = float3(0.0, 0.0, 0.0);
111+
if (PackedNormal != 0xFFFFFFFFu)
112+
{
113+
Normal.x = float( PackedNormal & 0xFFFFu) / 32767.0 - 1.0;
114+
Normal.y = float((PackedNormal >> 16u) & 0x7FFFu) / 16383.0 - 1.0;
115+
Normal.z = sqrt(max(1.0 - dot(Normal.xy, Normal.xy), 0.0));
116+
Normal.z *= (PackedNormal & 0x80000000u) != 0u ? -1.0 : 1.0;
117+
}
115118
return Normal;
116119
}
117120
#else

0 commit comments

Comments
 (0)