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. "onlyAO": { value: 0 },
  21. "aoClamp": { value: 0.5 },
  22. "lumInfluence": { value: 0.5 }
  23. },
  24. vertexShader: [
  25. "varying vec2 vUv;",
  26. "void main() {",
  27. "vUv = uv;",
  28. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  29. "}"
  30. ].join( "\n" ),
  31. fragmentShader: [
  32. "uniform float cameraNear;",
  33. "uniform float cameraFar;",
  34. "#ifdef USE_LOGDEPTHBUF",
  35. "uniform float logDepthBufFC;",
  36. "#endif",
  37. "uniform bool onlyAO;", // use only ambient occlusion pass?
  38. "uniform vec2 size;", // texture width, height
  39. "uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
  40. "uniform float lumInfluence;", // how much luminance affects occlusion
  41. "uniform sampler2D tDiffuse;",
  42. "uniform sampler2D tDepth;",
  43. "varying vec2 vUv;",
  44. // "#define PI 3.14159265",
  45. "#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
  46. "#define EULER 2.718281828459045",
  47. // user variables
  48. "const int samples = 8;", // ao sample count
  49. "const float radius = 5.0;", // ao radius
  50. "const bool useNoise = false;", // use noise instead of pattern for sample dithering
  51. "const float noiseAmount = 0.0003;", // dithering amount
  52. "const float diffArea = 0.4;", // self-shadowing reduction
  53. "const float gDisplace = 0.4;", // gauss bell center
  54. // RGBA depth
  55. "#include <packing>",
  56. // generating noise / pattern texture for dithering
  57. "vec2 rand( const vec2 coord ) {",
  58. "vec2 noise;",
  59. "if ( useNoise ) {",
  60. "float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
  61. "float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
  62. "noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
  63. "} else {",
  64. "float ff = fract( 1.0 - coord.s * ( size.x / 2.0 ) );",
  65. "float gg = fract( coord.t * ( size.y / 2.0 ) );",
  66. "noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
  67. "}",
  68. "return ( noise * 2.0 - 1.0 ) * noiseAmount;",
  69. "}",
  70. "float readDepth( const in vec2 coord ) {",
  71. "float cameraFarPlusNear = cameraFar + cameraNear;",
  72. "float cameraFarMinusNear = cameraFar - cameraNear;",
  73. "float cameraCoef = 2.0 * cameraNear;",
  74. "#ifdef USE_LOGDEPTHBUF",
  75. "float logz = unpackRGBAToDepth( texture2D( tDepth, coord ) );",
  76. "float w = pow(2.0, (logz / logDepthBufFC)) - 1.0;",
  77. "float z = (logz / w) + 1.0;",
  78. "#else",
  79. "float z = unpackRGBAToDepth( texture2D( tDepth, coord ) );",
  80. "#endif",
  81. "return cameraCoef / ( cameraFarPlusNear - z * cameraFarMinusNear );",
  82. "}",
  83. "float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
  84. "float garea = 2.0;", // gauss bell width
  85. "float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
  86. // reduce left bell width to avoid self-shadowing
  87. "if ( diff < gDisplace ) {",
  88. "garea = diffArea;",
  89. "} else {",
  90. "far = 1;",
  91. "}",
  92. "float dd = diff - gDisplace;",
  93. "float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
  94. "return gauss;",
  95. "}",
  96. "float calcAO( float depth, float dw, float dh ) {",
  97. "float dd = radius - depth * radius;",
  98. "vec2 vv = vec2( dw, dh );",
  99. "vec2 coord1 = vUv + dd * vv;",
  100. "vec2 coord2 = vUv - dd * 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 z = 1.0 - dz / 2.0;",
  121. "float l = 0.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. };