1
0

InterleavedBuffer.js 2.4 KB

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