DepthLimitedBlurShader.js 3.7 KB

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