1
\$\begingroup\$

I'd like to understand why I get a completely different result when I replace the effects of a model (as suggested by How To: Draw a Model with a Custom Effect) with a BasicEffect that I instantiate myself:

var basicEffect = new BasicEffect( game.GraphicsDevice );
basicEffect.EnableDefaultLighting();
foreach (var mesh in _modelHead.Meshes)
    foreach (var part in mesh.MeshParts)
        part.Effect = basicEffect;

My goal is of course to use an effect of my own later, but for the moment I'd really like to understand what's going on!? What is different with the predefined BasicEffect from the one I instantiate myself? When I inspect the properties of the two instances they seem to be all the same.

enter image description here

  1. How it looks with the "default" BasicEffect
  2. How it looks when I instantiate a new BasicEffect
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The model file contains data about materials, in this case it looks like the diffuse color or texture. When you create a new BasicEffect object it doesn't contain the right colors and such, so you get white.

See here:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.basiceffect.diffusecolor.aspx

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.basiceffect.texture.aspx

If you want to use the same texture, you can do this (I haven't tested though):

foreach (var mesh in _modelHead.Meshes)
{
    foreach (var part in mesh.MeshParts)
    {
        var specificEffect = new BasicEffect(game.GraphicsDevice);
        specificEffect.EnableDefaultLighting();
        specificEffect.Texture = ((BasicEffect)part.Effect).Texture;
        specificEffect.DiffuseColor = ((BasicEffect)part.Effect).DiffuseColor;
        part.Effect = specificEffect;
    }
}

Obviously this code can be optimized a lot as it becomes redundant after the first call.

EDIT:

Just to explain why the properties seem the same, once your code runs once the effects become the same, so if you're seeing anything but the first call the properties will be the same.

\$\endgroup\$
2
  • \$\begingroup\$ Ah, this makes sense, I guess. I had just arrived to the conclusion that it must have something to do with materials myself (there are no texture in the models yet). So, then I of course wonder why the first BasicEffect works??? Is something happening in the ContentPipeline that sets the DiffuseColor to the color of the material? \$\endgroup\$
    – danbystrom
    Jan 17, 2012 at 13:19
  • \$\begingroup\$ Basically, yes. The model's materials are stored in the file, so that all the components of the material are brought over. Some of the parts though, (like Self Illumination) behave differently in XNA than they do when rendered with, say, 3ds Max. \$\endgroup\$ Jan 17, 2012 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .