BufferGeometryLoader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Sphere } from '../math/Sphere';
  2. import { Vector3 } from '../math/Vector3';
  3. import { BufferAttribute } from '../core/BufferAttribute';
  4. import { BufferGeometry } from '../core/BufferGeometry';
  5. import { FileLoader } from './FileLoader';
  6. import { DefaultLoadingManager } from './LoadingManager';
  7. /**
  8. * @author mrdoob / http://mrdoob.com/
  9. */
  10. function BufferGeometryLoader( manager ) {
  11. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  12. }
  13. Object.assign( BufferGeometryLoader.prototype, {
  14. load: function ( url, onLoad, onProgress, onError ) {
  15. var scope = this;
  16. var loader = new FileLoader( scope.manager );
  17. loader.setResponseType( 'json' );
  18. loader.load( url, function ( json ) {
  19. onLoad( scope.parse( json ) );
  20. }, onProgress, onError );
  21. },
  22. parse: function ( json ) {
  23. var geometry = new BufferGeometry();
  24. var index = json.data.index;
  25. var TYPED_ARRAYS = {
  26. 'Int8Array': Int8Array,
  27. 'Uint8Array': Uint8Array,
  28. 'Uint8ClampedArray': Uint8ClampedArray,
  29. 'Int16Array': Int16Array,
  30. 'Uint16Array': Uint16Array,
  31. 'Int32Array': Int32Array,
  32. 'Uint32Array': Uint32Array,
  33. 'Float32Array': Float32Array,
  34. 'Float64Array': Float64Array
  35. };
  36. if ( index !== undefined ) {
  37. var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  38. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  39. }
  40. var attributes = json.data.attributes;
  41. for ( var key in attributes ) {
  42. var attribute = attributes[ key ];
  43. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  44. geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
  45. }
  46. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  47. if ( groups !== undefined ) {
  48. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  49. var group = groups[ i ];
  50. geometry.addGroup( group.start, group.count, group.materialIndex );
  51. }
  52. }
  53. var boundingSphere = json.data.boundingSphere;
  54. if ( boundingSphere !== undefined ) {
  55. var center = new Vector3();
  56. if ( boundingSphere.center !== undefined ) {
  57. center.fromArray( boundingSphere.center );
  58. }
  59. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  60. }
  61. return geometry;
  62. }
  63. } );
  64. export { BufferGeometryLoader };
粤ICP备19079148号