Browse Source

Added BufferGeometryLoader and BufferGeometryExporter.

Mr.doob 12 years ago
parent
commit
14e56ec912

+ 44 - 0
examples/js/exporters/BufferGeometryExporter.js

@@ -0,0 +1,44 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.BufferGeometryExporter = function () {};
+
+THREE.BufferGeometryExporter.prototype = {
+
+	constructor: THREE.BufferGeometryExporter,
+
+	parse: function ( geometry ) {
+
+		var output = {
+			metadata: {
+				version: 4.0,
+				type: 'BufferGeometry',
+				generator: 'BufferGeometryExporter'
+			},
+			attributes: {}
+		};
+
+		for ( var key in geometry.attributes ) {
+
+			var attribute = geometry.attributes[ key ];
+
+			output.attributes[ key ] = {
+				itemSize: attribute.itemSize,
+				type: attribute.array.constructor.name,
+				array: Array.apply( [], attribute.array )
+			}
+
+		}
+
+		if ( geometry.offsets.length > 0 ) {
+
+			output.offsets = JSON.parse( JSON.stringify( geometry.offsets ) );
+
+		}
+
+		return output;
+
+	}
+
+};

+ 59 - 0
src/loaders/BufferGeometryLoader.js

@@ -0,0 +1,59 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.BufferGeometryLoader = function ( manager ) {
+
+	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
+
+};
+
+THREE.BufferGeometryLoader.prototype = {
+
+	constructor: THREE.BufferGeometryLoader,
+
+	load: function ( url, onLoad, onProgress, onError ) {
+
+		var scope = this;
+
+		var loader = new THREE.XHRLoader();
+		loader.setCrossOrigin( this.crossOrigin );
+		loader.load( url, function ( text ) {
+
+			onLoad( scope.parse( JSON.parse( text ) ) );
+
+		} );
+
+	},
+
+	setCrossOrigin: function ( value ) {
+
+		this.crossOrigin = value;
+
+	},
+
+	parse: function ( json ) {
+
+		var geometry = new THREE.BufferGeometry();
+
+		for ( var key in json.attributes ) {
+
+			var attribute = json.attributes[ key ];
+			geometry.attributes[ key ] = {
+				itemSize: attribute.itemSize,
+				array: new self[ attribute.type ]( attribute.array )
+			}
+
+		}
+
+		if ( json.offsets !== undefined ) {
+
+			geometry.offsets = JSON.parse( JSON.stringify( json.offsets ) );
+
+		}
+
+		return geometry;
+
+	}
+
+};