From 7e8e2e1542398c12bfa08b185e412e4af9125746 Mon Sep 17 00:00:00 2001 From: BNiels Date: Mon, 8 Apr 2019 10:42:44 +0200 Subject: [PATCH] Normals are now oriented the right way (without a branch) --- resources/shaders/path_tracer.hlsl | 4 +++- resources/shaders/raytracing.hlsl | 2 +- resources/shaders/rt_hybrid.hlsl | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/resources/shaders/path_tracer.hlsl b/resources/shaders/path_tracer.hlsl index db0e9586..7b634bc7 100644 --- a/resources/shaders/path_tracer.hlsl +++ b/resources/shaders/path_tracer.hlsl @@ -222,6 +222,8 @@ void RaygenEntry() float3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float3 V = normalize(cpos - wpos); + normal = lerp(normal, -normal, dot(normal, V) < 0); + float3 result = float3(0, 0, 0); nextRand(rand_seed); @@ -323,7 +325,7 @@ void ReflectionHit(inout HitInfo payload, in MyAttributes attr) //float3 fN = N; float3 fN = normalize(mul(output_data.normal, TBN)); - if (dot(fN, V) <= 0.0f) fN = -fN; + fN = lerp(fN, -fN, dot(fN, V) < 0); // Irradiance #ifdef OLDSCHOOL diff --git a/resources/shaders/raytracing.hlsl b/resources/shaders/raytracing.hlsl index 2e5f701b..fe8dd7a5 100644 --- a/resources/shaders/raytracing.hlsl +++ b/resources/shaders/raytracing.hlsl @@ -285,7 +285,7 @@ void ClosestHitEntry(inout HitInfo payload, in MyAttributes attr) const float3x3 TBN = float3x3(T, B, N); float3 fN = normalize(mul(output_data.normal, TBN)); - if (dot(fN, V) <= 0.0f) fN = -fN; + fN = lerp(fN, -fN, dot(fN, V) < 0); // Irradiance float3 flipped_N = fN; diff --git a/resources/shaders/rt_hybrid.hlsl b/resources/shaders/rt_hybrid.hlsl index e5edb405..d9cafbc3 100644 --- a/resources/shaders/rt_hybrid.hlsl +++ b/resources/shaders/rt_hybrid.hlsl @@ -177,6 +177,8 @@ void RaygenEntry() return; } + normal = lerp(normal, -normal, dot(normal, V) < 0); + // Describe the surface for mip level generation SurfaceHit sfhit; sfhit.pos = wpos; @@ -292,7 +294,7 @@ void ReflectionHit(inout ReflectionHitInfo payload, in MyAttributes attr) float3x3 TBN = float3x3(T, B, N); float3 fN = normalize(mul(output_data.normal, TBN)); - if (dot(fN, V) <= 0.0f) fN = -fN; + fN = lerp(fN, -fN, dot(fN, V) < 0); //Shading float3 flipped_N = fN;