簡體   English   中英

模糊圖像monogame像素着色器的部分

[英]Blur parts of image monogame pixelshader

所以我設法想出了一個着色器,它將根據某些線段的位置來阻止圖像的一部分(總結像素着色器,它只是檢查從光中心到任何給定像素的線段是否相交任何“牆”線段,因此如果是這樣,則不會繪制圖像)。

然而,我的問題是光線的截止非常尖銳,我想稍微模糊一下,而不是模糊光線與牆壁部分相遇的硬邊緣。 什么是實現這一目標的好方法? 我附上了正在運行的着色器的 gif 和相關代碼。

燈光着色器

紅色圈出的東西應該是模糊的,綠色應該保持清晰

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), 0, 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, coords, float2(160.0f, 200.0f), float2(250.0f, 200.0f)) || intersect(centerPos, coords, float2(200.0f, 140.0f), float2(250.0f, 200.0f)))
        color = float4(0, 0, 0, 0);
    return color;
}

任何幫助/提示將不勝感激。

除了計算從中心到當前像素的線是否與線段相交,還可以計算出交點的准確position,然后利用交點到當前像素的距離來柔化陰影,順着一些東西的行:

float2 intersectionPos;
if (intersect(centerPos, coords, float2(160.0f, 200.0f), float2(200.0f, 140.0f), intersectionPos) ||
    intersect(centerPos, coords, float2(160.0f, 200.0f), float2(250.0f, 200.0f), intersectionPos) || 
    intersect(centerPos, coords, float2(200.0f, 140.0f), float2(250.0f, 200.0f), intersectionPos)) 
{
    color.w *= lerp(1, 0, 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
} 

對於更漂亮的結果,您可以使用非線性插值 function,或者計算到直線上最近點的距離而不是射線交點,但基本思想是相同的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM