- Home /
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
newTriangles.Add(faceCount * 4 + 1 ); //2
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 ); //1
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 [red] cube with Color (255,0,0) purely from code? I could work from that.
Answer by Statement · Dec 27, 2013 at 11:09 PM
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.
I went into your answer not knowing a thing about how shaders work. I grabbed a VertexShader that took pos and color; then simply added a color whenever a vertex was created. At the end I populated the list and to my amazement it all worked out! Thank you so much! $$anonymous$$y only issue is that I oversaw that, since this is vertex lit, it will not cast shadows. Is there a way to add shadow functionality to a vertex shader..? (I am not very knowledgeable about custom shaders)
A simple way is to write (or modify) a surface shader. If you don't know how to write shaders and if you have no desire to learn it, check the asset store for visual shader tools where you can just drag things together to compose shaders, kismet style (I guess - haven't used it but seem popular). If you want to learn shaders, I guess apart from learning CG shading language you should also study ShaderLab which is Unitys wrapper into shaders. You can also google for existing shaders - a shader that adds color to a textured, lit, shadowed, shadowcasting cube is very simple to make and I would be truly amazed if there isn't one out there. Check the Unify commmunity wiki for stuff, too.
http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html
http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html
Your answer
Follow this Question
Related Questions
Change Color of model from Down to up! 1 Answer
Colour cubes after CombineMesh() 0 Answers
How to make colored voxels?!? 1 Answer
can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers
Save a procedurally generated mesh and load it after? 0 Answers