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 HD2400 | 1.00 | 17.00 | 3.00 | 4.00 | 11.00 | ||
Radeon HD5870 | 0.50 | 0.95 | 0.50 | 0.50 | 0.80 | ||
GeForce 6200 | 1.00 | 12.00 | 4.00 | 2.00 | 12.00 | ||
GeForce 8800 | 7.00 | 43.00 | 12.00 | 12.00 | 24.00 | ||
Decoding, GPU cycles | |||||||
Radeon HD2400 | 1.00 | 17.00 | 3.00 | 4.00 | 11.00 | ||
Radeon HD5870 | 0.50 | 0.95 | 0.50 | 1.00 | 0.80 | ||
GeForce 6200 | 4.00 | 7.00 | 6.00 | 4.00 | 12.00 | ||
GeForce 8800 | 15.00 | 23.00 | 15.00 | 12.00 | 29.00 | ||
Encoding, D3D ALU+TEX instruction slots | |||||||
SM3.0 | 1 | 26 | 4 | 5 | 17 | ||
Decoding, D3D ALU+TEX instruction slots | |||||||
SM3.0 | 8 | 18 | 9 | 8 | 22 |
质量对比
Quality comparison in a single table. PSNR based, higher numbers are better.
Method | PSNR, dB |
---|---|
#1: X & Y | 18.629 |
#3: Spherical | 42.042 |
#4: Spheremap | 48.071 |
#7: Stereographic | 44.147 |
#8: Per pixel view | 38.730 |
以上是关于Gbuffer的法向量的压缩的主要内容,如果未能解决你的问题,请参考以下文章