SSAOShader.js 6.8 KB

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