I am trying to use Unity to display a model and color some triangles on the model to high light those parts of the model.
I have created a scene, with a camera, a light source and a cube. After the scene loads I have a script on the cube where I am importing a new mesh and replacing the cube mesh with the new mesh. I am also giving each triangle a random color.
void Update () {
if (Time.time > 10 && newShapeLoaded)
{
System.Collections.Generic.List<facet> newModel = readStl(@"C:\testing1\stls\sphere.stl");
System.Collections.Generic.List<Vector3> vertices = new System.Collections.Generic.List<Vector3>();
foreach (facet f in newModel)
{
vertices.Add(f.vecA);
vertices.Add(f.vecB);
vertices.Add(f.vecC);
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices.ToArray();
int[] t = new int[vertices.Count];
Color32[] colors = new Color32[vertices.Count];
Color32 currentColor = new Color32();
for (int i = 0; i < vertices.Count; i++)
{
t[i] = i;
colors[i] = currentColor;
if (i % 3 == 0)
{
currentColor = new Color(
Random.Range(0.0f, 1.0f),
Random.Range(0.0f, 1.0f),
Random.Range(0.0f, 1.0f),
1.0f);
}
colors[i] = currentColor;
}
mesh.triangles = t;
mesh.colors32 = colors;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.UploadMeshData(true);
newShapeLoaded = false;
}
}/* Update() */
This was not working and searching I found I needed to change the material type to a material that uses vertex color shading. I found the Particles>VertexLit Blended Shader colors the triangle but then the triangles are drawn in the wrong order so the shape appears inside out.
Is their anyone that knows more about Unity and 3D that could give me some advice or let me know where I have gone wrong with this?
(edit) So I have investigated this further and I believe the comment made about the particle material being the reason for the shape appears inside out is correct. I also noticed that the original cube has this problems before I change the mesh as well. So I have tried to find a material with a shader that will still let me color the triangles but not break the render order of the triangles. Unfortunatly I haven't been able to get anything to work. I have found a thread here where several people have tried to create a shader to do this and I have downloaded their projects and run them and tried to get them to work with very limited success. This lead me to also find several tutorials on shader programming to try and understand what was happening, but these have not given me any better understanding of how to get vertex coloring working.
I am now wondering if I have gone off track because all I really want to do is to make a simple model and change the color of some triangles on the model. I though this would be like a simple hello world exercise for 3D graphics, but I have put over a week into this now and tried several different approaches and still have not been able to get this working.
I would be very grateful if someone could show me an example of changing colors of triangles on a mesh? Or if someone that understands 3D graphics could just point me in the right direction to a tutorial or guide that will help me learn what I need to know to get this working and let me know if this is the correct way to try and be able to change colors on a mesh?