BufferGeometryLoader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 attributes = json.attributes;
  23. for ( var key in attributes ) {
  24. var attribute = attributes[ key ];
  25. var typedArray = new self[ attribute.type ]( attribute.array );
  26. geometry.addAttribute( key, new THREE.BufferAttribute( typedArray, attribute.itemSize ) );
  27. }
  28. var offsets = json.offsets;
  29. if ( offsets !== undefined ) {
  30. geometry.offsets = JSON.parse( JSON.stringify( offsets ) );
  31. }
  32. var boundingSphere = json.boundingSphere;
  33. if ( boundingSphere !== undefined ) {
  34. var center = new THREE.Vector3();
  35. if ( boundingSphere.center !== undefined ) {
  36. center.fromArray( boundingSphere.center );
  37. }
  38. geometry.boundingSphere = new THREE.Sphere( center, boundingSphere.radius );
  39. }
  40. return geometry;
  41. }
  42. };