1 /**
2 * @author mikael emtinger / http://gomo.se/
3 * @author alteredq / http://alteredqualia.com/
4 */
5
6 /**@constructor*/
7 THREE.SpritePlugin = function ( ) {
8
9 var _gl, _renderer, _sprite = {};
10
11 this.init = function ( renderer ) {
12
13 _gl = renderer.context;
14 _renderer = renderer;
15
16 _sprite.vertices = new Float32Array( 8 + 8 );
17 _sprite.faces = new Uint16Array( 6 );
18
19 var i = 0;
20
21 _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = -1; // vertex 0
22 _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 0; // uv 0
23
24 _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = -1; // vertex 1
25 _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 0; // uv 1
26
27 _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // vertex 2
28 _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // uv 2
29
30 _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = 1; // vertex 3
31 _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 1; // uv 3
32
33 i = 0;
34
35 _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 1; _sprite.faces[ i++ ] = 2;
36 _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 2; _sprite.faces[ i++ ] = 3;
37
38 _sprite.vertexBuffer = _gl.createBuffer();
39 _sprite.elementBuffer = _gl.createBuffer();
40
41 _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
42 _gl.bufferData( _gl.ARRAY_BUFFER, _sprite.vertices, _gl.STATIC_DRAW );
43
44 _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
45 _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _sprite.faces, _gl.STATIC_DRAW );
46
47 _sprite.program = createProgram( THREE.ShaderSprite[ "sprite" ] );
48
49 _sprite.attributes = {};
50 _sprite.uniforms = {};
51
52 _sprite.attributes.position = _gl.getAttribLocation ( _sprite.program, "position" );
53 _sprite.attributes.uv = _gl.getAttribLocation ( _sprite.program, "uv" );
54
55 _sprite.uniforms.uvOffset = _gl.getUniformLocation( _sprite.program, "uvOffset" );
56 _sprite.uniforms.uvScale = _gl.getUniformLocation( _sprite.program, "uvScale" );
57
58 _sprite.uniforms.rotation = _gl.getUniformLocation( _sprite.program, "rotation" );
59 _sprite.uniforms.scale = _gl.getUniformLocation( _sprite.program, "scale" );
60 _sprite.uniforms.alignment = _gl.getUniformLocation( _sprite.program, "alignment" );
61
62 _sprite.uniforms.color = _gl.getUniformLocation( _sprite.program, "color" );
63 _sprite.uniforms.map = _gl.getUniformLocation( _sprite.program, "map" );
64 _sprite.uniforms.opacity = _gl.getUniformLocation( _sprite.program, "opacity" );
65
66 _sprite.uniforms.useScreenCoordinates = _gl.getUniformLocation( _sprite.program, "useScreenCoordinates" );
67 _sprite.uniforms.affectedByDistance = _gl.getUniformLocation( _sprite.program, "affectedByDistance" );
68 _sprite.uniforms.screenPosition = _gl.getUniformLocation( _sprite.program, "screenPosition" );
69 _sprite.uniforms.modelViewMatrix = _gl.getUniformLocation( _sprite.program, "modelViewMatrix" );
70 _sprite.uniforms.projectionMatrix = _gl.getUniformLocation( _sprite.program, "projectionMatrix" );
71
72 _sprite.uniforms.fogType = _gl.getUniformLocation( _sprite.program, "fogType" );
73 _sprite.uniforms.fogDensity = _gl.getUniformLocation( _sprite.program, "fogDensity" );
74 _sprite.uniforms.fogNear = _gl.getUniformLocation( _sprite.program, "fogNear" );
75 _sprite.uniforms.fogFar = _gl.getUniformLocation( _sprite.program, "fogFar" );
76 _sprite.uniforms.fogColor = _gl.getUniformLocation( _sprite.program, "fogColor" );
77
78 _sprite.attributesEnabled = false;
79
80 };
81
82 this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
83
84 var sprites = scene.__webglSprites,
85 nSprites = sprites.length;
86
87 if ( ! nSprites ) return;
88
89 var attributes = _sprite.attributes,
90 uniforms = _sprite.uniforms;
91
92 var invAspect = viewportHeight / viewportWidth;
93
94 var halfViewportWidth = viewportWidth * 0.5,
95 halfViewportHeight = viewportHeight * 0.5;
96
97 var mergeWith3D = true;
98
99 // setup gl
100
101 _gl.useProgram( _sprite.program );
102
103 if ( ! _sprite.attributesEnabled ) {
104
105 _gl.enableVertexAttribArray( attributes.position );
106 _gl.enableVertexAttribArray( attributes.uv );
107
108 _sprite.attributesEnabled = true;
109
110 }
111
112 _gl.disable( _gl.CULL_FACE );
113 _gl.enable( _gl.BLEND );
114 _gl.depthMask( true );
115
116 _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
117 _gl.vertexAttribPointer( attributes.position, 2, _gl.FLOAT, false, 2 * 8, 0 );
118 _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
119
120 _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
121
122 _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera._projectionMatrixArray );
123
124 _gl.activeTexture( _gl.TEXTURE0 );
125 _gl.uniform1i( uniforms.map, 0 );
126
127 var oldFogType = 0;
128 var sceneFogType = 0;
129 var fog = scene.fog;
130
131 if ( fog ) {
132
133 _gl.uniform3f( uniforms.fogColor, fog.color.r, fog.color.g, fog.color.b );
134
135 if ( fog instanceof THREE.Fog ) {
136
137 _gl.uniform1f( uniforms.fogNear, fog.near );
138 _gl.uniform1f( uniforms.fogFar, fog.far );
139
140 _gl.uniform1i( uniforms.fogType, 1 );
141 oldFogType = 1;
142 sceneFogType = 1;
143
144 } else if ( fog instanceof THREE.FogExp2 ) {
145
146 _gl.uniform1f( uniforms.fogDensity, fog.density );
147
148 _gl.uniform1i( uniforms.fogType, 2 );
149 oldFogType = 2;
150 sceneFogType = 2;
151
152 }
153
154 } else {
155
156 _gl.uniform1i( uniforms.fogType, 0 );
157 oldFogType = 0;
158 sceneFogType = 0;
159
160 }
161
162
163 // update positions and sort
164
165 var i, sprite, screenPosition, size, fogType, scale = [];
166
167 for( i = 0; i < nSprites; i ++ ) {
168
169 sprite = sprites[ i ];
170
171 if ( ! sprite.visible || sprite.opacity === 0 ) continue;
172
173 if ( ! sprite.useScreenCoordinates ) {
174
175 sprite._modelViewMatrix.multiply( camera.matrixWorldInverse, sprite.matrixWorld );
176 sprite.z = - sprite._modelViewMatrix.elements[ 14 ];
177
178 } else {
179
180 sprite.z = - sprite.position.z;
181
182 }
183
184 }
185
186 sprites.sort( painterSortStable );
187
188 // render all sprites
189
190 for( i = 0; i < nSprites; i ++ ) {
191
192 sprite = sprites[ i ];
193
194 if ( ! sprite.visible || sprite.opacity === 0 ) continue;
195
196 if ( sprite.map && sprite.map.image && sprite.map.image.width ) {
197
198 if ( sprite.useScreenCoordinates ) {
199
200 _gl.uniform1i( uniforms.useScreenCoordinates, 1 );
201 _gl.uniform3f(
202 uniforms.screenPosition,
203 ( sprite.position.x - halfViewportWidth ) / halfViewportWidth,
204 ( halfViewportHeight - sprite.position.y ) / halfViewportHeight,
205 Math.max( 0, Math.min( 1, sprite.position.z ) )
206 );
207
208 } else {
209
210 _gl.uniform1i( uniforms.useScreenCoordinates, 0 );
211 _gl.uniform1i( uniforms.affectedByDistance, sprite.affectedByDistance ? 1 : 0 );
212 _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrix.elements );
213
214 }
215
216 if ( scene.fog && sprite.fog ) {
217
218 fogType = sceneFogType;
219
220 } else {
221
222 fogType = 0;
223
224 }
225
226 if ( oldFogType !== fogType ) {
227
228 _gl.uniform1i( uniforms.fogType, fogType );
229 oldFogType = fogType;
230
231 }
232
233 size = 1 / ( sprite.scaleByViewport ? viewportHeight : 1 );
234
235 scale[ 0 ] = size * invAspect * sprite.scale.x;
236 scale[ 1 ] = size * sprite.scale.y;
237
238 _gl.uniform2f( uniforms.uvScale, sprite.uvScale.x, sprite.uvScale.y );
239 _gl.uniform2f( uniforms.uvOffset, sprite.uvOffset.x, sprite.uvOffset.y );
240 _gl.uniform2f( uniforms.alignment, sprite.alignment.x, sprite.alignment.y );
241
242 _gl.uniform1f( uniforms.opacity, sprite.opacity );
243 _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
244
245 _gl.uniform1f( uniforms.rotation, sprite.rotation );
246 _gl.uniform2fv( uniforms.scale, scale );
247
248 if ( sprite.mergeWith3D && !mergeWith3D ) {
249
250 _gl.enable( _gl.DEPTH_TEST );
251 mergeWith3D = true;
252
253 } else if ( ! sprite.mergeWith3D && mergeWith3D ) {
254
255 _gl.disable( _gl.DEPTH_TEST );
256 mergeWith3D = false;
257
258 }
259
260 _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
261 _renderer.setTexture( sprite.map, 0 );
262
263 _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
264
265 }
266
267 }
268
269 // restore gl
270
271 _gl.enable( _gl.CULL_FACE );
272 _gl.enable( _gl.DEPTH_TEST );
273 _gl.depthMask( true );
274
275 };
276
277 function createProgram ( shader ) {
278
279 var program = _gl.createProgram();
280
281 var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
282 var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
283
284 _gl.shaderSource( fragmentShader, shader.fragmentShader );
285 _gl.shaderSource( vertexShader, shader.vertexShader );
286
287 _gl.compileShader( fragmentShader );
288 _gl.compileShader( vertexShader );
289
290 _gl.attachShader( program, fragmentShader );
291 _gl.attachShader( program, vertexShader );
292
293 _gl.linkProgram( program );
294
295 return program;
296
297 };
298
299 function painterSortStable ( a, b ) {
300
301 if ( a.z !== b.z ) {
302
303 return b.z - a.z;
304
305 } else {
306
307 return b.id - a.id;
308
309 }
310
311 };
312
313 };
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