Skip to content

Commit bb126a2

Browse files
authored
FBXLoader: Loading FBX files with out-of-bounds material assignments lead to incorrect geometry groups and subsequent errors (#30581)
In line with other FBX loaders, a default material is now created.
1 parent a8a6faf commit bb126a2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/jsm/loaders/FBXLoader.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
Mesh,
2323
MeshLambertMaterial,
2424
MeshPhongMaterial,
25+
MeshStandardMaterial,
2526
NumberKeyframeTrack,
2627
Object3D,
2728
PerspectiveCamera,
@@ -1298,6 +1299,35 @@ class FBXTreeParser {
12981299

12991300
}
13001301

1302+
// Sanitization: If geometry has groups, then it must match the provided material array.
1303+
// If not, we need to clean up the `group.materialIndex` properties inside the groups and point at a (new) default material.
1304+
// This isn't well defined; Unity creates default material, while Blender implicitly uses the previous material in the list.
1305+
if ( geometry.groups.length > 0 ) {
1306+
1307+
let needsDefaultMaterial = false;
1308+
1309+
for ( let i = 0, il = geometry.groups.length; i < il; i ++ ) {
1310+
1311+
const group = geometry.groups[ i ];
1312+
1313+
if ( group.materialIndex < 0 || group.materialIndex >= materials.length ) {
1314+
1315+
group.materialIndex = materials.length;
1316+
needsDefaultMaterial = true;
1317+
1318+
}
1319+
1320+
}
1321+
1322+
if ( needsDefaultMaterial ) {
1323+
1324+
const defaultMaterial = new MeshStandardMaterial();
1325+
materials.push( defaultMaterial );
1326+
1327+
}
1328+
1329+
}
1330+
13011331
if ( geometry.FBX_Deformer ) {
13021332

13031333
model = new SkinnedMesh( geometry, material );

0 commit comments

Comments
 (0)