2013-01-04 105 views
0

我正在尝试编写一个后期处理着色器,它可以模糊屏幕的中心,然后逐渐消失。HLSL:绘制居中圆

感谢这里发布的一些代码,我有一个反锯齿的圆被模糊,但它在屏幕的左上角。问题似乎在我的算法计算圆:

float4 PS_GaussianBlur(float2 texCoord : TEXCOORD) : COLOR0 
{ 
float4 insideColor = float4(0.0f, 0.0f, 0.0f, 0.0f); 
float4 outsideColor = tex2D(colorMap, texCoord); 

//Perform the blur: 
for (int i = 0; i < KERNEL_SIZE; ++i) 
    insideColor += tex2D(colorMap, texCoord + offsets[i]) * weights[i]; 

float dist = (texCoord.x * texCoord.x) + (texCoord.y * texCoord.y); 
float distFromCenter = .5 - dist; // positive when inside the circle 
float thresholdWidth = 0.1; // a constant you'd tune to get the right level of softness 
float antialiasedCircle = saturate((distFromCenter/thresholdWidth) + 0.5); 
return lerp(outsideColor, insideColor, antialiasedCircle); 
} 

在此先感谢!

回答

1

试试这个:

float dx = 0.5 - texCoord.x, dy = 0.5 - texCoord.y; 
float dist = dx * dx + dy * dy; 
float distFromCenter = 0.25 - dist; // positive when inside the circle 
+0

这做到了!万分感谢! – Michael