MultiplyPoint3x4 矩阵变换
public Vector3 MultiplyPoint3x4(Vector3 v);
通过指定的矩阵来变换向量v
下面的代码是通过矩阵m来变换v
void Start () {
Matrix4x4 m = Matrix4x4.identity;
Vector3 v = Vector3.one;
Vector3 v2 = m.MultiplyPoint3x4(v);
}
MultiplyVector 矩阵变换
public Vector3 MultiplyVector(Vector3 v);
通过指定的矩阵来变换向量v,只改变向量的方向SetTRS 重设Matrix4*4矩阵
public void SetTRS(Vector3 pos, Quaternion q, Vector3 s);
功能:用来重设Matrix4*4矩阵
参数:pos 位置向量 q 旋转角度
s 缩放向量
我们可以这么写代码
Matrix4x4 m1 = Matrix4x4.identity;
m1.SetTRS(pos, q, s);
Vector3 v2 = m1.MultiplyPoint3x4(v1);
例子:
下面看看如何把向量v1沿y轴增加5个单位,绕y轴旋转45度,缩放2倍
代码:
public class SetTRS : MonoBehaviour
{
// Use this for initialization
Vector3 v1 = Vector3.one;
Vector3 v2 = Vector3.zero;
void Start()
{
Matrix4x4 m1 = Matrix4x4.identity;
//Position沿y轴增加5个单位,绕y轴旋转45度,缩放2倍
m1.SetTRS(Vector3.up * 5, Quaternion.Euler(Vector3.up * 45.0f), Vector3.one * 2.0f);
//也可以使用如下静态方法设置m1变换
//m1=Matrix4x4.TRS(Vector3.up * 5, Quaternion.Euler(Vector3.up * 45.0f),Vector3.one*2.0f);
v2 = m1.MultiplyPoint3x4(v1);
}
void FixedUpdate()
{
Debug.DrawLine(Vector3.zero, v1, Color.green);
Debug.DrawLine(Vector3.zero, v2, Color.red);
}
}
其中绿色线是v1,红色线是v1沿y轴增加5个单位,绕y轴旋转45度,缩放2倍的效果