- Home /
setting vertex color of mesh not working
I'm trying to set the vertex color of a mesh. http://docs.unity3d.com/Documentation/ScriptReference/Mesh-colors.html
I have the following script attached to two objects in the scene. One of them is a custom mesh exported as FBX from 3DSMax and the other is a Unity Cube object.
void Update ()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
colors[i] = Color.red;
}
mesh.colors = colors;
}
What am I missing here? Why isn't the mesh changing color? Oh and I know that I could just change the material color I'm trying a more advanced effect I just paired down the code for this example.
Thanks guys
Answer by robertbu · Feb 04, 2013 at 06:04 PM
What you have should work. Are you sure your shader supports vertex color? What you are doing here is essentially what I did in the ChangeColor() method in this post, and it worked fine. There is a very simple vertex shader in this post as well.
http://answers.unity3d.com/questions/391561/create-a-mesh-and-color-cubes.html
AAAAWWWWWW of course I was using the default diffused shader. Thanks!!! If anyone else is interested here's a shader on the Wiki that supports multiple vertex colors: http://wiki.unity3d.com/index.php?title=VertexColor
robert, I have the exact same problem, I attempting to change vert alpha's for use in a fog of war, in order to test it i set the visible area to yellow ins$$anonymous$$d of alpha = 0; absolutely nothing happened. I read your post and applied a vertex shader, still nothing happened. Is there any chance i could beg your assistance?
In a new scene with some simple geometry, try your code with the shader at the link. If you cannot get it to work, post a new question along with your code.
Answer by cockratice · Nov 30, 2016 at 07:38 AM
Hi I wonder if someone knows how to assign the new colors to the mesh as a slow transition over time, when using mesh.colors = colors the modification happens instantly but I am looking for if to happen as a slow transition. I've been trying using:
mesh.colors = Color.Lerp (colorsA, colorsB, t); or
But since colorsA and colorsB are arrays containing the different colors for each one of the mesh vertices I haven't managed to make Lerp work.
you can lerp whole materials for that. Reffer to: https://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.Lerp.html
Your answer
Follow this Question
Related Questions
Render voxels with RGB data passed to them 1 Answer
Changing Mesh Vertex Colors in editor 0 Answers
How would I change the color of a texture within an object with multiple textures? 1 Answer
Change color of mesh triangle based on Y position in world space 1 Answer
How to change the color of a mesh? 0 Answers