Skip to content

Instantly share code, notes, and snippets.

@equalent
Last active June 12, 2021 18:24
Show Gist options
  • Select an option

  • Save equalent/febc1128f0e85b2a39a723e4df4ed6b5 to your computer and use it in GitHub Desktop.

Select an option

Save equalent/febc1128f0e85b2a39a723e4df4ed6b5 to your computer and use it in GitHub Desktop.
Unpack a normal map. Use with BC5-encoded normal map to reconstruct Z and remap the values to [-1, 1] range
/*
A BC5 normal map texture visualization shader for RenderDoc.
Usage:
1. Save this to %APPDATA%/qrenderdoc/ on Windows or ~/.local/share/qrenderdoc elsewhere
2. Set Channels mode to Custom in RenderDoc Texture Viewer
3. Select the shader from the dropdown menu
*/
SamplerState pointSampler : register(s0);
Texture2DArray<float4> texDisplayTex2DArray : register(t2);
float4 unpackNrmMap(float4 sampledMap)
{
float2 xy = sampledMap.rg * 2.0f - float2(1.0f, 1.0f);
float z = sqrt(saturate(1.0f - dot(xy, xy)));
return float4(xy.xy, z, 1.0f);
}
float4 main(float4 pos : SV_Position, float4 uv : TEXCOORD0) : SV_Target0
{
float4 col = texDisplayTex2DArray.Sample(pointSampler, float3(uv.xy, 0), 0);
return (unpackNrmMap(col) + float4(1.0f, 1.0f, 1.0f, 0.0f)) * float4(0.5f, 0.5f, 0.5f, 1.0f);
}
float4 unpackNrmMap(float4 sampledMap)
{
float2 xy = sampledMap.rg * 2.0f - float2(1.0f, 1.0f);
float z = sqrt(saturate(1.0f - dot(xy, xy)));
return float4(xy.xy, z, 1.0f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment