Import vertex colored meshes with obj files

Hello,
Is it possible to import OBJ files with mesh colored by vertex?

I am new to Babylon.js… here is a part of my code:

const assetsManager = new BABYLON.AssetsManager(scene);
const meshTask = assetsManager.addMeshTask("mtask", "", "/data/objects/", "Ignatius.obj");
assetsManager.load();
meshTask.onSuccess = function (task) {
    console.log("loading success");

    let ms = task.loadedMeshes.filter(function (mesh) {
        return mesh.geometry._totalVertices > 0;
    });
    const meshes = ms;

    meshes.forEach(function (mesh) {
        console.log(mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind));
    });
};

It always displays “false”… The OBJ file seems correct as it loads with colors per vertex when using meshlab…

Cannot answer your question, but while you wait, you might read the file with a text editor (if this is not a binary format), and make ABSOLUTELY sure that there are vertex colors in the file.

I scanned the code and I don’t think it reads any vertex colors. From a brief search on the web, it seems using vertex colors in obj is non-standard which is probably why it’s not implemented. I would be okay with supporting it under a flag though. Submit a PR if anyone wants to do it.

I am keen to have a look. I think R G B come after X Y Z. @Mickael_PASTOR - can you share an OBJ file?

Hi,
Thanks for all your answers! Here is a wetransfer link of a red colored stanford bunny with “hi !” written in black. For the purpose of the example, there is no normal vectors in the file: https://we.tl/t-I6YBfcfNLM

I got the red bunny with “HI” in MeshLab, so looks good.

I found where VCG imports OBJ files. They have RGB as additional tokens - if R,G & B are < 1 they are scaled by 255, but are otherwise used as is. We have Color4.FromInts(r,g,b,a)), so I will probably go the other way. Also, they support an optional alpha after the RGB, which defaults to 1.

More importantly also, they are always generating vertex colors (when import mask option for Vertex Colors is enabled) and that is inefficient for memory as this is non-standard. The reason that I mention that is that if we do a pass to detect presence of Vertex Colors we could use that to decide if we are going to add the Color Vertices, so I won’t want to default to Gray like they did, as we already have a default material. I will have a look at the importer again, perhaps the file parsing is into an intermediate format. Anyway, will report back here and will work on it tomorrow. I need to look through the code to find the unit tests as this is my first importer modification.

I’ll start on a PR. How do you mean under a flag? Please read my previous comment as we may be able to auto-detect color vertices and if that is sufficient.

If we can auto-detect, what about something like:
public static IGNORE_VERTEX_COLORS = false;

I think we would want to import them by default, if available. That is not backwards compatible, but also we would perhaps choose the most desired default behaviour. Anyway, we can discuss on the PR as well. Cheers.

Yes, this is what I mean. Let’s discuss backwards compatibility once you have the PR ready.

@Mickael_PASTOR made some progress. I will try to push PR tomorrow :slight_smile:

1 Like

@Mickael_PASTOR I think what you have sent me is not an exported OBJ file a right-handed system (like most systems), while BabylonJS is left-handed.
https://doc.babylonjs.com/how_to/obj
The obj model should be exported with -Z axis forward, and Y axis upward to be compatible with Babylon.js

I have also added an option to compute normals and it’s looking more or less how it does in MeshLab (just backwards):

You can see here an example where I change the scene to right-handed how the eyes on the rabbit switch (big/small):
https://www.babylonjs-playground.com/#D694ZZ

1 Like

There is a PR created, but it will involve some discussion

cheers

2 Likes

You guys are so incredible!! I am used to Three.js and I was hesitating of using Babylon, now I am fully convinced!
The last result looks exactly like in MeshLab. I tried the playground and again, thanks for the info, I did not know about the left-handed system.

Pretty amazed again! Thanks wery much! I will follow with great attention the progress of the PR.

2 Likes

The PR has been merged!!!
Again thanks very much for the amazing job you’ve done!

2 Likes

Here is a working PG for anybody searching for syntax:
https://www.babylonjs-playground.com/#2A4IK3#1

1 Like