tyflow中script节点欧拉角转换四元数的函数

发布时间 2023-04-03 00:29:32作者: Jom

最近在研究tyflow script,script操作符支持C#编程,但和unity中不同的是,untiy提供了非常多的方法来转换,而tyflow的API只提供了tf.SetRot方法而且只接受四元数输入;

四元数并不直观,从XYZ的欧拉角到四元数,需要做一些变换,详细请看

https://www.3dgep.com/understanding-quaternions/

 

tyflow不提供,只能自己写,这是一个简单的函数,通过输入一个point3型的变量,输出一个Quat型的四元数

public Quat EulerToQuaternion(Point3 euler)
{
float yaw = euler.y * Mathf.Deg2Rad;
float pitch = euler.x * Mathf.Deg2Rad;
float roll = euler.z * Mathf.Deg2Rad;

float cy = Mathf.Cos(yaw * 0.5f);
float sy = Mathf.Sin(yaw * 0.5f);
float cp = Mathf.Cos(pitch * 0.5f);
float sp = Mathf.Sin(pitch * 0.5f);
float cr = Mathf.Cos(roll * 0.5f);
float sr = Mathf.Sin(roll * 0.5f);

Quat q = new Quat();
q.w = cy * cp * cr + sy * sp * sr;
q.x = cy * sp * cr + sy * cp * sr;
q.y = sy * cp * cr - cy * sp * sr;
q.z = cy * cp * sr - sy * sp * cr;

return q;
}

 

调用的话,可以在任意过程里调用,这里示例了在simulationStep中,先获取设置随机seed,生成随机数并乘以90度,将其转换为四元数赋值的过程

public void simulationStep()
{      
    for (int i = 0; i < eventParticleCount; i++)
    {
        int sInx = tf.GetSimIndex(i); 
        float eventAge = tf.GetEventAge(sInx); 
        tf.SetSeed(sInx,(int)GetFloat("seed"));
        Point3 randRot = new Point3(0,0,tf.GetRandInt(sInx,0,4)*90);
        tf.SetRot(sInx,EulerToQuaternion(randRot));
    }   
}