Click or drag to resize
DigitalRuneHow To: Change Effect Parameter Bindings

Materials contain effect parameter bindings. These bindings automatically resolve and apply effect parameters at runtime. This topic explains how to change effect parameter bindings at runtime.

This topic contains the following sections:

Accessing effect parameter bindings

The effect parameters are grouped into categories.

  • Global Parameters: These effect parameters are identical for all meshes that use the same effect and effect technique. They do not depend on the object that is being rendered or on the location of the object in the scene. Examples of global parameters are: view matrix, projection matrix, camera position, etc.

  • Material Parameters: These effect parameters define the material of a mesh. Multiple meshes can share the same material. Material parameters are independent of the location of the object in the scene. Examples of material parameters are: diffuse color, albedo texture, specular color, gloss texture, normal map, etc.

  • Local Parameters: These effect parameters depend on the location of the mesh in the scene. Multiple meshes which are close to each other in the scene may share the same parameter values. Examples of local parameters are: local environment maps, local lights, etc.

  • Per-Instance Parameters: These effect parameters can be different for each mesh instance. Examples of instance parameters are: world matrix, skinning matrices.

  • Per-Pass Parameters: These effect parameters can be different in each effect pass. Examples of per-pass parameters are: pass index.

The following code snippets show how to access the effect parameter bindings for a certain category of effect parameters:

At run-time the bindings for global effect parameters are stored per Effect and can be accessed using the helper method GetParameterBindings.

Example (C#): Get bindings for global parameters
// If the Effect is given:
var parameterBindings = effect.GetParameterBindings();

// If the Mesh is given:
var effectBinding = mesh.Materials[materialIndex]["name of render pass"];
var parameterBindings = effectBinding.Effect.GetParameterBindings();

// If the MeshNode is given:
var effectBinding = meshNode.MaterialInstances[materialIndex]["name of render pass"];
var parameterBindings = effectBinding.Effect.GetParameterBindings();

Bindings for material parameters are stored per Material.

Example (C#): Get bindings for material parameters
// If the Mesh is given:
var effectBinding = mesh.Materials[materialIndex]["name of render pass"];
var parameterBindings = effectBinding.ParameterBindings;

// If the MeshNode is given:
var effectBinding = meshNode.MaterialInstances[materialIndex].Material["name of render pass"];
var parameterBindings = effectBinding.ParameterBindings;

Bindings for local, per-instance, and per-pass parameters are stored per MaterialInstance.

Example (C#): Get bindings for local, per-instance and per-pass parameters
var effectBinding = meshNode.MaterialInstances[index]["name of render pass"];
var parameterBindings = effectBinding.ParameterBindings;
Changing effect parameters

Effect parameters can be changed using the EffectBindingSet methods. The following example shows how to change material parameters.

Example (C#): Changing material parameters
// The mesh in this example has a single material which uses the XNA BasicEffect.
var effectBinding = mesh.Materials[0]["Default"];
effectBinding.Set("DiffuseColor", new Vector3(1, 0, 0));
effectBinding.Set("SpecularColor", new Vector3(1, 1, 0));
effectBinding.Set("SpecularPower", 100f);
effectBinding.Set("Texture", diffuseTexture);
See Also