Gbuffer的法向量的压缩

Posted 潘李亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gbuffer的法向量的压缩相关的知识,希望对你有一定的参考价值。

BTW: 自己看到了记录一下,大家随意。

原文链接:https://aras-p.info/texts/CompactNormalStorage.html

1.直接存在RGB通道上。

half4 encode(half3 n){return half4(n.xyz*0.5+0.5,0);}
half3 decode(half4 c){return c.rgb*2-1;}


2.存储法线的XY在颜色通道上,计算Z

half4 encode(half3 n){return half4(n.xy*0.5+0.5,0.0);}
half3 decode(half2 c)
{
    half3 n;
    n.xy = c*2-1;
    n.z = sqrt(1-dot(n.xy,n.xy));
    return n;
}

优点:容易实现

缺点:误差较大。


3.球面坐标(Spherical Coordinates)

#define kPI 3.1415926536f
half4 encode (half3 n)
{
    return half4((half2(atan2(n.y,n.x)/kPI, n.z)+1.0)*0.5,0,0);
}
half3 decode (half2 enc)
{
    half2 ang = enc*2-1;
    half2 scth;
    sincos(ang.x * kPI, scth.x, scth.y);
    half2 scphi = half2(sqrt(1.0 - ang.y*ang.y), ang.y);
    return half3(scth.y*scphi.x, scth.x*scphi.x, scphi.y);
}

优点:适合大部分法线,不一定需要view space

缺点:使用了三角函数,计算量比较大。


4.Spheremap Transform 

half2 encode (half3 n)
{
    half2 enc = normalize(n.xy) * (sqrt(-n.z*0.5+0.5));
    enc = enc*0.5+0.5;
    return enc;
}
half3 decode (half4 enc)
{
    half4 nn = enc*half4(2,2,0,0) + half4(-1,-1,1,-1);
    half l = dot(nn.xyz,-nn.xyw);
    nn.z = l;
    nn.xy *= sqrt(l);
    return nn.xyz * 2 + half3(0,0,-1);
}

优点:效果还不错,计算量小。CryEngine 3使用的方案。 presented by Martin Mittring in "A bit more Deferred" presentation


5.Lambert Azimuthal Equal-Area projection

half2 encode (half3 n)
{
    half f = sqrt(8*n.z+8);
    return n.xy / f + 0.5;
}
half3 decode (half4 enc)
{
    half2 fenc = enc*4-2;
    half f = dot(fenc,fenc);
    half g = sqrt(1-f/4);
    half3 n;
    n.xy = fenc*g;
    n.z = 1-f/2;
    return n;
}

优点:效果还不错,计算量小。 by Sean Barrett


6.Stereographic Projection 

half4 encode (half3 n)
{
    half scale = 1.7777;
    half2 enc = n.xy / (n.z+1);
    enc /= scale;
    enc = enc*0.5+0.5;
    return half4(enc,0,0);
}

half3 decode (half4 enc)
{
    half scale = 1.7777;
    half3 nn =
        enc.xyz*half3(2*scale,2*scale,0) +
        half3(-scale,-scale,1);
    half g = 2.0 / dot(nn.xyz,nn.xyz);
    half3 n;
    n.xy = g*nn.xy;
    n.z = g-1;
    return n;
}

优点:效果还不错,计算量小。


8. Per-pixel View Space

float3x3 make_view_mat (float3 view)
{
    view = normalize(view);
    float3 x,y,z;
    z = -view;
    x = normalize (float3(z.z, 0, -z.x));
    y = cross (z,x);
    return float3x3 (x,y,z);
}

half4 encode (half3 n, float3 view)
{
    return half4(mul (make_view_mat(view), n).xy*0.5+0.5,0,0);
}

half3 decode (half4 enc, float3 view)
{
    half3 n;
    n.xy = enc*2-1;
    n.z = sqrt(1+dot(n.xy,-n.xy));
    n = mul(n, make_view_mat(view));
    return n;
}

优点:效果还不错,计算量小。

性能对比:

GPU performance comparison in a single table:

#1: X & Y#3: Spherical#4: Spheremap#7: Stereo#8: PPView
Encoding, GPU cycles
Radeon HD24001.0017.003.004.0011.00
Radeon HD58700.500.950.500.500.80
GeForce 62001.0012.004.002.0012.00
GeForce 88007.0043.0012.0012.0024.00
Decoding, GPU cycles
Radeon HD24001.0017.003.004.0011.00
Radeon HD58700.500.950.501.000.80
GeForce 62004.007.006.004.0012.00
GeForce 880015.0023.0015.0012.0029.00
Encoding, D3D ALU+TEX instruction slots
SM3.01264517
Decoding, D3D ALU+TEX instruction slots
SM3.08189822

质量对比

Quality comparison in a single table. PSNR based, higher numbers are better.

MethodPSNR, dB
#1: X & Y18.629
#3: Spherical42.042
#4: Spheremap48.071
#7: Stereographic44.147
#8: Per pixel view38.730

以上是关于Gbuffer的法向量的压缩的主要内容,如果未能解决你的问题,请参考以下文章

Gbuffer的法向量的压缩

Gbuffer的法向量的压缩

meshlab的法向量怎么修改

离散点云的法向量如何计算?

openGL中的法向量问题

旋转平面使其具有一定的法向量