UNPKG

9.69 kBJavaScriptView Raw
1import { Vector4 } from '../math/Vector4.js';
2import { Vector3 } from '../math/Vector3.js';
3import { Vector2 } from '../math/Vector2.js';
4import { Color } from '../math/Color.js';
5import { StaticDrawUsage } from '../constants.js';
6
7const _vector = new Vector3();
8const _vector2 = new Vector2();
9
10function BufferAttribute( array, itemSize, normalized ) {
11
12 if ( Array.isArray( array ) ) {
13
14 throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
15
16 }
17
18 this.name = '';
19
20 this.array = array;
21 this.itemSize = itemSize;
22 this.count = array !== undefined ? array.length / itemSize : 0;
23 this.normalized = normalized === true;
24
25 this.usage = StaticDrawUsage;
26 this.updateRange = { offset: 0, count: - 1 };
27
28 this.version = 0;
29
30}
31
32Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', {
33
34 set: function ( value ) {
35
36 if ( value === true ) this.version ++;
37
38 }
39
40} );
41
42Object.assign( BufferAttribute.prototype, {
43
44 isBufferAttribute: true,
45
46 onUploadCallback: function () {},
47
48 setUsage: function ( value ) {
49
50 this.usage = value;
51
52 return this;
53
54 },
55
56 copy: function ( source ) {
57
58 this.name = source.name;
59 this.array = new source.array.constructor( source.array );
60 this.itemSize = source.itemSize;
61 this.count = source.count;
62 this.normalized = source.normalized;
63
64 this.usage = source.usage;
65
66 return this;
67
68 },
69
70 copyAt: function ( index1, attribute, index2 ) {
71
72 index1 *= this.itemSize;
73 index2 *= attribute.itemSize;
74
75 for ( let i = 0, l = this.itemSize; i < l; i ++ ) {
76
77 this.array[ index1 + i ] = attribute.array[ index2 + i ];
78
79 }
80
81 return this;
82
83 },
84
85 copyArray: function ( array ) {
86
87 this.array.set( array );
88
89 return this;
90
91 },
92
93 copyColorsArray: function ( colors ) {
94
95 const array = this.array;
96 let offset = 0;
97
98 for ( let i = 0, l = colors.length; i < l; i ++ ) {
99
100 let color = colors[ i ];
101
102 if ( color === undefined ) {
103
104 console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
105 color = new Color();
106
107 }
108
109 array[ offset ++ ] = color.r;
110 array[ offset ++ ] = color.g;
111 array[ offset ++ ] = color.b;
112
113 }
114
115 return this;
116
117 },
118
119 copyVector2sArray: function ( vectors ) {
120
121 const array = this.array;
122 let offset = 0;
123
124 for ( let i = 0, l = vectors.length; i < l; i ++ ) {
125
126 let vector = vectors[ i ];
127
128 if ( vector === undefined ) {
129
130 console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
131 vector = new Vector2();
132
133 }
134
135 array[ offset ++ ] = vector.x;
136 array[ offset ++ ] = vector.y;
137
138 }
139
140 return this;
141
142 },
143
144 copyVector3sArray: function ( vectors ) {
145
146 const array = this.array;
147 let offset = 0;
148
149 for ( let i = 0, l = vectors.length; i < l; i ++ ) {
150
151 let vector = vectors[ i ];
152
153 if ( vector === undefined ) {
154
155 console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
156 vector = new Vector3();
157
158 }
159
160 array[ offset ++ ] = vector.x;
161 array[ offset ++ ] = vector.y;
162 array[ offset ++ ] = vector.z;
163
164 }
165
166 return this;
167
168 },
169
170 copyVector4sArray: function ( vectors ) {
171
172 const array = this.array;
173 let offset = 0;
174
175 for ( let i = 0, l = vectors.length; i < l; i ++ ) {
176
177 let vector = vectors[ i ];
178
179 if ( vector === undefined ) {
180
181 console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
182 vector = new Vector4();
183
184 }
185
186 array[ offset ++ ] = vector.x;
187 array[ offset ++ ] = vector.y;
188 array[ offset ++ ] = vector.z;
189 array[ offset ++ ] = vector.w;
190
191 }
192
193 return this;
194
195 },
196
197 applyMatrix3: function ( m ) {
198
199 if ( this.itemSize === 2 ) {
200
201 for ( let i = 0, l = this.count; i < l; i ++ ) {
202
203 _vector2.fromBufferAttribute( this, i );
204 _vector2.applyMatrix3( m );
205
206 this.setXY( i, _vector2.x, _vector2.y );
207
208 }
209
210 } else if ( this.itemSize === 3 ) {
211
212 for ( let i = 0, l = this.count; i < l; i ++ ) {
213
214 _vector.fromBufferAttribute( this, i );
215 _vector.applyMatrix3( m );
216
217 this.setXYZ( i, _vector.x, _vector.y, _vector.z );
218
219 }
220
221 }
222
223 return this;
224
225 },
226
227 applyMatrix4: function ( m ) {
228
229 for ( let i = 0, l = this.count; i < l; i ++ ) {
230
231 _vector.x = this.getX( i );
232 _vector.y = this.getY( i );
233 _vector.z = this.getZ( i );
234
235 _vector.applyMatrix4( m );
236
237 this.setXYZ( i, _vector.x, _vector.y, _vector.z );
238
239 }
240
241 return this;
242
243 },
244
245 applyNormalMatrix: function ( m ) {
246
247 for ( let i = 0, l = this.count; i < l; i ++ ) {
248
249 _vector.x = this.getX( i );
250 _vector.y = this.getY( i );
251 _vector.z = this.getZ( i );
252
253 _vector.applyNormalMatrix( m );
254
255 this.setXYZ( i, _vector.x, _vector.y, _vector.z );
256
257 }
258
259 return this;
260
261 },
262
263 transformDirection: function ( m ) {
264
265 for ( let i = 0, l = this.count; i < l; i ++ ) {
266
267 _vector.x = this.getX( i );
268 _vector.y = this.getY( i );
269 _vector.z = this.getZ( i );
270
271 _vector.transformDirection( m );
272
273 this.setXYZ( i, _vector.x, _vector.y, _vector.z );
274
275 }
276
277 return this;
278
279 },
280
281 set: function ( value, offset ) {
282
283 if ( offset === undefined ) offset = 0;
284
285 this.array.set( value, offset );
286
287 return this;
288
289 },
290
291 getX: function ( index ) {
292
293 return this.array[ index * this.itemSize ];
294
295 },
296
297 setX: function ( index, x ) {
298
299 this.array[ index * this.itemSize ] = x;
300
301 return this;
302
303 },
304
305 getY: function ( index ) {
306
307 return this.array[ index * this.itemSize + 1 ];
308
309 },
310
311 setY: function ( index, y ) {
312
313 this.array[ index * this.itemSize + 1 ] = y;
314
315 return this;
316
317 },
318
319 getZ: function ( index ) {
320
321 return this.array[ index * this.itemSize + 2 ];
322
323 },
324
325 setZ: function ( index, z ) {
326
327 this.array[ index * this.itemSize + 2 ] = z;
328
329 return this;
330
331 },
332
333 getW: function ( index ) {
334
335 return this.array[ index * this.itemSize + 3 ];
336
337 },
338
339 setW: function ( index, w ) {
340
341 this.array[ index * this.itemSize + 3 ] = w;
342
343 return this;
344
345 },
346
347 setXY: function ( index, x, y ) {
348
349 index *= this.itemSize;
350
351 this.array[ index + 0 ] = x;
352 this.array[ index + 1 ] = y;
353
354 return this;
355
356 },
357
358 setXYZ: function ( index, x, y, z ) {
359
360 index *= this.itemSize;
361
362 this.array[ index + 0 ] = x;
363 this.array[ index + 1 ] = y;
364 this.array[ index + 2 ] = z;
365
366 return this;
367
368 },
369
370 setXYZW: function ( index, x, y, z, w ) {
371
372 index *= this.itemSize;
373
374 this.array[ index + 0 ] = x;
375 this.array[ index + 1 ] = y;
376 this.array[ index + 2 ] = z;
377 this.array[ index + 3 ] = w;
378
379 return this;
380
381 },
382
383 onUpload: function ( callback ) {
384
385 this.onUploadCallback = callback;
386
387 return this;
388
389 },
390
391 clone: function () {
392
393 return new this.constructor( this.array, this.itemSize ).copy( this );
394
395 },
396
397 toJSON: function () {
398
399 return {
400 itemSize: this.itemSize,
401 type: this.array.constructor.name,
402 array: Array.prototype.slice.call( this.array ),
403 normalized: this.normalized
404 };
405
406 }
407
408} );
409
410//
411
412function Int8BufferAttribute( array, itemSize, normalized ) {
413
414 BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
415
416}
417
418Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
419Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
420
421
422function Uint8BufferAttribute( array, itemSize, normalized ) {
423
424 BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
425
426}
427
428Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
429Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
430
431
432function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
433
434 BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
435
436}
437
438Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
439Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
440
441
442function Int16BufferAttribute( array, itemSize, normalized ) {
443
444 BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
445
446}
447
448Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
449Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
450
451
452function Uint16BufferAttribute( array, itemSize, normalized ) {
453
454 BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
455
456}
457
458Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
459Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
460
461
462function Int32BufferAttribute( array, itemSize, normalized ) {
463
464 BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
465
466}
467
468Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
469Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
470
471
472function Uint32BufferAttribute( array, itemSize, normalized ) {
473
474 BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
475
476}
477
478Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
479Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
480
481
482function Float32BufferAttribute( array, itemSize, normalized ) {
483
484 BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
485
486}
487
488Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
489Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
490
491
492function Float64BufferAttribute( array, itemSize, normalized ) {
493
494 BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
495
496}
497
498Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
499Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
500
501//
502
503export {
504 Float64BufferAttribute,
505 Float32BufferAttribute,
506 Uint32BufferAttribute,
507 Int32BufferAttribute,
508 Uint16BufferAttribute,
509 Int16BufferAttribute,
510 Uint8ClampedBufferAttribute,
511 Uint8BufferAttribute,
512 Int8BufferAttribute,
513 BufferAttribute
514};