UltimateZoneBuilder/Source/Resources/display2d.fx

140 lines
3.2 KiB
HLSL
Raw Normal View History

// 2D display rendering shader
2007-10-19 17:05:21 +00:00
// Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
2008-05-18 11:43:28 +00:00
// Vertex input data
struct VertexData
{
float3 pos : POSITION;
float4 color : COLOR0;
float2 uv : TEXCOORD0;
};
2007-10-19 17:05:21 +00:00
// Pixel input data
struct PixelData
{
float4 pos : POSITION;
float4 color : COLOR0;
2007-10-19 17:05:21 +00:00
float2 uv : TEXCOORD0;
};
2008-05-18 11:43:28 +00:00
// Render settings
2007-10-19 17:05:21 +00:00
// x = texel width
// y = texel height
// z = FSAA blend factor
// w = transparency
2008-05-18 11:43:28 +00:00
float4 rendersettings;
// Transform settings
float4x4 transformsettings;
2007-10-19 17:05:21 +00:00
// Texture1 input
texture texture1
<
string UIName = "Texture1";
string ResourceType = "2D";
>;
// Texture sampler settings
sampler2D texture1samp = sampler_state
{
Texture = <texture1>;
2007-10-20 12:34:27 +00:00
MagFilter = Point;
MinFilter = Point;
MipFilter = Point;
2007-10-19 17:05:21 +00:00
AddressU = Wrap;
AddressV = Wrap;
2008-05-23 09:54:51 +00:00
MipMapLodBias = 0f;
};
2007-10-19 17:05:21 +00:00
2008-05-18 11:43:28 +00:00
// Texture sampler settings
sampler2D texture1linear = sampler_state
{
Texture = <texture1>;
MagFilter = Linear;
MinFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
2008-05-23 09:54:51 +00:00
MipMapLodBias = 0f;
};
2008-05-18 11:43:28 +00:00
// Transformation
PixelData vs_transform(VertexData vd)
{
PixelData pd = (PixelData)0;
pd.pos = mul(float4(vd.pos, 1.0f), transformsettings);
pd.color = vd.color;
pd.uv = vd.uv;
return pd;
}
2007-10-19 17:05:21 +00:00
// This blends the max of 2 pixels
float4 addcolor(float4 c1, float4 c2)
{
return float4(max(c1.r, c2.r),
max(c1.g, c2.g),
max(c1.b, c2.b),
2007-10-20 12:34:27 +00:00
saturate(c1.a + c2.a * 0.5f));
2007-10-19 17:05:21 +00:00
}
2008-05-18 11:43:28 +00:00
// Pixel shader for antialiased drawing
2007-11-11 13:51:01 +00:00
float4 ps_fsaa(PixelData pd) : COLOR
2007-10-19 17:05:21 +00:00
{
2007-10-20 12:34:27 +00:00
// Take this pixel's color
2007-10-19 17:05:21 +00:00
float4 c = tex2D(texture1samp, pd.uv);
2007-10-20 12:34:27 +00:00
// If this pixel is not drawn on...
if(c.a < 0.1f)
{
// Mix the colors of nearby pixels
float4 n = (float4)0;
2008-05-18 11:43:28 +00:00
n = addcolor(n, tex2D(texture1samp, float2(pd.uv.x + rendersettings.x, pd.uv.y)));
n = addcolor(n, tex2D(texture1samp, float2(pd.uv.x - rendersettings.x, pd.uv.y)));
n = addcolor(n, tex2D(texture1samp, float2(pd.uv.x, pd.uv.y + rendersettings.y)));
n = addcolor(n, tex2D(texture1samp, float2(pd.uv.x, pd.uv.y - rendersettings.y)));
2007-10-20 12:34:27 +00:00
// If any pixels nearby where found, return a blend, otherwise return nothing
//if(n.a > 0.1f) return float4(n.rgb, n.a * settings.z); else return (float4)0;
2008-05-18 11:43:28 +00:00
return float4(n.rgb, n.a * rendersettings.z * rendersettings.w);
2007-10-20 12:34:27 +00:00
}
2008-05-18 11:43:28 +00:00
else return float4(c.rgb, c.a * rendersettings.w) * pd.color;
2007-10-19 17:05:21 +00:00
}
2008-05-18 11:43:28 +00:00
// Pixel shader for normal drawing
2007-11-11 13:51:01 +00:00
float4 ps_normal(PixelData pd) : COLOR
{
// Take this pixel's color
float4 c = tex2D(texture1samp, pd.uv);
2008-05-18 11:43:28 +00:00
return float4(c.rgb, c.a * rendersettings.w) * pd.color;
}
// Pixel shader for text
float4 ps_text(PixelData pd) : COLOR
{
// Take this pixel's color
float4 c = tex2D(texture1linear, pd.uv);
return float4(c.rgb, c.a * rendersettings.w) * pd.color;
2007-11-11 13:51:01 +00:00
}
2007-10-19 17:05:21 +00:00
// Technique for shader model 2.0
technique SM20
{
pass p0
{
2008-05-18 11:43:28 +00:00
VertexShader = compile vs_2_0 vs_transform();
2007-11-11 13:51:01 +00:00
PixelShader = compile ps_2_0 ps_fsaa();
}
pass p1
{
2008-05-18 11:43:28 +00:00
VertexShader = compile vs_2_0 vs_transform();
2007-11-11 13:51:01 +00:00
PixelShader = compile ps_2_0 ps_normal();
2007-10-19 17:05:21 +00:00
}
2008-05-18 11:43:28 +00:00
pass p2
{
VertexShader = compile vs_2_0 vs_transform();
PixelShader = compile ps_2_0 ps_text();
}
2007-10-19 17:05:21 +00:00
}