2017-05-15 139 views
2

你好,我想獲得一個快速的圓角矩形GLSL着色器,但我只是設法做到這一點,使用此功能(https://github.com/marklundin/glsl-sdf-primitives/blob/master/udRoundBox.glsl)填充矩形:接壤圓角矩形在GLSL

float udRoundBox(vec3 p, vec3 b, float r) 
{ 
    return length(max(abs(p)-b,0.0))-r; 
} 

我已經一直試圖找到一個邊界而不是填充的版本,甚至試圖想出一個,但沒有運氣。有沒有人有解決這個問題?

回答

1

由於您使用帶符號距離函數,最簡單的方法是使用減法運算符從最初的一個減去較小的圓角框。

它應該是這樣的:

// unsigned round box 
float udRoundBox(vec3 p, vec3 b, float r) 
{ 
    return length(max(abs(p)-b,0.0))-r; 
} 

// substracts shape d1 from shape d2 
float opS(float d1, float d2) 
{ 
    return max(-d1,d2); 
} 

// to get the border of a udRoundBox, simply substract a smaller udRoundBox ! 
float udRoundBoxBorder(vec3 p, vec3 b, float r, float borderFactor) 
{ 
    return opS(udRoundBox(p, b*borderFactor, r), udRoundBox(p, b, r)); 
} 

borderFactor需要在[0:1]的值,它是大,小的邊框會。

這裏是一個ShaderToy樣品展示這一點: enter image description here

2

使用次箱體擠出第一個是一個壞主意,因爲它會導致性能差。不是計算單個對象,而是計算兩個對象。 SDF的性能通常受到您在距離函數中添加的對象數量的約束;這就是爲什麼mod/floor函數在製作SDF時非常有用的原因。您可以爲(幾乎)不增加成本添加無限數量的對象。

在這裏使用框簽名的確切功能http://iquilezles.org/www/articles/distfunctions/distfunctions.htm如果你想自己寫,請嘗試找出一種方法來獲得框上最近的點,然後返回到這一點的距離。

您可以通過減去距離的術語來得到一個圓角框,但您已經計算出它(例如r)。

您可以通過使用距離的絕對值來獲得邊框。 Abs(de)基本上通過將物體內部(負距離)設爲正值來消除它。這會使對象具有「空白」邊框。如果你想讓邊框更大,就簡單地減去另一個詞來增加它的大小,就像你做一個圓形框一樣。

這適用於任何形狀,只要距離估計是正確的。 SDF非常適合這樣的小技巧。

3

我覺得這裏就是你要搜索的內容...

//--------------------------------------------------------- 
// draw rectangle frame with rounded edges 
//--------------------------------------------------------- 
float roundedFrame (vec2 pos, vec2 size, float radius, float thickness) 
{ 
    float d = length(max(abs(uv - pos),size) - size) - radius; 
    return smoothstep(0.55, 0.45, abs(d/thickness) * 5.0); 
} 

看看我的shadertoy例子https://www.shadertoy.com/view/MssyRN screen shot example

+0

很好。謝謝。 – Robinson