浏览代码

VTKLoader using EventTarget.
JSONLoader next...

Mr.doob 13 年之前
父节点
当前提交
c50d6e16ca
共有 2 个文件被更改,包括 107 次插入90 次删除
  1. 59 52
      examples/js/loaders/VTKLoader.js
  2. 48 38
      examples/webgl_loader_vtk.html

+ 59 - 52
examples/js/loaders/VTKLoader.js

@@ -2,104 +2,111 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.VTKLoader = function () {};
+THREE.VTKLoader = function () {
 
-THREE.VTKLoader.prototype = new THREE.Loader();
-THREE.VTKLoader.prototype.constructor = THREE.VTKLoader;
+	THREE.EventTarget.call( this );
 
-THREE.VTKLoader.prototype.load = function ( url, callback ) {
+};
+
+THREE.VTKLoader.prototype = {
+
+	constructor: THREE.VTKLoader,
+
+	load: function ( url ) {
+
+		var scope = this;
+		var xhr = new XMLHttpRequest();
 
-	var that = this;
-	var xhr = new XMLHttpRequest();
+		xhr.onreadystatechange = function () {
 
-	xhr.onreadystatechange = function () {
+			if ( xhr.readyState == 4 ) {
 
-		if ( xhr.readyState == 4 ) {
+				if ( xhr.status == 200 || xhr.status == 0 ) {
 
-			if ( xhr.status == 200 || xhr.status == 0 ) {
+					scope.dispatchEvent( { type: 'complete', content: scope.parse( xhr.responseText ) } );
 
-				callback( that.parse( xhr.responseText ) );
+				} else {
 
-			} else {
+					scope.dispatchEvent( { type: 'error', status: xhr.status } );
 
-				console.error( 'THREE.VTKLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
+				}
 
 			}
 
-		}
+		};
 
-	};
+		xhr.open( 'GET', url, true );
+		xhr.send( null );
 
-	xhr.open( "GET", url, true );
-	xhr.send( null );
+	},
 
-};
+	parse: function ( data ) {
 
-THREE.VTKLoader.prototype.parse = function ( data ) {
+		var geometry = new THREE.Geometry();
 
-	var geometry = new THREE.Geometry();
+		function vertex( x, y, z ) {
 
-	function vertex( x, y, z ) {
+			geometry.vertices.push( new THREE.Vector3( x, y, z ) );
 
-		geometry.vertices.push( new THREE.Vector3( x, y, z ) );
+		}
 
-	}
+		function face3( a, b, c ) {
 
-	function face3( a, b, c ) {
+			geometry.faces.push( new THREE.Face3( a, b, c ) );
 
-		geometry.faces.push( new THREE.Face3( a, b, c ) );
+		}
 
-	}
+		function face4( a, b, c, d ) {
 
-	function face4( a, b, c, d ) {
+			geometry.faces.push( new THREE.Face4( a, b, c, d ) );
 
-		geometry.faces.push( new THREE.Face4( a, b, c, d ) );
+		}
 
-	}
+		var pattern, result;
 
-	var pattern, result;
+		// float float float
 
-	// float float float
+		pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
 
-	pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
+		while ( ( result = pattern.exec( data ) ) != null ) {
 
-	while ( ( result = pattern.exec( data ) ) != null ) {
+			// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
 
-		// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
+			vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
 
-		vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
+		}
 
-	}
+		// 3 int int int
 
-	// 3 int int int
+		pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
 
-	pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
+		while ( ( result = pattern.exec( data ) ) != null ) {
 
-	while ( ( result = pattern.exec( data ) ) != null ) {
+			// ["3 1 2 3", "1", "2", "3"]
 
-		// ["3 1 2 3", "1", "2", "3"]
+			face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
 
-		face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
+		}
 
-	}
+		// 4 int int int int
 
-	// 4 int int int int
+		pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
 
-	pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
+		while ( ( result = pattern.exec( data ) ) != null ) {
 
-	while ( ( result = pattern.exec( data ) ) != null ) {
+			// ["4 1 2 3 4", "1", "2", "3", "4"]
 
-		// ["4 1 2 3 4", "1", "2", "3", "4"]
+			face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
 
-		face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
+		}
 
-	}
+		geometry.computeCentroids();
+		geometry.computeFaceNormals();
+		geometry.computeVertexNormals();
+		geometry.computeBoundingSphere();
 
-	geometry.computeCentroids();
-	geometry.computeFaceNormals();
-	geometry.computeVertexNormals();
-	geometry.computeBoundingSphere();
+		return geometry;
 
-	return geometry;
+	}
 
 }

+ 48 - 38
examples/webgl_loader_vtk.html

@@ -48,71 +48,81 @@
 
 			var cross;
 
+			init();
+			animate();
 
-			// scene and camera
+			function init() {
 
-			scene = new THREE.Scene();
+				// scene and camera
 
-			camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
-			camera.position.z = 0.2;
+				scene = new THREE.Scene();
 
+				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
+				camera.position.z = 0.2;
+				scene.add( camera );
 
-			scene.add( camera );
+				controls = new THREE.TrackballControls( camera );
 
-			controls = new THREE.TrackballControls( camera );
+				controls.rotateSpeed = 5.0;
+				controls.zoomSpeed = 5;
+				controls.panSpeed = 2;
 
-			controls.rotateSpeed = 5.0;
-			controls.zoomSpeed = 5;
-			controls.panSpeed = 2;
+				controls.noZoom = false;
+				controls.noPan = false;
 
-			controls.noZoom = false;
-			controls.noPan = false;
+				controls.staticMoving = true;
+				controls.dynamicDampingFactor = 0.3;
 
-			controls.staticMoving = true;
-			controls.dynamicDampingFactor = 0.3;
+				// light
 
-			// light
+				var dirLight = new THREE.DirectionalLight( 0xffffff );
+				dirLight.position.set( 200, 200, 1000 ).normalize();
+				camera.add( dirLight );
+				camera.add( dirLight.target );
 
-			var dirLight = new THREE.DirectionalLight( 0xffffff );
-			dirLight.position.set( 200, 200, 1000 ).normalize();
-			camera.add( dirLight );
-			camera.add( dirLight.target );
+				var material = new THREE.MeshLambertMaterial( { color:0xffffff} );
 
-			// renderer
+				var loader = new THREE.VTKLoader();
+				loader.addEventListener( 'complete', function ( event ) {
 
-			renderer = new THREE.WebGLRenderer( { antialias: false } );
-			renderer.setClearColorHex( 0x000000, 1 );
-			renderer.setSize( window.innerWidth, window.innerHeight );
+					var geometry = event.content;
 
-			container = document.createElement( 'div' );
-			document.body.appendChild( container );
-			container.appendChild( renderer.domElement );
+					var mesh = new THREE.Mesh( geometry, material );
+					mesh.doubleSided = true;
+					mesh.position.setY( - 0.09 );
+					scene.add( mesh );
 
-			stats = new Stats();
-			stats.domElement.style.position = 'absolute';
-			stats.domElement.style.top = '0px';
-			container.appendChild( stats.domElement );
+				} );
+				loader.load ( "models/vtk/bunny.vtk" );
 
+				// renderer
 
-			var material =  new THREE.MeshLambertMaterial( { color:0xffffff} );
+				renderer = new THREE.WebGLRenderer( { antialias: false } );
+				renderer.setClearColorHex( 0x000000, 1 );
+				renderer.setSize( window.innerWidth, window.innerHeight );
 
-			var loader=new THREE.VTKLoader();
-			loader.load ("models/vtk/bunny.vtk", function(geom){
-				var mesh = new THREE.Mesh(geom, material );
-				mesh.doubleSided=true;
-				mesh.position.setY(-0.09);
-				scene.add( mesh );
-				animate();
-				});
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+				container.appendChild( renderer.domElement );
+
+				stats = new Stats();
+				stats.domElement.style.position = 'absolute';
+				stats.domElement.style.top = '0px';
+				container.appendChild( stats.domElement );
+
+			}
 
 			function animate() {
 
 				requestAnimationFrame( animate );
+
 				controls.update();
 				renderer.render( scene, camera );
+
 				stats.update();
 
 			}
+
 		</script>
 
 	</body>