Skip to content

Commit 2c0c7d0

Browse files
authored
Merge pull request #6923 from davepagurek/fix/model-import
Fix vertices with different texture coordinates in imported models getting collapsed
2 parents 913e48e + 57a2e11 commit 2c0c7d0

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

src/webgl/loading.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,19 @@ function parseObj(model, lines, materials= {}) {
410410
vertParts[i] = parseInt(vertParts[i]) - 1;
411411
}
412412

413-
if (!usedVerts[vertParts[0]]) {
414-
usedVerts[vertParts[0]] = {};
413+
if (!usedVerts[vertString]) {
414+
usedVerts[vertString] = {};
415415
}
416416

417-
if (usedVerts[vertParts[0]][currentMaterial] === undefined) {
417+
if (usedVerts[vertString][currentMaterial] === undefined) {
418418
const vertIndex = model.vertices.length;
419419
model.vertices.push(loadedVerts.v[vertParts[0]].copy());
420420
model.uvs.push(loadedVerts.vt[vertParts[1]] ?
421421
loadedVerts.vt[vertParts[1]].slice() : [0, 0]);
422422
model.vertexNormals.push(loadedVerts.vn[vertParts[2]] ?
423423
loadedVerts.vn[vertParts[2]].copy() : new p5.Vector());
424424

425-
usedVerts[vertParts[0]][currentMaterial] = vertIndex;
425+
usedVerts[vertString][currentMaterial] = vertIndex;
426426
face.push(vertIndex);
427427
if (currentMaterial
428428
&& materials[currentMaterial]
@@ -431,7 +431,7 @@ function parseObj(model, lines, materials= {}) {
431431
coloredVerts.add(loadedVerts.v[vertParts[0]]); //since a set would only push unique values
432432
}
433433
} else {
434-
face.push(usedVerts[vertParts[0]][currentMaterial]);
434+
face.push(usedVerts[vertString][currentMaterial]);
435435
}
436436
}
437437

test/unit/assets/cube-textures.obj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Simple Cube OBJ File that maps each face
2+
# to a texture
3+
4+
# Vertices
5+
v 0.0 0.0 0.0
6+
v 1.0 0.0 0.0
7+
v 1.0 1.0 0.0
8+
v 0.0 1.0 0.0
9+
v 0.0 0.0 1.0
10+
v 1.0 0.0 1.0
11+
v 1.0 1.0 1.0
12+
v 0.0 1.0 1.0
13+
14+
# Texture coords
15+
vt 0 0
16+
vt 1 0
17+
vt 1 1
18+
vt 0 1
19+
20+
# Faces
21+
f 1/1 2/2 3/3 4/4
22+
f 5/1 6/2 7/3 8/4
23+
f 1/1 5/2 8/3 4/4
24+
f 2/1 6/2 7/3 3/4
25+
f 4/1 3/2 7/3 8/4
26+
f 1/1 2/2 6/3 5/4

test/unit/visual/cases/webgl.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,21 @@ visualSuite('WebGL', function() {
111111
});
112112
});
113113
});
114+
visualTest(
115+
'Object with different texture coordinates per use of vertex keeps the coordinates intact',
116+
async function(p5, screenshot) {
117+
p5.createCanvas(50, 50, p5.WEBGL);
118+
const tex = await new Promise(resolve => p5.loadImage('unit/assets/cat.jpg', resolve));
119+
const cube = await new Promise(resolve => p5.loadModel('unit/assets/cube-textures.obj', resolve));
120+
cube.normalize();
121+
p5.background(255);
122+
p5.texture(tex);
123+
p5.rotateX(p5.PI / 4);
124+
p5.rotateY(p5.PI / 4);
125+
p5.scale(80/400);
126+
p5.model(cube);
127+
screenshot();
128+
}
129+
);
114130
});
115-
});
131+
});
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)