1 /**
2 * @author mikael emtinger / http://gomo.se/
3 * @author alteredq / http://alteredqualia.com/
4 */
5
6 /**@constructor*/
7 THREE.LensFlarePlugin = function ( ) {
8
9 var _gl, _renderer, _lensFlare = {};
10
11 this.init = function ( renderer ) {
12
13 _gl = renderer.context;
14 _renderer = renderer;
15
16 _lensFlare.vertices = new Float32Array( 8 + 8 );
17 _lensFlare.faces = new Uint16Array( 6 );
18
19 var i = 0;
20 _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex
21 _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc.
22
23 _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1;
24 _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0;
25
26 _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
27 _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
28
29 _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1;
30 _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1;
31
32 i = 0;
33 _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2;
34 _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3;
35
36 // buffers
37
38 _lensFlare.vertexBuffer = _gl.createBuffer();
39 _lensFlare.elementBuffer = _gl.createBuffer();
40
41 _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
42 _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
43
44 _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
45 _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
46
47 // textures
48
49 _lensFlare.tempTexture = _gl.createTexture();
50 _lensFlare.occlusionTexture = _gl.createTexture();
51
52 _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
53 _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null );
54 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
55 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
56 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
57 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
58
59 _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
60 _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
61 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
62 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
63 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
64 _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
65
66 if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) {
67
68 _lensFlare.hasVertexTexture = false;
69 _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ] );
70
71 } else {
72
73 _lensFlare.hasVertexTexture = true;
74 _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ] );
75
76 }
77
78 _lensFlare.attributes = {};
79 _lensFlare.uniforms = {};
80
81 _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
82 _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" );
83
84 _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" );
85 _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
86 _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" );
87 _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
88 _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" );
89 _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
90 _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
91 _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
92
93 _lensFlare.attributesEnabled = false;
94
95 };
96
97
98 /*
99 * Render lens flares
100 * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
101 * reads these back and calculates occlusion.
102 * Then _lensFlare.update_lensFlares() is called to re-position and
103 * update transparency of flares. Then they are rendered.
104 *
105 */
106
107 this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
108
109 var flares = scene.__webglFlares,
110 nFlares = flares.length;
111
112 if ( ! nFlares ) return;
113
114 var tempPosition = new THREE.Vector3();
115
116 var invAspect = viewportHeight / viewportWidth,
117 halfViewportWidth = viewportWidth * 0.5,
118 halfViewportHeight = viewportHeight * 0.5;
119
120 var size = 16 / viewportHeight,
121 scale = new THREE.Vector2( size * invAspect, size );
122
123 var screenPosition = new THREE.Vector3( 1, 1, 0 ),
124 screenPositionPixels = new THREE.Vector2( 1, 1 );
125
126 var uniforms = _lensFlare.uniforms,
127 attributes = _lensFlare.attributes;
128
129 // set _lensFlare program and reset blending
130
131 _gl.useProgram( _lensFlare.program );
132
133 if ( ! _lensFlare.attributesEnabled ) {
134
135 _gl.enableVertexAttribArray( _lensFlare.attributes.vertex );
136 _gl.enableVertexAttribArray( _lensFlare.attributes.uv );
137
138 _lensFlare.attributesEnabled = true;
139
140 }
141
142 // loop through all lens flares to update their occlusion and positions
143 // setup gl and common used attribs/unforms
144
145 _gl.uniform1i( uniforms.occlusionMap, 0 );
146 _gl.uniform1i( uniforms.map, 1 );
147
148 _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
149 _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
150 _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
151
152 _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
153
154 _gl.disable( _gl.CULL_FACE );
155 _gl.depthMask( false );
156
157 var i, j, jl, flare, sprite;
158
159 for ( i = 0; i < nFlares; i ++ ) {
160
161 size = 16 / viewportHeight;
162 scale.set( size * invAspect, size );
163
164 // calc object screen position
165
166 flare = flares[ i ];
167
168 tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] );
169
170 camera.matrixWorldInverse.multiplyVector3( tempPosition );
171 camera.projectionMatrix.multiplyVector3( tempPosition );
172
173 // setup arrays for gl programs
174
175 screenPosition.copy( tempPosition )
176
177 screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth;
178 screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight;
179
180 // screen cull
181
182 if ( _lensFlare.hasVertexTexture || (
183 screenPositionPixels.x > 0 &&
184 screenPositionPixels.x < viewportWidth &&
185 screenPositionPixels.y > 0 &&
186 screenPositionPixels.y < viewportHeight ) ) {
187
188 // save current RGB to temp texture
189
190 _gl.activeTexture( _gl.TEXTURE1 );
191 _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
192 _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
193
194
195 // render pink quad
196
197 _gl.uniform1i( uniforms.renderType, 0 );
198 _gl.uniform2f( uniforms.scale, scale.x, scale.y );
199 _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
200
201 _gl.disable( _gl.BLEND );
202 _gl.enable( _gl.DEPTH_TEST );
203
204 _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
205
206
207 // copy result to occlusionMap
208
209 _gl.activeTexture( _gl.TEXTURE0 );
210 _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
211 _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
212
213
214 // restore graphics
215
216 _gl.uniform1i( uniforms.renderType, 1 );
217 _gl.disable( _gl.DEPTH_TEST );
218
219 _gl.activeTexture( _gl.TEXTURE1 );
220 _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
221 _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
222
223
224 // update object positions
225
226 flare.positionScreen.copy( screenPosition )
227
228 if ( flare.customUpdateCallback ) {
229
230 flare.customUpdateCallback( flare );
231
232 } else {
233
234 flare.updateLensFlares();
235
236 }
237
238 // render flares
239
240 _gl.uniform1i( uniforms.renderType, 2 );
241 _gl.enable( _gl.BLEND );
242
243 for ( j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) {
244
245 sprite = flare.lensFlares[ j ];
246
247 if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) {
248
249 screenPosition.x = sprite.x;
250 screenPosition.y = sprite.y;
251 screenPosition.z = sprite.z;
252
253 size = sprite.size * sprite.scale / viewportHeight;
254
255 scale.x = size * invAspect;
256 scale.y = size;
257
258 _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
259 _gl.uniform2f( uniforms.scale, scale.x, scale.y );
260 _gl.uniform1f( uniforms.rotation, sprite.rotation );
261
262 _gl.uniform1f( uniforms.opacity, sprite.opacity );
263 _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
264
265 _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
266 _renderer.setTexture( sprite.texture, 1 );
267
268 _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
269
270 }
271
272 }
273
274 }
275
276 }
277
278 // restore gl
279
280 _gl.enable( _gl.CULL_FACE );
281 _gl.enable( _gl.DEPTH_TEST );
282 _gl.depthMask( true );
283
284 };
285
286 function createProgram ( shader ) {
287
288 var program = _gl.createProgram();
289
290 var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
291 var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
292
293 _gl.shaderSource( fragmentShader, shader.fragmentShader );
294 _gl.shaderSource( vertexShader, shader.vertexShader );
295
296 _gl.compileShader( fragmentShader );
297 _gl.compileShader( vertexShader );
298
299 _gl.attachShader( program, fragmentShader );
300 _gl.attachShader( program, vertexShader );
301
302 _gl.linkProgram( program );
303
304 return program;
305
306 };
307
308 };
nike free rn
new balance hombre baratas
cinturones gucci
ugg rebajas
cinturon gucci
ray ban baratas
nike cortez
peuterey mujer
christian louboutin madrid
mbt zapatos
gafas ray ban baratas
mbt ofertas
air max blancas
mbt barcelona
nike air max 90
woolrich barcelona
nike mujer
botas ugg
gafas de sol carrera aratas
air max 2016 baratas
oakley baratas
nike air max 2016