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. if ( index !== undefined ) {
  25. var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  26. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  27. }
  28. var attributes = json.data.attributes;
  29. for ( var key in attributes ) {
  30. var attribute = attributes[ key ];
  31. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  32. geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
  33. }
  34. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  35. if ( groups !== undefined ) {
  36. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  37. var group = groups[ i ];
  38. geometry.addGroup( group.start, group.count, group.materialIndex );
  39. }
  40. }
  41. var boundingSphere = json.data.boundingSphere;
  42. if ( boundingSphere !== undefined ) {
  43. var center = new Vector3();
  44. if ( boundingSphere.center !== undefined ) {
  45. center.fromArray( boundingSphere.center );
  46. }
  47. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  48. }
  49. return geometry;
  50. }
  51. } );
  52. var TYPED_ARRAYS = {
  53. Int8Array: Int8Array,
  54. Uint8Array: Uint8Array,
  55. // Workaround for IE11 pre KB2929437. See #11440
  56. Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
  57. Int16Array: Int16Array,
  58. Uint16Array: Uint16Array,
  59. Int32Array: Int32Array,
  60. Uint32Array: Uint32Array,
  61. Float32Array: Float32Array,
  62. Float64Array: Float64Array
  63. };
  64. export { BufferGeometryLoader };
粤ICP备19079148号