When the player loses all of their lives, I want the entire game screen to go grayscale, but not stop updating immediately. I'd also prefer it fade to grayscale instead of suddenly lose all color. Everything I've found so far is either about taking a screenshot and making it grayscale, or making a specific texture grayscale. Is there a way to change the entire playing field and all objects within to grayscale without iterating through everything?
Answer
The easiest way to do this is to create a shader for it (See code below). Draw everything to a render target, then use the shader to draw that to your back buffer
In your game code record when the player dies, then in subsequent renders interpolate a shader parameter between 1 (full colour) and 0 (full grey-scale) according toCurrentTime-DeadTime / FadeTime
float ColourAmount;
Texture coloredTexture;
sampler coloredTextureSampler = sampler_state
{
texture = ;
};
float4 GreyscalePixelShaderFunction(float2 textureCoordinate : TEXCOORD0) : COLOR0
{
float4 color = tex2D(coloredTextureSampler, textureCoordinate);
float3 colrgb = color.rgb;
float greycolor = dot(colrgb, float3(0.3, 0.59, 0.11));
colrgb.rgb = lerp(dot(greycolor, float3(0.3, 0.59, 0.11)), colrgb, ColourAmount);
return float4(colrgb.rgb, color.a);
}
technique Grayscale
{
pass GreyscalePass
{
PixelShader = compile ps_2_0 GreyscalePixelShaderFunction();
}
}
The reason for the 0.3, 0.59 and 0.11 is because the human eye doesn't treat colours equally, those values give a better greyscale image.
No comments:
Post a Comment