HLSL float 转 int32

Shader Model 3.0及更低使用

encode (fDist is the normalized distance):
    const float4 bitSh    = float4(   256*256*256, 256*256,   256,         1);
    const float4 bitMsk = float4(   0,      1.0/256.0,    1.0/256.0,    1.0/256.0);

    float4 comp;
    comp    = fDist * bitSh;
    comp    = frac(comp);
    comp    -= comp.xxyz * bitMsk;
    return comp;

decode (vec is the rgba encoded value):
    const float4 bitShifts = float4(1.0/(256.0*256.0*256.0), 1.0/(256.0*256.0), 1.0/256.0, 1);
    return dot(vec.xyzw , bitShifts);

 

转换会损失精度。要求高精度的场合下不建议用。

posted @ 2010-10-07 18:48  Yuri  阅读(782)  评论(0编辑  收藏  举报