Entradas anteriores
Nube de tags
Blogroll
Posts Más Vistos
Blog Stats
- 9,304 hits
Microsoft Student Partner – Mza – Arg
Hi, this time I’ll play a little with effects (don’t worry, I’m awful at HLSL so this won’t be complicate).
Generally, the problem in multi texture effects is to decide “how much” of each material apply. There are hundreds of approaches and different algorithms. I’ll use a very simple one: We have two textures, a base texture (for example a base skin texture for a character) and a brush texture (lets say, a piece of cloth). The second texture is a transparent texture with some areas painted.
What we are going to do is, take the base texture, and remove the areas that are not transparent on the second one. Although this sound complicated, this is super easy to do. I’ll post some code of the .fx file now:
First, notice I’ll be using two samplers. One for each texture I’ll be using.
|
texture TextureMap; texture Brush0; |
I’ll skip the vertex shader since it’s very simple. I’ll go straight to the algorithm:
|
// this will remove part of the base texture in order to apply the second material |
As you see, it’s really simple. 1 means full color without transparency. I’m removing the amount of color of the second texture. Piece of cake !
Now some XNA – C# code. First I’m loading an effect and applying to the model. Since a lot of people don’t know how to do this I’ll pos it.
model = Content.Load<Model>("cube"); effect = Content.Load<Effect>("MultiTexture"); // apply the effect foreach (ModelMesh mesh in model.Meshes) { foreach (ModelMeshPart meshPart in mesh.MeshParts) { meshPart.Effect = effect; } } |
And now, the drawing part where we assign the textures:
Matrix View = Matrix.CreateLookAt(new Vector3(0, 2, -6), Vector3.Zero, Vector3.UnitX); Matrix Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1, 100); Matrix[] container = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(container); foreach (ModelMesh mesh in model.Meshes) { foreach (Effect effect in mesh.Effects) { effect.Parameters["view"].SetValue(View); effect.Parameters["projection"].SetValue(Projection); effect.Parameters["world"].SetValue(container[mesh.ParentBone.Index] * Matrix.CreateRotationY(MathHelper.ToRadians(rotation))); effect.Parameters["TextureMap"].SetValue(baseTexture); effect.Parameters["Brush0"].SetValue(secondTexture); } mesh.Draw(); } |
And of course, here is the solution to download !