Transform Matrix4x4
(1) 如下代码块是计算RectTransform组件的四条边的坐标实例:
void Start()
{
//获取RectTransform的四条边的坐标
RectTransform tran = GetComponent<RectTransform>();
//使用父节点的坐标矩阵
Transform parent = tran.parent;
Matrix4x4 matrix = parent.worldToLocalMatrix;
//获得四个顶点的世界坐标
Vector3[] corners = new Vector3[4];
tran.GetWorldCorners(corners);
//世界坐标转本地坐标
for(int i = 0; i < corners.Length; ++i)
{
corners[i] = matrix.MultiplyPoint3x4(corners[i]);
}
//四条边本地坐标
float bottom = corners[0].y;
float top = corners[2].y;
float left = corners[0].x;
float right = corners[2].x;
Debug.LogError("bottom=" + bottom + ",top=" + top + ",left=" + left + ",right=" + right);
}
(2) 如下代码块是计算Transform组件的Position的实例:
void Start()
{
//计算世界坐标
Transform tran = GetComponent<Transform>();
//使用父节点的坐标矩阵
Transform parent = tran.parent;
Matrix4x4 matrix = parent.localToWorldMatrix;
//计算
Vector3 pos = matrix.MultiplyPoint3x4(tran.localPosition);
Debug.LogError("caculate pos=" + pos + ",real pos=" + tran.position);
}
Camera Matrix
Camera.worldToCameraMatrix
Camera.cameraToWorldMatrix
Camera.ResetWorldToCameraMatrix
Camera.projectionMatrix
Camera.ResetProjectionMatrix
GL Matrix
public class ExampleClass : MonoBehaviour
{
void OnPostRender()
{
// Set your materials
GL.PushMatrix();
// yourMaterial.SetPass( );
// Draw your stuff
GL.PopMatrix();
}
}