InterleavedBufferAttribute.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { BufferAttribute } from './BufferAttribute.js';
  3. const _vector = /*@__PURE__*/ new Vector3();
  4. class InterleavedBufferAttribute {
  5. constructor( interleavedBuffer, itemSize, offset, normalized = false ) {
  6. this.isInterleavedBufferAttribute = true;
  7. this.name = '';
  8. this.data = interleavedBuffer;
  9. this.itemSize = itemSize;
  10. this.offset = offset;
  11. this.normalized = normalized === true;
  12. }
  13. get count() {
  14. return this.data.count;
  15. }
  16. get array() {
  17. return this.data.array;
  18. }
  19. set needsUpdate( value ) {
  20. this.data.needsUpdate = value;
  21. }
  22. applyMatrix4( m ) {
  23. for ( let i = 0, l = this.data.count; i < l; i ++ ) {
  24. _vector.fromBufferAttribute( this, i );
  25. _vector.applyMatrix4( m );
  26. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  27. }
  28. return this;
  29. }
  30. applyNormalMatrix( m ) {
  31. for ( let i = 0, l = this.count; i < l; i ++ ) {
  32. _vector.fromBufferAttribute( this, i );
  33. _vector.applyNormalMatrix( m );
  34. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  35. }
  36. return this;
  37. }
  38. transformDirection( m ) {
  39. for ( let i = 0, l = this.count; i < l; i ++ ) {
  40. _vector.fromBufferAttribute( this, i );
  41. _vector.transformDirection( m );
  42. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  43. }
  44. return this;
  45. }
  46. setX( index, x ) {
  47. this.data.array[ index * this.data.stride + this.offset ] = x;
  48. return this;
  49. }
  50. setY( index, y ) {
  51. this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
  52. return this;
  53. }
  54. setZ( index, z ) {
  55. this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
  56. return this;
  57. }
  58. setW( index, w ) {
  59. this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
  60. return this;
  61. }
  62. getX( index ) {
  63. return this.data.array[ index * this.data.stride + this.offset ];
  64. }
  65. getY( index ) {
  66. return this.data.array[ index * this.data.stride + this.offset + 1 ];
  67. }
  68. getZ( index ) {
  69. return this.data.array[ index * this.data.stride + this.offset + 2 ];
  70. }
  71. getW( index ) {
  72. return this.data.array[ index * this.data.stride + this.offset + 3 ];
  73. }
  74. setXY( index, x, y ) {
  75. index = index * this.data.stride + this.offset;
  76. this.data.array[ index + 0 ] = x;
  77. this.data.array[ index + 1 ] = y;
  78. return this;
  79. }
  80. setXYZ( index, x, y, z ) {
  81. index = index * this.data.stride + this.offset;
  82. this.data.array[ index + 0 ] = x;
  83. this.data.array[ index + 1 ] = y;
  84. this.data.array[ index + 2 ] = z;
  85. return this;
  86. }
  87. setXYZW( index, x, y, z, w ) {
  88. index = index * this.data.stride + this.offset;
  89. this.data.array[ index + 0 ] = x;
  90. this.data.array[ index + 1 ] = y;
  91. this.data.array[ index + 2 ] = z;
  92. this.data.array[ index + 3 ] = w;
  93. return this;
  94. }
  95. clone( data ) {
  96. if ( data === undefined ) {
  97. console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interleaved buffer attribute will deinterleave buffer data.' );
  98. const array = [];
  99. for ( let i = 0; i < this.count; i ++ ) {
  100. const index = i * this.data.stride + this.offset;
  101. for ( let j = 0; j < this.itemSize; j ++ ) {
  102. array.push( this.data.array[ index + j ] );
  103. }
  104. }
  105. return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized );
  106. } else {
  107. if ( data.interleavedBuffers === undefined ) {
  108. data.interleavedBuffers = {};
  109. }
  110. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  111. data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data );
  112. }
  113. return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized );
  114. }
  115. }
  116. toJSON( data ) {
  117. if ( data === undefined ) {
  118. console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interleaved buffer attribute will deinterleave buffer data.' );
  119. const array = [];
  120. for ( let i = 0; i < this.count; i ++ ) {
  121. const index = i * this.data.stride + this.offset;
  122. for ( let j = 0; j < this.itemSize; j ++ ) {
  123. array.push( this.data.array[ index + j ] );
  124. }
  125. }
  126. // deinterleave data and save it as an ordinary buffer attribute for now
  127. return {
  128. itemSize: this.itemSize,
  129. type: this.array.constructor.name,
  130. array: array,
  131. normalized: this.normalized
  132. };
  133. } else {
  134. // save as true interleaved attribtue
  135. if ( data.interleavedBuffers === undefined ) {
  136. data.interleavedBuffers = {};
  137. }
  138. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  139. data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data );
  140. }
  141. return {
  142. isInterleavedBufferAttribute: true,
  143. itemSize: this.itemSize,
  144. data: this.data.uuid,
  145. offset: this.offset,
  146. normalized: this.normalized
  147. };
  148. }
  149. }
  150. }
  151. export { InterleavedBufferAttribute };