# Texturing voxel faces separately

I am creating a 3D level editor which should be used to generate the very basic layout (level geometry) and a few basic game-logic entities of maps used in my game. So far, I decided to use voxels (i have a fbx untextured model file and do NOT draw this myself via vertices etc. as I already found tutorials on that which are not suitable for me) as the way to create the level geometry and most of this works fine. The next step would be to implement code that allows me to texturize these cubes by specifically applying textures to faces.

Using something like the following Draw method I can apply a Texture2D to the whole voxel (so I am not able to chose the face to which I want to apply the texture) and this works fine for now (though I have sometime issues orienting it, which should not be part of this question)

public void Draw() {
foreach(ModelMesh mesh in this.Model.Meshes) {
foreach(ModelMeshPart part in mesh.MeshParts) {
BasicEffect effect = (BasicEffect)part.Effect;

effect.World = this.World;
effect.View = Engine.View;
effect.Projection = Engine.Projection;

if(this.Texture != null) {
// UNLIT textures
effect.Texture = this.Texture;
effect.TextureEnabled = true;
} else
// LIT placeholder voxel
effect.EnableDefaultLighting();
}
mesh.Draw();
}
}


I will have about 10 textures which I want to be able to apply programmatically on each face of the voxels - which approach would you suggest me to achieve the desired result?

• What if you had a mesh part for each face? Then you could texture each part on its own. – Thraka Aug 2 '12 at 17:28
• @Thraka: Neither have I enough experience in modelling nor did I create the model myself. Therefore I do not understand what you suggest. – Christian Ivicevic Aug 2 '12 at 21:33
• This seems to be an incredibly ineffective way of drawing multiple single voxels and might not work at all that way. How many voxels do you expect to draw and can you alter the mesh or create a new one? Are the individual mesh parts the faces of the voxels? – Darcara Aug 2 '12 at 22:39
• @Darcara: Thinking about the efficiency I have approximated an upper bound of 750 entities (unmerged voxels + custom entities) for a level - after some optimization it would be easy to merge voxels to bigger blocks (and therefore decreasing the number of entities) but at the beginning I have to have the option to build the world with voxels and individually choose the material (texture) on their visible faces during the creation. Concerning the structure of the model... I have no idea as I have used one box I found in a sample project - shame on me :( – Christian Ivicevic Aug 2 '12 at 23:19
• Do you have access to the UV's? Unless I'm misunderstanding this a texture-map and modified UV's will do this for you. – salmonmoose Aug 3 '12 at 6:00

The next best thing would be to create a mesh where each ModelMeshPart is one face of your cube. Then you can set the texture for each face individually. This will however still not scale very well, but should fast enough for 750 cubes.
• You have to generate at runtime, unless all combinations are known beforehand, because with only 10 textures there are 10^6 possible combinations. Creating a cube map is relatively easy. Look at the numbered view here, you'll see that each square corresponds to one face. Create a big enough RenderTarget2D (it's Texture2D) and draw your stock textures into the right places using SpriteBatch. Now the render target is the texture that contains your cube map. – Darcara Aug 3 '12 at 11:35
• I was able to draw the RenderTarget2D and assign it to the cube but it does not wrap automatically! What is the general approach to wrap this cube map around the cube? – Christian Ivicevic Aug 3 '12 at 14:28