You may sometimes need to indicate something important to your players within your game world. In a 3D game world you can use a flashing indicator to attract the attention of the player. The purpose of the indicator may be to direct the player towards something, or to reveal danger.
This animated colour shader animates a given object or limb by flashing or transitioning its hue from one setting to another. Giving the appearance of a flashing object.
The shader considers the alpha channel of your colour settings, therefore with the correct transparency setting in your program, you can have the object disappear and reappear in the sequence.
The shader constants are self explanitory. Color1 and Color2 are what you need to set to determine the hue; and the AnimationSpeed constant controls the animation speed.
+ Code Snippetstring Description = "This shader will cause its assigned object or limb to animate from one colour to another. Regards Chris Tate";
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 WorldView : WorldView;
float4 eyePos : CameraPosition;
// Get the current time for the
float Time : TIME;
// Obtain the required brightness of the highlightes
float Brightness
<
string UIWidget = "slider";
float UIMax = 1.0;
float UIMin = 0.0;
float UIStep = 0.01;
> = 0.333;
float AnimationSpeed
<
string UIWidget = "slider";
float UIMax = 10.0;
float UIMin = 0.0;
float UIStep = 0.1;
> = 1.0;
// Main interpolated colours
float4 Color1 = {0.8,0.2,0.2,1.0};
float4 Color2 = {0.2,0.8,0.2,1.0};
// Set the ambience which has an affect on the overall brightness of the object and its surrounding environment colour
float3 Ambience = {0.2, 0.2, 0.2} ;
struct app_in
{
float4 pos : POSITION;
float3 normal : NORMAL;
};
struct vs_out
{
float4 pos : POSITION;
float angle : TEXCOORD0;
};
vs_out DefaultVertexShader( app_in IN )
{
vs_out OUT;
OUT.pos = mul( IN.pos, WorldViewProj );
float4 wPos = mul( IN.pos, World );
float3 wNormal = mul( IN.normal, (float3x3)World );
float angle = saturate( dot( normalize(wNormal), normalize(eyePos.xyz - wPos.xyz) ) );
OUT.angle = angle / 1.5;
return OUT;
}
float4 PixelAnimator( vs_out IN ) : COLOR
{
// Get shading
float shading = saturate(IN.angle) + Brightness;
// Get ambience
float4 color =float4(Ambience, 0.0);
// Add animated colour
color += lerp(Color1*shading, Color2*shading, sin( Time * (AnimationSpeed*2) ));
return color;
}
technique Normal
{
pass p0
{
ALPHABLENDENABLE = TRUE;
VertexShader = compile vs_2_0 DefaultVertexShader( );
PixelShader = compile ps_2_0 PixelAnimator( );
}
}