BufferGeometryLoader.js 5.9 KB

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