SSAOShader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. *
  4. * References:
  5. * http://john-chapman-graphics.blogspot.com/2013/01/ssao-tutorial.html
  6. * https://learnopengl.com/Advanced-Lighting/SSAO
  7. * https://github.com/McNopper/OpenGL/blob/master/Example28/shader/ssao.frag.glsl
  8. */
  9. THREE.SSAOShader = {
  10. defines: {
  11. "PERSPECTIVE_CAMERA": 1,
  12. "KERNEL_SIZE": 64
  13. },
  14. uniforms: {
  15. "tDiffuse": { value: null },
  16. "tNormal": { value: null },
  17. "tDepth": { value: null },
  18. "tNoise": { value: null },
  19. "kernel": { value: null },
  20. "cameraNear": { value: null },
  21. "cameraFar": { value: null },
  22. "resolution": { value: new THREE.Vector2() },
  23. "cameraProjectionMatrix": { value: new THREE.Matrix4() },
  24. "cameraInverseProjectionMatrix": { value: new THREE.Matrix4() },
  25. "kernelRadius": { value: 8 },
  26. "minDistance": { value: 0.005 },
  27. "maxDistance": { value: 0.05 },
  28. },
  29. vertexShader: [
  30. "varying vec2 vUv;",
  31. "void main() {",
  32. " vUv = uv;",
  33. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  34. "}"
  35. ].join( "\n" ),
  36. fragmentShader: [
  37. "uniform sampler2D tDiffuse;",
  38. "uniform sampler2D tNormal;",
  39. "uniform sampler2D tDepth;",
  40. "uniform sampler2D tNoise;",
  41. "uniform vec3 kernel[ KERNEL_SIZE ];",
  42. "uniform vec2 resolution;",
  43. "uniform float cameraNear;",
  44. "uniform float cameraFar;",
  45. "uniform mat4 cameraProjectionMatrix;",
  46. "uniform mat4 cameraInverseProjectionMatrix;",
  47. "uniform float kernelRadius;",
  48. "uniform float minDistance;", // avoid artifacts caused by neighbour fragments with minimal depth difference
  49. "uniform float maxDistance;", // avoid the influence of fragments which are too far away
  50. "varying vec2 vUv;",
  51. "#include <packing>",
  52. "float getDepth( const in vec2 screenPosition ) {",
  53. " return texture2D( tDepth, screenPosition ).x;",
  54. "}",
  55. "float getLinearDepth( const in vec2 screenPosition ) {",
  56. " #if PERSPECTIVE_CAMERA == 1",
  57. " float fragCoordZ = texture2D( tDepth, screenPosition ).x;",
  58. " float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );",
  59. " return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );",
  60. " #else",
  61. " return texture2D( depthSampler, coord ).x;",
  62. " #endif",
  63. "}",
  64. "float getViewZ( const in float depth ) {",
  65. " #if PERSPECTIVE_CAMERA == 1",
  66. " return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );",
  67. " #else",
  68. " return orthographicDepthToViewZ( depth, cameraNear, cameraFar );",
  69. " #endif",
  70. "}",
  71. "vec3 getViewPosition( const in vec2 screenPosition, const in float depth, const in float viewZ ) {",
  72. " float clipW = cameraProjectionMatrix[2][3] * viewZ + cameraProjectionMatrix[3][3];",
  73. " vec4 clipPosition = vec4( ( vec3( screenPosition, depth ) - 0.5 ) * 2.0, 1.0 );",
  74. " clipPosition *= clipW; // unprojection.",
  75. " return ( cameraInverseProjectionMatrix * clipPosition ).xyz;",
  76. "}",
  77. "vec3 getViewNormal( const in vec2 screenPosition ) {",
  78. " return unpackRGBToNormal( texture2D( tNormal, screenPosition ).xyz );",
  79. "}",
  80. "void main() {",
  81. " float depth = getDepth( vUv );",
  82. " float viewZ = getViewZ( depth );",
  83. " vec3 viewPosition = getViewPosition( vUv, depth, viewZ );",
  84. " vec3 viewNormal = getViewNormal( vUv );",
  85. " vec2 noiseScale = vec2( resolution.x / 4.0, resolution.y / 4.0 );",
  86. " vec3 random = texture2D( tNoise, vUv * noiseScale ).xyz;",
  87. // compute matrix used to reorient a kernel vector
  88. " vec3 tangent = normalize( random - viewNormal * dot( random, viewNormal ) );",
  89. " vec3 bitangent = cross( viewNormal, tangent );",
  90. " mat3 kernelMatrix = mat3( tangent, bitangent, viewNormal );",
  91. " float occlusion = 0.0;",
  92. " for ( int i = 0; i < KERNEL_SIZE; i ++ ) {",
  93. " vec3 sampleVector = kernelMatrix * kernel[ i ];", // reorient sample vector in view space
  94. " vec3 samplePoint = viewPosition + ( sampleVector * kernelRadius );", // calculate sample point
  95. " vec4 samplePointNDC = cameraProjectionMatrix * vec4( samplePoint, 1.0 );", // project point and calculate NDC
  96. " samplePointNDC /= samplePointNDC.w;",
  97. " vec2 samplePointUv = samplePointNDC.xy * 0.5 + 0.5;", // compute uv coordinates
  98. " float realDepth = getLinearDepth( samplePointUv );", // get linear depth from depth texture
  99. " float sampleDepth = viewZToOrthographicDepth( samplePoint.z, cameraNear, cameraFar );", // compute linear depth of the sample view Z value
  100. " float delta = sampleDepth - realDepth;",
  101. " if ( delta > minDistance && delta < maxDistance ) {", // if fragment is before sample point, increase occlusion
  102. " occlusion += 1.0;",
  103. " }",
  104. " }",
  105. " occlusion = clamp( occlusion / float( KERNEL_SIZE ), 0.0, 1.0 );",
  106. " gl_FragColor = vec4( vec3( 1.0 - occlusion ), 1.0 );",
  107. "}"
  108. ].join( "\n" )
  109. };
  110. THREE.SSAODepthShader = {
  111. defines: {
  112. "PERSPECTIVE_CAMERA": 1
  113. },
  114. uniforms: {
  115. "tDepth": { value: null },
  116. "cameraNear": { value: null },
  117. "cameraFar": { value: null },
  118. },
  119. vertexShader: [
  120. "varying vec2 vUv;",
  121. "void main() {",
  122. " vUv = uv;",
  123. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  124. "}"
  125. ].join( "\n" ),
  126. fragmentShader: [
  127. "uniform sampler2D tDepth;",
  128. "uniform float cameraNear;",
  129. "uniform float cameraFar;",
  130. "varying vec2 vUv;",
  131. "#include <packing>",
  132. "float getLinearDepth( const in vec2 screenPosition ) {",
  133. " #if PERSPECTIVE_CAMERA == 1",
  134. " float fragCoordZ = texture2D( tDepth, screenPosition ).x;",
  135. " float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );",
  136. " return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );",
  137. " #else",
  138. " return texture2D( depthSampler, coord ).x;",
  139. " #endif",
  140. "}",
  141. "void main() {",
  142. " float depth = getLinearDepth( vUv );",
  143. " gl_FragColor = vec4( vec3( 1.0 - depth ), 1.0 );",
  144. "}"
  145. ].join( "\n" )
  146. };
  147. THREE.SSAOBlurShader = {
  148. uniforms: {
  149. "tDiffuse": { value: null },
  150. "resolution": { value: new THREE.Vector2() }
  151. },
  152. vertexShader: [
  153. "varying vec2 vUv;",
  154. "void main() {",
  155. " vUv = uv;",
  156. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  157. "}"
  158. ].join( "\n" ),
  159. fragmentShader: [
  160. "uniform sampler2D tDiffuse;",
  161. "uniform vec2 resolution;",
  162. "varying vec2 vUv;",
  163. "void main() {",
  164. " vec2 texelSize = ( 1.0 / resolution );",
  165. " float result = 0.0;",
  166. " for ( int i = - 2; i <= 2; i ++ ) {",
  167. " for ( int j = - 2; j <= 2; j ++ ) {",
  168. " vec2 offset = ( vec2( float( i ), float( j ) ) ) * texelSize;",
  169. " result += texture2D( tDiffuse, vUv + offset ).r;",
  170. " }",
  171. " }",
  172. " gl_FragColor = vec4( vec3( result / ( 5.0 * 5.0 ) ), 1.0 );",
  173. "}"
  174. ].join( "\n" )
  175. };