浏览代码

Editor: Added PLY support.

Mr.doob 12 年之前
父节点
当前提交
b3e3cd7668
共有 2 个文件被更改,包括 50 次插入19 次删除
  1. 27 0
      editor/index.html
  2. 23 19
      examples/js/loaders/PLYLoader.js

+ 27 - 0
editor/index.html

@@ -73,6 +73,7 @@
 		<script src="../examples/js/loaders/BinaryLoader.js"></script>
 		<script src="../examples/js/loaders/ColladaLoader.js"></script>
 		<script src="../examples/js/loaders/OBJLoader.js"></script>
+		<script src="../examples/js/loaders/PLYLoader.js"></script>
 		<script src="../examples/js/loaders/STLLoader.js"></script>
 		<script src="../examples/js/loaders/UTF8Loader.js"></script>
 		<script src="../examples/js/loaders/VTKLoader.js"></script>
@@ -393,6 +394,32 @@
 
 						break;
 
+					case 'ply':
+
+						var reader = new FileReader();
+						reader.addEventListener( 'load', function ( event ) {
+
+							var contents = event.target.result;
+
+							console.log( contents );
+
+							var geometry = new THREE.PLYLoader().parse( contents );
+							geometry.sourceType = "ply";
+							geometry.sourceFile = file.name;
+
+							var material = new THREE.MeshPhongMaterial();
+
+							var mesh = new THREE.Mesh( geometry, material );
+							mesh.name = filename;
+
+							signals.objectAdded.dispatch( mesh );
+							signals.objectSelected.dispatch( mesh );
+
+						}, false );
+						reader.readAsText( file );
+
+						break;
+
 					case 'stl':
 
 						var reader = new FileReader();

+ 23 - 19
examples/js/loaders/PLYLoader.js

@@ -36,8 +36,7 @@ THREE.PLYLoader.prototype = {
 
 		request.addEventListener( 'load', function ( event ) {
 
-            var geometry;
-            geometry = scope.parse( event.target.response );
+			var geometry = scope.parse( event.target.response );
 
 			scope.dispatchEvent( { type: 'load', content: geometry } );
 
@@ -70,7 +69,7 @@ THREE.PLYLoader.prototype = {
 		for(var i = 0; i < buf.byteLength; i++) {
 			str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
 		}
-		
+
 		return str;
 
 	},
@@ -79,23 +78,28 @@ THREE.PLYLoader.prototype = {
 
 		// currently only supports ASCII encoded files.
 		return true;
-		
-    },
 
-	parse: function (buf) {
+	},
+
+	parse: function ( data ) {
+
+		if ( data instanceof ArrayBuffer ) {
+
+			return this.isASCII( data )
+				? this.parseASCII( this.bin2str( data ) )
+				: this.parseBinary( data );
 
-		if( this.isASCII(buf) )	{
-			var str = this.bin2str(buf);
-			return this.parseASCII(str);
 		} else {
-			return this.parseBinary(buf);
+
+			return this.parseASCII( data );
+
 		}
 
-    },
+	},
 
 	parseASCII: function ( data ) {
 
-		// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)	
+		// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
 
 		var geometry = new THREE.Geometry();
 
@@ -106,13 +110,13 @@ THREE.PLYLoader.prototype = {
 		if ( ( result = patternHeader.exec( data ) ) != null ) {
 			header = result [ 1 ];
 		}
-		
+
 		var patternBody = /end_header([\s\S]*)$/;
 		var body = "";
 		if ( ( result = patternBody.exec( data ) ) != null ) {
 			body = result [ 1 ];
 		}
-		
+
 		var patternVertexCount = /element[\s]+vertex[\s]+(\d+)/g;
 		var vertexCount = 0;
 		if ( ( result = patternVertexCount.exec( header ) ) != null ) {
@@ -124,9 +128,9 @@ THREE.PLYLoader.prototype = {
 		if ( ( result = patternFaceCount.exec( header ) ) != null ) {
 			faceCount = parseInt( result[ 1 ] );
 		}
-		
+
 		if ( vertexCount != 0 && faceCount != 0 ) {
-			// Vertex			
+			// Vertex
 			// assume x y z
 			var patternVertex = /([-+]?[0-9]+\.?[0-9]*([eE][-+]?[0-9]+)?)+[\s]+([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)+[\s]+([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)+/g;
 			for ( var i = 0; i < vertexCount; i++) {
@@ -137,7 +141,7 @@ THREE.PLYLoader.prototype = {
 					return geometry;
 				}
 			}
-			
+
 			// Face
 			// assume 3 index0 index1 index2
 			var patternFace = /3[\s]+([-+]?[0-9]+)[\s]+([-+]?[0-9]+)[\s]+([-+]?[0-9]+)/g;
@@ -149,7 +153,7 @@ THREE.PLYLoader.prototype = {
 					return geometry;
 				}
 			}
-			
+
 		} else {
 			console.error( 'Header error: vertexCount(' + vertexCount + '), faceCount(' + faceCount + ').' );
 		}
@@ -165,7 +169,7 @@ THREE.PLYLoader.prototype = {
 
 		// not supported yet
 		console.error('Not supported yet.');
-		
+
 	}