bumpFragmentFunctions.fx 4.0 KB

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