-
Notifications
You must be signed in to change notification settings - Fork 9
/
BloomBlendPass.js
148 lines (99 loc) · 4.73 KB
/
BloomBlendPass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @author mattatz / http://mattatz.github.io/
*/
THREE.BloomBlendPass = function ( amount, opacity, resolution ) {
THREE.Pass.call(this);
this.amount = ( amount !== undefined ) ? amount : 1.0;
this.opacity = ( opacity !== undefined ) ? opacity : 1.0;
this.resolution = ( resolution !== undefined ) ? resolution : new THREE.Vector2(512, 512);
// render targets
var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
this.renderTargetX = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
this.renderTargetY = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, pars );
var kernel = [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join( "\n" );
this.blurMaterial = new THREE.ShaderMaterial( {
uniforms: {
"tDiffuse" : { type : "t", value : null },
"increment" : { type : "v2", value : new THREE.Vector2() }
},
vertexShader: kernel,
fragmentShader: [
"uniform sampler2D tDiffuse;",
"uniform vec2 increment;",
"varying vec2 vUv;",
"void main() {",
"vec4 color = vec4(0.0);",
"color += texture2D(tDiffuse, (vUv - increment * 4.0)) * 0.051;",
"color += texture2D(tDiffuse, (vUv - increment * 3.0)) * 0.0918;",
"color += texture2D(tDiffuse, (vUv - increment * 2.0)) * 0.12245;",
"color += texture2D(tDiffuse, (vUv - increment * 1.0)) * 0.1531;",
"color += texture2D(tDiffuse, (vUv + increment * 0.0)) * 0.1633;",
"color += texture2D(tDiffuse, (vUv + increment * 1.0)) * 0.1531;",
"color += texture2D(tDiffuse, (vUv + increment * 2.0)) * 0.12245;",
"color += texture2D(tDiffuse, (vUv + increment * 3.0)) * 0.0918;",
"color += texture2D(tDiffuse, (vUv + increment * 4.0)) * 0.051;",
"gl_FragColor = color;",
"}"
].join( "\n" ),
} );
this.blendMaterial = new THREE.ShaderMaterial( {
uniforms : {
"tDiffuse" : { type : "t", value : null },
"tBlend" : { type : "t", value : null },
"opacity" : { type : "f", value : this.opacity },
},
vertexShader : kernel,
fragmentShader : [
"uniform sampler2D tDiffuse;",
"uniform sampler2D tBlend;",
"uniform float opacity;",
"varying vec2 vUv;",
"void main() {",
"vec4 base = texture2D(tDiffuse, vUv);",
"vec4 blend = texture2D(tBlend, vUv);",
// screen blending
"vec4 color = (1.0 - ((1.0 - base) * (1.0 - blend)));",
"gl_FragColor = color * opacity + base * ( 1. - opacity );",
"}"
].join( "\n" )
} );
this.enabled = true;
this.needsSwap = true;
this.clear = false;
this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
this.scene = new THREE.Scene();
this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
this.scene.add( this.quad );
};
THREE.BloomBlendPass.prototype = Object.assign(Object.create(THREE.Pass.prototype), {
constructor: THREE.BloomBlendPass,
render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
this.quad.material = this.blurMaterial;
// horizontal blur
this.blurMaterial.uniforms[ "tDiffuse" ].value = readBuffer;
this.blurMaterial.uniforms[ "increment" ].value.set( this.amount / readBuffer.width, 0.0 );
renderer.render( this.scene, this.camera, this.renderTargetX, false);
// vertical blur
this.blurMaterial.uniforms[ "tDiffuse" ].value = this.renderTargetX;
this.blurMaterial.uniforms[ "increment" ].value.set( 0.0, this.amount / this.renderTargetX.height);
renderer.render( this.scene, this.camera, this.renderTargetY, false);
// screen blending original buffer and blurred buffer
this.quad.material = this.blendMaterial;
this.blendMaterial.uniforms[ "tDiffuse" ].value = readBuffer;
this.blendMaterial.uniforms[ "tBlend" ].value = this.renderTargetY;
this.blendMaterial.uniforms[ "opacity" ].value = this.opacity;
if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST );
if( this.renderToScreen ) {
renderer.render( this.scene, this.camera );
} else {
renderer.render( this.scene, this.camera, writeBuffer, this.clear );
}
}
});