Dan Byström
Dan Byström

Reputation: 9244

XNA effects and models

Cross-Post: https://gamedev.stackexchange.com/questions/22526/xna-effects-and-models-question/22530#22530

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

Upvotes: 1

Views: 809

Answers (1)

annonymously
annonymously

Reputation: 4708

You don't have to create the whole effect again, just modify the properties you need to on the original Effect property of the ModelMeshPart.

So instead of:

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

Do this:

foreach (var mesh in _modelHead.Meshes)
    foreach (var part in mesh.MeshParts)
        ((BasicEffect)part.Effect).EnableDefaultLighting();

This will preserve the specular and diffuse color which have been lost by using the first method, and save on code space.

Also, because each ModelMeshPart can have its own material this makes sure you don't lose any of the data you want to keep.

Upvotes: 1

Related Questions