ParallaxShader.js 5.0 KB

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