DepthLimitedBlurShader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. Vector2
  3. } from 'three';
  4. /** @module DepthLimitedBlurShader */
  5. /**
  6. * TODO
  7. *
  8. * Used by {@link SAOPass}.
  9. *
  10. * @constant
  11. * @type {Object}
  12. */
  13. const DepthLimitedBlurShader = {
  14. name: 'DepthLimitedBlurShader',
  15. defines: {
  16. 'KERNEL_RADIUS': 4,
  17. 'DEPTH_PACKING': 1,
  18. 'PERSPECTIVE_CAMERA': 1
  19. },
  20. uniforms: {
  21. 'tDiffuse': { value: null },
  22. 'size': { value: new Vector2( 512, 512 ) },
  23. 'sampleUvOffsets': { value: [ new Vector2( 0, 0 ) ] },
  24. 'sampleWeights': { value: [ 1.0 ] },
  25. 'tDepth': { value: null },
  26. 'cameraNear': { value: 10 },
  27. 'cameraFar': { value: 1000 },
  28. 'depthCutoff': { value: 10 },
  29. },
  30. vertexShader: /* glsl */`
  31. #include <common>
  32. uniform vec2 size;
  33. varying vec2 vUv;
  34. varying vec2 vInvSize;
  35. void main() {
  36. vUv = uv;
  37. vInvSize = 1.0 / size;
  38. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  39. }`,
  40. fragmentShader: /* glsl */`
  41. #include <common>
  42. #include <packing>
  43. uniform sampler2D tDiffuse;
  44. uniform sampler2D tDepth;
  45. uniform float cameraNear;
  46. uniform float cameraFar;
  47. uniform float depthCutoff;
  48. uniform vec2 sampleUvOffsets[ KERNEL_RADIUS + 1 ];
  49. uniform float sampleWeights[ KERNEL_RADIUS + 1 ];
  50. varying vec2 vUv;
  51. varying vec2 vInvSize;
  52. float getDepth( const in vec2 screenPosition ) {
  53. #if DEPTH_PACKING == 1
  54. return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );
  55. #else
  56. return texture2D( tDepth, screenPosition ).x;
  57. #endif
  58. }
  59. float getViewZ( const in float depth ) {
  60. #if PERSPECTIVE_CAMERA == 1
  61. return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
  62. #else
  63. return orthographicDepthToViewZ( depth, cameraNear, cameraFar );
  64. #endif
  65. }
  66. void main() {
  67. float depth = getDepth( vUv );
  68. if( depth >= ( 1.0 - EPSILON ) ) {
  69. discard;
  70. }
  71. float centerViewZ = -getViewZ( depth );
  72. bool rBreak = false, lBreak = false;
  73. float weightSum = sampleWeights[0];
  74. vec4 diffuseSum = texture2D( tDiffuse, vUv ) * weightSum;
  75. for( int i = 1; i <= KERNEL_RADIUS; i ++ ) {
  76. float sampleWeight = sampleWeights[i];
  77. vec2 sampleUvOffset = sampleUvOffsets[i] * vInvSize;
  78. vec2 sampleUv = vUv + sampleUvOffset;
  79. float viewZ = -getViewZ( getDepth( sampleUv ) );
  80. if( abs( viewZ - centerViewZ ) > depthCutoff ) rBreak = true;
  81. if( ! rBreak ) {
  82. diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;
  83. weightSum += sampleWeight;
  84. }
  85. sampleUv = vUv - sampleUvOffset;
  86. viewZ = -getViewZ( getDepth( sampleUv ) );
  87. if( abs( viewZ - centerViewZ ) > depthCutoff ) lBreak = true;
  88. if( ! lBreak ) {
  89. diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;
  90. weightSum += sampleWeight;
  91. }
  92. }
  93. gl_FragColor = diffuseSum / weightSum;
  94. }`
  95. };
  96. const BlurShaderUtils = {
  97. createSampleWeights: function ( kernelRadius, stdDev ) {
  98. const weights = [];
  99. for ( let i = 0; i <= kernelRadius; i ++ ) {
  100. weights.push( gaussian( i, stdDev ) );
  101. }
  102. return weights;
  103. },
  104. createSampleOffsets: function ( kernelRadius, uvIncrement ) {
  105. const offsets = [];
  106. for ( let i = 0; i <= kernelRadius; i ++ ) {
  107. offsets.push( uvIncrement.clone().multiplyScalar( i ) );
  108. }
  109. return offsets;
  110. },
  111. configure: function ( material, kernelRadius, stdDev, uvIncrement ) {
  112. material.defines[ 'KERNEL_RADIUS' ] = kernelRadius;
  113. material.uniforms[ 'sampleUvOffsets' ].value = BlurShaderUtils.createSampleOffsets( kernelRadius, uvIncrement );
  114. material.uniforms[ 'sampleWeights' ].value = BlurShaderUtils.createSampleWeights( kernelRadius, stdDev );
  115. material.needsUpdate = true;
  116. }
  117. };
  118. function gaussian( x, stdDev ) {
  119. return Math.exp( - ( x * x ) / ( 2.0 * ( stdDev * stdDev ) ) ) / ( Math.sqrt( 2.0 * Math.PI ) * stdDev );
  120. }
  121. export { DepthLimitedBlurShader, BlurShaderUtils };
粤ICP备19079148号