八面体法线压缩

2023-07-30  本文已影响0人  暴走TA

简述: 最近在将法线信息存储到切线空间的UV通道时发现,Z值偶尔会有为负数的情况,基本出现在角度比较尖锐的地方,这时候用 1-X2-Y2 的方式就会出现错误
在网上搜索到了大佬的压缩方式,当场跪地膜拜,果然高中的数学水平还是有限
知乎地址

处理文件时使用的压缩计算方式为 :

    // Octahedron Normal Vectors
    FVector2D OctNormal = (FVector2D)ResultNormal / FVector::DotProduct(FVector::OneVector, FMath::Abs(ResultNormal));
    if (ResultNormal.Z <= 0)
    {
        OctNormal = (FVector2D(1,1) - FMath::Abs(FVector2D(OctNormal.Y, OctNormal.X))) * FVector2D(OctNormal.X >= 0 ? 1 : -1, OctNormal.Y >= 0 ? 1 : -1);
    }

在着色器里的解压缩计算

    float3 OctahedronToUnitVector( float2 Oct )
    {
        float3 N = float3( Oct, 1 - dot( 1, abs(Oct) ) );
        if( N.z < 0 )
        {
            N.xy = ( 1 - abs(N.yx) ) * ( N.xy >= 0 ? float2(1,1) : float2(-1,-1) );
        }
        return normalize(N);
    }
上一篇 下一篇

猜你喜欢

热点阅读