/* Shader description: */ //--------------------------------------------------------------------------- // Types: #if defined(DIRECT3D) #define BONEMATRIX_TYPE float4x3 #else #define BONEMATRIX_TYPE float3x4 #endif //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Constant variables: //--------------------------------------------------------------------------- sampler2D Base; float3 g_AlphaTestFunction; float g_AlphaTestRef; //--------------------------------------------------------------------------- // Functions: //--------------------------------------------------------------------------- /* This fragment writes projected depth to all color component outputs. */ void WriteDepthToColor(float4 WorldPosProjected, float3 WorldViewVector, out float4 OutputColor) { float Depth = WorldPosProjected.z / WorldPosProjected.w; OutputColor.x = Depth; OutputColor.yzw = 1.0f; } //--------------------------------------------------------------------------- /* This fragment is responsible for sampling a texture and returning its value as a RGB value and an A value. */ void TextureRGBASample(float2 TexCoord, sampler2D Sampler, bool Saturate, out float4 ColorOut) { ColorOut = tex2D(Sampler, TexCoord); if (Saturate) { ColorOut = saturate(ColorOut); } } //--------------------------------------------------------------------------- /* Separate a float4 into a float3 and a float. */ void SplitColorAndOpacity(float4 ColorAndOpacity, out float3 Color, out float Opacity) { Color.rgb = ColorAndOpacity.rgb; Opacity = ColorAndOpacity.a; } //--------------------------------------------------------------------------- /* This fragment is responsible for alpha testing based on the alpha reference value and alpha test function. */ void ApplyAlphaTest(float AlphaTestValue, float3 AlphaTestFunction, float AlphaTestRef) { // These tests will evaluate to less than 0 if they are true. float fLessTest = (AlphaTestValue - AlphaTestRef); float fGreaterTest = -fLessTest; float fEqualTest = 0.0; if (fLessTest == 0.0) fEqualTest = -1.0; // Clip if AlphaTestValue < AlphaTestRef for: // TEST_LESS // TEST_EQUAL // TEST_LESSEQUAL clip(fGreaterTest * AlphaTestFunction.x); // Clip if AlphaTestValue > AlphaTestRef for: // TEST_EQUAL // TEST_GREATER // TEST_GREATEREQUAL clip(fLessTest * AlphaTestFunction.y); // Clip if AlphaTestValue == AlphaTestRef for: // TEST_LESS // TEST_GREATER // TEST_NOTEQUAL clip(fEqualTest * AlphaTestFunction.z); } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Input: //--------------------------------------------------------------------------- struct Input { float4 PosProjected : POSITION0; float4 WorldPosProjected : TEXCOORD0; float3 WorldViewVector : TEXCOORD1; float2 UVSet0 : TEXCOORD2; }; //--------------------------------------------------------------------------- // Output: //--------------------------------------------------------------------------- struct Output { float4 Color : COLOR0; }; //--------------------------------------------------------------------------- // Main(): //--------------------------------------------------------------------------- Output Main(Input In) { Output Out; // Function call #0 WriteDepthToColor(In.WorldPosProjected, In.WorldViewVector, Out.Color); // Function call #1 float4 ColorOut_CallOut1; TextureRGBASample(In.UVSet0, Base, bool(false), ColorOut_CallOut1); // Function call #2 float3 Color_CallOut2; float Opacity_CallOut2; SplitColorAndOpacity(ColorOut_CallOut1, Color_CallOut2, Opacity_CallOut2); // Function call #3 ApplyAlphaTest(Opacity_CallOut2, g_AlphaTestFunction, g_AlphaTestRef); return Out; }