Optimize terrain heightmap decoding to use shared code
This commit is contained in:
@@ -286,7 +286,7 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value)
|
||||
case 29:
|
||||
{
|
||||
Value inXY = tryGetValue(node->GetBox(0), Value::Zero).AsFloat2();
|
||||
value = writeLocal(ValueType::Float3, String::Format(TEXT("float3({0}, sqrt(saturate(1.0 - dot({0}.xy, {0}.xy))))"), inXY.Value), node);
|
||||
value = writeLocal(ValueType::Float3, String::Format(TEXT("float3({0}, sqrt(saturate(1.0 - dot({0}, {0}))))"), inXY.Value), node);
|
||||
break;
|
||||
}
|
||||
// Mad
|
||||
|
||||
@@ -5,28 +5,30 @@
|
||||
|
||||
#include "./Flax/Common.hlsl"
|
||||
|
||||
float DecodeHeightmapHeight(float4 value)
|
||||
{
|
||||
return (float)((int)(value.x * 255.0) + ((int)(value.y * 255) << 8)) / 65535.0;
|
||||
}
|
||||
|
||||
float3 DecodeHeightmapNormal(float4 value, out bool isHole)
|
||||
{
|
||||
isHole = (value.b + value.a) >= 1.9f;
|
||||
float2 normalTemp = float2(value.b, value.a) * 2.0f - 1.0f;
|
||||
float3 normal = float3(normalTemp.x, sqrt(1.0 - saturate(dot(normalTemp, normalTemp))), normalTemp.y);
|
||||
return normalize(normal);
|
||||
}
|
||||
|
||||
float SampleHeightmap(Texture2D<float4> heightmap, float2 uv, float mipOffset = 0.0f)
|
||||
{
|
||||
// Sample heightmap
|
||||
float4 value = heightmap.SampleLevel(SamplerPointClamp, uv, mipOffset);
|
||||
|
||||
// Decode heightmap
|
||||
float height = (float)((int)(value.x * 255.0) + ((int)(value.y * 255) << 8)) / 65535.0;
|
||||
return height;
|
||||
return DecodeHeightmapHeight(value);
|
||||
}
|
||||
|
||||
float SampleHeightmap(Texture2D<float4> heightmap, float2 uv, out float3 normal, out bool isHole, float mipOffset = 0.0f)
|
||||
{
|
||||
// Sample heightmap
|
||||
float4 value = heightmap.SampleLevel(SamplerPointClamp, uv, mipOffset);
|
||||
|
||||
// Decode heightmap
|
||||
float height = (float)((int)(value.x * 255.0) + ((int)(value.y * 255) << 8)) / 65535.0;
|
||||
float2 normalTemp = float2(value.b, value.a) * 2.0f - 1.0f;
|
||||
normal = float3(normalTemp.x, sqrt(1.0 - saturate(dot(normalTemp, normalTemp))), normalTemp.y);
|
||||
isHole = (value.b + value.a) >= 1.9f;
|
||||
normal = normalize(normal);
|
||||
return height;
|
||||
normal = DecodeHeightmapNormal(value, isHole);
|
||||
return DecodeHeightmapHeight(value);
|
||||
}
|
||||
|
||||
float3 SampleHeightmap(Texture2D<float4> heightmap, float3 localPosition, float4 localToUV, out float3 normal, out bool isHole, float mipOffset = 0.0f)
|
||||
@@ -36,12 +38,9 @@ float3 SampleHeightmap(Texture2D<float4> heightmap, float3 localPosition, float4
|
||||
float4 value = heightmap.SampleLevel(SamplerPointClamp, uv, mipOffset);
|
||||
|
||||
// Decode heightmap
|
||||
isHole = (value.b + value.a) >= 1.9f;
|
||||
float height = (float)((int)(value.x * 255.0) + ((int)(value.y * 255) << 8)) / 65535.0;
|
||||
normal = DecodeHeightmapNormal(value, isHole);
|
||||
float height = DecodeHeightmapHeight(value);;
|
||||
float3 position = float3(localPosition.x, height, localPosition.z);
|
||||
float2 normalTemp = float2(value.b, value.a) * 2.0f - 1.0f;
|
||||
normal = float3(normalTemp.x, sqrt(1.0 - saturate(dot(normalTemp, normalTemp))), normalTemp.y);
|
||||
normal = normalize(normal);
|
||||
|
||||
// UVs outside the heightmap are empty
|
||||
isHole = isHole || any(uv < 0.0f) || any(uv > 1.0f);
|
||||
|
||||
Reference in New Issue
Block a user