BufferGeometryLoader.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  34. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  35. geometry.addAttribute( key, bufferAttribute );
  36. }
  37. var morphAttributes = json.data.morphAttributes;
  38. if ( morphAttributes ) {
  39. for ( var key in morphAttributes ) {
  40. var attributeArray = morphAttributes[ key ];
  41. var array = [];
  42. for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
  43. var attribute = attributeArray[ i ];
  44. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  45. var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  46. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  47. array.push( bufferAttribute );
  48. }
  49. geometry.morphAttributes[ key ] = array;
  50. }
  51. }
  52. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  53. if ( groups !== undefined ) {
  54. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  55. var group = groups[ i ];
  56. geometry.addGroup( group.start, group.count, group.materialIndex );
  57. }
  58. }
  59. var boundingSphere = json.data.boundingSphere;
  60. if ( boundingSphere !== undefined ) {
  61. var center = new Vector3();
  62. if ( boundingSphere.center !== undefined ) {
  63. center.fromArray( boundingSphere.center );
  64. }
  65. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  66. }
  67. if ( json.name ) geometry.name = json.name;
  68. if ( json.userData ) geometry.userData = json.userData;
  69. return geometry;
  70. },
  71. setPath: function ( value ) {
  72. this.path = value;
  73. return this;
  74. }
  75. } );
  76. var TYPED_ARRAYS = {
  77. Int8Array: Int8Array,
  78. Uint8Array: Uint8Array,
  79. // Workaround for IE11 pre KB2929437. See #11440
  80. Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
  81. Int16Array: Int16Array,
  82. Uint16Array: Uint16Array,
  83. Int32Array: Int32Array,
  84. Uint32Array: Uint32Array,
  85. Float32Array: Float32Array,
  86. Float64Array: Float64Array
  87. };
  88. export { BufferGeometryLoader };