InterleavedBufferAttribute.js 4.6 KB

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