常用函数合集

 帧动画
fixed2 frameAni(fixed2 uv, fixed Startnum, fixed Row, fixed Columns)
{
     fixed startnum = floor(Startnum);
     fixed scalex = reciprocal(Row);
     fixed row = floor(startnum * scalex);
     fixed column = fmod(startnum, Row);
     fixed scaley = reciprocal(Columns);
     fixed2 initUV = uv * fixed2(scalex, scaley) + fixed2(0.0, -scaley);  //或者 + fixed2(0.0, scaley * (Columns - 1))
     fixed2 frameuv = initUV + fixed2(column * scalex, -row * scaley);
     return frameuv;
}

从上往下,从左往右

 

fixed2 frameAni(fixed2 uv, fixed Startnum, fixed Row, fixed Columns)
{
     fixed startnum = floor(Startnum);
     fixed scalex = reciprocal(Row);
     fixed row = floor(startnum * scalex);
     fixed column = fmod(startnum, Row);
     fixed scaley = reciprocal(Columns);
     fixed2 initUV = uv * fixed2(scalex, scaley) + fixed2(0.0, -scaley);  //或者 + fixed2(0.0, scaley * (Columns - 1))
     fixed2 frameuv = initUV - fixed2(column * scalex, row * scaley);
     return frameuv;
}

从下往上,从左往右

图片要repeat模式

 

重映射

half remap(half x, half t1, half t2, half s1, half s2)
{
   return (x - t1) * reciprocal(t2 - t1) * (s2 - s1) + s1; }

 

 

倒数

float reciprocal(float original)
{
     return rsqrt(original) * rsqrt(original);
}

 

三维旋转

float3 Rotate3D (float3 vertex, float3 degrees)
{
    float3 alpha = degrees * UNITY_PI * 0.555555555556;  //UNITY_PI / 180.0;
    float3 sina, cosa;
    sincos(alpha, sina, cosa);
    float3x3 rx = float3x3 (
            1.0, 0.0, 0.0,
            0.0, cosa.x, -sina.x,
            0.0, sina.x, cosa.x
            );
    float3x3 ry = float3x3 (
            cosa.y, 0.0, sina.y,
            0.0, 1.0, 0.0,
            -sina.y, 0.0, cosa.y
            );
    float3x3 rz = float3x3 (
            cosa.z, -sina.z, 0.0,
            sina.z, cosa.z, 0.0,
            0.0, 0.0, 1.0
            );
    float3x3 m = mul(rz, mul(ry, rx));
    float3 final = mul(m, vertex);
    return final;
}

 

二维旋转

float2 Rotate2D (float2 uv, float degrees)
{
    float alpha = degrees * UNITY_PI * 0.005555555555555556;    //UNITY_PI / 180
    float2x2 matrix2D = float2x2(cos(alpha), sin(alpha), -sin(alpha), cos(alpha));
    float2 final = mul(matrix2D, uv);
    return final;
}

 

posted @ 2024-05-24 09:52  terrificia  阅读(5)  评论(0编辑  收藏  举报