Gbuffer的法向量的压缩

这篇博客探讨了几种不同的法线存储方法,包括直接存储、球面坐标、球面映射、立体投影等,并提供了编码和解码的实现。文章通过GPU性能对比和质量分析,展示了各种方法在效率和精度上的优缺点。例如,球面坐标法虽然精度高,但计算量大;而球面映射和立体投影则在效果和计算量之间取得了平衡。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值