BufferGeometryLoader.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { Sphere } from '../math/Sphere.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { BufferAttribute } from '../core/BufferAttribute.js';
  4. import { BufferGeometry } from '../core/BufferGeometry.js';
  5. import { FileLoader } from './FileLoader.js';
  6. import { Loader } from './Loader.js';
  7. import { InstancedBufferGeometry } from '../core/InstancedBufferGeometry.js';
  8. import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
  9. import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';
  10. import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
  11. function BufferGeometryLoader( manager ) {
  12. Loader.call( this, manager );
  13. }
  14. BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  15. constructor: BufferGeometryLoader,
  16. load: function ( url, onLoad, onProgress, onError ) {
  17. const scope = this;
  18. const loader = new FileLoader( scope.manager );
  19. loader.setPath( scope.path );
  20. loader.setRequestHeader( scope.requestHeader );
  21. loader.setWithCredentials( scope.withCredentials );
  22. loader.load( url, function ( text ) {
  23. try {
  24. onLoad( scope.parse( JSON.parse( text ) ) );
  25. } catch ( e ) {
  26. if ( onError ) {
  27. onError( e );
  28. } else {
  29. console.error( e );
  30. }
  31. scope.manager.itemError( url );
  32. }
  33. }, onProgress, onError );
  34. },
  35. parse: function ( json ) {
  36. const interleavedBufferMap = {};
  37. const arrayBufferMap = {};
  38. function getInterleavedBuffer( json, uuid ) {
  39. if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];
  40. const interleavedBuffers = json.interleavedBuffers;
  41. const interleavedBuffer = interleavedBuffers[ uuid ];
  42. const buffer = getArrayBuffer( json, interleavedBuffer.buffer );
  43. const array = new TYPED_ARRAYS[ interleavedBuffer.type ]( buffer );
  44. const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
  45. ib.uuid = interleavedBuffer.uuid;
  46. interleavedBufferMap[ uuid ] = ib;
  47. return ib;
  48. }
  49. function getArrayBuffer( json, uuid ) {
  50. if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];
  51. const arrayBuffers = json.arrayBuffers;
  52. const arrayBuffer = arrayBuffers[ uuid ];
  53. const ab = new Uint32Array( arrayBuffer ).buffer;
  54. arrayBufferMap[ uuid ] = ab;
  55. return ab;
  56. }
  57. const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
  58. const index = json.data.index;
  59. if ( index !== undefined ) {
  60. const typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  61. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  62. }
  63. const attributes = json.data.attributes;
  64. for ( const key in attributes ) {
  65. const attribute = attributes[ key ];
  66. let bufferAttribute;
  67. if ( attribute.isInterleavedBufferAttribute ) {
  68. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  69. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  70. } else {
  71. const typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  72. const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
  73. bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
  74. }
  75. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  76. geometry.setAttribute( key, bufferAttribute );
  77. }
  78. const morphAttributes = json.data.morphAttributes;
  79. if ( morphAttributes ) {
  80. for ( const key in morphAttributes ) {
  81. const attributeArray = morphAttributes[ key ];
  82. const array = [];
  83. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  84. const attribute = attributeArray[ i ];
  85. let bufferAttribute;
  86. if ( attribute.isInterleavedBufferAttribute ) {
  87. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  88. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  89. } else {
  90. const typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  91. bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  92. }
  93. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  94. array.push( bufferAttribute );
  95. }
  96. geometry.morphAttributes[ key ] = array;
  97. }
  98. }
  99. const morphTargetsRelative = json.data.morphTargetsRelative;
  100. if ( morphTargetsRelative ) {
  101. geometry.morphTargetsRelative = true;
  102. }
  103. const groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  104. if ( groups !== undefined ) {
  105. for ( let i = 0, n = groups.length; i !== n; ++ i ) {
  106. const group = groups[ i ];
  107. geometry.addGroup( group.start, group.count, group.materialIndex );
  108. }
  109. }
  110. const boundingSphere = json.data.boundingSphere;
  111. if ( boundingSphere !== undefined ) {
  112. const center = new Vector3();
  113. if ( boundingSphere.center !== undefined ) {
  114. center.fromArray( boundingSphere.center );
  115. }
  116. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  117. }
  118. if ( json.name ) geometry.name = json.name;
  119. if ( json.userData ) geometry.userData = json.userData;
  120. return geometry;
  121. }
  122. } );
  123. const TYPED_ARRAYS = {
  124. Int8Array: Int8Array,
  125. Uint8Array: Uint8Array,
  126. // Workaround for IE11 pre KB2929437. See #11440
  127. Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
  128. Int16Array: Int16Array,
  129. Uint16Array: Uint16Array,
  130. Int32Array: Int32Array,
  131. Uint32Array: Uint32Array,
  132. Float32Array: Float32Array,
  133. Float64Array: Float64Array
  134. };
  135. export { BufferGeometryLoader };
粤ICP备19079148号