XNA – Using Effect

Posted: July 19, 2009 in C#, Game Programming, Programming, XNA
Tags: , ,

xna_logo

The XNA Content Pipeline is pretty good. It implifies the process for importing and loading models, textures, and other content. Unfortunately, it is optimized for loading data, not code. Effect files are essentially code, so an alternative approach could provide additional benefits.

Like the XNA Content Pipeline, Effect Generator compiles your .fx files at build time, reducing game load times. Additionally, Effect Generator further simplifies run-time loading and effect parameterization.

Place your effect file in Content folder :

Untitled


We start by declaring an Effect:

Effect effect;


We’ll use the Content pipeline to load an instance of the Effect class:

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


And render using the effect:

effect.CurrentTechnique = effect.Techniques["Pretransformed"];
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
    pass.Begin();

    pass.End();
}
effect.End();


The complete code :

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace EffectsExample
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GraphicsDevice device;
        Effect effect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Effect";

            base.Initialize();
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(GraphicsDevice);

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

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            device.Clear(Color.DarkSlateBlue);

            effect.CurrentTechnique = effect.Techniques["Pretransformed"];
            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                pass.End();
            }
            effect.End();

            base.Draw(gameTime);
        }
    }
}



Other source :
http://msdn.microsoft.com/en-us/library/bb203872.aspx
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/The_effect_file.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s