1 /**
2 * @author alteredq / http://alteredqualia.com/
3 */
4
5 /**@constructor*/
6 THREE.BufferGeometry = function () {
7
8 THREE.GeometryLibrary.push( this );
9
10 this.id = THREE.GeometryIdCount ++;
11
12 // attributes
13
14 this.attributes = {};
15
16 // attributes typed arrays are kept only if dynamic flag is set
17
18 this.dynamic = false;
19
20 // boundings
21
22 this.boundingBox = null;
23 this.boundingSphere = null;
24
25 this.hasTangents = false;
26
27 // for compatibility
28
29 this.morphTargets = [];
30
31 };
32
33 THREE.BufferGeometry.prototype = {
34
35 constructor : THREE.BufferGeometry,
36
37 applyMatrix: function ( matrix ) {
38
39 var positionArray;
40 var normalArray;
41
42 if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array;
43 if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array;
44
45 if ( positionArray !== undefined ) {
46
47 matrix.multiplyVector3Array( positionArray );
48 this.verticesNeedUpdate = true;
49
50 }
51
52 if ( normalArray !== undefined ) {
53
54 var normalMatrix = new THREE.Matrix3();
55 normalMatrix.getInverse( matrix ).transpose();
56
57 normalMatrix.multiplyVector3Array( normalArray );
58
59 this.normalizeNormals();
60
61 this.normalsNeedUpdate = true;
62
63 }
64
65 },
66
67 computeBoundingBox: function () {
68
69 if ( ! this.boundingBox ) {
70
71 this.boundingBox = {
72
73 min: new THREE.Vector3( Infinity, Infinity, Infinity ),
74 max: new THREE.Vector3( -Infinity, -Infinity, -Infinity )
75
76 };
77
78 }
79
80 var positions = this.attributes[ "position" ].array;
81
82 if ( positions ) {
83
84 var bb = this.boundingBox;
85 var x, y, z;
86
87 for ( var i = 0, il = positions.length; i < il; i += 3 ) {
88
89 x = positions[ i ];
90 y = positions[ i + 1 ];
91 z = positions[ i + 2 ];
92
93 // bounding box
94
95 if ( x < bb.min.x ) {
96
97 bb.min.x = x;
98
99 } else if ( x > bb.max.x ) {
100
101 bb.max.x = x;
102
103 }
104
105 if ( y < bb.min.y ) {
106
107 bb.min.y = y;
108
109 } else if ( y > bb.max.y ) {
110
111 bb.max.y = y;
112
113 }
114
115 if ( z < bb.min.z ) {
116
117 bb.min.z = z;
118
119 } else if ( z > bb.max.z ) {
120
121 bb.max.z = z;
122
123 }
124
125 }
126
127 }
128
129 if ( positions === undefined || positions.length === 0 ) {
130
131 this.boundingBox.min.set( 0, 0, 0 );
132 this.boundingBox.max.set( 0, 0, 0 );
133
134 }
135
136 },
137
138 computeBoundingSphere: function () {
139
140 if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
141
142 var positions = this.attributes[ "position" ].array;
143
144 if ( positions ) {
145
146 var radiusSq, maxRadiusSq = 0;
147 var x, y, z;
148
149 for ( var i = 0, il = positions.length; i < il; i += 3 ) {
150
151 x = positions[ i ];
152 y = positions[ i + 1 ];
153 z = positions[ i + 2 ];
154
155 radiusSq = x * x + y * y + z * z;
156 if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
157
158 }
159
160 this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
161
162 }
163
164 },
165
166 computeVertexNormals: function () {
167
168 if ( this.attributes[ "position" ] && this.attributes[ "index" ] ) {
169
170 var i, il;
171 var j, jl;
172
173 var nVertexElements = this.attributes[ "position" ].array.length;
174
175 if ( this.attributes[ "normal" ] === undefined ) {
176
177 this.attributes[ "normal" ] = {
178
179 itemSize: 3,
180 array: new Float32Array( nVertexElements ),
181 numItems: nVertexElements
182
183 };
184
185 } else {
186
187 // reset existing normals to zero
188
189 for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) {
190
191 this.attributes[ "normal" ].array[ i ] = 0;
192
193 }
194
195 }
196
197 var offsets = this.offsets;
198
199 var indices = this.attributes[ "index" ].array;
200 var positions = this.attributes[ "position" ].array;
201 var normals = this.attributes[ "normal" ].array;
202
203 var vA, vB, vC, x, y, z,
204
205 pA = new THREE.Vector3(),
206 pB = new THREE.Vector3(),
207 pC = new THREE.Vector3(),
208
209 cb = new THREE.Vector3(),
210 ab = new THREE.Vector3();
211
212 for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
213
214 var start = offsets[ j ].start;
215 var count = offsets[ j ].count;
216 var index = offsets[ j ].index;
217
218 for ( i = start, il = start + count; i < il; i += 3 ) {
219
220 vA = index + indices[ i ];
221 vB = index + indices[ i + 1 ];
222 vC = index + indices[ i + 2 ];
223
224 x = positions[ vA * 3 ];
225 y = positions[ vA * 3 + 1 ];
226 z = positions[ vA * 3 + 2 ];
227 pA.set( x, y, z );
228
229 x = positions[ vB * 3 ];
230 y = positions[ vB * 3 + 1 ];
231 z = positions[ vB * 3 + 2 ];
232 pB.set( x, y, z );
233
234 x = positions[ vC * 3 ];
235 y = positions[ vC * 3 + 1 ];
236 z = positions[ vC * 3 + 2 ];
237 pC.set( x, y, z );
238
239 cb.sub( pC, pB );
240 ab.sub( pA, pB );
241 cb.crossSelf( ab );
242
243 normals[ vA * 3 ] += cb.x;
244 normals[ vA * 3 + 1 ] += cb.y;
245 normals[ vA * 3 + 2 ] += cb.z;
246
247 normals[ vB * 3 ] += cb.x;
248 normals[ vB * 3 + 1 ] += cb.y;
249 normals[ vB * 3 + 2 ] += cb.z;
250
251 normals[ vC * 3 ] += cb.x;
252 normals[ vC * 3 + 1 ] += cb.y;
253 normals[ vC * 3 + 2 ] += cb.z;
254
255 }
256
257 }
258
259 this.normalizeNormals();
260
261 this.normalsNeedUpdate = true;
262
263 }
264
265 },
266
267 normalizeNormals: function () {
268
269 var normals = this.attributes[ "normal" ].array;
270
271 var x, y, z, n;
272
273 for ( var i = 0, il = normals.length; i < il; i += 3 ) {
274
275 x = normals[ i ];
276 y = normals[ i + 1 ];
277 z = normals[ i + 2 ];
278
279 n = 1.0 / Math.sqrt( x * x + y * y + z * z );
280
281 normals[ i ] *= n;
282 normals[ i + 1 ] *= n;
283 normals[ i + 2 ] *= n;
284
285 }
286
287 },
288
289 computeTangents: function () {
290
291 // based on http://www.terathon.com/code/tangent.html
292 // (per vertex tangents)
293
294 if ( this.attributes[ "index" ] === undefined ||
295 this.attributes[ "position" ] === undefined ||
296 this.attributes[ "normal" ] === undefined ||
297 this.attributes[ "uv" ] === undefined ) {
298
299 console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" );
300 return;
301
302 }
303
304 var indices = this.attributes[ "index" ].array;
305 var positions = this.attributes[ "position" ].array;
306 var normals = this.attributes[ "normal" ].array;
307 var uvs = this.attributes[ "uv" ].array;
308
309 var nVertices = positions.length / 3;
310
311 if ( this.attributes[ "tangent" ] === undefined ) {
312
313 var nTangentElements = 4 * nVertices;
314
315 this.attributes[ "tangent" ] = {
316
317 itemSize: 4,
318 array: new Float32Array( nTangentElements ),
319 numItems: nTangentElements
320
321 };
322
323 }
324
325 var tangents = this.attributes[ "tangent" ].array;
326
327 var tan1 = [], tan2 = [];
328
329 for ( var k = 0; k < nVertices; k ++ ) {
330
331 tan1[ k ] = new THREE.Vector3();
332 tan2[ k ] = new THREE.Vector3();
333
334 }
335
336 var xA, yA, zA,
337 xB, yB, zB,
338 xC, yC, zC,
339
340 uA, vA,
341 uB, vB,
342 uC, vC,
343
344 x1, x2, y1, y2, z1, z2,
345 s1, s2, t1, t2, r;
346
347 var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
348
349 function handleTriangle( a, b, c ) {
350
351 xA = positions[ a * 3 ];
352 yA = positions[ a * 3 + 1 ];
353 zA = positions[ a * 3 + 2 ];
354
355 xB = positions[ b * 3 ];
356 yB = positions[ b * 3 + 1 ];
357 zB = positions[ b * 3 + 2 ];
358
359 xC = positions[ c * 3 ];
360 yC = positions[ c * 3 + 1 ];
361 zC = positions[ c * 3 + 2 ];
362
363 uA = uvs[ a * 2 ];
364 vA = uvs[ a * 2 + 1 ];
365
366 uB = uvs[ b * 2 ];
367 vB = uvs[ b * 2 + 1 ];
368
369 uC = uvs[ c * 2 ];
370 vC = uvs[ c * 2 + 1 ];
371
372 x1 = xB - xA;
373 x2 = xC - xA;
374
375 y1 = yB - yA;
376 y2 = yC - yA;
377
378 z1 = zB - zA;
379 z2 = zC - zA;
380
381 s1 = uB - uA;
382 s2 = uC - uA;
383
384 t1 = vB - vA;
385 t2 = vC - vA;
386
387 r = 1.0 / ( s1 * t2 - s2 * t1 );
388
389 sdir.set(
390 ( t2 * x1 - t1 * x2 ) * r,
391 ( t2 * y1 - t1 * y2 ) * r,
392 ( t2 * z1 - t1 * z2 ) * r
393 );
394
395 tdir.set(
396 ( s1 * x2 - s2 * x1 ) * r,
397 ( s1 * y2 - s2 * y1 ) * r,
398 ( s1 * z2 - s2 * z1 ) * r
399 );
400
401 tan1[ a ].addSelf( sdir );
402 tan1[ b ].addSelf( sdir );
403 tan1[ c ].addSelf( sdir );
404
405 tan2[ a ].addSelf( tdir );
406 tan2[ b ].addSelf( tdir );
407 tan2[ c ].addSelf( tdir );
408
409 }
410
411 var i, il;
412 var j, jl;
413 var iA, iB, iC;
414
415 var offsets = this.offsets;
416
417 for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
418
419 var start = offsets[ j ].start;
420 var count = offsets[ j ].count;
421 var index = offsets[ j ].index;
422
423 for ( i = start, il = start + count; i < il; i += 3 ) {
424
425 iA = index + indices[ i ];
426 iB = index + indices[ i + 1 ];
427 iC = index + indices[ i + 2 ];
428
429 handleTriangle( iA, iB, iC );
430
431 }
432
433 }
434
435 var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
436 var n = new THREE.Vector3(), n2 = new THREE.Vector3();
437 var w, t, test;
438 var nx, ny, nz;
439
440 function handleVertex( v ) {
441
442 n.x = normals[ v * 3 ];
443 n.y = normals[ v * 3 + 1 ];
444 n.z = normals[ v * 3 + 2 ];
445
446 n2.copy( n );
447
448 t = tan1[ v ];
449
450 // Gram-Schmidt orthogonalize
451
452 tmp.copy( t );
453 tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
454
455 // Calculate handedness
456
457 tmp2.cross( n2, t );
458 test = tmp2.dot( tan2[ v ] );
459 w = ( test < 0.0 ) ? -1.0 : 1.0;
460
461 tangents[ v * 4 ] = tmp.x;
462 tangents[ v * 4 + 1 ] = tmp.y;
463 tangents[ v * 4 + 2 ] = tmp.z;
464 tangents[ v * 4 + 3 ] = w;
465
466 }
467
468 for ( j = 0, jl = offsets.length; j < jl; ++ j ) {
469
470 var start = offsets[ j ].start;
471 var count = offsets[ j ].count;
472 var index = offsets[ j ].index;
473
474 for ( i = start, il = start + count; i < il; i += 3 ) {
475
476 iA = index + indices[ i ];
477 iB = index + indices[ i + 1 ];
478 iC = index + indices[ i + 2 ];
479
480 handleVertex( iA );
481 handleVertex( iB );
482 handleVertex( iC );
483
484 }
485
486 }
487
488 this.hasTangents = true;
489 this.tangentsNeedUpdate = true;
490
491 },
492
493 deallocate: function () {
494
495 var index = THREE.GeometryLibrary.indexOf( this );
496 if ( index !== -1 ) THREE.GeometryLibrary.splice( index, 1 );
497
498 }
499
500 };
501
502
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