InterleavedBuffer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { MathUtils } from '../math/MathUtils.js';
  2. import { StaticDrawUsage } from '../constants.js';
  3. /**
  4. * @author benaadams / https://twitter.com/ben_a_adams
  5. */
  6. function InterleavedBuffer( array, stride ) {
  7. this.array = array;
  8. this.stride = stride;
  9. this.count = array !== undefined ? array.length / stride : 0;
  10. this.usage = StaticDrawUsage;
  11. this.updateRange = { offset: 0, count: - 1 };
  12. this.version = 0;
  13. this.uuid = MathUtils.generateUUID();
  14. }
  15. Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', {
  16. set: function ( value ) {
  17. if ( value === true ) this.version ++;
  18. }
  19. } );
  20. Object.assign( InterleavedBuffer.prototype, {
  21. isInterleavedBuffer: true,
  22. onUploadCallback: function () {},
  23. setUsage: function ( value ) {
  24. this.usage = value;
  25. return this;
  26. },
  27. copy: function ( source ) {
  28. this.array = new source.array.constructor( source.array );
  29. this.count = source.count;
  30. this.stride = source.stride;
  31. this.usage = source.usage;
  32. return this;
  33. },
  34. copyAt: function ( index1, attribute, index2 ) {
  35. index1 *= this.stride;
  36. index2 *= attribute.stride;
  37. for ( let i = 0, l = this.stride; i < l; i ++ ) {
  38. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  39. }
  40. return this;
  41. },
  42. set: function ( value, offset ) {
  43. if ( offset === undefined ) offset = 0;
  44. this.array.set( value, offset );
  45. return this;
  46. },
  47. clone: function ( data ) {
  48. if ( data.arrayBuffers === undefined ) {
  49. data.arrayBuffers = {};
  50. }
  51. if ( this.array.buffer._uuid === undefined ) {
  52. this.array.buffer._uuid = MathUtils.generateUUID();
  53. }
  54. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  55. data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer;
  56. }
  57. const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] );
  58. const ib = new InterleavedBuffer( array, this.stride );
  59. ib.setUsage( this.usage );
  60. return ib;
  61. },
  62. onUpload: function ( callback ) {
  63. this.onUploadCallback = callback;
  64. return this;
  65. },
  66. toJSON: function ( data ) {
  67. if ( data.arrayBuffers === undefined ) {
  68. data.arrayBuffers = {};
  69. }
  70. // generate UUID for array buffer if necessary
  71. if ( this.array.buffer._uuid === undefined ) {
  72. this.array.buffer._uuid = MathUtils.generateUUID();
  73. }
  74. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  75. data.arrayBuffers[ this.array.buffer._uuid ] = Array.prototype.slice.call( new Uint32Array( this.array.buffer ) );
  76. }
  77. //
  78. return {
  79. uuid: this.uuid,
  80. buffer: this.array.buffer._uuid,
  81. type: this.array.constructor.name,
  82. stride: this.stride
  83. };
  84. }
  85. } );
  86. export { InterleavedBuffer };