- Home /
Vertex coloring isn't working
So, I'm making a custom mesh directly from scripting, setting up the vertices, triangles, normals and UVs, and now also trying to change the vertices colors (Vertex colors).
However, even tho I don't have any errors when I run the script, no results shows up. The mesh aren't colored or anything, but the colors ARE getting actually getting inserted to the mesh.
This is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RoomCreator : MonoBehaviour {
private List<Vector3> Mesh_Vertices = new List<Vector3>();
private List<Vector2> Mesh_Uvs = new List<Vector2>();
private List<int> Mesh_Triangles = new List<int>();
private MeshFilter meshFilter;
void Start() {
meshFilter = GetComponent<MeshFilter>();
CreateRoom();
}
private void CreateRoom() {
meshFilter.mesh = CreateMesh();
}
private Mesh CreateMesh() {
Mesh m = new Mesh();
m.name = gameObject.name + "_Mesh";
CreateFace(new Vector3(0,0,0), 10,1,0);
CreateFace(new Vector3(10,0,0), 0,1,10);
m.vertices = Mesh_Vertices.ToArray();
m.triangles = Mesh_Triangles.ToArray();
m.uv = Mesh_Uvs.ToArray();
m.RecalculateNormals();
print("Vertices: " + Mesh_Vertices.Count); // Vertices: 8
print("Triangles: " + Mesh_Triangles.Count); // Triangles: 12
Color[] colors = ColorizeFaces(m.vertices, m.triangles);
m.colors = colors;
print("Colors: " + m.colors.Length); // Colors: 8
return m;
}
private Color[] ColorizeFaces(Vector3[] vertices, int[] triangles) {
Color[] colors = new Color[vertices.Length];
Color triColor = getRandomColor();
for (int i = 0; i < triangles.Length; i++)
{
int vertIndex = triangles[i];
if (i % 3 == 0)
triColor = getRandomColor();
colors[vertIndex] = triColor;
}
return colors;
}
private Color getRandomColor()
{
float red = Random.Range(0.0f, 1.0f);
float green = Random.Range(0.0f, 1.0f);
float blue = Random.Range(0.0f, 1.0f);
return new Color(red, green, blue);
}
private void CreateFace(Vector3 position,int width, int height, int depth) {
Mesh_Vertices.Add(new Vector3(position.x, position.y, position.z));
Mesh_Vertices.Add(new Vector3(position.x+width, position.y, position.z+depth));
Mesh_Vertices.Add(new Vector3(position.x, position.y+height, position.z));
Mesh_Vertices.Add(new Vector3(position.x+width, position.y+height, position.z+depth));
int a = Mesh_Vertices.Count-1;
int b = a-1;
int c = a-2;
int d = a-3;
Mesh_Triangles.Add(a);
Mesh_Triangles.Add(b);
Mesh_Triangles.Add(d);
Mesh_Triangles.Add(d);
Mesh_Triangles.Add(c);
Mesh_Triangles.Add(a);
Mesh_Uvs.Add(new Vector2(0,0));
Mesh_Uvs.Add(new Vector2(1,0));
Mesh_Uvs.Add(new Vector2(0,1));
Mesh_Uvs.Add(new Vector2(1,1));
}
}
Answer by Eric5h5 · Jan 17, 2014 at 03:57 AM
You need to use a shader that utilizes vertex colors; most of the built-in shaders don't.
I see.. Is there shaders you could recommend for this then?
You can make a custom shader that uses vertex colors...it depends on what you want the shader to do.
Answer by HappyMoo · Jan 18, 2014 at 06:43 PM
Use a Shader like this:
Shader "Custom/Vertex Colored Lighted" {
Properties {
}
SubShader {
Pass {
ColorMaterial AmbientAndDiffuse
Lighting On
}
}
}
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Set UVs depending on input 1 Answer
Generate mesh from faces, invisible 1 Answer
calculate normals 2 Answers
Change Halo Color with Code 1 Answer