bumpFragmentFunctions.fx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifdef BUMP
  2. #if BUMPDIRECTUV == 1
  3. #define vBumpUV vMainUV1
  4. #elif BUMPDIRECTUV == 2
  5. #define vBumpUV vMainUV2
  6. #else
  7. varying vec2 vBumpUV;
  8. #endif
  9. uniform sampler2D bumpSampler;
  10. #if defined(TANGENT) && defined(NORMAL)
  11. varying mat3 vTBN;
  12. #endif
  13. // Thanks to http://www.thetenthplanet.de/archives/1180
  14. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  15. {
  16. // flip the uv for the backface
  17. uv = gl_FrontFacing ? uv : -uv;
  18. // get edge vectors of the pixel triangle
  19. vec3 dp1 = dFdx(p);
  20. vec3 dp2 = dFdy(p);
  21. vec2 duv1 = dFdx(uv);
  22. vec2 duv2 = dFdy(uv);
  23. // solve the linear system
  24. vec3 dp2perp = cross(dp2, normal);
  25. vec3 dp1perp = cross(normal, dp1);
  26. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  27. vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y;
  28. // invert the tangent/bitangent if requested
  29. tangent *= vTangentSpaceParams.x;
  30. bitangent *= vTangentSpaceParams.y;
  31. // construct a scale-invariant frame
  32. float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent)));
  33. return mat3(tangent * invmax, bitangent * invmax, normal);
  34. }
  35. vec3 perturbNormal(mat3 cotangentFrame, vec2 uv)
  36. {
  37. vec3 map = texture2D(bumpSampler, uv).xyz;
  38. map = map * 255. / 127. - 128. / 127.;
  39. #ifdef NORMALXYSCALE
  40. map = normalize(map * vec3(vBumpInfos.y, vBumpInfos.y, 1.0));
  41. #endif
  42. return normalize(cotangentFrame * map);
  43. }
  44. #ifdef PARALLAX
  45. const float minSamples = 4.;
  46. const float maxSamples = 15.;
  47. const int iMaxSamples = 15;
  48. // http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262
  49. vec2 parallaxOcclusion(vec3 vViewDirCoT, vec3 vNormalCoT, vec2 texCoord, float parallaxScale) {
  50. float parallaxLimit = length(vViewDirCoT.xy) / vViewDirCoT.z;
  51. parallaxLimit *= parallaxScale;
  52. vec2 vOffsetDir = normalize(vViewDirCoT.xy);
  53. vec2 vMaxOffset = vOffsetDir * parallaxLimit;
  54. float numSamples = maxSamples + (dot(vViewDirCoT, vNormalCoT) * (minSamples - maxSamples));
  55. float stepSize = 1.0 / numSamples;
  56. // Initialize the starting view ray height and the texture offsets.
  57. float currRayHeight = 1.0;
  58. vec2 vCurrOffset = vec2(0, 0);
  59. vec2 vLastOffset = vec2(0, 0);
  60. float lastSampledHeight = 1.0;
  61. float currSampledHeight = 1.0;
  62. for (int i = 0; i < iMaxSamples; i++)
  63. {
  64. currSampledHeight = texture2D(bumpSampler, vBumpUV + vCurrOffset).w;
  65. // Test if the view ray has intersected the surface.
  66. if (currSampledHeight > currRayHeight)
  67. {
  68. float delta1 = currSampledHeight - currRayHeight;
  69. float delta2 = (currRayHeight + stepSize) - lastSampledHeight;
  70. float ratio = delta1 / (delta1 + delta2);
  71. vCurrOffset = (ratio)* vLastOffset + (1.0 - ratio) * vCurrOffset;
  72. // Force the exit of the loop
  73. break;
  74. }
  75. else
  76. {
  77. currRayHeight -= stepSize;
  78. vLastOffset = vCurrOffset;
  79. vCurrOffset += stepSize * vMaxOffset;
  80. lastSampledHeight = currSampledHeight;
  81. }
  82. }
  83. return vCurrOffset;
  84. }
  85. vec2 parallaxOffset(vec3 viewDir, float heightScale)
  86. {
  87. // calculate amount of offset for Parallax Mapping With Offset Limiting
  88. float height = texture2D(bumpSampler, vBumpUV).w;
  89. vec2 texCoordOffset = heightScale * viewDir.xy * height;
  90. return -texCoordOffset;
  91. }
  92. #endif
  93. #endif