SSAOShader.js 6.6 KB

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