InterleavedBuffer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import * as MathUtils from '../math/MathUtils.js';
  2. import { StaticDrawUsage } from '../constants.js';
  3. class InterleavedBuffer {
  4. constructor( array, stride ) {
  5. this.isInterleavedBuffer = true;
  6. this.array = array;
  7. this.stride = stride;
  8. this.count = array !== undefined ? array.length / stride : 0;
  9. this.usage = StaticDrawUsage;
  10. this._updateRange = { offset: 0, count: - 1 };
  11. this.updateRanges = [];
  12. this.version = 0;
  13. this.uuid = MathUtils.generateUUID();
  14. }
  15. onUploadCallback() {}
  16. set needsUpdate( value ) {
  17. if ( value === true ) this.version ++;
  18. }
  19. get updateRange() {
  20. console.warn( 'THREE.InterleavedBuffer: updateRange() is deprecated and will be removed in r169. Use addUpdateRange() instead.' ); // @deprecated, r159
  21. return this._updateRange;
  22. }
  23. setUsage( value ) {
  24. this.usage = value;
  25. return this;
  26. }
  27. addUpdateRange( start, count ) {
  28. this.updateRanges.push( { start, count } );
  29. }
  30. clearUpdateRanges() {
  31. this.updateRanges.length = 0;
  32. }
  33. copy( source ) {
  34. this.array = new source.array.constructor( source.array );
  35. this.count = source.count;
  36. this.stride = source.stride;
  37. this.usage = source.usage;
  38. return this;
  39. }
  40. copyAt( index1, attribute, index2 ) {
  41. index1 *= this.stride;
  42. index2 *= attribute.stride;
  43. for ( let i = 0, l = this.stride; i < l; i ++ ) {
  44. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  45. }
  46. return this;
  47. }
  48. set( value, offset = 0 ) {
  49. this.array.set( value, offset );
  50. return this;
  51. }
  52. clone( data ) {
  53. if ( data.arrayBuffers === undefined ) {
  54. data.arrayBuffers = {};
  55. }
  56. if ( this.array.buffer._uuid === undefined ) {
  57. this.array.buffer._uuid = MathUtils.generateUUID();
  58. }
  59. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  60. data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer;
  61. }
  62. const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] );
  63. const ib = new this.constructor( array, this.stride );
  64. ib.setUsage( this.usage );
  65. return ib;
  66. }
  67. onUpload( callback ) {
  68. this.onUploadCallback = callback;
  69. return this;
  70. }
  71. toJSON( data ) {
  72. if ( data.arrayBuffers === undefined ) {
  73. data.arrayBuffers = {};
  74. }
  75. // generate UUID for array buffer if necessary
  76. if ( this.array.buffer._uuid === undefined ) {
  77. this.array.buffer._uuid = MathUtils.generateUUID();
  78. }
  79. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  80. data.arrayBuffers[ this.array.buffer._uuid ] = Array.from( new Uint32Array( this.array.buffer ) );
  81. }
  82. //
  83. return {
  84. uuid: this.uuid,
  85. buffer: this.array.buffer._uuid,
  86. type: this.array.constructor.name,
  87. stride: this.stride
  88. };
  89. }
  90. }
  91. export { InterleavedBuffer };