- Home /
Multiple Colors in Mesh Programmatically?
I can programmatically assign my mesh any one color with code like this:
renderer.material.color = Color(1.0, 0.5, 1.0) # Light purple
But I'd like to programmatically assign my mesh multiple colors (per-face/triangles preferred, but per-vertex acceptable).
I saw that Mesh
has an attribute called colors
, so I tried using it like this (this is nearly identical to the example in the scripting documentation here: http://docs.unity3d.com/ScriptReference/Mesh-colors.html):
mesh = GetComponent(MeshFilter).mesh
colors = array(Color, mesh.vertices.Length)
for i in range(mesh.vertices.Length):
colors[i] = Color.Lerp(Color.red, Color.green, mesh.vertices[i].z)
mesh.colors = colors
But my mesh came out a solid white instead of being red, green, or a mix. What am I doing wrong?
This looks like it'll be super useful for me learning how to do this: http://docs.unity3d.com/$$anonymous$$anual/ShadersOverview.html
You likely are not using a shader that supports vertex colors. Note sure where you are going with the functionality, but with 3D modeling programs, you can assign different materials to different parts of the mesh. Then inside Unity you can use the materials array to change the color for one set of triangles.
$$anonymous$$y mesh is programmatically generated at runtime, so I won't be doing any of this outside Unity (thus there won't be an import step.)
Answer by ArtOfWarfare · Nov 03, 2014 at 03:16 AM
After reading up on Shaders for a few hours and seeing a few different examples, I figured out how to modify the diffuse shader to use vertex colors instead of texture colors. Here it is:
Shader "MyShader" {
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
struct Input {
fixed4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = IN.color;
}
ENDCG
}
Fallback "VertexLit"
}
I'm not quite sure what the Tags and LOD do, but the important parts are:
#pragma surface surf Lambert
Says that I'm going to define a surface
shader called surf
which I want to have use the Lambert
lighting system (that's how my mesh ends up with shadows and stuff rather than looking flat).
Since it's a surface
shader, Unity automagically figures out how to pipe data around for me. I just let it know that I need a COLOR
as Input
to surf
- it ends up grabbing the color of the vertex, which is correct. At that point, all I have to do is write the simple one line function surf
which just takes the vertex color and applies it to the SurfaceOutput
.
If someone else had told me all this hours ago, it would have been pretty helpful for me, I think, so hopefully now that I've written this here, it'll help others in the future.
Answer by Bunny83 · Nov 02, 2014 at 09:41 PM
Most normal shaders don't use the vertex color at all. Only a few specialized shaders like all the particle shaders. Just try different shaders and you will see the vertex colors.
You can easily write your own shader that suits your needs. See this page for more details on how to use vertex colors. It's the 4th example.
Thanks for that, Bunny83. I just began reading up on shaders less than an hour ago so this is still over my head. I copy and pasted that shader code into a new shader, created a new material and applied the shader to it, and then applied the material to my mesh. But now I have the problem that it all renders flat - whereas the diffuse shader interacted with the lightning and shadows, this shader just shows the vertex colors with no respect for lights and shadows. How can I modify it to both utilize the vertex colors as its doing now and to factor in lighting and shadows as it was when it was just using the diffuse shader?
Thanks again for your help. I've upvoted your answer since it was helpful in giving me more examples to look at, but it wasn't quite everything I was looking for. I posted another answer (which I've marked as accepted) which more comprehensively covers what I wanted (hopefully it'll help people in the future. Not sure. UnityAnswers seems so broken that I'm a bit skeptical that its search engine would actually ever turn this up, even if it was exactly what someone needed.)
Your answer
Follow this Question
Related Questions
Making a Game Accessible for the Blind and Visually Impaired 4 Answers
Random Color 1 Answer
Best way to mix color values? 4 Answers
Set Gameobject a certain color based off of other game objects 1 Answer
RGB Colors as Float Values Converter 0 Answers