- Home /
Colors do not work with a mesh created in script?
Hello,
I tried to generate mesh(just a triangle here) on the run from a script, and the colors don't work. The triangle appears always pink (looks like Color(255,0,255)). No matter what I set to the vertex colors to. Any idea why this could happen? Do I need to have a material/shader for this to work?
Here's the code
void Start() {
gameObject.AddComponent("MeshFilter"); gameObject.AddComponent("MeshRenderer"); Mesh mesh = GetComponent<MeshFilter>().mesh; mesh.Clear();
mesh.vertices = new Vector3[] { new Vector3(-0.5f, 0, 0), new Vector3(0, 1, 0), new Vector3(0.5f, 0, 0) };
mesh.normals = new Vector3[] { new Vector3(0, 0,-1), new Vector3(0, 0,-1), new Vector3(0, 0,-1) };
mesh.colors = new Color[] { new Color(0,0,0), new Color(0,0,0), new Color(0,0,0) };
mesh.triangles = new int[] { 0, 1, 2 };
}
Answer by Mike 3 · Mar 11, 2011 at 10:10 AM
Yup, pink generally means missing shader.
You'll want to add a material with a vertex coloured shader on - there are a few of them on the wiki if you don't have one, e.g:
http://www.unifycommunity.com/wiki/index.php?title=VertexColorUnlit
or
http://www.unifycommunity.com/wiki/index.php?title=VertexColor
Links are dead, use http://wiki.unity3d.com/index.php?title=VertexColorUnlit & http://wiki.unity3d.com/index.php?title=VertexColor ins$$anonymous$$d.
Answer by naro · Mar 11, 2011 at 11:23 AM
Here's the shader which you need to use with the mesh if you want to display only the interpolated vertex colors:
Shader "MyShader/VertexColor" {
Category {
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
}
SubShader { Pass { } }
}
}
I'm totally new to Unity and shaderLab language so I wasn't aware of this binding thing at all :)
EDIT: added vertex binding too (otherwise you get shader errors)
Answer by cupsster · Sep 12, 2013 at 11:21 PM
mesh.colors = new Color[] { new Color(0,0,0,0),
new Color(0,0,0,0),
new Color(0,0,0,0) };
I think you are missing last digit (alpha value) from color definition in your array.
Your answer
Follow this Question
Related Questions
minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers
Trouble recalculating 3D mesh's triangles after deleting verts 2 Answers
Vertex colours - can they improve performance? Do they affect batching? 1 Answer
using grayscale texture as heightmap, vertices and pixel don't match 0 Answers
Optimized vertex manipulation 1 Answer