Advertisement
thureinfree

POM

May 1st, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Texture2D g_TextureMap : register(t0);
  2. SamplerState g_TextureSampler : register(s0);
  3.  
  4. Texture2D g_HeightMap : register(t1);
  5. SamplerState g_HeightSampler : register(s1);
  6.  
  7. cbuffer cb_POM : register(b0)
  8. {
  9. matrix g_WorldViewProj;
  10. float g_HeightScale;
  11. float g_NumLayers;
  12. float2 g_UVOffset;
  13. }
  14.  
  15. struct VS_INPUT
  16. {
  17. float4 Position : POSITION;
  18. float2 TexCoord : TEXCOORD0;
  19. };
  20.  
  21. struct PS_INPUT
  22. {
  23. float4 Position : SV_POSITION;
  24. float2 TexCoord : TEXCOORD0;
  25. };
  26.  
  27. PS_INPUT VSMain(VS_INPUT input)
  28. {
  29. PS_INPUT output = (PS_INPUT)0;
  30. output.Position = mul(input.Position, g_WorldViewProj);
  31. output.TexCoord = input.TexCoord;
  32. return output;
  33. }
  34.  
  35. float2 POMOffset(float2 texCoord, float2 viewDir, float height)
  36. {
  37. float2 offset = height * viewDir.xy / viewDir.z;
  38. float2 newTexCoord = texCoord - offset * g_UVOffset;
  39. return lerp(texCoord, newTexCoord, step(0.0, height));
  40. }
  41.  
  42. float POMHeight(float2 texCoord, float2 viewDir, float maxHeight)
  43. {
  44. float2 offset = viewDir.xy / viewDir.z * g_UVOffset;
  45. float height = 0.0;
  46. float layerDepth = maxHeight / g_NumLayers;
  47. float currentDepth = 0.0;
  48. float2 currentTexCoord = texCoord;
  49. for (int i = 0; i < g_NumLayers; i++)
  50. {
  51. height = texture(g_HeightMap, currentTexCoord).r * g_HeightScale;
  52. height = lerp(currentDepth, height, step(currentDepth, height));
  53. if (height > 0.0)
  54. {
  55. currentTexCoord = POMOffset(currentTexCoord, viewDir, height);
  56. currentDepth += layerDepth;
  57. }
  58. else
  59. {
  60. break;
  61. }
  62. }
  63. return currentDepth;
  64. }
  65.  
  66. float4 PSMain(PS_INPUT input) : SV_Target
  67. {
  68. float3 viewDir = normalize(float3(input.TexCoord - 0.5, 1.0));
  69. float height = POMHeight(input.TexCoord, viewDir.xy, 1.0);
  70. float2 texCoord = POMOffset(input.TexCoord, viewDir.xy, height);
  71. float4 color = texture(g_TextureMap, texCoord);
  72. return color;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement