ParallaxShader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Parallax Occlusion shaders from
  2. // http://sunandblackcat.com/tipFullView.php?topicid=28
  3. // No tangent-space transforms logic based on
  4. // http://mmikkelsen3d.blogspot.sk/2012/02/parallaxpoc-mapping-and-no-tangent.html
  5. const ParallaxShader = {
  6. // Ordered from fastest to best quality.
  7. modes: {
  8. none: 'NO_PARALLAX',
  9. basic: 'USE_BASIC_PARALLAX',
  10. steep: 'USE_STEEP_PARALLAX',
  11. occlusion: 'USE_OCLUSION_PARALLAX', // a.k.a. POM
  12. relief: 'USE_RELIEF_PARALLAX'
  13. },
  14. uniforms: {
  15. 'bumpMap': { value: null },
  16. 'map': { value: null },
  17. 'parallaxScale': { value: null },
  18. 'parallaxMinLayers': { value: null },
  19. 'parallaxMaxLayers': { value: null }
  20. },
  21. vertexShader: /* glsl */`
  22. varying vec2 vUv;
  23. varying vec3 vViewPosition;
  24. varying vec3 vNormal;
  25. void main() {
  26. vUv = uv;
  27. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  28. vViewPosition = -mvPosition.xyz;
  29. vNormal = normalize( normalMatrix * normal );
  30. gl_Position = projectionMatrix * mvPosition;
  31. }`,
  32. fragmentShader: /* glsl */`
  33. uniform sampler2D bumpMap;
  34. uniform sampler2D map;
  35. uniform float parallaxScale;
  36. uniform float parallaxMinLayers;
  37. uniform float parallaxMaxLayers;
  38. varying vec2 vUv;
  39. varying vec3 vViewPosition;
  40. varying vec3 vNormal;
  41. #ifdef USE_BASIC_PARALLAX
  42. vec2 parallaxMap( in vec3 V ) {
  43. float initialHeight = texture2D( bumpMap, vUv ).r;
  44. // No Offset Limitting: messy, floating output at grazing angles.
  45. //"vec2 texCoordOffset = parallaxScale * V.xy / V.z * initialHeight;",
  46. // Offset Limiting
  47. vec2 texCoordOffset = parallaxScale * V.xy * initialHeight;
  48. return vUv - texCoordOffset;
  49. }
  50. #else
  51. vec2 parallaxMap( in vec3 V ) {
  52. // Determine number of layers from angle between V and N
  53. float numLayers = mix( parallaxMaxLayers, parallaxMinLayers, abs( dot( vec3( 0.0, 0.0, 1.0 ), V ) ) );
  54. float layerHeight = 1.0 / numLayers;
  55. float currentLayerHeight = 0.0;
  56. // Shift of texture coordinates for each iteration
  57. vec2 dtex = parallaxScale * V.xy / V.z / numLayers;
  58. vec2 currentTextureCoords = vUv;
  59. float heightFromTexture = texture2D( bumpMap, currentTextureCoords ).r;
  60. // while ( heightFromTexture > currentLayerHeight )
  61. // Infinite loops are not well supported. Do a "large" finite
  62. // loop, but not too large, as it slows down some compilers.
  63. for ( int i = 0; i < 30; i += 1 ) {
  64. if ( heightFromTexture <= currentLayerHeight ) {
  65. break;
  66. }
  67. currentLayerHeight += layerHeight;
  68. // Shift texture coordinates along vector V
  69. currentTextureCoords -= dtex;
  70. heightFromTexture = texture2D( bumpMap, currentTextureCoords ).r;
  71. }
  72. #ifdef USE_STEEP_PARALLAX
  73. return currentTextureCoords;
  74. #elif defined( USE_RELIEF_PARALLAX )
  75. vec2 deltaTexCoord = dtex / 2.0;
  76. float deltaHeight = layerHeight / 2.0;
  77. // Return to the mid point of previous layer
  78. currentTextureCoords += deltaTexCoord;
  79. currentLayerHeight -= deltaHeight;
  80. // Binary search to increase precision of Steep Parallax Mapping
  81. const int numSearches = 5;
  82. for ( int i = 0; i < numSearches; i += 1 ) {
  83. deltaTexCoord /= 2.0;
  84. deltaHeight /= 2.0;
  85. heightFromTexture = texture2D( bumpMap, currentTextureCoords ).r;
  86. // Shift along or against vector V
  87. if( heightFromTexture > currentLayerHeight ) { // Below the surface
  88. currentTextureCoords -= deltaTexCoord;
  89. currentLayerHeight += deltaHeight;
  90. } else { // above the surface
  91. currentTextureCoords += deltaTexCoord;
  92. currentLayerHeight -= deltaHeight;
  93. }
  94. }
  95. return currentTextureCoords;
  96. #elif defined( USE_OCLUSION_PARALLAX )
  97. vec2 prevTCoords = currentTextureCoords + dtex;
  98. // Heights for linear interpolation
  99. float nextH = heightFromTexture - currentLayerHeight;
  100. float prevH = texture2D( bumpMap, prevTCoords ).r - currentLayerHeight + layerHeight;
  101. // Proportions for linear interpolation
  102. float weight = nextH / ( nextH - prevH );
  103. // Interpolation of texture coordinates
  104. return prevTCoords * weight + currentTextureCoords * ( 1.0 - weight );
  105. #else // NO_PARALLAX
  106. return vUv;
  107. #endif
  108. }
  109. #endif
  110. vec2 perturbUv( vec3 surfPosition, vec3 surfNormal, vec3 viewPosition ) {
  111. vec2 texDx = dFdx( vUv );
  112. vec2 texDy = dFdy( vUv );
  113. vec3 vSigmaX = dFdx( surfPosition );
  114. vec3 vSigmaY = dFdy( surfPosition );
  115. vec3 vR1 = cross( vSigmaY, surfNormal );
  116. vec3 vR2 = cross( surfNormal, vSigmaX );
  117. float fDet = dot( vSigmaX, vR1 );
  118. vec2 vProjVscr = ( 1.0 / fDet ) * vec2( dot( vR1, viewPosition ), dot( vR2, viewPosition ) );
  119. vec3 vProjVtex;
  120. vProjVtex.xy = texDx * vProjVscr.x + texDy * vProjVscr.y;
  121. vProjVtex.z = dot( surfNormal, viewPosition );
  122. return parallaxMap( vProjVtex );
  123. }
  124. void main() {
  125. vec2 mapUv = perturbUv( -vViewPosition, normalize( vNormal ), normalize( vViewPosition ) );
  126. gl_FragColor = texture2D( map, mapUv );
  127. }`
  128. };
  129. export { ParallaxShader };