SSAOShader.js 5.3 KB

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