DepthLimitedBlurShader.js 3.7 KB

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