红色,绿色,蓝色着色器XNA

时间:2015-05-05 08:26:05

标签: c# xna

添加其他参数,以不同的速率移动红色和绿色分量(因此您的方块应循环显示多种颜色)。

以下是已为蓝色着色器实现的代码:

// 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";
}

/// <summary>
/// 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.
/// </summary>
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();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
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<Effect>(@"Shaders\color");


}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
    // TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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;
    }
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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<VertexPositionColor>
            (PrimitiveType.TriangleStrip, verts, 0, 2);
    }

    base.Draw(gameTime);
}

着色

float4x4 World;
float4x4 View;
float4x4 Projection;

float4 blueIntensity;

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();
    }
}

0 个答案:

没有答案