SAOShader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ( function () {
  2. /**
  3. * TODO
  4. */
  5. const SAOShader = {
  6. defines: {
  7. 'NUM_SAMPLES': 7,
  8. 'NUM_RINGS': 4,
  9. 'NORMAL_TEXTURE': 0,
  10. 'DIFFUSE_TEXTURE': 0,
  11. 'DEPTH_PACKING': 1,
  12. 'PERSPECTIVE_CAMERA': 1
  13. },
  14. uniforms: {
  15. 'tDepth': {
  16. value: null
  17. },
  18. 'tDiffuse': {
  19. value: null
  20. },
  21. 'tNormal': {
  22. value: null
  23. },
  24. 'size': {
  25. value: new THREE.Vector2( 512, 512 )
  26. },
  27. 'cameraNear': {
  28. value: 1
  29. },
  30. 'cameraFar': {
  31. value: 100
  32. },
  33. 'cameraProjectionMatrix': {
  34. value: new THREE.Matrix4()
  35. },
  36. 'cameraInverseProjectionMatrix': {
  37. value: new THREE.Matrix4()
  38. },
  39. 'scale': {
  40. value: 1.0
  41. },
  42. 'intensity': {
  43. value: 0.1
  44. },
  45. 'bias': {
  46. value: 0.5
  47. },
  48. 'minResolution': {
  49. value: 0.0
  50. },
  51. 'kernelRadius': {
  52. value: 100.0
  53. },
  54. 'randomSeed': {
  55. value: 0.0
  56. }
  57. },
  58. vertexShader:
  59. /* glsl */
  60. `
  61. varying vec2 vUv;
  62. void main() {
  63. vUv = uv;
  64. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  65. }`,
  66. fragmentShader:
  67. /* glsl */
  68. `
  69. #include <common>
  70. varying vec2 vUv;
  71. #if DIFFUSE_TEXTURE == 1
  72. uniform sampler2D tDiffuse;
  73. #endif
  74. uniform sampler2D tDepth;
  75. #if NORMAL_TEXTURE == 1
  76. uniform sampler2D tNormal;
  77. #endif
  78. uniform float cameraNear;
  79. uniform float cameraFar;
  80. uniform mat4 cameraProjectionMatrix;
  81. uniform mat4 cameraInverseProjectionMatrix;
  82. uniform float scale;
  83. uniform float intensity;
  84. uniform float bias;
  85. uniform float kernelRadius;
  86. uniform float minResolution;
  87. uniform vec2 size;
  88. uniform float randomSeed;
  89. // RGBA depth
  90. #include <packing>
  91. vec4 getDefaultColor( const in vec2 screenPosition ) {
  92. #if DIFFUSE_TEXTURE == 1
  93. return texture2D( tDiffuse, vUv );
  94. #else
  95. return vec4( 1.0 );
  96. #endif
  97. }
  98. float getDepth( const in vec2 screenPosition ) {
  99. #if DEPTH_PACKING == 1
  100. return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );
  101. #else
  102. return texture2D( tDepth, screenPosition ).x;
  103. #endif
  104. }
  105. float getViewZ( const in float depth ) {
  106. #if PERSPECTIVE_CAMERA == 1
  107. return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
  108. #else
  109. return orthographicDepthToViewZ( depth, cameraNear, cameraFar );
  110. #endif
  111. }
  112. vec3 getViewPosition( const in vec2 screenPosition, const in float depth, const in float viewZ ) {
  113. float clipW = cameraProjectionMatrix[2][3] * viewZ + cameraProjectionMatrix[3][3];
  114. vec4 clipPosition = vec4( ( vec3( screenPosition, depth ) - 0.5 ) * 2.0, 1.0 );
  115. clipPosition *= clipW; // unprojection.
  116. return ( cameraInverseProjectionMatrix * clipPosition ).xyz;
  117. }
  118. vec3 getViewNormal( const in vec3 viewPosition, const in vec2 screenPosition ) {
  119. #if NORMAL_TEXTURE == 1
  120. return unpackRGBToNormal( texture2D( tNormal, screenPosition ).xyz );
  121. #else
  122. return normalize( cross( dFdx( viewPosition ), dFdy( viewPosition ) ) );
  123. #endif
  124. }
  125. float scaleDividedByCameraFar;
  126. float minResolutionMultipliedByCameraFar;
  127. float getOcclusion( const in vec3 centerViewPosition, const in vec3 centerViewNormal, const in vec3 sampleViewPosition ) {
  128. vec3 viewDelta = sampleViewPosition - centerViewPosition;
  129. float viewDistance = length( viewDelta );
  130. float scaledScreenDistance = scaleDividedByCameraFar * viewDistance;
  131. return max(0.0, (dot(centerViewNormal, viewDelta) - minResolutionMultipliedByCameraFar) / scaledScreenDistance - bias) / (1.0 + pow2( scaledScreenDistance ) );
  132. }
  133. // moving costly divides into consts
  134. const float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );
  135. const float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );
  136. float getAmbientOcclusion( const in vec3 centerViewPosition ) {
  137. // precompute some variables require in getOcclusion.
  138. scaleDividedByCameraFar = scale / cameraFar;
  139. minResolutionMultipliedByCameraFar = minResolution * cameraFar;
  140. vec3 centerViewNormal = getViewNormal( centerViewPosition, vUv );
  141. // jsfiddle that shows sample pattern: https://jsfiddle.net/a16ff1p7/
  142. float angle = rand( vUv + randomSeed ) * PI2;
  143. vec2 radius = vec2( kernelRadius * INV_NUM_SAMPLES ) / size;
  144. vec2 radiusStep = radius;
  145. float occlusionSum = 0.0;
  146. float weightSum = 0.0;
  147. for( int i = 0; i < NUM_SAMPLES; i ++ ) {
  148. vec2 sampleUv = vUv + vec2( cos( angle ), sin( angle ) ) * radius;
  149. radius += radiusStep;
  150. angle += ANGLE_STEP;
  151. float sampleDepth = getDepth( sampleUv );
  152. if( sampleDepth >= ( 1.0 - EPSILON ) ) {
  153. continue;
  154. }
  155. float sampleViewZ = getViewZ( sampleDepth );
  156. vec3 sampleViewPosition = getViewPosition( sampleUv, sampleDepth, sampleViewZ );
  157. occlusionSum += getOcclusion( centerViewPosition, centerViewNormal, sampleViewPosition );
  158. weightSum += 1.0;
  159. }
  160. if( weightSum == 0.0 ) discard;
  161. return occlusionSum * ( intensity / weightSum );
  162. }
  163. void main() {
  164. float centerDepth = getDepth( vUv );
  165. if( centerDepth >= ( 1.0 - EPSILON ) ) {
  166. discard;
  167. }
  168. float centerViewZ = getViewZ( centerDepth );
  169. vec3 viewPosition = getViewPosition( vUv, centerDepth, centerViewZ );
  170. float ambientOcclusion = getAmbientOcclusion( viewPosition );
  171. gl_FragColor = getDefaultColor( vUv );
  172. gl_FragColor.xyz *= 1.0 - ambientOcclusion;
  173. }`
  174. };
  175. THREE.SAOShader = SAOShader;
  176. } )();