Add RenderTools::CalculateTangentFrame utility

This commit is contained in:
Wojtek Figat
2023-07-16 11:57:21 +02:00
parent 2645b69ec0
commit f0496d53e8
4 changed files with 43 additions and 55 deletions

View File

@@ -10,6 +10,7 @@
#include "Engine/Content/Assets/Model.h"
#include "Engine/Content/Assets/SkinnedModel.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Math/Packed.h"
#include "Engine/Engine/Time.h"
const Char* ToString(RendererType value)
@@ -538,6 +539,33 @@ void RenderTools::ComputeCascadeUpdateFrequency(int32 cascadeIndex, int32 cascad
}
}
void RenderTools::CalculateTangentFrame(FloatR10G10B10A2& resultNormal, FloatR10G10B10A2& resultTangent, const Float3& normal)
{
// Calculate tangent
const Float3 c1 = Float3::Cross(normal, Float3::UnitZ);
const Float3 c2 = Float3::Cross(normal, Float3::UnitY);
const Float3 tangent = c1.LengthSquared() > c2.LengthSquared() ? c1 : c2;
// Calculate bitangent sign
const Float3 bitangent = Float3::Normalize(Float3::Cross(normal, tangent));
const byte sign = static_cast<byte>(Float3::Dot(Float3::Cross(bitangent, normal), tangent) < 0.0f ? 1 : 0);
// Set tangent frame
resultNormal = Float1010102(normal * 0.5f + 0.5f, 0);
resultTangent = Float1010102(tangent * 0.5f + 0.5f, sign);
}
void RenderTools::CalculateTangentFrame(FloatR10G10B10A2& resultNormal, FloatR10G10B10A2& resultTangent, const Float3& normal, const Float3& tangent)
{
// Calculate bitangent sign
const Float3 bitangent = Float3::Normalize(Float3::Cross(normal, tangent));
const byte sign = static_cast<byte>(Float3::Dot(Float3::Cross(bitangent, normal), tangent) < 0.0f ? 1 : 0);
// Set tangent frame
resultNormal = Float1010102(normal * 0.5f + 0.5f, 0);
resultTangent = Float1010102(tangent * 0.5f + 0.5f, sign);
}
int32 MipLevelsCount(int32 width, bool useMipLevels)
{
if (!useMipLevels)