Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Camera.projectionMatrix

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public var projectionMatrix: Matrix4x4;
public Matrix4x4 projectionMatrix;
public projectionMatrix as Matrix4x4

Description

Set a custom projection matrix.

If you change this matrix, the camera no longer updates its rendering based on its fieldOfView. This lasts until you call ResetProjectionMatrix.

Use a custom projection only if you really need a non-standard projection. This property is used by Unity's water rendering to setup an oblique projection matrix. Using custom projections requires good knowledge of transformation and projection matrices.

	// Make camera wobble in a funky way!
	var originalProjection : Matrix4x4;
	originalProjection = camera.projectionMatrix;
	
	function Update () {
		var p : Matrix4x4 = originalProjection;
		// change some values from the original matrix
		p.m01 += Mathf.Sin (Time.time * 1.2) * 0.1;
		p.m10 += Mathf.Sin (Time.time * 1.5) * 0.1;
		camera.projectionMatrix = p;
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Matrix4x4 originalProjection;
    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;
        camera.projectionMatrix = p;
    }
    void Example() {
        originalProjection = camera.projectionMatrix;
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public originalProjection as Matrix4x4

	def Update() as void:
		p as Matrix4x4 = originalProjection
		p.m01 += (Mathf.Sin((Time.time * 1.2F)) * 0.1F)
		p.m10 += (Mathf.Sin((Time.time * 1.5F)) * 0.1F)
		camera.projectionMatrix = p

	def Example() as void:
		originalProjection = camera.projectionMatrix

	// 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.

@script ExecuteInEditMode

var left : float = -0.2; var right : float = 0.2; var top : float = 0.2; var bottom : float = -0.2;

function LateUpdate () { var cam : Camera = camera; var m : Matrix4x4 = PerspectiveOffCenter( left, right, bottom, top, cam.nearClipPlane, cam.farClipPlane ); cam.projectionMatrix = m; }

static function PerspectiveOffCenter( left : float, right : float, bottom : float, top : float, near : float, far : float ) : Matrix4x4 { var x : float = (2.0 * near) / (right - left); var y : float = (2.0 * near) / (top - bottom); var a : float = (right + left) / (right - left); var b : float = (top + bottom) / (top - bottom); var c : float = -(far + near) / (far - near); var d : float = -(2.0 * far * near) / (far - near); var e : float = -1.0;

var m : Matrix4x4 = 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; }
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;
        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;
    }
}
import UnityEngine
import System.Collections

[ExecuteInEditMode]
public class ExampleClass(MonoBehaviour):

	public left as float = (-0.2F)

	public right as float = 0.2F

	public top as float = 0.2F

	public bottom as float = (-0.2F)

	def LateUpdate() as void:
		cam as Camera = camera
		m as Matrix4x4 = PerspectiveOffCenter(left, right, bottom, top, cam.nearClipPlane, cam.farClipPlane)
		cam.projectionMatrix = m

	static def PerspectiveOffCenter(left as float, right as float, bottom as float, top as float, near as float, far as float) as Matrix4x4:
		x as float = ((2.0F * near) / (right - left))
		y as float = ((2.0F * near) / (top - bottom))
		a as float = ((right + left) / (right - left))
		b as float = ((top + bottom) / (top - bottom))
		c as float = ((-(far + near)) / (far - near))
		d as float = ((-((2.0F * far) * near)) / (far - near))
		e as float = (-1.0F)
		m as Matrix4x4 = 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