SAOShader.js 4.7 KB

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