BufferGeometryLoader.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import { InstancedBufferGeometry } from '../core/InstancedBufferGeometry.js';
  8. import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
  9. /**
  10. * @author mrdoob / http://mrdoob.com/
  11. */
  12. function BufferGeometryLoader( manager ) {
  13. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  14. }
  15. Object.assign( BufferGeometryLoader.prototype, {
  16. load: function ( url, onLoad, onProgress, onError ) {
  17. var scope = this;
  18. var loader = new FileLoader( scope.manager );
  19. loader.setPath( scope.path );
  20. loader.load( url, function ( text ) {
  21. onLoad( scope.parse( JSON.parse( text ) ) );
  22. }, onProgress, onError );
  23. },
  24. parse: function ( json ) {
  25. var geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
  26. var index = json.data.index;
  27. if ( index !== undefined ) {
  28. var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
  29. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  30. }
  31. var attributes = json.data.attributes;
  32. for ( var key in attributes ) {
  33. var attribute = attributes[ key ];
  34. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  35. var bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
  36. var bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
  37. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  38. geometry.addAttribute( key, bufferAttribute );
  39. }
  40. var morphAttributes = json.data.morphAttributes;
  41. if ( morphAttributes ) {
  42. for ( var key in morphAttributes ) {
  43. var attributeArray = morphAttributes[ key ];
  44. var array = [];
  45. for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
  46. var attribute = attributeArray[ i ];
  47. var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
  48. var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  49. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  50. array.push( bufferAttribute );
  51. }
  52. geometry.morphAttributes[ key ] = array;
  53. }
  54. }
  55. var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  56. if ( groups !== undefined ) {
  57. for ( var i = 0, n = groups.length; i !== n; ++ i ) {
  58. var group = groups[ i ];
  59. geometry.addGroup( group.start, group.count, group.materialIndex );
  60. }
  61. }
  62. var boundingSphere = json.data.boundingSphere;
  63. if ( boundingSphere !== undefined ) {
  64. var center = new Vector3();
  65. if ( boundingSphere.center !== undefined ) {
  66. center.fromArray( boundingSphere.center );
  67. }
  68. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  69. }
  70. if ( json.name ) geometry.name = json.name;
  71. if ( json.userData ) geometry.userData = json.userData;
  72. return geometry;
  73. },
  74. setPath: function ( value ) {
  75. this.path = value;
  76. return this;
  77. }
  78. } );
  79. var TYPED_ARRAYS = {
  80. Int8Array: Int8Array,
  81. Uint8Array: Uint8Array,
  82. // Workaround for IE11 pre KB2929437. See #11440
  83. Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
  84. Int16Array: Int16Array,
  85. Uint16Array: Uint16Array,
  86. Int32Array: Int32Array,
  87. Uint32Array: Uint32Array,
  88. Float32Array: Float32Array,
  89. Float64Array: Float64Array
  90. };
  91. export { BufferGeometryLoader };