1
votes

I have a mesh in my scene I'm trying to export to OBJ using the OBJExporter script that comes with Three.js. My mesh has multiple materials applied to it, meaning each face has a different materialIndex on it.

my mesh

This is one mesh with multiple materials applied to it using a THREE.MultiMaterial. According to the .obj specification under "Vertex Texture Coordinate Indices", which I don't fully understand, it says:

Optionally, texture coordinate indices can be used to specify texture coordinates when defining a face.

In the format

f v1/vt1 v2/vt2 v3/vt3

In the .obj data exported by the script, that section just looks like an incremental counter:

f 1/1/1 2/2/2 3/3/3
f 4/4/4 5/5/5 6/6/6
f 7/7/7 8/8/8 9/9/9
f 10/10/10 11/11/11 12/12/12

Additionally, when I save the exported string to a .obj file, and then re-import that using the OBJLoader, then apply a MultiMaterial to it, it only shows one material.

I'm trying to untangle the whole puzzle and have a lot of unknowns. I don't care about the material definition itself, I just care which face has a material index on it. Can the .obj format alone hold a material index for a multi-textured object per face?

1

1 Answers

0
votes

Yes, the .obj format can support multiple materials on the same object. The easiest way is to add a usemtl call for a group of faces. Something like:

usemtl 1
f 41/153/11 40/154/13 36/155/20 38/156/5
f 38/157/5 36/158/20 44/159/14 37/160/8
f 24/161/9 29/162/1 27/163/21 23/164/23
f 29/165/1 24/166/9 22/167/10 28/168/4
usemtl 3
f 55/185/2 54/186/5 51/187/8 56/188/3
f 51/189/8 53/190/7 50/191/4 56/192/3
f 54/193/5 49/194/6 53/195/7 51/196/8
f 55/197/2 52/198/1 49/199/6 54/200/5
f 49/201/6 52/202/1 50/203/4 53/204/7

In this case 1 and 3 are arbitrary identifiers I made up. They can be any string name.

To actually give some visual definition to the materials, you need to include a .mtl file along with the .obj file, like this example does. But to simply get faces to have their own material indices, you don't need to include that file.