How To: Use EffectParameters and EffectTechniques

Demonstrates how to use EffectParameters objects to interact with effect variables, and to use EffectTechniques to access different techniques in an Effect.

In this example, a wrapper class is created to expose EffectParameters and EffectTechniques for the custom effect. The example shows you how to use the EffectParameters and EffectTechniques. This class is used in the How To: Implement Shadow Mapping sample.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download ShadowMapping_Sample.zip.

Using EffectParameters and EffectTechniques

To Use EffectParameters

  1. In your game's LoadContent method, load your Effect from the Content Manager.

  2. Create an EffectParameter instance for each parameter in your effect that will be accessed during Draw or Update.

    You can use the Parameters indexed property on Effect to access any effect parameter, but this is slower than using EffectParameters. For this reason, you should create an EffectParameter for each effect parameter that changes frequently.

    public EffectParameter mWorld;
    public EffectParameter mCameraView;
    public EffectParameter CameraPos;
    public EffectParameter mCameraProj;
    
  3. Use the Parameters indexed property on Effect to access the effect parameter, and then assign it to your EffectParameter.

    mWorld = effect.Parameters["g_mWorld"];
    mCameraView = effect.Parameters["g_mCameraView"];
    CameraPos = effect.Parameters["g_CameraPos"];
    mCameraProj = effect.Parameters["g_mCameraProj"];
    
  4. Call SetValue on EffectParameter whenever you want to set a new value for the effect parameter.

    You must call SetValue before calling Begin on your Effect. If you change the value of an EffectParameter after calling Begin and before calling End, you must call CommitChanges for the new value to take effect.

    MyEffect.CameraPos.SetValue(CameraPos);
    MyEffect.mCameraView.SetValue(view);
    MyEffect.mCameraProj.SetValue(projection);
    MyEffect.LightPos.SetValue(LightPos);
    MyEffect.mLightView.SetValue(Matrix.CreateLookAt(LightPos, bounds.Center, Vector3.Up));
    

To Use EffectTechniques

  1. In your game's LoadContent method, load your Effect from the Content Manager.

  2. Create an EffectTechnique instance for each technique in your Effect that you will use during Update or Draw.

    You can use the Techniques indexed property on Effect to access any effect technique, but this is slower than using EffectTechniques. For this reason, you should create an EffectTechnique for each effect technique that you use.

    public EffectTechnique texture;
    public EffectTechnique shadows;
    public EffectTechnique shadowMap;
    
  3. Use the Techniques indexed property on Effect to access the effect technique, and then assign it to your EffectTechnique.

    texture = effect.Techniques["TextureRender"];
    shadowMap = effect.Techniques["ShadowMapRender"];
    shadows = effect.Techniques["ShadowRender"];
    
  4. Set the CurrentTechnique property on your Effect to the technique you wish to use when drawing.

    You can use your Effect multiple times per Draw with different techniques.

    private void DrawScene(EffectTechnique technique)
    {
        MyEffect.mWorld.SetValue(terrainWorld);
        MyEffect.MeshTexture.SetValue(terrainTex);
        foreach (ModelMesh mesh in terrain.Meshes)
        {
            foreach (Effect effect in mesh.Effects)
            {
                effect.CurrentTechnique = technique;
                mesh.Draw();
            }
        }
    }
    

See Also

Concepts

3D Graphics Overview

Tasks

How To: Render a Model

Reference

Effect
CurrentTechnique
Techniques
Parameters
EffectTechnique
EffectParameter