• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by nonopblic · Oct 18, 2017 at 09:07 AM · shadergraphicsshader programmingshaderlabcg

Can I see the calculation of unity_MatrixVP ?

I would like to know what is unity_MatrixVP.
I searched it in builtin shader files, whose version is 2017-1-2f1.

I found the UnityShaderVariables.cginc(20)

 #if defined(USING_STEREO_MATRICES)
     #define unity_MatrixVP unity_StereoMatrixVP[unity_StereoEyeIndex]    

UnityShaderVariables.cginc(178)

 #if defined(USING_STEREO_MATRICES)
   CBUFFER_START(UnityStereoGlobals)
       float4x4 unity_StereoMatrixVP[2];

and

UnityShaderVariables.cginc(226)

 #if !defined(USING_STEREO_MATRICES)
     float4x4 unity_MatrixVP;

But I could not found how Unity calculate unity_StereoMatrixVP and unity_MatrixVP.
Is is possible to see not the declaration but the definition of unity_MatrixVP ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Oct 18, 2017 at 11:19 AM

The VP matrix the the combination of the ViewMatrix (also known as Camera matrix) and the ProjectionMatrix of that camera. So this matrix transforms a point from worldspace directly into viewport space (-1 to 1).

The View or Camera matrix is basically just the WorldToLocal matrix of the gameobject where the camera is attached to. However the camera matrix is doing the conversion from left-handed to right handed space so it's scale is inverted on the z axis. You can access / set the "V" matrix by accessing Camera.worldToCameraMatrix. However for stereo view (VR / AR stuff) you actually have two of these, one for each eye.

The projection matrix converts the positions from camera space into viewport space (or clipspace). The final VP matrix is calculated by simple combining the matrices. Keep in mind that the order in which you combine matrices matters.

 Matrix4x4 VP = ProjectionMatrix * ViewMatrix;

The matrices are taken from the camera(s) involved. Unity sets the external shader variables automatically before rendering the corresponding camera. That's actually the main point of a "Camera". A Camera just represents rendering parameters.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CaptainScience · Aug 19, 2019 at 05:26 PM 1
Share

An important note is that because Unity supports so many platforms this answer is only mostly correct. Before you combine the matrices you have to run the projection matrix through GL.GetGPUProjection$$anonymous$$atrix() to convert it from the default OpenGL convention Unity uses in the scene to the proper conventions for whatever API is being used under the hood.

If forget that step, you can get things like weird upside-down/inside-out rendering on certain platforms.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access another sprite's texture from a 2D shader 0 Answers

Flat Shading / No Vertex Interpolation 2 Answers

How to achive the same look? Which shaders to use? 2 Answers

Single Pass Instanced - Hololens shader help 0 Answers

How to Fade out Custom Unity Shader ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges