DepthLimitedBlurShader.js 3.9 KB

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