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
6 changes: 3 additions & 3 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,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]);
Expand All @@ -652,9 +653,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;
Expand Down
26 changes: 21 additions & 5 deletions test/unit/io/loadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down