BlurNode.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.BlurNode = function( value, coord, radius, size ) {
  5. THREE.TempNode.call( this, 'v4' );
  6. this.requestUpdate = true;
  7. this.value = value;
  8. this.coord = coord || new THREE.UVNode();
  9. this.radius = new THREE.Vector2Node( 1, 1 );
  10. this.size = size;
  11. this.blurX = true;
  12. this.blurY = true;
  13. this.horizontal = new THREE.FloatNode( 1 / 64 );
  14. this.vertical = new THREE.FloatNode( 1 / 64 );
  15. };
  16. THREE.BlurNode.fBlurX = new THREE.FunctionNode( [
  17. "vec4 blurX( sampler2D texture, vec2 uv, float s ) {",
  18. " vec4 sum = vec4( 0.0 );",
  19. " sum += texture2D( texture, vec2( uv.x - 4.0 * s, uv.y ) ) * 0.051;",
  20. " sum += texture2D( texture, vec2( uv.x - 3.0 * s, uv.y ) ) * 0.0918;",
  21. " sum += texture2D( texture, vec2( uv.x - 2.0 * s, uv.y ) ) * 0.12245;",
  22. " sum += texture2D( texture, vec2( uv.x - 1.0 * s, uv.y ) ) * 0.1531;",
  23. " sum += texture2D( texture, vec2( uv.x, uv.y ) ) * 0.1633;",
  24. " sum += texture2D( texture, vec2( uv.x + 1.0 * s, uv.y ) ) * 0.1531;",
  25. " sum += texture2D( texture, vec2( uv.x + 2.0 * s, uv.y ) ) * 0.12245;",
  26. " sum += texture2D( texture, vec2( uv.x + 3.0 * s, uv.y ) ) * 0.0918;",
  27. " sum += texture2D( texture, vec2( uv.x + 4.0 * s, uv.y ) ) * 0.051;",
  28. " return sum;",
  29. "}"
  30. ].join( "\n" ) );
  31. THREE.BlurNode.fBlurY = new THREE.FunctionNode( [
  32. "vec4 blurY( sampler2D texture, vec2 uv, float s ) {",
  33. " vec4 sum = vec4( 0.0 );",
  34. " sum += texture2D( texture, vec2( uv.x, uv.y - 4.0 * s ) ) * 0.051;",
  35. " sum += texture2D( texture, vec2( uv.x, uv.y - 3.0 * s ) ) * 0.0918;",
  36. " sum += texture2D( texture, vec2( uv.x, uv.y - 2.0 * s ) ) * 0.12245;",
  37. " sum += texture2D( texture, vec2( uv.x, uv.y - 1.0 * s ) ) * 0.1531;",
  38. " sum += texture2D( texture, vec2( uv.x, uv.y ) ) * 0.1633;",
  39. " sum += texture2D( texture, vec2( uv.x, uv.y + 1.0 * s ) ) * 0.1531;",
  40. " sum += texture2D( texture, vec2( uv.x, uv.y + 2.0 * s ) ) * 0.12245;",
  41. " sum += texture2D( texture, vec2( uv.x, uv.y + 3.0 * s ) ) * 0.0918;",
  42. " sum += texture2D( texture, vec2( uv.x, uv.y + 4.0 * s ) ) * 0.051;",
  43. " return sum;",
  44. "}"
  45. ].join( "\n" ) );
  46. THREE.BlurNode.prototype = Object.create( THREE.TempNode.prototype );
  47. THREE.BlurNode.prototype.constructor = THREE.BlurNode;
  48. THREE.BlurNode.prototype.updateFrame = function( delta ) {
  49. if ( this.size ) {
  50. this.horizontal.number = this.radius.x / this.size.x;
  51. this.vertical.number = this.radius.y / this.size.y;
  52. } else if ( this.value.value && this.value.value.image ) {
  53. var image = this.value.value.image;
  54. this.horizontal.number = this.radius.x / image.width;
  55. this.vertical.number = this.radius.y / image.height;
  56. }
  57. };
  58. THREE.BlurNode.prototype.generate = function( builder, output ) {
  59. var material = builder.material, blurX = THREE.BlurNode.fBlurX, blurY = THREE.BlurNode.fBlurY;
  60. builder.include( blurX );
  61. builder.include( blurY );
  62. if ( builder.isShader( 'fragment' ) ) {
  63. var blurCode = [], code;
  64. if ( this.blurX ) {
  65. blurCode.push( blurX.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.horizontal.build( builder, 'fv1' ) + ')' );
  66. }
  67. if ( this.blurY ) {
  68. blurCode.push( blurY.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.vertical.build( builder, 'fv1' ) + ')' );
  69. }
  70. if ( blurCode.length == 2 ) code = '(' + blurCode.join( '+' ) + '/2.0)';
  71. else if ( blurCode.length ) code = '(' + blurCode[ 0 ] + ')';
  72. else code = 'vec4( 0.0 )';
  73. return builder.format( code, this.getType( builder ), output );
  74. } else {
  75. console.warn( "THREE.BlurNode is not compatible with " + builder.shader + " shader." );
  76. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  77. }
  78. };