Lostlogic

I'm using the Model object that comes with the XNA framework and have a loaded mesh from 3dsmax rendering on the screen. My question is, how can I apply a pixel shaders to materials on the mesh when rendering For example, the engine of the craft I am rendering is a separate material and I want to apply a glow shader just to the engine. I know which sub-material it is but don't know how to set the pixel shader to be used in the mesh.Draw() function

Any help would be appreicated.

Thanks!



Re: XNA Game Studio Express How to apply an effect to a mesh rendering?

Fluxtah

I assume it will probably be a ModelMeshPart, for model mesh parts you can apply your own shaders, it might be best to loop over the model and its mesh parts setting the desired effect on it in a preprocessing step.

The basic idea would be:

      foreach (ModelMesh mesh in Model.Meshes)
      {
        for (int i = 0; i < Mesh.MeshParts.Count; i++)
        {
          mesh.MeshPartsIdea.Effect = ContentManager.Load<Effect>(@"Path\To\Effect");
        }
      }


hope that helps





Re: XNA Game Studio Express How to apply an effect to a mesh rendering?

Lostlogic

Ok, I'm setting the effect now but how do I get it to render

I set it by:

foreach (ModelMesh mesh in myModel[1].Meshes)
{
for (int i = 0; i < mesh.MeshParts.Count; i++)
{
mesh.MeshPartsIdea.Effect = contentman.Load<Effect>("Content\\Fx\\glass");
}
}

My current redering loop fails to work now as it can't enumerate BasicEffect any longer. Here is the code that no longer works:

foreach (ModelMesh mesh in myModel[index].Meshes)
{
//This is where the mesh orientation is set, as well as our camera and projection
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(scale);

effect.World *= Matrix.CreateRotationY(rot.X);
effect.World *= Matrix.CreateRotationY(rot.Y);
effect.World *= Matrix.CreateRotationY(rot.Z);

effect.World *= Matrix.CreateTranslation(trans);

effect.View = Matrix.CreateLookAt(campos, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}

Thanks for the help so far. Do you think you can show me how to render the mesh using the effect





Re: XNA Game Studio Express How to apply an effect to a mesh rendering?

Ben Vanik

Lostlogic wrote:

//This is where the mesh orientation is set, as well as our camera and projection
foreach (BasicEffect effect in mesh.Effects)
{

I think I understand the issue... Simply change it from foreach( BasicEffect effect... ) to foreach( Effect effect ... ). If you know all effects are of the same type (like MyCustomEffect, if you wrote your own wrapper), then you can use that type instead. For my test projects, where I may have a mix of effect types on each model, I often use something like this:

foreach( Effect effect in mesh.Effects )
{
if( effect is BasicEffect )
{
...basic effect stuff...
}
else if( effect is MyCustomEffect )
{
....my custom stuff...
}
}

Hope that helps!





Re: XNA Game Studio Express How to apply an effect to a mesh rendering?

Kyle_W

No you have to change all of that code because the parameters inside the loop only pertain to BasicEffect. When you are using an Effect, you assign an effect to each mesh part as previously described, then you must set the Effect.CurrentTechnique property to the technique within the effect that you want to use, and you have to set all of the global variables of each effect to appropriate values from within your code. For example:

MyModel.Meshes[0].MeshParts[0].Effect.Parameters["WorldViewProj"].SetValue(WorldViewProjection);

The above line of code would set the HLSL global variable called WorldViewProj to the value of the variable WorldViewProjection within your XNA game. After setting all of the appropriate effect variables, you would simply call MyModel.Meshes[0].Draw() iterating through each mesh in the Meshes collection and it will draw all the ModelMeshPart objects on each mesh using their associated effect settings. If the active effect technique contains multiple passes, my understanding is that the Draw() call will iterate through them automatically.