InterleavedBuffer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = 0 ) {
  40. this.array.set( value, offset );
  41. return this;
  42. },
  43. clone: function ( data ) {
  44. if ( data.arrayBuffers === undefined ) {
  45. data.arrayBuffers = {};
  46. }
  47. if ( this.array.buffer._uuid === undefined ) {
  48. this.array.buffer._uuid = MathUtils.generateUUID();
  49. }
  50. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  51. data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer;
  52. }
  53. const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] );
  54. const ib = new InterleavedBuffer( array, this.stride );
  55. ib.setUsage( this.usage );
  56. return ib;
  57. },
  58. onUpload: function ( callback ) {
  59. this.onUploadCallback = callback;
  60. return this;
  61. },
  62. toJSON: function ( data ) {
  63. if ( data.arrayBuffers === undefined ) {
  64. data.arrayBuffers = {};
  65. }
  66. // generate UUID for array buffer if necessary
  67. if ( this.array.buffer._uuid === undefined ) {
  68. this.array.buffer._uuid = MathUtils.generateUUID();
  69. }
  70. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  71. data.arrayBuffers[ this.array.buffer._uuid ] = Array.prototype.slice.call( new Uint32Array( this.array.buffer ) );
  72. }
  73. //
  74. return {
  75. uuid: this.uuid,
  76. buffer: this.array.buffer._uuid,
  77. type: this.array.constructor.name,
  78. stride: this.stride
  79. };
  80. }
  81. } );
  82. export { InterleavedBuffer };
粤ICP备19079148号