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: Create and Apply Custom Effects

Demonstrates how to create a simple effect to set the diffuse color and world, view, and projection transformation of a 3D object.

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.

Applying an Effect

Bb203872.note(en-us,XNAGameStudio.31).gifNote
The steps described here apply to effects created with the Effect class. In contrast, the BasicEffect class is provided for users who want basic lighting, materials, texture, and transformation functionality, but do not want to write custom effects. For more information, see How To: Use BasicEffect.

To apply an effect

  1. Compose the effect description using either shader assembly language (ASM) or high-level shader language (HLSL).

    In this example, the effect file contains a vertex shader named Transform and a technique named TransformTechnique, which applies the vertex shader in a single pass.

    uniform extern float4x4 WorldViewProj : WORLDVIEWPROJECTION;
    
    struct VS_OUTPUT
    {
        float4 position : POSITION;
        float4 color : COLOR0;
    };
    
    VS_OUTPUT Transform(
        float4 Pos  : POSITION, 
        float4 Color : COLOR0 )
    {
        VS_OUTPUT Out = (VS_OUTPUT)0;
    
        Out.position = mul(Pos, WorldViewProj);
        Out.color = Color;
    
        return Out;
    }
    
    float4 PixelShader( VS_OUTPUT vsout ) : COLOR
    {
        return vsout.color;
    }
    
    technique TransformTechnique
    {
        pass P0
        {
            vertexShader = compile vs_2_0 Transform();
            pixelShader = compile ps_1_1 PixelShader();
        }
    }
    
  2. Add the effect file to the Content directory of your XNA Framework game.

  3. Right-click your Content node, point to Add, and then click Existing Item.

  4. In the Files of type: drop-down list, click All Files.

  5. Navigate to your .fx file, and click Add.

    Bb203872.note(en-us,XNAGameStudio.31).gifTip
    To create a new .fx file, point to Add, click New Item, and then select the Text File template. For the name of the file, use the .fx file extension.
  6. Create a new Effect by using the ContentManager.Load<Effect> method to load the asset.

    effect = Content.Load<Effect>("ReallySimpleEffect");
    

    In the effect file, there is a parameter named WorldViewProj, which is a matrix used by the vertex shader. The following code looks up the WorldViewProj parameter in Effect.Parameters, and calls EffectParameter.SetValue to initialize the value of the transform matrix.

    effect.Parameters["WorldViewProj"].SetValue(worldViewProjection);
    
  7. Set Effect.CurrentTechnique to the technique from the effect file you wish to apply.

    In this case, use Effect.Techniques to look up the technique named TransformTechnique from the effect file.

    effect.CurrentTechnique = effect.Techniques["TransformTechnique"];
    
  8. Call the Effect.Begin to begin applying the technique.

  9. For each pass in the technique, draw the desired geometry between calls to EffectPass.Begin and EffectPass.End.

  10. Call Effect.End to stop applying the technique.

    // The effect is a compiled effect created and compiled elsewhere
    // in the application.
    
    effect.Begin();
    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        pass.Begin();
    
        GraphicsDevice.DrawIndexedPrimitives(
            PrimitiveType.TriangleList,
            0,
            0,
            cubeVertices.Length,
            0,
            12);
    
        pass.End();
    }
    effect.End();
    

Community Additions

ADD
Show:
© 2015 Microsoft