DepthLimitedBlurShader.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ( function () {
  2. /**
  3. * TODO
  4. */
  5. var 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>', 'uniform vec2 size;', 'varying vec2 vUv;', 'varying vec2 vInvSize;', 'void main() {', ' vUv = uv;', ' vInvSize = 1.0 / size;', ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join( '\n' ),
  38. fragmentShader: [ '#include <common>', '#include <packing>', 'uniform sampler2D tDiffuse;', 'uniform sampler2D tDepth;', 'uniform float cameraNear;', 'uniform float cameraFar;', 'uniform float depthCutoff;', 'uniform vec2 sampleUvOffsets[ KERNEL_RADIUS + 1 ];', 'uniform float sampleWeights[ KERNEL_RADIUS + 1 ];', 'varying vec2 vUv;', 'varying vec2 vInvSize;', 'float getDepth( const in vec2 screenPosition ) {', ' #if DEPTH_PACKING == 1', ' return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );', ' #else', ' return texture2D( tDepth, screenPosition ).x;', ' #endif', '}', 'float getViewZ( const in float depth ) {', ' #if PERSPECTIVE_CAMERA == 1', ' return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );', ' #else', ' return orthographicDepthToViewZ( depth, cameraNear, cameraFar );', ' #endif', '}', 'void main() {', ' float depth = getDepth( vUv );', ' if( depth >= ( 1.0 - EPSILON ) ) {', ' discard;', ' }', ' float centerViewZ = -getViewZ( depth );', ' bool rBreak = false, lBreak = false;', ' float weightSum = sampleWeights[0];', ' vec4 diffuseSum = texture2D( tDiffuse, vUv ) * weightSum;', ' for( int i = 1; i <= KERNEL_RADIUS; i ++ ) {', ' float sampleWeight = sampleWeights[i];', ' vec2 sampleUvOffset = sampleUvOffsets[i] * vInvSize;', ' vec2 sampleUv = vUv + sampleUvOffset;', ' float viewZ = -getViewZ( getDepth( sampleUv ) );', ' if( abs( viewZ - centerViewZ ) > depthCutoff ) rBreak = true;', ' if( ! rBreak ) {', ' diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;', ' weightSum += sampleWeight;', ' }', ' sampleUv = vUv - sampleUvOffset;', ' viewZ = -getViewZ( getDepth( sampleUv ) );', ' if( abs( viewZ - centerViewZ ) > depthCutoff ) lBreak = true;', ' if( ! lBreak ) {', ' diffuseSum += texture2D( tDiffuse, sampleUv ) * sampleWeight;', ' weightSum += sampleWeight;', ' }', ' }', ' gl_FragColor = diffuseSum / weightSum;', '}' ].join( '\n' )
  39. };
  40. var BlurShaderUtils = {
  41. createSampleWeights: function ( kernelRadius, stdDev ) {
  42. var gaussian = function ( x, stdDev ) {
  43. return Math.exp( - ( x * x ) / ( 2.0 * ( stdDev * stdDev ) ) ) / ( Math.sqrt( 2.0 * Math.PI ) * stdDev );
  44. };
  45. var weights = [];
  46. for ( var i = 0; i <= kernelRadius; i ++ ) {
  47. weights.push( gaussian( i, stdDev ) );
  48. }
  49. return weights;
  50. },
  51. createSampleOffsets: function ( kernelRadius, uvIncrement ) {
  52. var offsets = [];
  53. for ( var i = 0; i <= kernelRadius; i ++ ) {
  54. offsets.push( uvIncrement.clone().multiplyScalar( i ) );
  55. }
  56. return offsets;
  57. },
  58. configure: function ( material, kernelRadius, stdDev, uvIncrement ) {
  59. material.defines[ 'KERNEL_RADIUS' ] = kernelRadius;
  60. material.uniforms[ 'sampleUvOffsets' ].value = BlurShaderUtils.createSampleOffsets( kernelRadius, uvIncrement );
  61. material.uniforms[ 'sampleWeights' ].value = BlurShaderUtils.createSampleWeights( kernelRadius, stdDev );
  62. material.needsUpdate = true;
  63. }
  64. };
  65. THREE.BlurShaderUtils = BlurShaderUtils;
  66. THREE.DepthLimitedBlurShader = DepthLimitedBlurShader;
  67. } )();