Click to Rate and Give Feedback
MSDN
MSDN Library
XNA Game Studio
XNA Game Studio 2.0
Programming Guide
Graphics
3D Graphics
"How To" Articles
 How To: Create and Apply Custom Eff...
XNA Game Studio 2.0
How To: Create and Apply Custom Effects
This example 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 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.

Note
The steps described here apply to effects created with the Effect class. In contrast, the BasicEffect class is provided for users who desire basic lighting, materials, texture, and transformation functionality but do not wish 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.

    Effect File (.fx)
    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. Right-click your Content node and point to Add, and then click Existing Item. In the Files of type: drop-down list, click All Files. Navigate to your .fx file and click Add.

    Tip
    To create a new .fx file, point to Add, and then New Item, and then choose the Text File template. For the name of the file, use the .fx file extension.
  3. Create a new Effect by using the ContentManager.Load<Effect> method to load the asset.

    C#
    effect = Content.Load<Effect>("ReallySimpleEffect");
    
  4. 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.

    C#
    effect.Parameters["WorldViewProj"].SetValue(worldViewProjection);
    
  5. Set Effect.CurrentTechnique to the technique from the effect file you wish to apply. In this case, we use Effect.Techniques to look up the technique named TransformTechnique from the effect file.

    C#
    effect.CurrentTechnique = effect.Techniques["TransformTechnique"];
    
  6. Call the Effect.Begin to begin applying the technique. For each pass in the technique, draw the desired geometry between calls to EffectPass.Begin and EffectPass.End. Call Effect.End to stop applying the technique.

    C#
    // The effect is a compiled effect created and compiled elsewhere
    // in the application.
    
    effect.Begin();
    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        pass.Begin();
    
        graphics.GraphicsDevice.DrawIndexedPrimitives(
            PrimitiveType.TriangleList,
            0,
            0,
            cubeVertices.Length,
            0,
            12);
    
        pass.End();
    }
    effect.End();
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker