InterleavedBuffer.js 2.6 KB

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