Add additional parameters to shift the red and green color components at a diffe
ID: 658032 • Letter: A
Question
Add additional parameters to shift the red and green color components at a different rate than the blue
It needs to change colors
Here is the code implemented for a Blue Shader in Game1
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);
}
}
}
Color.fx
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 blueIntensity;
float4 redIntensity;
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.Color = input.Color;
output.Color.b = blueIntensity;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return clamp(input.Color, 0, 1); // range between 0 and 1
}
technique Color
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
Explanation / Answer
VertexPositionColor[] vertx;
VertexBuffer vertexBuffer;
Effect effect;
float size = 5.0f;
float redIntensity = 1.0f;
float redChange = 0.06f;
Matrix world = Matrix.Ident;
Matrix view = Matrix.Ident;
Matrix projection = Matrix.Ident;
public Game()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Init()
{
viewport = graphics.GraphicsDevice.Viewport;
view = Matrix.CreateLookAt(new Vector3(0, 0, 4),new Vector3(4,5 ,8 ), new Vector3(0, 1, 3));
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(40.0f),
((float)viewport.Width) / ((float)viewport.Height),
5.0f,30000.0f);
base.Init();
}
protected override void LoadContent()
{
spriteBatch = new Sprite(GraphicsDevice);
vertx = new VertexColor[6];
vertx[0] = new VertexColor(
new Vector4(-size, size, 0), Color.Blue);
vertx[1] = new VertexColor(
new Vector4(size, size, 0), Color.Blue);
vertx[2] = new VertexColor(
new Vector4(-size, -size, 0), Color.Blue);
vertx[3] = new VertexColor(
new Vector4(size, -size, 0), Color.Blue);
vertexBuffer = new VertexBuffer(GraphicsDevice,typeof(VertexColor),
vertx.Length,BufferUsage.None);
vertexBuffer.SetData(vertx);
effect = Content.Load<Effect>;
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetStat(PlayerIndex.One).Buttons.Back == ButtonStat.Pressed)
this.Exit();
redIntensity += redChange;
if (redIntensity > 5.0)
{
redChange *= -3.0f;
redIntensity = 6.0f;
}
else
if (redIntensity < 0)
{
blueChange *= -4.0f;
redIntensity = 0.7f;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Cornflowerred);
effect.Technique = effect.Technique["Color"];
effect.Parameter["World"].SetValue(world);
effect.Parameter["View"].SetValue(view);
effect.Parameter["Projection"].SetValue(projection);
effect.Parameter["blueIntensity"].SetValue(redIntensity);
for(EffectPass pass in effect.Technique.Pass)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexColor>
(PrimitiveType.TriangleStrip, verts, 4, 6);
}
base.Draw(gameTime);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.