簡體   English   中英

紋理采樣的自適應深度偏差

[英]Adaptive depth bias for texture sampling

我有一個復雜的 3D 場景,我的深度緩沖區中的值范圍從近距離,幾厘米到幾公里。

對於一些不同的效果,我使用深度偏差、偏移來規避一些偽影(SSAO、Shadow)。 即使在通過比較當前剝離和前一次剝離的深度進行深度剝離的過程中,也會出現一些問題。

我已經解決了近距離拍攝的這些問題,但是當片段足夠遠時,偏差就變得過時了。

我想知道如何解決這種場景的偏見。 取決於當前像素的當前世界深度,或者可能完全禁用給定深度的效果?

關於這些問題是否有一些好的做法,我該如何解決?在此處輸入圖像描述

好像找到了辦法

我有聲音這個鏈接陰影偏差https://digitalrune.github.io/DigitalRune-Documentation/html/3f4d959e-9c98-4a97-8d85-7a73c261t45d7。

深度偏差和法線偏移值在陰影 map 紋素中指定。 例如,深度偏差 = 3 意味着像素移動了 3 個陰影 map 紋素的長度,靠近光。

通過保持與投影陰影 map 紋素成比例的偏差,相同的設置適用於所有距離。

我使用當前點和具有相同深度分量的相鄰像素之間的世界空間差異。 偏差接近“兩個相鄰像素之間的平均距離”。 像素越遠,偏差就越大(從接近近平面的幾毫米到遠平面的米)。

  • 因此,對於我的每個采樣點,我將其 position 從其 x 方向上的一些像素偏移(3 個像素在各種場景中給了我很好的結果)。

  • 我計算 currentPoint 和這個新的 offsetedPoint 之間的世界差異

  • 我使用這種差異作為我所有深度測試的偏差

代碼

float compute_depth_offset() {

    mat4 inv_mvp = inverse(mvp);
    vec2 currentPixel = vec2(gl_FragCoord.xy) / dim;
    vec2 nextPixel = vec2(gl_FragCoord.xy + vec2(depth_transparency_bias, 0.0)) / dim;

    vec4 currentNDC;
    vec4 nextNDC;

    currentNDC.xy = currentPixel * 2.0 - 1.0;
    currentNDC.z = (2.0 * gl_FragCoord.z - depth_range.near - depth_range.far) / (depth_range.far - depth_range.near);
    currentNDC.w = 1.0;

    nextNDC.xy = nextPixel * 2.0 - 1.0;
    nextNDC.z = currentNDC.z;
    nextNDC.w = currentNDC.w;

    vec4 world = (inv_mvp * currentNDC);
    world.xyz = world.xyz / world.w;

    vec4 nextWorld = (inv_mvp * nextNDC);
    nextWorld.xyz = nextWorld.xyz / nextWorld.w;

    return length(nextWorld.xyz - world.xyz);
}

最近我只使用了當前像素position的世界空間導數:

float compute_depth_offset(float zNear, float zFar)
{
    mat4 mvp = projection * modelView;
    mat4 inv_mvp = inverse(mvp);
    vec2 currentPixel = vec2(gl_FragCoord.xy) / dim;
    vec4 currentNDC;

    currentNDC.xy = currentPixel * 2.0 - 1.0;
    currentNDC.z = (2.0 * gl_FragCoord.z - 0.0 - 1.0) / (1.0 - 0.0);
    currentNDC.w = 1.0;

    vec4 world = (inv_mvp * currentNDC);
    world.xyz = world.xyz / world.w;

    vec3 depth = max(abs(dFdx(world.xyz)), abs(dFdy(world.xyz)));

    return depth.x + depth.y + depth.z; 
}

在此處輸入圖像描述

暫無
暫無

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

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