# Multi texturing in XNA

I am attempting to make a map editor for my game, just so I can visualize how the end result would be.

To my world I can add cubes (2 triangles) or corners (1 triangle), which are then added to a list of the vertices to draw, and the indices are added to a list of indices. Everything draws fine here, and I can set different colours for each vertex.

I would like to implement texturing, so that when you add a primitive to the scene it will let you choose a texture (that, I can handle), but then also blend with other primitive's of different textures if they are touching (everything is on a grid).

I would guess that I would have to store the texture name for each primitive, and somehow set that texture when drawing it. But I fail to see how you can set the effects texture when you are using one DrawUserIndexedPrimitives<> to draw the entire scene.

If anyone has a work around for this, or ideas on other ways of doing this, you help would be greatly appreciated.

Edit 1

I have incorporated the technique from this website here - http://toyfactorygame.com/wp.php/2010/05/06/simple-fast-gpu-driven-multi-textured-terrain/ (similar to Blau's answer below), and have the current working result...

Which maps to this...

Using each color channel as a texture..

Output = tex2D(TextureSampler0, input.TexCoords) * input.Color.r;
Output += tex2D(TextureSampler1, input.TexCoords) * input.Color.g;
Output += tex2D(TextureSampler2, input.TexCoords) * input.Color.b;
Output += tex2D(TextureSampler3, input.TexCoords) * input.Color.a;


But doing it this way means I can only be setting 4 textures to blend together each DrawPrimitives call.

Is there another way I can achieve multi texturing with more than 4 (preferably unlimited) textures? I know this is possible, as games like C&C generals allow you to set textures anywhere you want on the map.

Thanks.

Twitchy

• is easy to add more blending textures, but usually has no sense, you rarely will find a situation where you need to blend more than four textures per vertex. – Blau Jan 15 '12 at 15:02
• Why would you say that? Because unless there is some other way of achieving the effect I want, this looks the only way. – FrenchyNZ Jan 15 '12 at 17:37
• do you really need to apply more than four textures to a vertex at the same time? I suppose you realize that your vertex should not be restricted to use the same four textures ever, only to use four at the same time. – Blau Jan 15 '12 at 19:52
• And of course, you can use a texture atlas to index textures, or four texture atlas, you only have to organize your sprites a bit. :) – Blau Jan 15 '12 at 19:58
• I understand that a vertex should not be restricted to four textures, What I don't understand is to take in the textures that are at the same position of each vertex in a way that does not require color mapping. I guess I will have to use something like a TextureWeight array. But does that mean I have to change the size of the array each time I add a new texture? Or am I missing something here? – FrenchyNZ Jan 15 '12 at 20:47

   int[4] TextureId;

  finalTexColor = tex0sample * TextureWeight[0] + tex1sample * TextureWeight[1] +tex2sample * TextureWeight[2] + tex3sample * TextureWeight[3]