fireball/lib/loaders/BufferGeometryLoader.js
2018-12-25 17:29:22 +03:30

115 lines
2.4 KiB
JavaScript

import { Sphere } from '../math/Sphere.js';
import { Vector3 } from '../math/Vector3.js';
import { BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { FileLoader } from './FileLoader.js';
import { DefaultLoadingManager } from './LoadingManager.js';
/**
* @author mrdoob / http://mrdoob.com/
*/
function BufferGeometryLoader( manager ) {
this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
}
Object.assign( BufferGeometryLoader.prototype, {
load: function ( url, onLoad, onProgress, onError ) {
var scope = this;
var loader = new FileLoader( scope.manager );
loader.setPath( scope.path );
loader.load( url, function ( text ) {
onLoad( scope.parse( JSON.parse( text ) ) );
}, onProgress, onError );
},
parse: function ( json ) {
var geometry = new BufferGeometry();
var index = json.data.index;
if ( index !== undefined ) {
var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
}
var attributes = json.data.attributes;
for ( var key in attributes ) {
var attribute = attributes[ key ];
var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
}
var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
if ( groups !== undefined ) {
for ( var i = 0, n = groups.length; i !== n; ++ i ) {
var group = groups[ i ];
geometry.addGroup( group.start, group.count, group.materialIndex );
}
}
var boundingSphere = json.data.boundingSphere;
if ( boundingSphere !== undefined ) {
var center = new Vector3();
if ( boundingSphere.center !== undefined ) {
center.fromArray( boundingSphere.center );
}
geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
}
return geometry;
},
setPath: function ( value ) {
this.path = value;
return this;
}
} );
var TYPED_ARRAYS = {
Int8Array: Int8Array,
Uint8Array: Uint8Array,
// Workaround for IE11 pre KB2929437. See #11440
Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
Int16Array: Int16Array,
Uint16Array: Uint16Array,
Int32Array: Int32Array,
Uint32Array: Uint32Array,
Float32Array: Float32Array,
Float64Array: Float64Array
};
export { BufferGeometryLoader };