- Home /
Does anyone know a way of importing vertex colors from a .obj file?
Hi!
I'm working on a Mesh Combiner tool for merge meshes to reduce draw calls on mobile platforms. The tool must be able to save the combined mesh on disc to be used as a prefab.
Easiest way I found was to write my own parser for .obj file and it works great, except Unity does not want to load the vertex color part.
Looking into the sharedMesh variable of the MeshFilter component that has the original meshes (imported from .fbx file), the colors are all there. Doing the same on the .obj file I created, the array with the colors is empty.
I know for a fact that the colors are parsed, because I obviously checked the .obj file while working on the parser.
In conclusion, it seems that Unity ignores vertex color information from an .obj file.
Does anyone know a way of importing vertex colors from an. obj file?
Thank you in advance!
Answer by Bunny83 · Aug 20, 2017 at 02:11 AM
Uhm, the OBJ format doesn't support any vertex colors. It only supports position, normal and one set of texture coordinates. It also has support for parameter space vertices which are used to define free-form geometry. However most trivial parsers don't support this at all. Anyways the standard does not define any vertex colors. So if your file actually contains vertex colors it's either a custom extension of the OBJ format which nobody understands or the format you deal with is not an OBJ file in the first place. OBJ is a very simple and trivial format. It's not really suitable for what you want to do.
Do you actually work on an editor extension or on a runtime script / framework? Keep in mind that at runtime you can't create new assets / prefabs. Inside the editor you can simply serialize your "Mesh" instance into an .asset file. This will create a new "model asset" which supports everything the Mesh class supports. It's the actual Mesh asset format that Unity uses to store your imported meshes in your project after it has been imported.
If you still want to somehow use your "OBJ" file, you should post a stripped down example OBJ file that you're using that contains "vertex colors".
ps: This statement is a bit confusing as well:
Unity does not want to load the vertex color part
Didn't you say that you used your own OBJ parser? That would mean you do the loading, not Unity.
Hei @Bunny83. Thanks for the fast reply.
I read around forums and you are right: the standard .obj file format does not support vertex colors, but they are used informally by some people.
As for the parser, I only created the exporting (writing to file) part, since I was relying on Unity to load it back.
Anyway, I actually found this fbx exporter (https://github.com/$$anonymous$$ellanHiggins/UnityFBXExporter) which is the better version of what I was trying to do. Now I just create my mesh and set it to a $$anonymous$$eshFilter on an object that I instantiate, then call the function from this fbx exporter component to save my scene object into an fbx file. And it works perfectly!
I don't use any of this at runtime, only in editor to setup my environment (which is procedurally generated using prefabs, that's why I can't just save the scene as it is and have to save the newly created meshes into files).
Well, as i said if the meshes are created procedurally in Unity, just save the mesh as asset by using AssetDatabase.CreateAsset and just pass in the mesh object. $$anonymous$$esh assets can be stored in a generic ".asset" file.
Your answer