在Unity中,unity_ObjectToWorld是将“模型坐标”转化成“世界坐标”的四维矩阵。
重点是unity_ObjectToWorld矩阵的最后一列存储了物体Transform的世界坐标Position,所以我们可以在shader中提取这个位置信息做一些计算,下面是unity支持的几种写法:
float3 center = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
float3 center = float3(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23);
float3 center = mul(unity_ObjectToWorld , float(0,0,0,1)).xyz;
float3 center = unity_ObjectToWorld._14_24_34;
网友评论