diff --git a/src/webgl/loading.js b/src/webgl/loading.js index 51eef5b09d..d9aed75215 100755 --- a/src/webgl/loading.js +++ b/src/webgl/loading.js @@ -629,6 +629,7 @@ function loading(p5, fn){ model.vertexColors.push(1); } else { hasColorlessVertices = true; + model.vertexColors.push(-1, -1, -1, -1); } } else { face.push(usedVerts[vertString][currentMaterial]); @@ -650,9 +651,8 @@ 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) { + model.vertexColors = []; } return model; diff --git a/test/unit/io/loadModel.js b/test/unit/io/loadModel.js index f88a5807cc..590a12bb1e 100644 --- a/test/unit/io/loadModel.js +++ b/test/unit/io/loadModel.js @@ -79,11 +79,27 @@ 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 sentinel colors for uncolored vertices', async function() { + const model = await mockP5Prototype.loadModel(inconsistentColorObjFile); + assert.instanceOf(model, Geometry); + assert.equal( + model.vertexColors.length, + model.vertices.length * 4, + 'vertexColors should have four entries per vertex' + ); + const hasSentinel = model.vertexColors.some( + (_, i) => + i % 4 === 0 && + model.vertexColors[i] === -1 && + model.vertexColors[i + 1] === -1 && + model.vertexColors[i + 2] === -1 && + model.vertexColors[i + 3] === -1 + ); + const hasRealColor = model.vertexColors.some( + (_, i) => i % 4 === 0 && model.vertexColors[i] !== -1 + ); + assert.isTrue(hasSentinel, 'Uncolored vertices should have sentinel color'); + assert.isTrue(hasRealColor, 'Colored vertices should retain their color'); }); test('missing MTL file shows OBJ model without vertexColors', async function() {