BufferGeometryLoader.js 2.4 KB
Newer Older
B
bentok 已提交
1 2 3 4 5 6
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';
R
Rich Harris 已提交
7

M
Mr.doob 已提交
8 9 10 11
/**
 * @author mrdoob / http://mrdoob.com/
 */

M
Mr.doob 已提交
12
function BufferGeometryLoader( manager ) {
M
Mr.doob 已提交
13

R
Rich Harris 已提交
14
	this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
M
Mr.doob 已提交
15

M
Mr.doob 已提交
16
}
M
Mr.doob 已提交
17

R
Rich Harris 已提交
18
Object.assign( BufferGeometryLoader.prototype, {
M
Mr.doob 已提交
19 20 21 22 23

	load: function ( url, onLoad, onProgress, onError ) {

		var scope = this;

M
Mr.doob 已提交
24
		var loader = new FileLoader( scope.manager );
25
		loader.setPath( scope.path );
26
		loader.load( url, function ( text ) {
M
Mr.doob 已提交
27

28
			onLoad( scope.parse( JSON.parse( text ) ) );
M
Mr.doob 已提交
29 30 31 32 33 34 35

		}, onProgress, onError );

	},

	parse: function ( json ) {

R
Rich Harris 已提交
36
		var geometry = new BufferGeometry();
M
Mr.doob 已提交
37 38 39 40 41

		var index = json.data.index;

		if ( index !== undefined ) {

42
			var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
R
Rich Harris 已提交
43
			geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
M
Mr.doob 已提交
44 45 46 47 48 49 50 51

		}

		var attributes = json.data.attributes;

		for ( var key in attributes ) {

			var attribute = attributes[ key ];
52
			var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
M
Mr.doob 已提交
53

R
Rich Harris 已提交
54
			geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
M
Mr.doob 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

		}

		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 ) {

R
Rich Harris 已提交
76
			var center = new Vector3();
M
Mr.doob 已提交
77 78 79 80 81 82 83

			if ( boundingSphere.center !== undefined ) {

				center.fromArray( boundingSphere.center );

			}

R
Rich Harris 已提交
84
			geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
M
Mr.doob 已提交
85 86 87 88 89

		}

		return geometry;

90 91 92 93 94 95 96
	},

	setPath: function ( value ) {

		this.path = value;
		return this;

M
Mr.doob 已提交
97 98
	}

M
Mr.doob 已提交
99
} );
R
Rich Harris 已提交
100

101 102 103
var TYPED_ARRAYS = {
	Int8Array: Int8Array,
	Uint8Array: Uint8Array,
M
Mr.doob 已提交
104
	// Workaround for IE11 pre KB2929437. See #11440
105
	Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
106 107 108 109 110 111 112
	Int16Array: Int16Array,
	Uint16Array: Uint16Array,
	Int32Array: Int32Array,
	Uint32Array: Uint32Array,
	Float32Array: Float32Array,
	Float64Array: Float64Array
};
R
Rich Harris 已提交
113

114
export { BufferGeometryLoader };