SAOShader.js 5.1 KB

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