SSAOShader.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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": { type: "t", value: null },
  16. "tDepth": { type: "t", value: null },
  17. "size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
  18. "cameraNear": { type: "f", value: 1 },
  19. "cameraFar": { type: "f", value: 100 },
  20. "onlyAO": { type: "i", value: 0 },
  21. "aoClamp": { type: "f", value: 0.5 },
  22. "lumInfluence": { type: "f", 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. "uniform bool onlyAO;", // use only ambient occlusion pass?
  35. "uniform vec2 size;", // texture width, height
  36. "uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
  37. "uniform float lumInfluence;", // how much luminance affects occlusion
  38. "uniform sampler2D tDiffuse;",
  39. "uniform sampler2D tDepth;",
  40. "varying vec2 vUv;",
  41. // "#define PI 3.14159265",
  42. "#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
  43. "#define EULER 2.718281828459045",
  44. // helpers
  45. "float width = size.x;", // texture width
  46. "float height = size.y;", // texture height
  47. "float cameraFarPlusNear = cameraFar + cameraNear;",
  48. "float cameraFarMinusNear = cameraFar - cameraNear;",
  49. "float cameraCoef = 2.0 * cameraNear;",
  50. // user variables
  51. "const int samples = 8;", // ao sample count
  52. "const float radius = 5.0;", // ao radius
  53. "const bool useNoise = false;", // use noise instead of pattern for sample dithering
  54. "const float noiseAmount = 0.0003;", // dithering amount
  55. "const float diffArea = 0.4;", // self-shadowing reduction
  56. "const float gDisplace = 0.4;", // gauss bell center
  57. // RGBA depth
  58. "float unpackDepth( const in vec4 rgba_depth ) {",
  59. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  60. "float depth = dot( rgba_depth, bit_shift );",
  61. "return depth;",
  62. "}",
  63. // generating noise / pattern texture for dithering
  64. "vec2 rand( const vec2 coord ) {",
  65. "vec2 noise;",
  66. "if ( useNoise ) {",
  67. "float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
  68. "float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
  69. "noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
  70. "} else {",
  71. "float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",
  72. "float gg = fract( coord.t * ( height / 2.0 ) );",
  73. "noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
  74. "}",
  75. "return ( noise * 2.0 - 1.0 ) * noiseAmount;",
  76. "}",
  77. "float readDepth( const in vec2 coord ) {",
  78. // "return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",
  79. "return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",
  80. "}",
  81. "float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
  82. "float garea = 2.0;", // gauss bell width
  83. "float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
  84. // reduce left bell width to avoid self-shadowing
  85. "if ( diff < gDisplace ) {",
  86. "garea = diffArea;",
  87. "} else {",
  88. "far = 1;",
  89. "}",
  90. "float dd = diff - gDisplace;",
  91. "float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
  92. "return gauss;",
  93. "}",
  94. "float calcAO( float depth, float dw, float dh ) {",
  95. "float dd = radius - depth * radius;",
  96. "vec2 vv = vec2( dw, dh );",
  97. "vec2 coord1 = vUv + dd * vv;",
  98. "vec2 coord2 = vUv - dd * vv;",
  99. "float temp1 = 0.0;",
  100. "float temp2 = 0.0;",
  101. "int far = 0;",
  102. "temp1 = compareDepths( depth, readDepth( coord1 ), far );",
  103. // DEPTH EXTRAPOLATION
  104. "if ( far > 0 ) {",
  105. "temp2 = compareDepths( readDepth( coord2 ), depth, far );",
  106. "temp1 += ( 1.0 - temp1 ) * temp2;",
  107. "}",
  108. "return temp1;",
  109. "}",
  110. "void main() {",
  111. "vec2 noise = rand( vUv );",
  112. "float depth = readDepth( vUv );",
  113. "float tt = clamp( depth, aoClamp, 1.0 );",
  114. "float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
  115. "float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
  116. "float ao = 0.0;",
  117. "float dz = 1.0 / float( samples );",
  118. "float z = 1.0 - dz / 2.0;",
  119. "float l = 0.0;",
  120. "for ( int i = 0; i <= samples; i ++ ) {",
  121. "float r = sqrt( 1.0 - z );",
  122. "float pw = cos( l ) * r;",
  123. "float ph = sin( l ) * r;",
  124. "ao += calcAO( depth, pw * w, ph * h );",
  125. "z = z - dz;",
  126. "l = l + DL;",
  127. "}",
  128. "ao /= float( samples );",
  129. "ao = 1.0 - ao;",
  130. "vec3 color = texture2D( tDiffuse, vUv ).rgb;",
  131. "vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
  132. "float lum = dot( color.rgb, lumcoeff );",
  133. "vec3 luminance = vec3( lum );",
  134. "vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
  135. "if ( onlyAO ) {",
  136. "final = vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
  137. "}",
  138. "gl_FragColor = vec4( final, 1.0 );",
  139. "}"
  140. ].join("\n")
  141. };