在XNA 4.0 WinForms中使用Nvidia FXAA(着色器抗锯齿)的示例,可以通过以下步骤实现:
float4x4 World;
float4x4 View;
float4x4 Projection;
texture2D Texture;
sampler2D TextureSampler = sampler_state
{
Texture =<Texture>;
AddressU = CLAMP;
AddressV = CLAMP;
Filter = MIN_MAG_MIP_LINEAR;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 TextureCoordinate : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TextureCoordinate : TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = mul(input.Position, World);
output.Position = mul(output.Position, View);
output.Position = mul(output.Position, Projection);
output.TextureCoordinate = input.TextureCoordinate;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float2 texelSize = float2(1.0f / 1280.0f, 1.0f / 720.0f);
float3 color = float3(0.0f, 0.0f, 0.0f);
float3 luma = float3(0.299f, 0.587f, 0.114f);
float3 nw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, -1.0f) * texelSize).rgb;
float3 n = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, -1.0f) * texelSize).rgb;
float3 ne = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, -1.0f) * texelSize).rgb;
float3 w = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 0.0f) * texelSize).rgb;
float3 m = tex2D(TextureSampler, input.TextureCoordinate).rgb;
float3 e = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 0.0f) * texelSize).rgb;
float3 sw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 1.0f) * texelSize).rgb;
float3 s = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, 1.0f) * texelSize).rgb;
float3 se = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 1.0f) * texelSize).rgb;
float lumaNw = dot(nw, luma);
float lumaN = dot(n, luma);
float lumaNe = dot(ne, luma);
float lumaW = dot(w, luma);
float lumaM = dot(m, luma);
float lumaE = dot(e, luma);
float lumaSw = dot(sw, luma);
float lumaS = dot(s, luma);
float lumaSe = dot(se, luma);
float lumaMin = min(lumaM, min(min(lumaNw, lumaN), min(lumaNe, min(lumaW, min(lumaE, min(lumaSw, min(lumaS, lumaSe)))))));
float lumaMax = max(lumaM, max(max(lumaNw, lumaN), max(lumaNe, max(lumaW, max(lumaE, max(lumaSw, max(lumaS, lumaSe)))))));
float lumaRange = lumaMax - lumaMin;
if (lumaRange < max(0.001f, 0.03f * (lumaMax + lumaMin)))
{
color = m;
}
else
{
float3 nwDir = normalize(nw - m);
float3 nDir = normalize(n - m);
float3 neDir = normalize(ne - m);
float3 wDir = normalize(w - m);
float3 eDir = normalize(e - m);
float3 swDir = normalize(sw - m);
float3 sDir = normalize(s - m);
float3 seDir = normalize(se - m);
float3 dir = nwDir + nDir + neDir + wDir + eDir + swDir + sDir + seDir;
float dirReduce = max((lumaRange * 0.5f) / (dot(dir, dir) + 0.001f), 1.0f);
nwDir *= dirReduce;
nDir *= dirReduce;
neDir *= dirReduce;
wDir *= dirReduce;
eDir *= dirReduce;
swDir *= dirReduce;
sDir *= dirReduce;
seDir *= dirReduce;
float3 sampleNw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, -1.0f) * texelSize * dirReduce).rgb;
float3 sampleN = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, -1.0f) * texelSize * dirReduce).rgb;
float3 sampleNe = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, -1.0f) * texelSize * dirReduce).rgb;
float3 sampleW = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 0.0f) * texelSize * dirReduce).rgb;
float3 sampleE = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 0.0f) * texelSize * dirReduce).rgb;
float3 sampleSw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 1.0f) * texelSize * dirReduce).rgb;
float3 sampleS = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, 1.0f) * texelSize * dirReduce).rgb;
float3 sampleSe = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 1.0f) * texelSize * dirReduce).rgb;
float3 colorNw = (nw - m) / lumaRange;
float3 colorN = (n - m) / lumaRange;
float3 colorNe = (ne - m) / lumaRange;
float3 colorW = (w - m) / lumaRange;
float3 colorE = (e - m) / lumaRange;
float3 colorSw = (sw - m) / lumaRange;
float3 colorS = (s - m) / lumaRange;
float3 colorSe = (se - m) / lumaRange;
float3 colorNwDir = normalize(colorNw - colorM);
float3 colorNDir = normalize(colorN - colorM);
float3 colorNedDir = normalize(colorNe - colorM);
float3 colorWDir = normalize(colorW - colorM);
float3 colorEDir = normalize(colorE - colorM);
float3 colorSwDir = normalize(colorSw - colorM);
float3 colorSDir = normalize(colorS - colorM);
float3 colorSeDir = normalize(colorSe - colorM);
float3 colorDir = colorNwDir + colorNDir + colorNedDir + colorWDir + colorEDir + colorSwDir + colorSDir + colorSeDir;
float colorDirReduce = max((lumaRange * 0.5f) / (dot(colorDir, colorDir) + 0.001f), 1.0f);
colorNwDir *= colorDirReduce;
colorNDir *= colorDirReduce;
colorNedDir *= colorDirReduce;
colorWDir *= colorDirReduce;
colorEDir *= colorDirReduce;
colorSwDir *= colorDirReduce;
colorSDir *= colorDirReduce;
colorSeDir *= colorDirReduce;
float3 sampleColorNw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, -1.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorN = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, -1.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorNe = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, -1.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorW = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 0.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorE = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 0.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorSw = tex2D(TextureSampler, input.TextureCoordinate + float2(-1.0f, 1.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorS = tex2D(TextureSampler, input.TextureCoordinate + float2(0.0f, 1.0f) * texelSize * colorDirReduce).rgb;
float3 sampleColorSe = tex2D(TextureSampler, input.TextureCoordinate + float2(1.0f, 1.0f) * texelSize * colorDirReduce).rgb;
float3 colorLumaNw = (sampleColorNw - m) / lumaRange;
float3 colorLumaN = (sampleColorN - m) / lumaRange;
float3 colorLumaNe = (sampleColorNe - m) / lumaRange;
float3 colorLumaW = (sampleColorW - m) / lumaRange;
float3 colorLumaE = (sampleColorE - m) / lumaRange;
float3 colorLumaSw = (sampleColorSw - m) / lumaRange;
float3 colorLumaS = (sampleColorS - m) / lumaRange;
float3 colorLumaSe = (sampleColorSe - m) / lumaRange;
float3 colorLumaSum = colorLumaNw + colorLumaN + colorLumaNe + colorLumaW + colorLumaE + colorLumaSw + colorLumaS + colorLumaSe;
float3 colorLumaAvg = colorLumaSum / 8.0f;
float3 colorLumaMin = min(min(min(colorLumaNw, colorLumaN), min(colorLumaNe, colorLumaW)), min(colorLumaE, min(colorLumaSw, min(colorLumaS, colorLumaSe))));
float3 colorLumaMax = max(max(max(colorLumaNw, colorLumaN), max(colorLumaNe, colorLumaW)), max(colorLumaE, max(colorLumaSw, max(colorLumaS, colorLumaSe))));
float3 colorLumaRange = colorLumaMax - colorLumaMin;
float3 colorLumaAvgRange = colorLumaRange / 8.0f;
float3 colorLumaAvgNw = (colorLumaNw - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgN = (colorLumaN - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgNe = (colorLumaNe - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgW = (colorLumaW - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgE = (colorLumaE - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgSw = (colorLumaSw - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgS = (colorLumaS - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgSe = (colorLumaSe - colorLumaAvg) / colorLumaAvgRange;
float3 colorLumaAvgSum = colorLumaAvgNw + colorLumaAvgN + colorLumaAvgNe + colorLumaAvgW + colorLumaAvgE + colorLumaAvgSw + colorLumaAvgS + colorLumaAvgSe;
float3 colorLumaAvgSumDiv = colorLumaAvgSum / 8.0f;
float3 colorLumaAvgRangeDiv = colorLumaRange / 8.0f;
float3 colorLumaAvgNwDiv = (colorLumaNw - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgNDiv = (colorLumaN - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgNeDiv = (colorLumaNe - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgWDiv = (colorLumaW - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgEDiv = (colorLumaE - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgSwDiv = (colorLumaSw - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgSDiv = (colorLumaS - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgSeDiv = (colorLumaSe - colorLumaAvg) / colorLumaAvgRangeDiv;
float3 colorLumaAvgSumDiv = colorLumaAvgNwDiv + colorLumaAvgNDiv + colorLumaAvgNeDiv + colorLumaAvgWDiv + colorLumaAvgEDiv + colorLumaAvgSwDiv + colorLumaAvgSDiv + colorLumaAvgSeDiv;
float3 colorLumaAvgSumDivDiv = colorLumaAvgSumDiv / 8.0f;
float3 colorLumaAvgNwDiv = colorLumaAvgNwDiv / colorLumaAvgSumDiv;
float3 colorLumaAvgNDiv = colorLumaAvgN / colorLumaAvgSumDiv;
float3 colorLumaAvgNeDiv = colorLumaAvgNeDiv / colorLumaAvgSumDiv;
float3 colorLumaAvgWDiv = colorLumaAvgW / colorLumaAvgSumDiv;
float3 colorLumaAvgEDiv = colorLumaAvgE / colorLumaAvgSumDiv;
float3 colorLumaAvgSwDiv = colorLumaAvgSw / colorL
领取专属 10元无门槛券
手把手带您无忧上云