InterleavedBufferAttribute.js 4.9 KB

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