Add additional parameters to shift the red and green color components at a diffe
ID: 657911 • Letter: A
Question
Add additional parameters to shift the red and green color components at a different rate than the blue
Here is the code implemented for a Blue Shader:
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;
namespace MyFirstShader
{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Viewport viewport;
// Vertex data
VertexPositionColor[] verts;
VertexBuffer vertexBuffer;
// Effect (Shader object)
Effect effect;
float size = 2.0f;
float blueIntensity = 1.0f;
float blueChange = 0.01f;
// Movement and rotation stuff
Matrix world = Matrix.Identity;
Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
protected override void Initialize()
{
// TODO: Add your initialization logic here
viewport = graphics.GraphicsDevice.Viewport;
view = Matrix.CreateLookAt(new Vector3(0, 0, 10),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0));
projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(40.0f),
((float)viewport.Width) / ((float)viewport.Height),
1.0f,
10000.0f);
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Initialize vertices
verts = new VertexPositionColor[4];
verts[0] = new VertexPositionColor(
new Vector3(-size, size, 0), Color.White);
verts[1] = new VertexPositionColor(
new Vector3(size, size, 0), Color.White);
verts[2] = new VertexPositionColor(
new Vector3(-size, -size, 0), Color.White);
verts[3] = new VertexPositionColor(
new Vector3(size, -size, 0), Color.White);
// Set vertex data in VertexBuffer
vertexBuffer = new VertexBuffer(GraphicsDevice,
typeof(VertexPositionColor),
verts.Length,
BufferUsage.None);
vertexBuffer.SetData(verts);
// Load the effect
effect = Content.Load(@"Shaderscolor");
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
///Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
blueIntensity += blueChange;
if (blueIntensity > 1.0)
{
blueChange *= -1.0f;
blueIntensity = 1.0f;
}
else if (blueIntensity < 0)
{
blueChange *= -1.0f;
blueIntensity = 0.0f;
}
}
///
/// This is called when the game should draw itself.
///
///Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Set the vertex buffer on the GraphicsDevice
GraphicsDevice.SetVertexBuffer(vertexBuffer);
effect.CurrentTechnique = effect.Techniques["Color"];
effect.Parameters["World"].SetValue(world);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["blueIntensity"].SetValue(blueIntensity);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives
(PrimitiveType.TriangleStrip, verts, 0, 2);
}
base.Draw(gameTime);
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft..Framework;
using Microsoft.Framework.Audio;
using Microsoft.Framework.Content;
using Microsoft.Framework.GamerServices;
using Microsoft.Framework.Graphics;
using Microsoft.Framework.Input;
using Microsoft.Framework.Media;
namespace MyFirstShader
{
public class Game1
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Viewport viewport;
VertexPositionColor[] verts;
VertexBuffer vertexBuffer;
Effect effect;
float size = 2.0f;
float redIntensity = 1.0f;
float greenChange = 0.01f;
Matrix world = Matrix.Identity;
Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
viewport = graphics.GraphicsDevice.Viewport;
view = Matrix.CreateLookAt(new Vector3(0, 0, 10),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0));
projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(40.0f),
((float)viewport.Width) / ((float)viewport.Height),
1.0f,
10000.0f);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
verts = new VertexPositionColor[4];
verts[0] = new VertexPositionColor(
new Vector3(-size, size, 0), Color.White);
verts[1] = new VertexPositionColor(
new Vector3(size, size, 0), Color.White);
verts[2] = new VertexPositionColor(
new Vector3(-size, -size, 0), Color.White);
verts[3] = new VertexPositionColor(
new Vector3(size, -size, 0), Color.White);
vertexBuffer = new VertexBuffer(GraphicsDevice,
typeof(VertexPositionColor),
verts.Length,
BufferUsage.None);
vertexBuffer.SetData(verts);
effect = Content.Load(@"Shaderscolor");
}
protected override void UnloadContent()
{ }
protected override void Update(GameTime gameTime)
{ if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
redIntensity += greenChange;
if (redIntensity > 1.0)
{
greenChange *= -1.0f;
redIntensity = 1.0f;
}
else if (redIntensity < 0)
{
greenChange *= -1.0f;
redIntensity = 0.0f;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Cornflowerred);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
effect.CurrentTechnique = effect.Techniques["Color"];
effect.Parameters["World"].SetValue(world);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["blueIntensity"].SetValue(redIntensity);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives
(PrimitiveType.TriangleStrip, verts, 0, 2);
}
base.Draw(gameTime);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.