From c518e9db4e9424387ffaed048e1bbb2019fc492f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Sat, 29 Jul 2023 08:15:56 +0200 Subject: [PATCH] tangent interpolation fix --- WickedEngine/shaders/objectHF.hlsli | 4 +-- WickedEngine/shaders/surfaceHF.hlsli | 7 ++++- WickedEngine/wiScene_Components.h | 39 ++++++++++++++-------------- WickedEngine/wiVersion.cpp | 2 +- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/WickedEngine/shaders/objectHF.hlsli b/WickedEngine/shaders/objectHF.hlsli index bf9d55419..70ea6497f 100644 --- a/WickedEngine/shaders/objectHF.hlsli +++ b/WickedEngine/shaders/objectHF.hlsli @@ -206,10 +206,10 @@ struct VertexSurface color *= input.GetVertexColor(); } - normal = normalize(mul((float3x3)input.GetInstance().transformInverseTranspose.GetMatrix(), normal)); + normal = mul((float3x3)input.GetInstance().transformInverseTranspose.GetMatrix(), normal); tangent = input.GetTangent(); - tangent.xyz = normalize(mul((float3x3)input.GetInstance().transformInverseTranspose.GetMatrix(), tangent.xyz)); + tangent.xyz = mul((float3x3)input.GetInstance().transformInverseTranspose.GetMatrix(), tangent.xyz); uvsets = input.GetUVSets(); uvsets.xy = mad(uvsets.xy, material.texMulAdd.xy, material.texMulAdd.zw); diff --git a/WickedEngine/shaders/surfaceHF.hlsli b/WickedEngine/shaders/surfaceHF.hlsli index 809c6c754..262086327 100644 --- a/WickedEngine/shaders/surfaceHF.hlsli +++ b/WickedEngine/shaders/surfaceHF.hlsli @@ -328,6 +328,7 @@ struct Surface const bool is_hairparticle = geometry.flags & SHADERMESH_FLAG_HAIRPARTICLE; const bool is_emittedparticle = geometry.flags & SHADERMESH_FLAG_EMITTEDPARTICLE; const bool simple_lighting = is_hairparticle || is_emittedparticle; + const bool is_backface = flags & SURFACE_FLAG_BACKFACE; float3 n0 = unpack_unitvector(asuint(data0.w)); float3 n1 = unpack_unitvector(asuint(data1.w)); @@ -335,7 +336,7 @@ struct Surface N = attribute_at_bary(n0, n1, n2, bary); N = mul((float3x3)inst.transformInverseTranspose.GetMatrix(), N); N = normalize(N); - if ((flags & SURFACE_FLAG_BACKFACE) && !is_hairparticle && !is_emittedparticle) + if (is_backface && !is_hairparticle && !is_emittedparticle) { N = -N; } @@ -393,6 +394,10 @@ struct Surface const float4 t2 = buf[i2]; T = attribute_at_bary(t0, t1, t2, bary); T.xyz = mul((float3x3)inst.transformInverseTranspose.GetMatrix(), T.xyz); + if (is_backface) + { + T.xyz = -T.xyz; + } T.xyz = normalize(T.xyz); B = normalize(cross(T.xyz, N) * T.w); const float3x3 TBN = float3x3(T.xyz, B, N); diff --git a/WickedEngine/wiScene_Components.h b/WickedEngine/wiScene_Components.h index 9b772835b..fbb23a4c5 100644 --- a/WickedEngine/wiScene_Components.h +++ b/WickedEngine/wiScene_Components.h @@ -497,7 +497,10 @@ namespace wi::scene struct Vertex_POS { XMFLOAT3 pos = XMFLOAT3(0.0f, 0.0f, 0.0f); - uint32_t normal_wind = 0; + uint8_t n_x = 0; + uint8_t n_y = 0; + uint8_t n_z = 0; + uint8_t w = 0; constexpr void FromFULL(const XMFLOAT3& _pos, const XMFLOAT3& _nor, uint8_t wind) { @@ -517,32 +520,28 @@ namespace wi::scene } constexpr void MakeFromParams(const XMFLOAT3& normal) { - normal_wind = normal_wind & 0xFF000000; // reset only the normals - normal_wind |= uint32_t((normal.x * 0.5f + 0.5f) * 255.0f) << 0; - normal_wind |= uint32_t((normal.y * 0.5f + 0.5f) * 255.0f) << 8; - normal_wind |= uint32_t((normal.z * 0.5f + 0.5f) * 255.0f) << 16; + n_x = uint8_t((normal.x * 0.5f + 0.5f) * 255.0f); + n_y = uint8_t((normal.y * 0.5f + 0.5f) * 255.0f); + n_z = uint8_t((normal.z * 0.5f + 0.5f) * 255.0f); } constexpr void MakeFromParams(const XMFLOAT3& normal, uint8_t wind) { - normal_wind = 0; - normal_wind |= uint32_t((normal.x * 0.5f + 0.5f) * 255.0f) << 0; - normal_wind |= uint32_t((normal.y * 0.5f + 0.5f) * 255.0f) << 8; - normal_wind |= uint32_t((normal.z * 0.5f + 0.5f) * 255.0f) << 16; - normal_wind |= uint32_t(wind) << 24; + n_x = uint8_t((normal.x * 0.5f + 0.5f) * 255.0f); + n_y = uint8_t((normal.y * 0.5f + 0.5f) * 255.0f); + n_z = uint8_t((normal.z * 0.5f + 0.5f) * 255.0f); + w = wind; } constexpr XMFLOAT3 GetNor_FULL() const { XMFLOAT3 nor_FULL(0, 0, 0); - - nor_FULL.x = (float((normal_wind >> 0) & 0xFF) / 255.0f) * 2.0f - 1.0f; - nor_FULL.y = (float((normal_wind >> 8) & 0xFF) / 255.0f) * 2.0f - 1.0f; - nor_FULL.z = (float((normal_wind >> 16) & 0xFF) / 255.0f) * 2.0f - 1.0f; - + nor_FULL.x = (float(n_x) / 255.0f) * 2.0f - 1.0f; + nor_FULL.y = (float(n_y) / 255.0f) * 2.0f - 1.0f; + nor_FULL.z = (float(n_z) / 255.0f) * 2.0f - 1.0f; return nor_FULL; } constexpr uint8_t GetWind() const { - return (normal_wind >> 24) & 0xFF; + return w; } static constexpr wi::graphics::Format FORMAT = wi::graphics::Format::R32G32B32A32_FLOAT; @@ -622,10 +621,10 @@ namespace wi::scene XMStoreFloat4(&t, T); t.w = tan.w; - x = int8_t(t.x * 127.0f); - y = int8_t(t.y * 127.0f); - z = int8_t(t.z * 127.0f); - w = int8_t(t.w * 127.0f); + x = int8_t(t.x * 127.5f); + y = int8_t(t.y * 127.5f); + z = int8_t(t.z * 127.5f); + w = int8_t(t.w * 127.5f); } static constexpr wi::graphics::Format FORMAT = wi::graphics::Format::R8G8B8A8_SNORM; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index fe38e7ae2..d13aa491c 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 262; + const int revision = 263; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);