갱스
[Unity] Camera Projection Matrix 본문
Unity에서 camera의 projection matrix를 다루는 간단한 예제이다
Unity에서는 projection matrix를 수정한다고 해서 camera transform 객체의 position이나 rotation값이 바뀌지 않는다.
game view에 화면이 뿌려지기 직전에 projection matrix가 반영되므로, transform 객체와는 아예 별개라고 생각하면 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class CamMatrixControl : MonoBehaviour | |
{ | |
public float rotate_angle = 0; | |
public float scale = 1; | |
public Vector2 shearing = Vector2.zero; | |
public Vector2 translation = Vector2.zero; | |
private Matrix4x4 MatRotate = Matrix4x4.identity; | |
private Matrix4x4 MatScale = Matrix4x4.identity; | |
private Matrix4x4 MatXShearing = Matrix4x4.identity; | |
private Matrix4x4 MatYShearing = Matrix4x4.identity; | |
private Matrix4x4 MatTranslation = Matrix4x4.identity; | |
private Matrix4x4 OriMatProjection; | |
private Camera cam; | |
// Use this for initialization | |
void Start() | |
{ | |
cam = Camera.main; | |
OriMatProjection = cam.projectionMatrix; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
MatScale[0, 0] = scale; | |
MatScale[1, 1] = scale; | |
MatRotate[0, 0] = Mathf.Cos(rotate_angle * Mathf.Deg2Rad); | |
MatRotate[0, 1] = -Mathf.Sin(rotate_angle * Mathf.Deg2Rad); | |
MatRotate[1, 0] = Mathf.Sin(rotate_angle * Mathf.Deg2Rad); | |
MatRotate[1, 1] = Mathf.Cos(rotate_angle * Mathf.Deg2Rad); | |
MatXShearing[0, 1] = shearing.x; | |
MatYShearing[1, 0] = shearing.y; | |
MatTranslation[0, 2] = translation.x; | |
MatTranslation[1, 2] = translation.y; | |
cam.projectionMatrix = OriMatProjection * | |
MatScale * | |
MatRotate * | |
MatXShearing * | |
MatYShearing * | |
MatTranslation; | |
} | |
} |
'Computer Vision' 카테고리의 다른 글
IPython Notebook에서 OpenCV image 출력 (0) | 2015.08.05 |
---|---|
[OpenCV] Blob Labeling 예제 (0) | 2015.07.21 |
Comments