123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- import { Vector3 } from '../math/Vector3.js';
- import { BufferAttribute } from './BufferAttribute.js';
- import { denormalize, normalize } from '../math/MathUtils.js';
- const _vector = /*@__PURE__*/ new Vector3();
- class InterleavedBufferAttribute {
- constructor( interleavedBuffer, itemSize, offset, normalized = false ) {
- this.isInterleavedBufferAttribute = true;
- this.name = '';
- this.data = interleavedBuffer;
- this.itemSize = itemSize;
- this.offset = offset;
- this.normalized = normalized === true;
- }
- get count() {
- return this.data.count;
- }
- get array() {
- return this.data.array;
- }
- set needsUpdate( value ) {
- this.data.needsUpdate = value;
- }
- applyMatrix4( m ) {
- for ( let i = 0, l = this.data.count; i < l; i ++ ) {
- _vector.fromBufferAttribute( this, i );
- _vector.applyMatrix4( m );
- this.setXYZ( i, _vector.x, _vector.y, _vector.z );
- }
- return this;
- }
- applyNormalMatrix( m ) {
- for ( let i = 0, l = this.count; i < l; i ++ ) {
- _vector.fromBufferAttribute( this, i );
- _vector.applyNormalMatrix( m );
- this.setXYZ( i, _vector.x, _vector.y, _vector.z );
- }
- return this;
- }
- transformDirection( m ) {
- for ( let i = 0, l = this.count; i < l; i ++ ) {
- _vector.fromBufferAttribute( this, i );
- _vector.transformDirection( m );
- this.setXYZ( i, _vector.x, _vector.y, _vector.z );
- }
- return this;
- }
- setX( index, x ) {
- if ( this.normalized ) x = normalize( x, this.array );
- this.data.array[ index * this.data.stride + this.offset ] = x;
- return this;
- }
- setY( index, y ) {
- if ( this.normalized ) y = normalize( y, this.array );
- this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
- return this;
- }
- setZ( index, z ) {
- if ( this.normalized ) z = normalize( z, this.array );
- this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
- return this;
- }
- setW( index, w ) {
- if ( this.normalized ) w = normalize( w, this.array );
- this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
- return this;
- }
- getX( index ) {
- let x = this.data.array[ index * this.data.stride + this.offset ];
- if ( this.normalized ) x = denormalize( x, this.array );
- return x;
- }
- getY( index ) {
- let y = this.data.array[ index * this.data.stride + this.offset + 1 ];
- if ( this.normalized ) y = denormalize( y, this.array );
- return y;
- }
- getZ( index ) {
- let z = this.data.array[ index * this.data.stride + this.offset + 2 ];
- if ( this.normalized ) z = denormalize( z, this.array );
- return z;
- }
- getW( index ) {
- let w = this.data.array[ index * this.data.stride + this.offset + 3 ];
- if ( this.normalized ) w = denormalize( w, this.array );
- return w;
- }
- setXY( index, x, y ) {
- index = index * this.data.stride + this.offset;
- if ( this.normalized ) {
- x = normalize( x, this.array );
- y = normalize( y, this.array );
- }
- this.data.array[ index + 0 ] = x;
- this.data.array[ index + 1 ] = y;
- return this;
- }
- setXYZ( index, x, y, z ) {
- index = index * this.data.stride + this.offset;
- if ( this.normalized ) {
- x = normalize( x, this.array );
- y = normalize( y, this.array );
- z = normalize( z, this.array );
- }
- this.data.array[ index + 0 ] = x;
- this.data.array[ index + 1 ] = y;
- this.data.array[ index + 2 ] = z;
- return this;
- }
- setXYZW( index, x, y, z, w ) {
- index = index * this.data.stride + this.offset;
- if ( this.normalized ) {
- x = normalize( x, this.array );
- y = normalize( y, this.array );
- z = normalize( z, this.array );
- w = normalize( w, this.array );
- }
- this.data.array[ index + 0 ] = x;
- this.data.array[ index + 1 ] = y;
- this.data.array[ index + 2 ] = z;
- this.data.array[ index + 3 ] = w;
- return this;
- }
- clone( data ) {
- if ( data === undefined ) {
- console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interleaved buffer attribute will de-interleave buffer data.' );
- const array = [];
- for ( let i = 0; i < this.count; i ++ ) {
- const index = i * this.data.stride + this.offset;
- for ( let j = 0; j < this.itemSize; j ++ ) {
- array.push( this.data.array[ index + j ] );
- }
- }
- return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized );
- } else {
- if ( data.interleavedBuffers === undefined ) {
- data.interleavedBuffers = {};
- }
- if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
- data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data );
- }
- return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized );
- }
- }
- toJSON( data ) {
- if ( data === undefined ) {
- console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interleaved buffer attribute will de-interleave buffer data.' );
- const array = [];
- for ( let i = 0; i < this.count; i ++ ) {
- const index = i * this.data.stride + this.offset;
- for ( let j = 0; j < this.itemSize; j ++ ) {
- array.push( this.data.array[ index + j ] );
- }
- }
- // de-interleave data and save it as an ordinary buffer attribute for now
- return {
- itemSize: this.itemSize,
- type: this.array.constructor.name,
- array: array,
- normalized: this.normalized
- };
- } else {
- // save as true interleaved attribute
- if ( data.interleavedBuffers === undefined ) {
- data.interleavedBuffers = {};
- }
- if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
- data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data );
- }
- return {
- isInterleavedBufferAttribute: true,
- itemSize: this.itemSize,
- data: this.data.uuid,
- offset: this.offset,
- normalized: this.normalized
- };
- }
- }
- }
- export { InterleavedBufferAttribute };
|