ChuanXin 发表于 2021-4-3 14:35

unity_ObjectToWorld里的每一列分别代表什么意思?

unity_ObjectToWorld中,最后一列代表的是模型中心点的世界坐标,那其余三列分别是什么意思

rustum 发表于 2021-4-3 14:40

emm,题主可能和unity_ObjectToWorld矩阵推导配合的不是很好
首先我们把模型顶点从模型空间转换到世界空间用的就是unity_ObjectToWorld,那么这个矩阵的内容是什么呢,没错,就是这个模型相对于世界空间原点的缩放,旋转和平移(至于为什么是这个顺序,是因为S(缩放)不改变向量基位置和方向,只改变向量基大小,旋转(R)改变向量基方向,平移(T)改变向量基位置,而每一种变换都是以坐标轴为基准的,只有按照这个顺序才能把一个物体按照我们想象的样子置换到正确的位置,旋转和大小(试想一下如果先平移,后缩放,平移后的一个顶点位置为(0,5,0)再在Y方向放大5倍,其坐标就变成了(0,25,0)这显然不是我们想要的,更换任意两个变换都会得到我们并不想要的结果))
然后推一下unity_ObjectToWorld的公式
其中尤其需要注意的是对于旋转矩阵的处理,在Unity里面,我们约定旋转顺序是先Z轴,再X轴,最后Y轴,但是由于我们旋转会导致向量基方向变化,所以如果按照上面的顺序是有问题的,所以只要反过来写:先Y轴,再X轴,最后Z轴,得到的结果就是正确的,题主可以自己推算一下嗷
https://www.zhihu.com/equation?tex=%5C%3BM_%7BRz%7DM_%7BRx%7DM_%7BRy%7D+%3D++%5Cbegin%7Bbmatrix%7D%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29-%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%26-%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%26%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%2B%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%260%5C%5C-%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29-%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%26%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%26-%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%2B%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%260%5C%5C-%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%26%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%26%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%26t_z%5C%5C0%260%260%261%5Cend%7Bbmatrix%7D
https://www.zhihu.com/equation?tex=M_%7BLocal2World%7D+%3D+M_%7BT%7DM_%7BR%7DM_%7BS%7D+%3D+%5Cbegin%7Bbmatrix%7D1%260%260%26t_x%5C%5C0%261%260%26t_y%5C%5C0%260%261%26t_z%5C%5C0%260%260%261%5Cend%7Bbmatrix%7DM_%7BR%7D%5Cbegin%7Bbmatrix%7Dr_x%260%260%260%5C%5C0%26r_y%260%260%5C%5C0%260%26r_z%260%5C%5C0%260%260%261%5Cend%7Bbmatrix%7D
https://www.zhihu.com/equation?tex=%3D%5Cbegin%7Bbmatrix%7Dr_x%28%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29-%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%29%26-r_y%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%26r_z%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%2B%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%26t_x%5C%5C-r_x%28%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29%2B%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%29%26r_y%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%26-r_z%28%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%5Csin%5Cleft%28%5Ctheta_z%5Cright%29-%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%5Ccos%5Cleft%28%5Ctheta_z%5Cright%29%29%26t_y%5C%5C-r_x%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Csin%5Cleft%28%5Ctheta_y%5Cright%29%26r_y%5Csin%5Cleft%28%5Ctheta_x%5Cright%29%26r_z%5Ccos%5Cleft%28%5Ctheta_x%5Cright%29%5Ccos%5Cleft%28%5Ctheta_y%5Cright%29%26t_z%5C%5C0%260%260%261%5Cend%7Bbmatrix%7D
所以,unity_ObjectToWorld拆分意义如下
最后一列是模型中心的世界坐标前三行三列的每一列的向量长度分别对应X,Y,Z三个轴向的缩放值前三行三列的向量除以其对应轴的缩放值,并且将最后一列的前三行与最后一行的前三列置为0即为旋转矩阵,我们可以用这个旋转矩阵得到四元数,各个轴向的旋转角等

JoshWindsor 发表于 2021-4-3 14:49

(1,0,0)(0,1,0)(0,0,1) 这三个是localspace下的基元向量,而三个结果刚好就是矩阵的三列,也是是转换到worldspace下的基元向量
(1,0,0)(0,1,0)(0,0,1) 可以简单理解为localspace下的right, up, forward, 而三个结果是考虑了scale和translate的worldspace下的right, up, forward。但是这三个结果不是最终的transform.right,transform.up,transform.forward。因为http://transform.xxx仅仅考虑了rotate的影响。本质上,方向也不应该受到缩放和平移的影响。

HuldaGnodim 发表于 2021-4-3 14:49

代表着模型空间基向量转换到世界空间后的值
你可以认为模型空间的基向量是(1,0,0) (0,1,0) (0,0,1),经过unity_ObjectToWorld线性变换后得到了一组新的向量值。
因为线性相关的原因,模型空间下的点乘以这个由新的基向量组成的矩阵后,就转变为世界空间下的点了。

zifa2003293 发表于 2021-4-3 14:55

MVP矩阵了解一下你就明白了

DomDomm 发表于 2021-4-3 14:58

这是个渲染相关的函数。
这个函数的用意是指在渲染管线中的 模型空间 -> 世界空间 下的转换。
如果不清楚 可以先尝试看下 图形学基础。
B站:闫令琪(现代计算机图形学)
页: [1]
查看完整版本: unity_ObjectToWorld里的每一列分别代表什么意思?