SAOShader.js 5.0 KB

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