Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,19 @@ function loading(p5, fn){
if (model.vertexNormals.length === 0) {
model.computeNormals();
}
if (hasColoredVertices === hasColorlessVertices) {
// If both are true or both are false, throw an error because the model is inconsistent
throw new Error('Model coloring is inconsistent. Either all vertices should have colors or none should.');
if (hasColoredVertices && hasColorlessVertices) {
// Mixed model: some faces have a material diffuse color assigned, others do not.
// This is common in real-world OBJ exports (e.g. Blender, Sketchfab) where only
// some mesh groups have an explicit MTL material. Rather than crashing the sketch,
// we degrade gracefully: strip the partial vertex colors so the model renders
// with the default fill color instead of corrupted per-vertex coloring.
console.warn(
'p5.js: This OBJ model has mixed material coloring — some faces have a ' +
'material diffuse color and some do not. Vertex colors will not be applied. ' +
'Consider assigning a material to every face group in your 3D software, ' +
'or use a model where all faces share the same material.'
);
model.vertexColors = [];
}

return model;
Expand Down
17 changes: 12 additions & 5 deletions test/unit/io/loadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ suite('loadModel', function() {
assert.deepEqual(model.vertexColors, expectedColors);
});

test('inconsistent vertex coloring throws error', async function() {
// Attempt to load the model and catch the error
await expect(mockP5Prototype.loadModel(inconsistentColorObjFile))
.rejects
.toThrow('Model coloring is inconsistent. Either all vertices should have colors or none should.');
test('mixed material coloring loads model with empty vertexColors instead of crashing', async function() {
// eg1.obj has some faces without a material and some with one.
// Real-world exports from Blender/Sketchfab frequently produce this structure.
// The loader should degrade gracefully rather than throwing, so beginners'
// sketches don't crash when loading common 3D assets.
const model = await mockP5Prototype.loadModel(inconsistentColorObjFile);
assert.instanceOf(model, Geometry);
assert.equal(
model.vertexColors.length,
0,
'Mixed-material model should have no vertex colors (graceful degradation)'
);
});

test('missing MTL file shows OBJ model without vertexColors', async function() {
Expand Down