1
0

BufferGeometryLoader.js 2.4 KB

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