SAOShader.js 4.8 KB

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