SAOShader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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: /* glsl */`
  59. varying vec2 vUv;
  60. void main() {
  61. vUv = uv;
  62. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  63. }`,
  64. fragmentShader: /* glsl */`
  65. #include <common>
  66. varying vec2 vUv;
  67. #if DIFFUSE_TEXTURE == 1
  68. uniform sampler2D tDiffuse;
  69. #endif
  70. uniform sampler2D tDepth;
  71. #if NORMAL_TEXTURE == 1
  72. uniform sampler2D tNormal;
  73. #endif
  74. uniform float cameraNear;
  75. uniform float cameraFar;
  76. uniform mat4 cameraProjectionMatrix;
  77. uniform mat4 cameraInverseProjectionMatrix;
  78. uniform float scale;
  79. uniform float intensity;
  80. uniform float bias;
  81. uniform float kernelRadius;
  82. uniform float minResolution;
  83. uniform vec2 size;
  84. uniform float randomSeed;
  85. // RGBA depth
  86. #include <packing>
  87. vec4 getDefaultColor( const in vec2 screenPosition ) {
  88. #if DIFFUSE_TEXTURE == 1
  89. return texture2D( tDiffuse, vUv );
  90. #else
  91. return vec4( 1.0 );
  92. #endif
  93. }
  94. float getDepth( const in vec2 screenPosition ) {
  95. #if DEPTH_PACKING == 1
  96. return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );
  97. #else
  98. return texture2D( tDepth, screenPosition ).x;
  99. #endif
  100. }
  101. float getViewZ( const in float depth ) {
  102. #if PERSPECTIVE_CAMERA == 1
  103. return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
  104. #else
  105. return orthographicDepthToViewZ( depth, cameraNear, cameraFar );
  106. #endif
  107. }
  108. vec3 getViewPosition( const in vec2 screenPosition, const in float depth, const in float viewZ ) {
  109. float clipW = cameraProjectionMatrix[2][3] * viewZ + cameraProjectionMatrix[3][3];
  110. vec4 clipPosition = vec4( ( vec3( screenPosition, depth ) - 0.5 ) * 2.0, 1.0 );
  111. clipPosition *= clipW; // unprojection.
  112. return ( cameraInverseProjectionMatrix * clipPosition ).xyz;
  113. }
  114. vec3 getViewNormal( const in vec3 viewPosition, const in vec2 screenPosition ) {
  115. #if NORMAL_TEXTURE == 1
  116. return unpackRGBToNormal( texture2D( tNormal, screenPosition ).xyz );
  117. #else
  118. return normalize( cross( dFdx( viewPosition ), dFdy( viewPosition ) ) );
  119. #endif
  120. }
  121. float scaleDividedByCameraFar;
  122. float minResolutionMultipliedByCameraFar;
  123. float getOcclusion( const in vec3 centerViewPosition, const in vec3 centerViewNormal, const in vec3 sampleViewPosition ) {
  124. vec3 viewDelta = sampleViewPosition - centerViewPosition;
  125. float viewDistance = length( viewDelta );
  126. float scaledScreenDistance = scaleDividedByCameraFar * viewDistance;
  127. return max(0.0, (dot(centerViewNormal, viewDelta) - minResolutionMultipliedByCameraFar) / scaledScreenDistance - bias) / (1.0 + pow2( scaledScreenDistance ) );
  128. }
  129. // moving costly divides into consts
  130. const float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );
  131. const float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );
  132. float getAmbientOcclusion( const in vec3 centerViewPosition ) {
  133. // precompute some variables require in getOcclusion.
  134. scaleDividedByCameraFar = scale / cameraFar;
  135. minResolutionMultipliedByCameraFar = minResolution * cameraFar;
  136. vec3 centerViewNormal = getViewNormal( centerViewPosition, vUv );
  137. // jsfiddle that shows sample pattern: https://jsfiddle.net/a16ff1p7/
  138. float angle = rand( vUv + randomSeed ) * PI2;
  139. vec2 radius = vec2( kernelRadius * INV_NUM_SAMPLES ) / size;
  140. vec2 radiusStep = radius;
  141. float occlusionSum = 0.0;
  142. float weightSum = 0.0;
  143. for( int i = 0; i < NUM_SAMPLES; i ++ ) {
  144. vec2 sampleUv = vUv + vec2( cos( angle ), sin( angle ) ) * radius;
  145. radius += radiusStep;
  146. angle += ANGLE_STEP;
  147. float sampleDepth = getDepth( sampleUv );
  148. if( sampleDepth >= ( 1.0 - EPSILON ) ) {
  149. continue;
  150. }
  151. float sampleViewZ = getViewZ( sampleDepth );
  152. vec3 sampleViewPosition = getViewPosition( sampleUv, sampleDepth, sampleViewZ );
  153. occlusionSum += getOcclusion( centerViewPosition, centerViewNormal, sampleViewPosition );
  154. weightSum += 1.0;
  155. }
  156. if( weightSum == 0.0 ) discard;
  157. return occlusionSum * ( intensity / weightSum );
  158. }
  159. void main() {
  160. float centerDepth = getDepth( vUv );
  161. if( centerDepth >= ( 1.0 - EPSILON ) ) {
  162. discard;
  163. }
  164. float centerViewZ = getViewZ( centerDepth );
  165. vec3 viewPosition = getViewPosition( vUv, centerDepth, centerViewZ );
  166. float ambientOcclusion = getAmbientOcclusion( viewPosition );
  167. gl_FragColor = getDefaultColor( vUv );
  168. gl_FragColor.xyz *= 1.0 - ambientOcclusion;
  169. }`
  170. };
  171. THREE.SAOShader = SAOShader;
  172. } )();