Unity升级到5.6及以后,所有跟UNITY_MATRIX_MVP运算的矩阵或者向量的mul方法,会被自动转成UnityObjectToClipPos方法
float4x4 m = mul(Unity_MATRIX_MVP, rm);
float4x4 m = UnityObjectToClipPos(rm);
在代码中遇到了这样的报错:
void Update()
{
Matrix4x4 RM = new Matrix4x4();
RM[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);
RM[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);
RM[1, 1] = 1;
RM[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);
RM[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);
RM[3, 3] = 1;
GetComponent<Renderer>().material.SetMatrix("rm", RM);
}
float4x4 rm;
struct v2f {
float4 pos:POSITION;
};
v2f vert(appdata_base v)
{
v2f o;
float4x4 m = UnityObjectToClipPos(rm);
o.pos = mul(m,v.vertex);
return o;
}
解决办法如下:
v2f vert(appdata_base v)
{
v2f o;
float4x4 m1 = UNITY_MATRIX_MVP;
float4x4 m = mul(m1,rm);
o.pos = mul(m,v.vertex);
return o;
}
原因是mul和UnityObjectToClipPos所需的参数不一样,查文档如下: 这里我们可以看到UnityObjectToClipPos与mul传入的参数类型不一致,不能隐式转换导致的报错
|