Przeglądaj źródła

OBJExporter: Add support for colored point clouds.

Mugen87 4 lat temu
rodzic
commit
abeaf948a4

+ 70 - 0
examples/js/exporters/OBJExporter.js

@@ -13,6 +13,7 @@ THREE.OBJExporter.prototype = {
 		var indexNormals = 0;
 
 		var vertex = new THREE.Vector3();
+		var color = new THREE.Color();
 		var normal = new THREE.Vector3();
 		var uv = new THREE.Vector2();
 
@@ -235,6 +236,69 @@ THREE.OBJExporter.prototype = {
 
 		};
 
+		var parsePoints = function ( points ) {
+
+			var nbVertex = 0;
+
+			var geometry = points.geometry;
+
+			if ( geometry instanceof THREE.Geometry ) {
+
+				geometry = new THREE.BufferGeometry().setFromObject( points );
+
+			}
+
+			if ( geometry instanceof THREE.BufferGeometry ) {
+
+				var vertices = geometry.getAttribute( 'position' );
+				var colors = geometry.getAttribute( 'color' );
+
+				output += 'o ' + points.name + '\n';
+
+				if ( vertices !== undefined ) {
+
+					for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
+
+						vertex.fromBufferAttribute( vertices, i );
+						vertex.applyMatrix4( points.matrixWorld );
+
+						output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
+
+						if ( colors !== undefined ) {
+
+							color.fromBufferAttribute( colors, i );
+
+							output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
+
+						}
+
+						output += '\n';
+
+					}
+
+				}
+
+				output += 'p ';
+
+				for ( j = 1, l = vertices.count; j <= l; j ++ ) {
+
+					output += ( indexVertex + j ) + ' ';
+
+				}
+
+				output += '\n';
+
+			} else {
+
+				console.warn( 'THREE.OBJExporter.parseLine(): geometry type unsupported', geometry );
+
+			}
+
+			// update index
+			indexVertex += nbVertex;
+
+		};
+
 		object.traverse( function ( child ) {
 
 			if ( child instanceof THREE.Mesh ) {
@@ -249,6 +313,12 @@ THREE.OBJExporter.prototype = {
 
 			}
 
+			if ( child instanceof THREE.Points ) {
+
+				parsePoints( child );
+
+			}
+
 		} );
 
 		return output;

+ 9 - 2
examples/js/loaders/OBJLoader.js

@@ -375,7 +375,10 @@ THREE.OBJLoader = ( function () {
 
 				for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
 
-					this.addVertexPoint( this.parseVertexIndex( vertices[ vi ], vLen ) );
+					var index = this.parseVertexIndex( vertices[ vi ], vLen );
+
+					this.addVertexPoint( index );
+					this.addColor( index );
 
 				}
 
@@ -724,7 +727,11 @@ THREE.OBJLoader = ( function () {
 
 				buffergeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( geometry.vertices, 3 ) );
 
-				buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );
+				if ( geometry.normals.length > 0 ) {
+
+					buffergeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );
+
+				}
 
 				if ( geometry.colors.length > 0 ) {
 

+ 72 - 0
examples/jsm/exporters/OBJExporter.js

@@ -1,9 +1,11 @@
 import {
 	BufferGeometry,
+	Color,
 	Geometry,
 	Line,
 	Matrix3,
 	Mesh,
+	Points,
 	Vector2,
 	Vector3
 } from "../../../build/three.module.js";
@@ -23,6 +25,7 @@ OBJExporter.prototype = {
 		var indexNormals = 0;
 
 		var vertex = new Vector3();
+		var color = new Color();
 		var normal = new Vector3();
 		var uv = new Vector2();
 
@@ -245,6 +248,69 @@ OBJExporter.prototype = {
 
 		};
 
+		var parsePoints = function ( points ) {
+
+			var nbVertex = 0;
+
+			var geometry = points.geometry;
+
+			if ( geometry instanceof Geometry ) {
+
+				geometry = new BufferGeometry().setFromObject( points );
+
+			}
+
+			if ( geometry instanceof BufferGeometry ) {
+
+				var vertices = geometry.getAttribute( 'position' );
+				var colors = geometry.getAttribute( 'color' );
+
+				output += 'o ' + points.name + '\n';
+
+				if ( vertices !== undefined ) {
+
+					for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
+
+						vertex.fromBufferAttribute( vertices, i );
+						vertex.applyMatrix4( points.matrixWorld );
+
+						output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
+
+						if ( colors !== undefined ) {
+
+							color.fromBufferAttribute( colors, i );
+
+							output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
+
+						}
+
+						output += '\n';
+
+					}
+
+				}
+
+				output += 'p ';
+
+				for ( j = 1, l = vertices.count; j <= l; j ++ ) {
+
+					output += ( indexVertex + j ) + ' ';
+
+				}
+
+				output += '\n';
+
+			} else {
+
+				console.warn( 'THREE.OBJExporter.parseLine(): geometry type unsupported', geometry );
+
+			}
+
+			// update index
+			indexVertex += nbVertex;
+
+		};
+
 		object.traverse( function ( child ) {
 
 			if ( child instanceof Mesh ) {
@@ -259,6 +325,12 @@ OBJExporter.prototype = {
 
 			}
 
+			if ( child instanceof Points ) {
+
+				parsePoints( child );
+
+			}
+
 		} );
 
 		return output;

+ 9 - 2
examples/jsm/loaders/OBJLoader.js

@@ -391,7 +391,10 @@ var OBJLoader = ( function () {
 
 				for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
 
-					this.addVertexPoint( this.parseVertexIndex( vertices[ vi ], vLen ) );
+					var index = this.parseVertexIndex( vertices[ vi ], vLen );
+
+					this.addVertexPoint( index );
+					this.addColor( index );
 
 				}
 
@@ -740,7 +743,11 @@ var OBJLoader = ( function () {
 
 				buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) );
 
-				buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );
+				if ( geometry.normals.length > 0 ) {
+
+					buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );
+
+				}
 
 				if ( geometry.colors.length > 0 ) {
 

+ 23 - 2
examples/misc_exporter_obj.html

@@ -29,7 +29,8 @@
 			<button id="cube">cube</button>
 			<button id="cylinder">cylinder</button>
 			<button id="multiple">multiple</button>
-			<button id="transformed">transformed</button><br /><br />
+			<button id="transformed">transformed</button>
+			<button id="points">points</button><br /><br />
 			<button id="export">export to obj</button>
 		</div>
 
@@ -58,7 +59,7 @@
 
 					const child = scene.children[ i ];
 
-					if ( child.isMesh ) {
+					if ( child.isMesh || child.isPoints ) {
 
 						child.geometry.dispose();
 						scene.remove( child );
@@ -114,6 +115,21 @@
 
 					}
 
+				} else if ( type === 6 ) {
+
+					const points = [ 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0 ];
+					const colors = [ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0 ];
+
+					const geometry = new THREE.BufferGeometry();
+					geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
+					geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
+
+					const material = new THREE.PointsMaterial( { size: 10, vertexColors: true } );
+
+					const pointCloud = new THREE.Points( geometry, material );
+					pointCloud.name = 'point cloud';
+					scene.add( pointCloud );
+
 				}
 
 			}
@@ -164,6 +180,11 @@
 
 					addGeometry( 5 );
 
+				} );
+				document.getElementById( 'points' ).addEventListener( 'click', function () {
+
+					addGeometry( 6 );
+
 				} );
 
 				exportButton = document.getElementById( 'export' );