- Home /
Set World Camera Position in View Matrix
I need the MVP matrix for a shader to be centered around the camera therefore the camera position that is used to create the view matrix has to be set to (0,0,0), is there an easy way to modify the view matrix without recalculating it entirely so that it will consider the camera at the world origin?
Answer by Owen-Reynolds · Mar 20, 2015 at 04:22 PM
Unity uses just regular graphics matrixes, so:
http://www.codinglabs.net/article_world_view_projection_matrix.aspx
About a third of the way down it shows where the translation parts are. This was just the first search result, so you can try 2nd, 3rd ... if you don't like the way they explain it.
Taking what I learned from that and a few other places I came up with this code but it doesn't seem to work:
$$anonymous$$atrix4x4 $$anonymous$$ = transform.localToWorld$$anonymous$$atrix;
$$anonymous$$atrix4x4 V = Camera.main.worldToCamera$$anonymous$$atrix;
V.SetColumn(3, new Vector4(0, 0, 0, 1));
$$anonymous$$atrix4x4 P = Camera.main.projection$$anonymous$$atrix;
renderer.material.Set$$anonymous$$atrix ("_Custom$$anonymous$$VP", P * V * $$anonymous$$);
EDIT: Sorry never $$anonymous$$d my error was in my vertex shader, code works perfectly now.
Your answer
Follow this Question
Related Questions
Camera View Matrix in Script 1 Answer
Viewer position lost in Space. How to get back to scene? 1 Answer
How do I calculate a view matrix using Matrix4x4.LookAt? 2 Answers
Phone doesn't show what my unity camera is showing. 1 Answer
How can I know if a gameObject is 'seen' by a particular camera? 11 Answers