SSAOShader.js 6.8 KB

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