- Home /
Creating models with color only, from script
Hello, this is my first question on unity answers.
I am trying to create a model from script using the Mesh class, and represent each face with a separate color. I have a mesh filter, and a renderer attached to the game object. At the moment, it is a simple cube.
My problem is that I can not find out how to properly set these colors I desire. I have read about Mesh.colors and colors32, but none of the shaders will show these colors. Only the GUI/Text shader has been successful in getting close to what I'm looking for. The cube, upon creation turns grey, such as when you have an object without a material.
When I import a model as normal, Unity automatically creates as many materials as needed by the file, a unique material for each color. This would be great, if I was importing models, but I'm making procedural meshes. And even if I create several materials from code, applying them to the Cube does nothing to tell it which faces I want colored, and it only shows one of the materials anyway.
This troubles me, because I may want non-textured models to be loaded from an outside source (for example, user generated content, or a storage cache outside the unity folder), but it seems that even with a custom loader written, Unity does not expose the methods of applying these colors properly, and mostly gives support for skinned/uv models imported into the editor. This seems backwards to me, because with raw opengl, I can support solid colors easier than a texturing/uv system. However, I really enjoy Unity, and for all other aspects love it's rendering engine, so I'd like to be able to use it for my projects instead of deploying my own engine.
Let me note, I do not want to use textures at all, I'm purposely attempting to use vertex colored models. An example of the art style would be Mario 64, or the old runescape models. In those games, the characters and some world geometry used simple vertex colors, and any texturing was solely for detail effects.
Is there any way to accomplish what I want? It may be a product of me missing a critical documentation piece, and if so, would you please point me in the right direction? Thank you in advance for reading this question.
Answer by Eric5h5 · Dec 13, 2012 at 06:43 PM
You need a shader that uses vertex colors in order to use Mesh.colors. Most of the built-in shaders do not, so you can either find and download one that does, or make your own (see here for shader source).
If that is the case, how does Unity internally apply several material colors when you import a model from an external app? Is this feature not exposed to the scripting library? Thanks again
Not really sure what you mean. Vertex colors in an imported model will not show up unless you use a shader that has vertex color support. $$anonymous$$aterials and shaders are two different things.
$$anonymous$$y apologies, I believe I am confusing the term material between blender and unity. When I assign two separate "materials" in blender, all I'm actually doing is setting it to vertex lit, with two separate colors being assigned to whichever verts I specify. Checking the model file, it is storing this. In unity, it somehow converts these to Unity materials, which appear to be different in nature, but in the conversion process, it is retaining my selection of verts to color. However, this seems to be a oneway process, and I can not modify these colors after the fact, doing so breaks the linkage and just colors the entire mesh with the last applied unity material. I was asking is there anyway to access this behind the scenes process that unity does, so that I can modify these imported "blender materials" in Unity, but after your answers, and more googling on the subject, I think that there may not actually be an easy way. Of course, I could always just remodel and reimport back into unity to fix these, but this only works if I'm using the assets in the project folder, not if I am dynamically loading assets from a folder cache or the web.
Thank you for your interest in helping me understand.
After stewing on my thoughts for a moment, and re-reading what you posted, I went and played around with what is called a "submesh". This achieved exactly what I was looking for in combination with Unity materials and vertex colors. Thanks for your help!