SSAOShader.js 6.6 KB

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