Applying HLSL Using Textures

Coloring a rectangle red requires only the simplest HLSL shader, and it's something you'll rarely find in the latest video games. Typically, you'll be applying a texture to an object and then tweaking the way the texture appears by applying shininess or fog or some other effect.

In this section, you'll apply a custom HLSL file to your rectangle while applying color from the trees texture as well.

Open your Red.fx file again and replace the code in the file with the code shown here:

float4x4 xWorldViewProjection;

Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state
{ texture = <xColoredTexture> ;
magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR;
AddressU = mirror; AddressV = mirror;};

struct VertexIn
{
    float4 position : POSITION;
    float2 textureCoordinates : TEXCOORD0;
};

struct VertexOut
{
    float4 Position : POSITION;
    float2 textureCoordinates : TEXCOORD0;
};

VertexOut VertexShader(VertexIn input)
{
    VertexOut Output = (VertexOut)0;
    Output.Position =mul(input.position, xWorldViewProjection);
    Output.textureCoordinates = input.textureCoordinates;

    return Output;
}

float4 PixelShader(VertexOut input) : COLOR0
{
    float4 output = tex2D(ColoredTextureSampler,
        input.textureCoordinates);
    return output;
}

technique Textured
{
    pass Pass0
    {
        VertexShader = compile vs_1_1 VertexShader(  );
        PixelShader = compile ps_1_1 PixelShader(  );
    }
}

While you're still new to HLSL, this file isn't incredibly complex, and for the most part it should make sense ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.