OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / examples / js / nodes / utils / BlurNode.js
1 /**
2  * @author sunag / http://www.sunag.com.br/
3  */
4
5 THREE.BlurNode = function( value, coord, radius, size ) {
6
7         THREE.TempNode.call( this, 'v4' );
8
9         this.requestUpdate = true;
10
11         this.value = value;
12         this.coord = coord || new THREE.UVNode();
13         this.radius = new THREE.Vector2Node( 1, 1 );
14         this.size = size;
15
16         this.blurX = true;
17         this.blurY = true;
18
19         this.horizontal = new THREE.FloatNode( 1 / 64 );
20         this.vertical = new THREE.FloatNode( 1 / 64 );
21
22 };
23
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;",
36 "       return sum;",
37 "}"
38 ].join( "\n" ) );
39
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;",
52 "       return sum;",
53 "}"
54 ].join( "\n" ) );
55
56 THREE.BlurNode.prototype = Object.create( THREE.TempNode.prototype );
57 THREE.BlurNode.prototype.constructor = THREE.BlurNode;
58
59 THREE.BlurNode.prototype.updateFrame = function( delta ) {
60
61         if ( this.size ) {
62
63                 this.horizontal.number = this.radius.x / this.size.x;
64                 this.vertical.number = this.radius.y / this.size.y;
65
66         } else if ( this.value.value && this.value.value.image ) {
67
68                 var image = this.value.value.image;
69
70                 this.horizontal.number = this.radius.x / image.width;
71                 this.vertical.number = this.radius.y / image.height;
72
73         }
74
75 };
76
77 THREE.BlurNode.prototype.generate = function( builder, output ) {
78
79         var material = builder.material, blurX = THREE.BlurNode.fBlurX, blurY = THREE.BlurNode.fBlurY;
80
81         builder.include( blurX );
82         builder.include( blurY );
83
84         if ( builder.isShader( 'fragment' ) ) {
85
86                 var blurCode = [], code;
87
88                 if ( this.blurX ) {
89
90                         blurCode.push( blurX.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.horizontal.build( builder, 'fv1' ) + ')' );
91
92                 }
93
94                 if ( this.blurY ) {
95
96                         blurCode.push( blurY.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' + this.coord.build( builder, 'v2' ) + ',' + this.vertical.build( builder, 'fv1' ) + ')' );
97
98                 }
99
100                 if ( blurCode.length == 2 ) code = '(' + blurCode.join( '+' ) + '/2.0)';
101                 else if ( blurCode.length ) code = '(' + blurCode[ 0 ] + ')';
102                 else code = 'vec4( 0.0 )';
103
104                 return builder.format( code, this.getType( builder ), output );
105
106         } else {
107
108                 console.warn( "THREE.BlurNode is not compatible with " + builder.shader + " shader." );
109
110                 return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
111
112         }
113
114 };