Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How To: Use EffectParameters and EffectTechniques

This example demonstrates how to use EffectParameters objects to interface with effect variables, and use EffectTechniques to access different techniques in an Effect.

In this example, a wrapper class is created for a custom effect that exposes EffectParameters and EffectTechniques for the custom effect. We also examine how those EffectParameters and EffectTechniques can be used. This class is used in the How To: Implement Shadow Mapping sample.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

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. Any effect parameter can be accessed using the Parameters indexed property on Effect, 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. Access the effect parameter using the Parameters indexed property on Effect, and 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. Any effect technique can be accessed using the Techniques indexed property on Effect, 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. Access the effect technique using the Techniques indexed property on Effect, and 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();
            }
        }
    }
    

Community Additions

ADD
Show:
© 2015 Microsoft