DepthLimitedBlurShader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. console.warn( "THREE.DepthLimitedBlurShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * TODO
  4. */
  5. THREE.DepthLimitedBlurShader = {
  6. defines: {
  7. "KERNEL_RADIUS": 4,
  8. "DEPTH_PACKING": 1,
  9. "PERSPECTIVE_CAMERA": 1
  10. },
  11. uniforms: {
  12. "tDiffuse": { value: null },
  13. "size": { value: new THREE.Vector2( 512, 512 ) },
  14. "sampleUvOffsets": { value: [ new THREE.Vector2( 0, 0 ) ] },
  15. "sampleWeights": { value: [ 1.0 ] },
  16. "tDepth": { value: null },
  17. "cameraNear": { value: 10 },
  18. "cameraFar": { value: 1000 },
  19. "depthCutoff": { value: 10 },
  20. },
  21. vertexShader: [
  22. "#include <common>",
  23. "uniform vec2 size;",
  24. "varying vec2 vUv;",
  25. "varying vec2 vInvSize;",
  26. "void main() {",
  27. " vUv = uv;",
  28. " vInvSize = 1.0 / size;",
  29. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  30. "}"
  31. ].join( "\n" ),
  32. fragmentShader: [
  33. "#include <common>",
  34. "#include <packing>",
  35. "uniform sampler2D tDiffuse;",
  36. "uniform sampler2D tDepth;",
  37. "uniform float cameraNear;",
  38. "uniform float cameraFar;",
  39. "uniform float depthCutoff;",
  40. "uniform vec2 sampleUvOffsets[ KERNEL_RADIUS + 1 ];",
  41. "uniform float sampleWeights[ KERNEL_RADIUS + 1 ];",
  42. "varying vec2 vUv;",
  43. "varying vec2 vInvSize;",
  44. "float getDepth( const in vec2 screenPosition ) {",
  45. " #if DEPTH_PACKING == 1",
  46. " return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );",
  47. " #else",
  48. " return texture2D( tDepth, screenPosition ).x;",
  49. " #endif",
  50. "}",
  51. "float getViewZ( const in float depth ) {",
  52. " #if PERSPECTIVE_CAMERA == 1",
  53. " return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );",
  54. " #else",
  55. " return orthographicDepthToViewZ( depth, cameraNear, cameraFar );",
  56. " #endif",
  57. "}",
  58. "void main() {",
  59. " float depth = getDepth( vUv );",
  60. " if( depth >= ( 1.0 - EPSILON ) ) {",
  61. " discard;",
  62. " }",
  63. " float centerViewZ = -getViewZ( depth );",
  64. " bool rBreak = false, lBreak = false;",
  65. " float weightSum = sampleWeights[0];",
  66. " vec4 diffuseSum = texture2D( tDiffuse, vUv ) * weightSum;",
  67. " for( int i = 1; i <= KERNEL_RADIUS; i ++ ) {",
  68. " float sampleWeight = sampleWeights[i];",
  69. " vec2 sampleUvOffset = sampleUvOffsets[i] * vInvSize;",
  70. " vec2 sampleUv = vUv + sampleUvOffset;",
  71. " float viewZ = -getViewZ( getDepth( sampleUv ) );",
  72. " if( abs( viewZ - centerViewZ ) > depthCutoff ) rBreak = true;",
  73. " if( ! rBreak ) {",
  74. " diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;",
  75. " weightSum += sampleWeight;",
  76. " }",
  77. " sampleUv = vUv - sampleUvOffset;",
  78. " viewZ = -getViewZ( getDepth( sampleUv ) );",
  79. " if( abs( viewZ - centerViewZ ) > depthCutoff ) lBreak = true;",
  80. " if( ! lBreak ) {",
  81. " diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;",
  82. " weightSum += sampleWeight;",
  83. " }",
  84. " }",
  85. " gl_FragColor = diffuseSum / weightSum;",
  86. "}"
  87. ].join( "\n" )
  88. };
  89. THREE.BlurShaderUtils = {
  90. createSampleWeights: function ( kernelRadius, stdDev ) {
  91. var gaussian = function ( x, stdDev ) {
  92. return Math.exp( - ( x * x ) / ( 2.0 * ( stdDev * stdDev ) ) ) / ( Math.sqrt( 2.0 * Math.PI ) * stdDev );
  93. };
  94. var weights = [];
  95. for ( var i = 0; i <= kernelRadius; i ++ ) {
  96. weights.push( gaussian( i, stdDev ) );
  97. }
  98. return weights;
  99. },
  100. createSampleOffsets: function ( kernelRadius, uvIncrement ) {
  101. var offsets = [];
  102. for ( var i = 0; i <= kernelRadius; i ++ ) {
  103. offsets.push( uvIncrement.clone().multiplyScalar( i ) );
  104. }
  105. return offsets;
  106. },
  107. configure: function ( material, kernelRadius, stdDev, uvIncrement ) {
  108. material.defines[ "KERNEL_RADIUS" ] = kernelRadius;
  109. material.uniforms[ "sampleUvOffsets" ].value = THREE.BlurShaderUtils.createSampleOffsets( kernelRadius, uvIncrement );
  110. material.uniforms[ "sampleWeights" ].value = THREE.BlurShaderUtils.createSampleWeights( kernelRadius, stdDev );
  111. material.needsUpdate = true;
  112. }
  113. };