From 1b4dff01c0d488d3fccea11ab508ba7ca96101eb Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Thu, 30 Nov 2023 11:22:16 +0100 Subject: [PATCH] GLTFExporter: Restore multi material support for non-indexed geometries. (#27268) --- examples/jsm/exporters/GLTFExporter.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/jsm/exporters/GLTFExporter.js b/examples/jsm/exporters/GLTFExporter.js index 5d6ac77e80a4c2..a9eb9ff00fb37c 100644 --- a/examples/jsm/exporters/GLTFExporter.js +++ b/examples/jsm/exporters/GLTFExporter.js @@ -1835,6 +1835,24 @@ class GLTFWriter { if ( isMultiMaterial && geometry.groups.length === 0 ) return null; + let didForceIndices = false; + + if ( isMultiMaterial && geometry.index === null ) { + + const indices = []; + + for ( let i = 0, il = geometry.attributes.position.count; i < il; i ++ ) { + + indices[ i ] = i; + + } + + geometry.setIndex( indices ); + + didForceIndices = true; + + } + const materials = isMultiMaterial ? mesh.material : [ mesh.material ]; const groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ]; @@ -1882,6 +1900,12 @@ class GLTFWriter { } + if ( didForceIndices === true ) { + + geometry.setIndex( null ); + + } + meshDef.primitives = primitives; if ( ! json.meshes ) json.meshes = [];