BufferGeometryLoader.js 1.9 KB

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