BufferGeometryLoader.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.load( url, function ( text ) {
  13. onLoad( scope.parse( JSON.parse( text ) ) );
  14. }, onProgress, onError );
  15. },
  16. parse: function ( json ) {
  17. var geometry = new THREE.BufferGeometry();
  18. var index = json.data.index;
  19. var TYPED_ARRAYS = {
  20. 'Int8Array': Int8Array,
  21. 'Uint8Array': Uint8Array,
  22. 'Uint8ClampedArray': Uint8ClampedArray,
  23. 'Int16Array': Int16Array,
  24. 'Uint16Array': Uint16Array,
  25. 'Int32Array': Int32Array,
  26. 'Uint32Array': Uint32Array,
  27. 'Float32Array': Float32Array,
  28. 'Float64Array': Float64Array
  29. };
  30. if ( index !== undefined ) {
  31. var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  32. geometry.setIndex( new THREE.BufferAttribute( typedArray, 1 ) );
  33. }
  34. var attributes = json.data.attributes;
  35. for ( var key in attributes ) {
  36. var attribute = attributes[ key ];
  37. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  38. geometry.addAttribute( key, new THREE.BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
  39. }
  40. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  41. if ( groups !== undefined ) {
  42. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  43. var group = groups[ i ];
  44. geometry.addGroup( group.start, group.count, group.materialIndex );
  45. }
  46. }
  47. var boundingSphere = json.data.boundingSphere;
  48. if ( boundingSphere !== undefined ) {
  49. var center = new THREE.Vector3();
  50. if ( boundingSphere.center !== undefined ) {
  51. center.fromArray( boundingSphere.center );
  52. }
  53. geometry.boundingSphere = new THREE.Sphere( center, boundingSphere.radius );
  54. }
  55. return geometry;
  56. }
  57. };
粤ICP备19079148号