How to handle multiple effect files in XNA

Posted by Adam 'Pi' Burch on Game Development See other posts from Game Development or by Adam 'Pi' Burch
Published on 2012-03-07T00:48:23Z Indexed on 2012/04/09 11:50 UTC
Read the original article Hit count: 315

Filed under:
|

So I'm using ModelMesh and it's built in Effects parameter to draw a mesh with some shaders I'm playing with. I have a simple GUI that lets me change these parameters to my heart's desire. My question is, how do I handle shaders that have unique parameters? For example, I want a 'shiny' parameter that affects shaders with Phong-type specular components, but for an environment mapping shader such a parameter doesn't make a lot of sense. How I have it right now is that every time I call the ModelMesh's Draw() function, I set all the Effect parameters as so

foreach (ModelMesh m in model.Meshes)
        {

            if (isDrawBunny == true)//Slightly change the way the world matrix is calculated if using the bunny object, since it is not quite centered in object space
            {
                world = boneTransforms[m.ParentBone.Index] * Matrix.CreateScale(scale) * rotation * Matrix.CreateTranslation(position + bunnyPositionTransform);
            }
            else //If not rendering the bunny, draw normally
            {
                world = boneTransforms[m.ParentBone.Index] * Matrix.CreateScale(scale) * rotation * Matrix.CreateTranslation(position);
            }

            foreach (Effect e in m.Effects)
            {
                Matrix ViewProjection = camera.ViewMatrix * camera.ProjectionMatrix;
                e.Parameters["ViewProjection"].SetValue(ViewProjection);
                e.Parameters["World"].SetValue(world);
                e.Parameters["diffuseLightPosition"].SetValue(lightPositionW);
                e.Parameters["CameraPosition"].SetValue(camera.Position);
                e.Parameters["LightColor"].SetValue(lightColor);
                e.Parameters["MaterialColor"].SetValue(materialColor);
                e.Parameters["shininess"].SetValue(shininess);

                //e.Parameters
                //e.Parameters["normal"]

            }
            m.Draw();

Note the prescience of the example! The solutions I've thought of involve preloading all the shaders, and updating the unique parameters as needed. So my question is, is there a best practice I'm missing here? Is there a way to pull the parameters a given Effect needs from that Effect?

Thank you all for your time!

© Game Development or respective owner

Related posts about XNA

Related posts about shaders