UNPKG

8.27 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';
5
6/**
7 * @author mrdoob / http://mrdoob.com/
8 */
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.dynamic = false;
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 setArray: function ( array ) {
49
50 if ( Array.isArray( array ) ) {
51
52 throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
53
54 }
55
56 this.count = array !== undefined ? array.length / this.itemSize : 0;
57 this.array = array;
58
59 return this;
60
61 },
62
63 setDynamic: function ( value ) {
64
65 this.dynamic = value;
66
67 return this;
68
69 },
70
71 copy: function ( source ) {
72
73 this.name = source.name;
74 this.array = new source.array.constructor( source.array );
75 this.itemSize = source.itemSize;
76 this.count = source.count;
77 this.normalized = source.normalized;
78
79 this.dynamic = source.dynamic;
80
81 return this;
82
83 },
84
85 copyAt: function ( index1, attribute, index2 ) {
86
87 index1 *= this.itemSize;
88 index2 *= attribute.itemSize;
89
90 for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
91
92 this.array[ index1 + i ] = attribute.array[ index2 + i ];
93
94 }
95
96 return this;
97
98 },
99
100 copyArray: function ( array ) {
101
102 this.array.set( array );
103
104 return this;
105
106 },
107
108 copyColorsArray: function ( colors ) {
109
110 var array = this.array, offset = 0;
111
112 for ( var i = 0, l = colors.length; i < l; i ++ ) {
113
114 var color = colors[ i ];
115
116 if ( color === undefined ) {
117
118 console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
119 color = new Color();
120
121 }
122
123 array[ offset ++ ] = color.r;
124 array[ offset ++ ] = color.g;
125 array[ offset ++ ] = color.b;
126
127 }
128
129 return this;
130
131 },
132
133 copyVector2sArray: function ( vectors ) {
134
135 var array = this.array, offset = 0;
136
137 for ( var i = 0, l = vectors.length; i < l; i ++ ) {
138
139 var vector = vectors[ i ];
140
141 if ( vector === undefined ) {
142
143 console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
144 vector = new Vector2();
145
146 }
147
148 array[ offset ++ ] = vector.x;
149 array[ offset ++ ] = vector.y;
150
151 }
152
153 return this;
154
155 },
156
157 copyVector3sArray: function ( vectors ) {
158
159 var array = this.array, offset = 0;
160
161 for ( var i = 0, l = vectors.length; i < l; i ++ ) {
162
163 var vector = vectors[ i ];
164
165 if ( vector === undefined ) {
166
167 console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
168 vector = new Vector3();
169
170 }
171
172 array[ offset ++ ] = vector.x;
173 array[ offset ++ ] = vector.y;
174 array[ offset ++ ] = vector.z;
175
176 }
177
178 return this;
179
180 },
181
182 copyVector4sArray: function ( vectors ) {
183
184 var array = this.array, offset = 0;
185
186 for ( var i = 0, l = vectors.length; i < l; i ++ ) {
187
188 var vector = vectors[ i ];
189
190 if ( vector === undefined ) {
191
192 console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
193 vector = new Vector4();
194
195 }
196
197 array[ offset ++ ] = vector.x;
198 array[ offset ++ ] = vector.y;
199 array[ offset ++ ] = vector.z;
200 array[ offset ++ ] = vector.w;
201
202 }
203
204 return this;
205
206 },
207
208 set: function ( value, offset ) {
209
210 if ( offset === undefined ) offset = 0;
211
212 this.array.set( value, offset );
213
214 return this;
215
216 },
217
218 getX: function ( index ) {
219
220 return this.array[ index * this.itemSize ];
221
222 },
223
224 setX: function ( index, x ) {
225
226 this.array[ index * this.itemSize ] = x;
227
228 return this;
229
230 },
231
232 getY: function ( index ) {
233
234 return this.array[ index * this.itemSize + 1 ];
235
236 },
237
238 setY: function ( index, y ) {
239
240 this.array[ index * this.itemSize + 1 ] = y;
241
242 return this;
243
244 },
245
246 getZ: function ( index ) {
247
248 return this.array[ index * this.itemSize + 2 ];
249
250 },
251
252 setZ: function ( index, z ) {
253
254 this.array[ index * this.itemSize + 2 ] = z;
255
256 return this;
257
258 },
259
260 getW: function ( index ) {
261
262 return this.array[ index * this.itemSize + 3 ];
263
264 },
265
266 setW: function ( index, w ) {
267
268 this.array[ index * this.itemSize + 3 ] = w;
269
270 return this;
271
272 },
273
274 setXY: function ( index, x, y ) {
275
276 index *= this.itemSize;
277
278 this.array[ index + 0 ] = x;
279 this.array[ index + 1 ] = y;
280
281 return this;
282
283 },
284
285 setXYZ: function ( index, x, y, z ) {
286
287 index *= this.itemSize;
288
289 this.array[ index + 0 ] = x;
290 this.array[ index + 1 ] = y;
291 this.array[ index + 2 ] = z;
292
293 return this;
294
295 },
296
297 setXYZW: function ( index, x, y, z, w ) {
298
299 index *= this.itemSize;
300
301 this.array[ index + 0 ] = x;
302 this.array[ index + 1 ] = y;
303 this.array[ index + 2 ] = z;
304 this.array[ index + 3 ] = w;
305
306 return this;
307
308 },
309
310 onUpload: function ( callback ) {
311
312 this.onUploadCallback = callback;
313
314 return this;
315
316 },
317
318 clone: function () {
319
320 return new this.constructor( this.array, this.itemSize ).copy( this );
321
322 }
323
324} );
325
326//
327
328function Int8BufferAttribute( array, itemSize, normalized ) {
329
330 BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
331
332}
333
334Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
335Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
336
337
338function Uint8BufferAttribute( array, itemSize, normalized ) {
339
340 BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
341
342}
343
344Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
345Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
346
347
348function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
349
350 BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
351
352}
353
354Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
355Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
356
357
358function Int16BufferAttribute( array, itemSize, normalized ) {
359
360 BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
361
362}
363
364Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
365Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
366
367
368function Uint16BufferAttribute( array, itemSize, normalized ) {
369
370 BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
371
372}
373
374Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
375Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
376
377
378function Int32BufferAttribute( array, itemSize, normalized ) {
379
380 BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
381
382}
383
384Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
385Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
386
387
388function Uint32BufferAttribute( array, itemSize, normalized ) {
389
390 BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
391
392}
393
394Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
395Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
396
397
398function Float32BufferAttribute( array, itemSize, normalized ) {
399
400 BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
401
402}
403
404Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
405Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
406
407
408function Float64BufferAttribute( array, itemSize, normalized ) {
409
410 BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
411
412}
413
414Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
415Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
416
417//
418
419export {
420 Float64BufferAttribute,
421 Float32BufferAttribute,
422 Uint32BufferAttribute,
423 Int32BufferAttribute,
424 Uint16BufferAttribute,
425 Int16BufferAttribute,
426 Uint8ClampedBufferAttribute,
427 Uint8BufferAttribute,
428 Int8BufferAttribute,
429 BufferAttribute
430};