2
0

SSAOShader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * Screen-space ambient occlusion shader
  5. * - ported from
  6. * SSAO GLSL shader v1.2
  7. * assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)
  8. * original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)
  9. * - modifications
  10. * - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)
  11. * - refactoring and optimizations
  12. */
  13. THREE.SSAOShader = {
  14. uniforms: {
  15. "tDiffuse": { value: null },
  16. "tDepth": { value: null },
  17. "size": { value: new THREE.Vector2( 512, 512 ) },
  18. "cameraNear": { value: 1 },
  19. "cameraFar": { value: 100 },
  20. "radius": { value: 32 },
  21. "onlyAO": { value: 0 },
  22. "aoClamp": { value: 0.25 },
  23. "lumInfluence": { value: 0.7 }
  24. },
  25. vertexShader: [
  26. "varying vec2 vUv;",
  27. "void main() {",
  28. "vUv = uv;",
  29. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  30. "}"
  31. ].join( "\n" ),
  32. fragmentShader: [
  33. "uniform float cameraNear;",
  34. "uniform float cameraFar;",
  35. "#ifdef USE_LOGDEPTHBUF",
  36. "uniform float logDepthBufFC;",
  37. "#endif",
  38. "uniform float radius;", // ao radius
  39. "uniform bool onlyAO;", // use only ambient occlusion pass?
  40. "uniform vec2 size;", // texture width, height
  41. "uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
  42. "uniform float lumInfluence;", // how much luminance affects occlusion
  43. "uniform sampler2D tDiffuse;",
  44. "uniform sampler2D tDepth;",
  45. "varying vec2 vUv;",
  46. // "#define PI 3.14159265",
  47. "#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
  48. "#define EULER 2.718281828459045",
  49. // user variables
  50. "const int samples = 64;", // ao sample count
  51. "const bool useNoise = true;", // use noise instead of pattern for sample dithering
  52. "const float noiseAmount = 0.0004;", // dithering amount
  53. "const float diffArea = 0.4;", // self-shadowing reduction
  54. "const float gDisplace = 0.4;", // gauss bell center
  55. // RGBA depth
  56. "#include <packing>",
  57. // generating noise / pattern texture for dithering
  58. "vec2 rand( const vec2 coord ) {",
  59. "vec2 noise;",
  60. "if ( useNoise ) {",
  61. "float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
  62. "float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
  63. "noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
  64. "} else {",
  65. "float ff = fract( 1.0 - coord.s * ( size.x / 2.0 ) );",
  66. "float gg = fract( coord.t * ( size.y / 2.0 ) );",
  67. "noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
  68. "}",
  69. "return ( noise * 2.0 - 1.0 ) * noiseAmount;",
  70. "}",
  71. "float readDepth( const in vec2 coord ) {",
  72. "float cameraFarPlusNear = cameraFar + cameraNear;",
  73. "float cameraFarMinusNear = cameraFar - cameraNear;",
  74. "float cameraCoef = 2.0 * cameraNear;",
  75. "#ifdef USE_LOGDEPTHBUF",
  76. "float logz = unpackRGBAToDepth( texture2D( tDepth, coord ) );",
  77. "float w = pow(2.0, (logz / logDepthBufFC)) - 1.0;",
  78. "float z = (logz / w) + 1.0;",
  79. "#else",
  80. "float z = unpackRGBAToDepth( texture2D( tDepth, coord ) );",
  81. "#endif",
  82. "return cameraCoef / ( cameraFarPlusNear - z * cameraFarMinusNear );",
  83. "}",
  84. "float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
  85. "float garea = 8.0;", // gauss bell width
  86. "float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
  87. // reduce left bell width to avoid self-shadowing
  88. "if ( diff < gDisplace ) {",
  89. "garea = diffArea;",
  90. "} else {",
  91. "far = 1;",
  92. "}",
  93. "float dd = diff - gDisplace;",
  94. "float gauss = pow( EULER, -2.0 * ( dd * dd ) / ( garea * garea ) );",
  95. "return gauss;",
  96. "}",
  97. "float calcAO( float depth, float dw, float dh ) {",
  98. "vec2 vv = vec2( dw, dh );",
  99. "vec2 coord1 = vUv + radius * vv;",
  100. "vec2 coord2 = vUv - radius * vv;",
  101. "float temp1 = 0.0;",
  102. "float temp2 = 0.0;",
  103. "int far = 0;",
  104. "temp1 = compareDepths( depth, readDepth( coord1 ), far );",
  105. // DEPTH EXTRAPOLATION
  106. "if ( far > 0 ) {",
  107. "temp2 = compareDepths( readDepth( coord2 ), depth, far );",
  108. "temp1 += ( 1.0 - temp1 ) * temp2;",
  109. "}",
  110. "return temp1;",
  111. "}",
  112. "void main() {",
  113. "vec2 noise = rand( vUv );",
  114. "float depth = readDepth( vUv );",
  115. "float tt = clamp( depth, aoClamp, 1.0 );",
  116. "float w = ( 1.0 / size.x ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
  117. "float h = ( 1.0 / size.y ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
  118. "float ao = 0.0;",
  119. "float dz = 1.0 / float( samples );",
  120. "float l = 0.0;",
  121. "float z = 1.0 - dz / 2.0;",
  122. "for ( int i = 0; i <= samples; i ++ ) {",
  123. "float r = sqrt( 1.0 - z );",
  124. "float pw = cos( l ) * r;",
  125. "float ph = sin( l ) * r;",
  126. "ao += calcAO( depth, pw * w, ph * h );",
  127. "z = z - dz;",
  128. "l = l + DL;",
  129. "}",
  130. "ao /= float( samples );",
  131. "ao = 1.0 - ao;",
  132. "vec3 color = texture2D( tDiffuse, vUv ).rgb;",
  133. "vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
  134. "float lum = dot( color.rgb, lumcoeff );",
  135. "vec3 luminance = vec3( lum );",
  136. "vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
  137. "if ( onlyAO ) {",
  138. "final = vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
  139. "}",
  140. "gl_FragColor = vec4( final, 1.0 );",
  141. "}"
  142. ].join( "\n" )
  143. };