- Home /
Colored Voxel Meshs
Hey,
I'm trying to make a voxel editor for Ludum Dare starting tomorrow and I'm pretty much good to go except for one final aspect: Making my meshes colored. Basically I let the user put down a bunch of cubes wherever his cursor is at and when he clicks "Save" I combine the cubes into one mesh to reduce the draw calls and then save it as a prefab. This works and it lets me use the objects in the editor, but currently the meshes are missing textures, they are pink. Does anybody know how I could extend it to carry over the colors of each individual cube over to the mesh? I know it's possible and I also know I'm probably going to need vertex colors, but I have absolutely no experience when it comes to 3D mesh manipulation so I would appreciate if somebody could tell me what exactly I'm going to have to do.
Greetings,
Chelmney
Answer by robertbu · Aug 22, 2013 at 11:10 PM
Each mesh has 'colors' array. The array must be the same length as the 'vertices' array, and the colors array must be set after the vertices array. Each triangle is defined by three vertices, and the vertex color at some point in that triangle is interpolated from the color of the three vertices.
So how easy or hard it will be to add color will depend on how you are combining your cubes. In particular it will depend on whether you are sharing vertices between cubes. A vertex can only have one color, so there is no way to have two different colors for two cubes that share vertices. If you are currently sharing vertices, you will have to add logic to break this sharing. That's what @DaveA means by "Up to 8 cubes per vertex position."
If they do not share vertices, then things become much easier. You just have to set the color for the new vertices added to the combined mesh. If you are using Unity's built-in cube, there will be 24 vertices.
Note vertex color will only show up with a shader that uses vertex colors.
Answer by DaveA · Aug 22, 2013 at 09:46 PM
Use sub-meshes. Each sub-mesh has its own material, hence can have own color and texture.
@DaveA - It appears from the question that the only property he wants to set is color, and the reason he is combining blocks is performance. From a performance standpoint wouldn't he be better of just setting the vertex colors for the vertices associated with each block and not using submeshes? If he uses multiple different materials wouldn't he get a separate draw call for each block? He would need a shader that handles vertex color, but the whole mesh would draw in a single draw call. At least that is how I understand it from my shaky understanding of meshes.
Yeah that's probably right. So he'll have to duplicate vertices for adjacent cubes that have different colors though, wouldn't he? Up to 8 cubes per vertex position. Fun little algorithm.
Your answer
Follow this Question
Related Questions
Render voxels with RGB data passed to them 1 Answer
How to check if object is skewed ? 2 Answers
Alter Meshes in Editor by Script without warning 2 Answers
Changing Mesh Vertex Colors in editor 0 Answers
How to make colored voxels?!? 1 Answer