InterleavedBufferAttribute.js 5.7 KB

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