如何解决图像 monogame pixelshader 的模糊部分
所以我设法想出了一个着色器,它会根据某些线段的位置来阻挡图像的一部分(总结一下像素着色器,它只是检查从灯光中心到任何给定的像素与任何“墙”线段相交,因此不会绘制图像。
然而,我的问题是光线的截止非常尖锐,我想稍微模糊一下,不要模糊光线与墙壁部分相遇的硬边缘。什么是实现这一目标的好方法?我附上了一个正在运行的着色器的 gif 图像和相关代码。
Stuff circled in red should be blurry,green should remain sharp
float4 MainPS(VertexShaderOutput input) : COLOR
{
float4 color = tex2D(s0,input.TextureCoordinates);
float2 centerPos = float2(150.0f + mX,150.0f + mY);
float2 coords = float2((input.TextureCoordinates.x * 300) + mX,(input.TextureCoordinates.y * 300) + mY);
//change alpha based on distance
color.w = map(distance(centerPos,coords),150,.5,.1f);
//If the line segment from the center to this pixel intersects any given line segment (hardcoded for now,don't draw it)
if (intersect(centerPos,coords,float2(160.0f,200.0f),float2(200.0f,140.0f)) ||
intersect(centerPos,float2(250.0f,200.0f)) || intersect(centerPos,140.0f),200.0f)))
color = float4(0,0);
return color;
}
任何帮助/提示将不胜感激。
解决方法
除了计算从中心到当前像素的线是否与线段相交,还可以计算出交点的准确位置,然后利用交点与当前像素的距离来柔化阴影,类似以下内容:
float2 intersectionPos;
if (intersect(centerPos,coords,float2(160.0f,200.0f),float2(200.0f,140.0f),intersectionPos) ||
intersect(centerPos,float2(250.0f,intersectionPos) ||
intersect(centerPos,intersectionPos))
{
color.w *= lerp(1,saturate(distance(coords,intersectionPos) / BLUR_DISTANCE))
}
其中 BLUR_DISTANCE
是一些小数字,表示距离线段有多远模糊像素,intersect
定义为:
bool intersect(float2 origin,float2 point,float2 segEnd1,float2 segEnd2,inout float2 intersectionPos) {
// implement this to set intersectionPos *only* if there's actually an intersection
}
为了获得更漂亮的结果,您可以使用非线性插值函数,或者计算到直线上最近点的距离而不是光线交点,但基本思想是相同的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。