Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Texture2D g_TextureMap : register(t0);
- SamplerState g_TextureSampler : register(s0);
- Texture2D g_HeightMap : register(t1);
- SamplerState g_HeightSampler : register(s1);
- cbuffer cb_POM : register(b0)
- {
- matrix g_WorldViewProj;
- float g_HeightScale;
- float g_NumLayers;
- float2 g_UVOffset;
- }
- struct VS_INPUT
- {
- float4 Position : POSITION;
- float2 TexCoord : TEXCOORD0;
- };
- struct PS_INPUT
- {
- float4 Position : SV_POSITION;
- float2 TexCoord : TEXCOORD0;
- };
- PS_INPUT VSMain(VS_INPUT input)
- {
- PS_INPUT output = (PS_INPUT)0;
- output.Position = mul(input.Position, g_WorldViewProj);
- output.TexCoord = input.TexCoord;
- return output;
- }
- float2 POMOffset(float2 texCoord, float2 viewDir, float height)
- {
- float2 offset = height * viewDir.xy / viewDir.z;
- float2 newTexCoord = texCoord - offset * g_UVOffset;
- return lerp(texCoord, newTexCoord, step(0.0, height));
- }
- float POMHeight(float2 texCoord, float2 viewDir, float maxHeight)
- {
- float2 offset = viewDir.xy / viewDir.z * g_UVOffset;
- float height = 0.0;
- float layerDepth = maxHeight / g_NumLayers;
- float currentDepth = 0.0;
- float2 currentTexCoord = texCoord;
- for (int i = 0; i < g_NumLayers; i++)
- {
- height = texture(g_HeightMap, currentTexCoord).r * g_HeightScale;
- height = lerp(currentDepth, height, step(currentDepth, height));
- if (height > 0.0)
- {
- currentTexCoord = POMOffset(currentTexCoord, viewDir, height);
- currentDepth += layerDepth;
- }
- else
- {
- break;
- }
- }
- return currentDepth;
- }
- float4 PSMain(PS_INPUT input) : SV_Target
- {
- float3 viewDir = normalize(float3(input.TexCoord - 0.5, 1.0));
- float height = POMHeight(input.TexCoord, viewDir.xy, 1.0);
- float2 texCoord = POMOffset(input.TexCoord, viewDir.xy, height);
- float4 color = texture(g_TextureMap, texCoord);
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement