left | 左侧 x 坐标。 |
right | 右侧 x 坐标。 |
bottom | 下方 y 坐标。 |
top | 上方 y 坐标。 |
zNear | 近深度裁剪面值。 |
zFar | 远深度裁剪面值。 |
Matrix4x4 投影矩阵。
创建正交投影矩阵。
返回矩阵在用作摄像机的投影矩阵时,会创建
处于 top
、top
、top
和 bottom
之间的区域的投影,将 zNear
和 zFar
作为一个立方体中的近和远深度裁剪面,
从 (left, bottom, near) = (-1, -1, -1) 到 (right, top, far) = (1, 1, 1)。
返回矩阵嵌入了 z 翻转操作,其用途是取消摄像机视图矩阵执行的 z 翻转。
如果视图矩阵是单位矩阵或某种不执行 z 翻转的自定义矩阵,请考虑将投影矩阵
的第三列(即 m02、m12、m22 和 m32)乘以 -1。
Unity 中的投影矩阵遵循 OpenGL 约定,即裁剪空间近平面处于 z=-1
,而远平面处于 z=1
。
请注意,根据使用的图形 API,着色器中的投影矩阵可能会遵循不同的约定,例如 D3D 样式
裁剪空间的近平面处于 0,而远平面处于 1;“反转 Z”投影的近平面处于 1,而远平面处于 0。
若要计算适合于传递给着色器变量的投影矩阵值,请使用 GL.GetGPUProjectionMatrix。
using UnityEngine;
public class ExampleScript : MonoBehaviour { void Start() { // create orthographic matrix var matrix = Matrix4x4.Ortho(-4, 4, -2, 2, 1, 100); // will print: // 0.25000 0.00000 0.00000 0.00000 // 0.00000 0.50000 0.00000 0.00000 // 0.00000 0.00000 -0.02020 -1.02020 // 0.00000 0.00000 0.00000 1.00000 Debug.Log("projection matrix\n" + matrix);
// get shader-compatible projection matrix value var shaderMatrix = GL.GetGPUProjectionMatrix(matrix, false); // on a Direct3D-like graphics API, will print: // 0.25000 0.00000 0.00000 0.00000 // 0.00000 0.50000 0.00000 0.00000 // 0.00000 0.00000 0.01010 1.01010 // 0.00000 0.00000 0.00000 1.00000 Debug.Log("shader projection matrix\n" + shaderMatrix); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.