فهرست منبع

GLTFExporter: Prevent exporting empty geometry

Fernando Serrano 7 سال پیش
والد
کامیت
a6bb8b2820
2فایلهای تغییر یافته به همراه71 افزوده شده و 16 حذف شده
  1. 44 15
      examples/js/exporters/GLTFExporter.js
  2. 27 1
      examples/misc_exporter_gltf.html

+ 44 - 15
examples/js/exporters/GLTFExporter.js

@@ -425,12 +425,6 @@ THREE.GLTFExporter.prototype = {
 		 */
 		function processAccessor( attribute, geometry, start, count ) {
 
-			if ( ! outputJSON.accessors ) {
-
-				outputJSON.accessors = [];
-
-			}
-
 			var types = {
 
 				1: 'SCALAR',
@@ -484,6 +478,13 @@ THREE.GLTFExporter.prototype = {
 
 			}
 
+			// Skip creating an accessor if the attribute doesn't have data to export
+			if ( count === 0) {
+
+				return -1;
+
+			}
+
 			var minMax = getMinMax( attribute, start, count );
 
 			var bufferViewTarget;
@@ -510,6 +511,12 @@ THREE.GLTFExporter.prototype = {
 
 			};
 
+			if ( ! outputJSON.accessors ) {
+
+				outputJSON.accessors = [];
+
+			}
+
 			outputJSON.accessors.push( gltfAccessor );
 
 			return outputJSON.accessors.length - 1;
@@ -872,12 +879,6 @@ THREE.GLTFExporter.prototype = {
 		 */
 		function processMesh( mesh ) {
 
-			if ( ! outputJSON.meshes ) {
-
-				outputJSON.meshes = [];
-
-			}
-
 			var geometry = mesh.geometry;
 
 			var mode;
@@ -962,8 +963,13 @@ THREE.GLTFExporter.prototype = {
 				}
 
 				if ( attributeName.substr( 0, 5 ) !== 'MORPH' ) {
+					
+					var accessor = processAccessor( attribute, geometry );
+					if ( accessor !== -1 ) {
 
-					attributes[ attributeName ] = processAccessor( attribute, geometry );
+						attributes[ attributeName ] = accessor;
+
+					}
 
 				}
 
@@ -1109,7 +1115,12 @@ THREE.GLTFExporter.prototype = {
 
 				}
 
-				primitives.push( primitive );
+				// Skip meshes without exportable attributes
+				if ( Object.keys(primitive.attributes).length > 0 ) {
+
+					primitives.push( primitive );
+
+				}
 
 			}
 
@@ -1119,8 +1130,20 @@ THREE.GLTFExporter.prototype = {
 
 			}
 
+			if ( primitives.length === 0 ) {
+
+				return -1;
+
+			}
+
 			gltfMesh.primitives = primitives;
 
+			if ( ! outputJSON.meshes ) {
+
+				outputJSON.meshes = [];
+
+			}
+			
 			outputJSON.meshes.push( gltfMesh );
 
 			return outputJSON.meshes.length - 1;
@@ -1420,7 +1443,13 @@ THREE.GLTFExporter.prototype = {
 
 			if ( object.isMesh || object.isLine || object.isPoints ) {
 
-				gltfNode.mesh = processMesh( object );
+				var mesh = processMesh( object );
+
+				if ( mesh !== -1 ) {
+
+					gltfNode.mesh = mesh;
+
+				}
 
 			} else if ( object.isCamera ) {
 

+ 27 - 1
examples/misc_exporter_gltf.html

@@ -26,6 +26,7 @@
 			<button id="export_scene">Export Scene1</button>
 			<button id="export_scenes">Export Scene1 and Scene 2</button>
 			<button id="export_object">Export Sphere</button>
+			<button id="export_empty">Export empty geometry</button>
 			<button id="export_obj">Export WaltHead</button>
 			<button id="export_objects">Export Sphere and Grid</button>
 			<button id="export_scene_object">Export Scene1 and Sphere</button>
@@ -88,6 +89,13 @@
 
 			} );
 
+
+			document.getElementById( 'export_empty' ).addEventListener( 'click', function () {
+
+			exportGLTF( empty );
+
+			} );
+
 			document.getElementById( 'export_object' ).addEventListener( 'click', function () {
 
 				exportGLTF( sphere );
@@ -145,7 +153,7 @@
 			var container;
 
 			var camera, object, scene1, scene2, renderer;
-			var gridHelper, sphere, waltHead;
+			var gridHelper, sphere, waltHead, empty;
 
 			init();
 			animate();
@@ -417,6 +425,24 @@
 
 				scene1.add( object );
 
+				// ---------------------------------------------------------------------
+				// Empty buffer geometry
+				// ---------------------------------------------------------------------
+				var geometry = new THREE.BufferGeometry();
+				var numElements = 6;
+
+				var positions = new Float32Array( ( numElements ) * 3 );
+				var colors = new Float32Array( ( numElements ) * 3 );
+
+				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
+				geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
+				geometry.setDrawRange( 0, 0 );
+
+				empty = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
+				empty.name = 'Custom buffered empty (drawrange)';
+				scene1.add( empty );
+
+
 				// ---------------------------------------------------------------------
 				// Points
 				// ---------------------------------------------------------------------