Browse Source

Merge pull request #13410 from fernandojsg/fixindices

GLTFExporter: Added forceIndices for non-indexed geometry
Mr.doob 7 years ago
parent
commit
90cea0ace1

+ 4 - 3
editor/js/Menubar.File.js

@@ -197,9 +197,10 @@ Menubar.File = function ( editor ) {
 
 			saveArrayBuffer( result, 'scene.glb' );
 
-		}, { binary: true } );
-
-
+			// forceIndices: true to allow compatibility with facebook
+			// https://github.com/mrdoob/three.js/issues/13397
+		}, { binary: true, forceIndices: true } );
+		
 	} );
 	options.add( option );
 

+ 14 - 1
examples/js/exporters/GLTFExporter.js

@@ -68,7 +68,8 @@ THREE.GLTFExporter.prototype = {
 			onlyVisible: true,
 			truncateDrawRange: true,
 			embedImages: true,
-			animations: []
+			animations: [],
+			forceIndices: false
 		};
 
 		options = Object.assign( {}, DEFAULT_OPTIONS, options );
@@ -809,6 +810,18 @@ THREE.GLTFExporter.prototype = {
 
 				gltfMesh.primitives[ 0 ].indices = processAccessor( geometry.index, geometry );
 
+			} else if ( options.forceIndices ) {
+
+				var numFaces = geometry.attributes.position.count;
+				var indices = new Uint32Array( numFaces );
+				for ( var i = 0; i < numFaces; i ++ ) {
+
+					indices[ i ] = i;
+
+				}
+
+				gltfMesh.primitives[ 0 ].indices = processAccessor( new THREE.Uint32BufferAttribute( indices, 1 ), geometry );
+
 			}
 
 			// We've just one primitive per mesh

+ 3 - 1
examples/misc_exporter_gltf.html

@@ -33,6 +33,7 @@
 			<label><input id="option_visible" name="visible" type="checkbox" checked="checked"/>Only Visible</label>
 			<label><input id="option_drawrange" name="visible" type="checkbox" checked="checked"/>Truncate drawRange</label>
 			<label><input id="option_binary" name="visible" type="checkbox">Binary (<code>.glb</code>)</label>
+			<label><input id="option_forceindices" name="visible" type="checkbox">Force indices</label>
 		</div>
 
 		<script src="../build/three.js"></script>
@@ -50,7 +51,8 @@
 					trs: document.getElementById('option_trs').checked,
 					onlyVisible: document.getElementById('option_visible').checked,
 					truncateDrawRange: document.getElementById('option_drawrange').checked,
-					binary: document.getElementById('option_binary').checked
+					binary: document.getElementById('option_binary').checked,
+					forceIndices: document.getElementById('option_forceindices').checked
 				};
 				gltfExporter.parse( input, function( result ) {