设置自定义投影矩阵。
如果您更改此矩阵,相机将不再根据其 fieldOfView 更新其渲染。此外,这会保留摄像机的 nearClipPlane 和 farClipPlane 不变,因此您需要手动更新它们以避免不一致。这将持续到您调用 ResetProjectionMatrix 为止。
只有在您确实需要非标准投影时才使用自定义投影。此属性由 Unity 的水渲染用于设置斜投影矩阵。使用自定义投影需要您对变换和投影矩阵有很好的了解。
请注意,传递给着色器的投影矩阵可能会根据平台和其他状态而修改。如果您需要从摄像机的投影中计算用于着色器的投影矩阵,请使用 GL.GetGPUProjectionMatrix。
其他资源:Camera.nonJitteredProjectionMatrix
// Make camera wobble in a funky way! using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Matrix4x4 originalProjection; Camera cam;
void Start() { cam = GetComponent<Camera>(); originalProjection = cam.projectionMatrix; }
void Update() { Matrix4x4 p = originalProjection; p.m01 += Mathf.Sin(Time.time * 1.2F) * 0.1F; p.m10 += Mathf.Sin(Time.time * 1.5F) * 0.1F; cam.projectionMatrix = p; } }
// Set an off-center projection, where perspective's vanishing // point is not necessarily in the center of the screen. // // left/right/top/bottom define near plane size, i.e. // how offset are corners of camera's near plane. // Tweak the values and you can see camera's frustum change.
using UnityEngine; using System.Collections;
[ExecuteInEditMode] public class ExampleClass : MonoBehaviour { public float left = -0.2F; public float right = 0.2F; public float top = 0.2F; public float bottom = -0.2F; void LateUpdate() { Camera cam = Camera.main; Matrix4x4 m = PerspectiveOffCenter(left, right, bottom, top, cam.nearClipPlane, cam.farClipPlane); cam.projectionMatrix = m; }
static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far) { float x = 2.0F * near / (right - left); float y = 2.0F * near / (top - bottom); float a = (right + left) / (right - left); float b = (top + bottom) / (top - bottom); float c = -(far + near) / (far - near); float d = -(2.0F * far * near) / (far - near); float e = -1.0F; Matrix4x4 m = new Matrix4x4(); m[0, 0] = x; m[0, 1] = 0; m[0, 2] = a; m[0, 3] = 0; m[1, 0] = 0; m[1, 1] = y; m[1, 2] = b; m[1, 3] = 0; m[2, 0] = 0; m[2, 1] = 0; m[2, 2] = c; m[2, 3] = d; m[3, 0] = 0; m[3, 1] = 0; m[3, 2] = e; m[3, 3] = 0; return m; } }