bumpFragmentFunctions.fx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifdef BUMP
  2. varying vec2 vBumpUV;
  3. uniform vec2 vBumpInfos;
  4. uniform sampler2D bumpSampler;
  5. // Thanks to http://www.thetenthplanet.de/archives/1180
  6. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  7. {
  8. // get edge vectors of the pixel triangle
  9. vec3 dp1 = dFdx(p);
  10. vec3 dp2 = dFdy(p);
  11. vec2 duv1 = dFdx(uv);
  12. vec2 duv2 = dFdy(uv);
  13. // solve the linear system
  14. vec3 dp2perp = cross(dp2, normal);
  15. vec3 dp1perp = cross(normal, dp1);
  16. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  17. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  18. // construct a scale-invariant frame
  19. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  20. return mat3(tangent * invmax, binormal * invmax, normal);
  21. }
  22. vec3 perturbNormal(vec3 viewDir, mat3 cotangentFrame, vec2 uv)
  23. {
  24. vec3 map = texture2D(bumpSampler, uv).xyz;
  25. map = map * 255. / 127. - 128. / 127.;
  26. return normalize(cotangentFrame * map);
  27. }
  28. #ifdef PARALLAX
  29. uniform float vParallaxScaleBias;
  30. const float minSamples = 4.;
  31. const float maxSamples = 15.;
  32. const int iMaxSamples = 15;
  33. // http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262
  34. vec2 parallaxOcclusion(vec3 vViewDirCoT, vec3 vNormalCoT, vec2 texCoord, float parallaxScale) {
  35. // Calculate the parallax offset vector max length.
  36. // This is equivalent to the tangent of the angle between the
  37. // viewer position and the fragment location.
  38. float parallaxLimit = length(vViewDirCoT.xy) / vViewDirCoT.z;
  39. // Scale the parallax limit according to heightmap scale.
  40. parallaxLimit *= parallaxScale;
  41. // Calculate the parallax offset vector direction and maximum offset.
  42. vec2 vOffsetDir = normalize(vViewDirCoT.xy);
  43. vec2 vMaxOffset = vOffsetDir * parallaxLimit;
  44. // Calculate how many samples should be taken along the view ray
  45. // to find the surface intersection. This is based on the angle
  46. // between the surface normal and the view vector.
  47. float numSamples = maxSamples + (dot(vViewDirCoT, vNormalCoT) * (minSamples - maxSamples));
  48. // Specify the view ray step size. Each sample will shift the current
  49. // view ray by this amount.
  50. float stepSize = 1.0 / numSamples;
  51. // Calculate the texture coordinate partial derivatives in screen
  52. // space for the tex2Dgrad texture sampling instruction.
  53. //vec2 dx = dFdx(vBumpUV); // Uncomment whevener GL_EXT_shader_texture_lod with texture2DGradEXT will work
  54. //vec2 dy = dFdy(vBumpUV);
  55. // Initialize the starting view ray height and the texture offsets.
  56. float currRayHeight = 1.0;
  57. vec2 vCurrOffset = vec2(0, 0);
  58. vec2 vLastOffset = vec2(0, 0);
  59. float lastSampledHeight = 1.0;
  60. float currSampledHeight = 1.0;
  61. for (int i = 0; i < iMaxSamples; i++)
  62. {
  63. // Sample the heightmap at the current texcoord offset. The heightmap
  64. // is stored in the alpha channel of the height/normal map.
  65. currSampledHeight = texture2D(bumpSampler, vBumpUV + vCurrOffset).w;
  66. // Uncomment whevener GL_EXT_shader_texture_lod with texture2DGradEXT will work
  67. //currSampledHeight = texture2DGradEXT(bumpSampler, vBumpUV + vCurrOffset, dx, dy).w;
  68. // Test if the view ray has intersected the surface.
  69. if (currSampledHeight > currRayHeight)
  70. {
  71. // Find the relative height delta before and after the intersection.
  72. // This provides a measure of how close the intersection is to
  73. // the final sample location.
  74. float delta1 = currSampledHeight - currRayHeight;
  75. float delta2 = (currRayHeight + stepSize) - lastSampledHeight;
  76. float ratio = delta1 / (delta1 + delta2);
  77. // Interpolate between the final two segments to
  78. // find the true intersection point offset.
  79. vCurrOffset = (ratio)* vLastOffset + (1.0 - ratio) * vCurrOffset;
  80. // Force the exit of the loop
  81. break;
  82. }
  83. else
  84. {
  85. // take the next view ray height step,
  86. currRayHeight -= stepSize;
  87. // save the current texture coordinate offset and increment
  88. // to the next sample location,
  89. vLastOffset = vCurrOffset;
  90. vCurrOffset += stepSize * vMaxOffset;
  91. // and finally save the current heightmap height.
  92. lastSampledHeight = currSampledHeight;
  93. }
  94. }
  95. return vCurrOffset;
  96. }
  97. vec2 parallaxOffset(vec3 viewDir, float heightScale)
  98. {
  99. // calculate amount of offset for Parallax Mapping With Offset Limiting
  100. float height = texture2D(bumpSampler, vBumpUV).w;
  101. vec2 texCoordOffset = heightScale * viewDir.xy * height;
  102. return -texCoordOffset;
  103. }
  104. #endif
  105. #endif