DepthLimitedBlurShader.js 3.7 KB

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