# Render voxels with RGB data passed to them

I followed along with a guide on creating a nice voxel terrain generator, but it’s flexibility is very slim. I want to be able to pass RGB color data so when the face is rendered, it has a solid color texture. The original code would have required me to have a single texture sheet with all possible textures I wanted; I don’t see how this would work with every possible RGB color.

This function draws vertices for a single face (of 6), then calls Cube method:

``````void CubeBottom (int x, int y, int z) {
newVertices.Add(new Vector3 (x,  y-1,  z ));
newVertices.Add(new Vector3 (x + 1, y-1,  z ));
newVertices.Add(new Vector3 (x + 1, y-1,  z + 1));
newVertices.Add(new Vector3 (x,  y-1,  z + 1));
Cube(x, y, z);
}
``````

Then the Cube method, where the old texture code was:

``````void Cube (int x,int y,int z) {
newTriangles.Add(faceCount * 4 + 1 ); //2
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 + 3 ); //4

//Vector2 texturePos = new Vector2(0,0); //Old code, switch to RGB

//newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
//newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
//newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
//newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));

faceCount++;
}
``````

At the moment it only generates 1 cube at x,y,z = 0,0,0 which of course is magenta and texture-less.

How and where would I implement the ability to pass RGB data and apply it to each cube/voxel? This script is kept in an empty gameObject with a Mesh Renderer, Mesh Filter, and Mesh Collider.

More simply, how would I draw a cube with Color (255,0,0) purely from code? I could work from that.

You need to set mesh.colors.

If you design your code to have a `SetColor(1f, 0f, 0f);` then you could just store that color as a member variable. Whenever your builder add vertices (given they are NOT shared among triangles) just add an equal amount of colors to a Color list with the most recently set Color via SetColor. When you finally construct the mesh, just populate mesh.colors just as you populate mesh.vertices and mesh.uv.

You will also require a shader that uses the vertex color for it to have any effect.