2 * @author sunag / http://www.sunag.com.br/
5 THREE.BlurNode = function( value, coord, radius, size ) {
7 THREE.TempNode.call( this, 'v4' );
9 this.requestUpdate = true;
12 this.coord = coord || new THREE.UVNode();
13 this.radius = new THREE.Vector2Node( 1, 1 );
19 this.horizontal = new THREE.FloatNode( 1 / 64 );
20 this.vertical = new THREE.FloatNode( 1 / 64 );
24 THREE.BlurNode.fBlurX = new THREE.FunctionNode( [
25 "vec4 blurX( sampler2D texture, vec2 uv, float s ) {",
26 " vec4 sum = vec4( 0.0 );",
27 " sum += texture2D( texture, vec2( uv.x - 4.0 * s, uv.y ) ) * 0.051;",
28 " sum += texture2D( texture, vec2( uv.x - 3.0 * s, uv.y ) ) * 0.0918;",
29 " sum += texture2D( texture, vec2( uv.x - 2.0 * s, uv.y ) ) * 0.12245;",
30 " sum += texture2D( texture, vec2( uv.x - 1.0 * s, uv.y ) ) * 0.1531;",
31 " sum += texture2D( texture, vec2( uv.x, uv.y ) ) * 0.1633;",
32 " sum += texture2D( texture, vec2( uv.x + 1.0 * s, uv.y ) ) * 0.1531;",
33 " sum += texture2D( texture, vec2( uv.x + 2.0 * s, uv.y ) ) * 0.12245;",
34 " sum += texture2D( texture, vec2( uv.x + 3.0 * s, uv.y ) ) * 0.0918;",
35 " sum += texture2D( texture, vec2( uv.x + 4.0 * s, uv.y ) ) * 0.051;",
40 THREE.BlurNode.fBlurY = new THREE.FunctionNode( [
41 "vec4 blurY( sampler2D texture, vec2 uv, float s ) {",
42 " vec4 sum = vec4( 0.0 );",
43 " sum += texture2D( texture, vec2( uv.x, uv.y - 4.0 * s ) ) * 0.051;",
44 " sum += texture2D( texture, vec2( uv.x, uv.y - 3.0 * s ) ) * 0.0918;",
45 " sum += texture2D( texture, vec2( uv.x, uv.y - 2.0 * s ) ) * 0.12245;",
46 " sum += texture2D( texture, vec2( uv.x, uv.y - 1.0 * s ) ) * 0.1531;",
47 " sum += texture2D( texture, vec2( uv.x, uv.y ) ) * 0.1633;",
48 " sum += texture2D( texture, vec2( uv.x, uv.y + 1.0 * s ) ) * 0.1531;",
49 " sum += texture2D( texture, vec2( uv.x, uv.y + 2.0 * s ) ) * 0.12245;",
50 " sum += texture2D( texture, vec2( uv.x, uv.y + 3.0 * s ) ) * 0.0918;",
51 " sum += texture2D( texture, vec2( uv.x, uv.y + 4.0 * s ) ) * 0.051;",
56 THREE.BlurNode.prototype = Object.create( THREE.TempNode.prototype );
57 THREE.BlurNode.prototype.constructor = THREE.BlurNode;
59 THREE.BlurNode.prototype.updateFrame = function( delta ) {
63 this.horizontal.number = this.radius.x / this.size.x;
64 this.vertical.number = this.radius.y / this.size.y;
66 } else if ( this.value.value && this.value.value.image ) {
68 var image = this.value.value.image;
70 this.horizontal.number = this.radius.x / image.width;
71 this.vertical.number = this.radius.y / image.height;
77 THREE.BlurNode.prototype.generate = function( builder, output ) {
79 var material = builder.material, blurX = THREE.BlurNode.fBlurX, blurY = THREE.BlurNode.fBlurY;
81 builder.include( blurX );
82 builder.include( blurY );
84 if ( builder.isShader( 'fragment' ) ) {
86 var blurCode = [], code;
90 blurCode.push( blurX.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.horizontal.build( builder, 'fv1' ) + ')' );
96 blurCode.push( blurY.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.vertical.build( builder, 'fv1' ) + ')' );
100 if ( blurCode.length == 2 ) code = '(' + blurCode.join( '+' ) + '/2.0)';
101 else if ( blurCode.length ) code = '(' + blurCode[ 0 ] + ')';
102 else code = 'vec4( 0.0 )';
104 return builder.format( code, this.getType( builder ), output );
108 console.warn( "THREE.BlurNode is not compatible with " + builder.shader + " shader." );
110 return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );