BufferGeometryLoader.js 2.0 KB

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