BufferGeometryLoader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. import { getTypedArray } from '../utils.js';
  12. class BufferGeometryLoader extends Loader {
  13. constructor( manager ) {
  14. super( manager );
  15. }
  16. load( 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( 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 = getTypedArray( 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 = getTypedArray( 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 = getTypedArray( 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. if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );
  77. if ( attribute.updateRange !== undefined ) {
  78. bufferAttribute.updateRange.offset = attribute.updateRange.offset;
  79. bufferAttribute.updateRange.count = attribute.updateRange.count;
  80. }
  81. geometry.setAttribute( key, bufferAttribute );
  82. }
  83. const morphAttributes = json.data.morphAttributes;
  84. if ( morphAttributes ) {
  85. for ( const key in morphAttributes ) {
  86. const attributeArray = morphAttributes[ key ];
  87. const array = [];
  88. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  89. const attribute = attributeArray[ i ];
  90. let bufferAttribute;
  91. if ( attribute.isInterleavedBufferAttribute ) {
  92. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  93. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  94. } else {
  95. const typedArray = getTypedArray( attribute.type, attribute.array );
  96. bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  97. }
  98. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  99. array.push( bufferAttribute );
  100. }
  101. geometry.morphAttributes[ key ] = array;
  102. }
  103. }
  104. const morphTargetsRelative = json.data.morphTargetsRelative;
  105. if ( morphTargetsRelative ) {
  106. geometry.morphTargetsRelative = true;
  107. }
  108. const groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  109. if ( groups !== undefined ) {
  110. for ( let i = 0, n = groups.length; i !== n; ++ i ) {
  111. const group = groups[ i ];
  112. geometry.addGroup( group.start, group.count, group.materialIndex );
  113. }
  114. }
  115. const boundingSphere = json.data.boundingSphere;
  116. if ( boundingSphere !== undefined ) {
  117. const center = new Vector3();
  118. if ( boundingSphere.center !== undefined ) {
  119. center.fromArray( boundingSphere.center );
  120. }
  121. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  122. }
  123. if ( json.name ) geometry.name = json.name;
  124. if ( json.userData ) geometry.userData = json.userData;
  125. return geometry;
  126. }
  127. }
  128. export { BufferGeometryLoader };
粤ICP备19079148号