PerPixelDisplacement.hlsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // This is implementation of parallax occlusion mapping (POM)
  2. // This function require that the caller define a callback for the height sampling name ComputePerPixelHeightDisplacement
  3. // A PerPixelHeightDisplacementParam is used to provide all data necessary to calculate the heights to ComputePerPixelHeightDisplacement it doesn't need to be
  4. // visible by the POM algorithm.
  5. // This function is compatible with tiled uv.
  6. // it return the offset to apply to the UVSet provide in PerPixelHeightDisplacementParam
  7. // viewDirTS is view vector in texture space matching the UVSet
  8. // ref: https://www.gamedev.net/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262
  9. real2 ParallaxOcclusionMapping(real lod, real lodThreshold, int numSteps, real3 viewDirTS, PerPixelHeightDisplacementParam ppdParam, out real outHeight)
  10. {
  11. // Convention: 1.0 is top, 0.0 is bottom - POM is always inward, no extrusion
  12. real stepSize = 1.0 / (real)numSteps;
  13. // View vector is from the point to the camera, but we want to raymarch from camera to point, so reverse the sign
  14. // The length of viewDirTS vector determines the furthest amount of displacement:
  15. // real parallaxLimit = -length(viewDirTS.xy) / viewDirTS.z;
  16. // real2 parallaxDir = normalize(Out.viewDirTS.xy);
  17. // real2 parallaxMaxOffsetTS = parallaxDir * parallaxLimit;
  18. // Above code simplify to
  19. real2 parallaxMaxOffsetTS = (viewDirTS.xy / -viewDirTS.z);
  20. real2 texOffsetPerStep = stepSize * parallaxMaxOffsetTS;
  21. // Do a first step before the loop to init all value correctly
  22. real2 texOffsetCurrent = real2(0.0, 0.0);
  23. real prevHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
  24. texOffsetCurrent += texOffsetPerStep;
  25. real currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
  26. real rayHeight = 1.0 - stepSize; // Start at top less one sample
  27. // Linear search
  28. for (int stepIndex = 0; stepIndex < numSteps; ++stepIndex)
  29. {
  30. // Have we found a height below our ray height ? then we have an intersection
  31. if (currHeight > rayHeight)
  32. break; // end the loop
  33. prevHeight = currHeight;
  34. rayHeight -= stepSize;
  35. texOffsetCurrent += texOffsetPerStep;
  36. // Sample height map which in this case is stored in the alpha channel of the normal map:
  37. currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
  38. }
  39. // Found below and above points, now perform line interesection (ray) with piecewise linear heightfield approximation
  40. // Refine the search with secant method
  41. #define POM_SECANT_METHOD 1
  42. #if POM_SECANT_METHOD
  43. real pt0 = rayHeight + stepSize;
  44. real pt1 = rayHeight;
  45. real delta0 = pt0 - prevHeight;
  46. real delta1 = pt1 - currHeight;
  47. real delta;
  48. real2 offset;
  49. // Secant method to affine the search
  50. // Ref: Faster Relief Mapping Using the Secant Method - Eric Risser
  51. for (int i = 0; i < 3; ++i)
  52. {
  53. // intersectionHeight is the height [0..1] for the intersection between view ray and heightfield line
  54. real intersectionHeight = (pt0 * delta1 - pt1 * delta0) / (delta1 - delta0);
  55. // Retrieve offset require to find this intersectionHeight
  56. offset = (1 - intersectionHeight) * texOffsetPerStep * numSteps;
  57. currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam);
  58. delta = intersectionHeight - currHeight;
  59. if (abs(delta) <= 0.01)
  60. break;
  61. // intersectionHeight < currHeight => new lower bounds
  62. if (delta < 0.0)
  63. {
  64. delta1 = delta;
  65. pt1 = intersectionHeight;
  66. }
  67. else
  68. {
  69. delta0 = delta;
  70. pt0 = intersectionHeight;
  71. }
  72. }
  73. #else // regular POM intersection
  74. //real pt0 = rayHeight + stepSize;
  75. //real pt1 = rayHeight;
  76. //real delta0 = pt0 - prevHeight;
  77. //real delta1 = pt1 - currHeight;
  78. //real intersectionHeight = (pt0 * delta1 - pt1 * delta0) / (delta1 - delta0);
  79. //real2 offset = (1 - intersectionHeight) * texOffsetPerStep * numSteps;
  80. // A bit more optimize
  81. real delta0 = currHeight - rayHeight;
  82. real delta1 = (rayHeight + stepSize) - prevHeight;
  83. real ratio = delta0 / (delta0 + delta1);
  84. real2 offset = texOffsetCurrent - ratio * texOffsetPerStep;
  85. currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam);
  86. #endif
  87. outHeight = currHeight;
  88. // Fade the effect with lod (allow to avoid pop when switching to a discrete LOD mesh)
  89. offset *= (1.0 - saturate(lod - lodThreshold));
  90. return offset;
  91. }