BufferGeometryLoader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.load( url, function ( text ) {
  18. onLoad( scope.parse( JSON.parse( text ) ) );
  19. }, onProgress, onError );
  20. },
  21. parse: function ( json ) {
  22. var geometry = new BufferGeometry();
  23. var index = json.data.index;
  24. var TYPED_ARRAYS = {
  25. 'Int8Array': Int8Array,
  26. 'Uint8Array': Uint8Array,
  27. 'Uint8ClampedArray': Uint8ClampedArray,
  28. 'Int16Array': Int16Array,
  29. 'Uint16Array': Uint16Array,
  30. 'Int32Array': Int32Array,
  31. 'Uint32Array': Uint32Array,
  32. 'Float32Array': Float32Array,
  33. 'Float64Array': Float64Array
  34. };
  35. if ( index !== undefined ) {
  36. var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  37. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  38. }
  39. var attributes = json.data.attributes;
  40. for ( var key in attributes ) {
  41. var attribute = attributes[ key ];
  42. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  43. geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
  44. }
  45. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  46. if ( groups !== undefined ) {
  47. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  48. var group = groups[ i ];
  49. geometry.addGroup( group.start, group.count, group.materialIndex );
  50. }
  51. }
  52. var boundingSphere = json.data.boundingSphere;
  53. if ( boundingSphere !== undefined ) {
  54. var center = new Vector3();
  55. if ( boundingSphere.center !== undefined ) {
  56. center.fromArray( boundingSphere.center );
  57. }
  58. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  59. }
  60. return geometry;
  61. }
  62. } );
  63. export { BufferGeometryLoader };