Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
5
Question by Daniel-Brauer · Mar 07, 2010 at 05:24 PM · transformprojection-matrix

How do I reproduce the MVP matrix?

I'm trying to reproduce glstate.matrix.mvp. Does this look right?

objectTransform.localToWorldMatrix* 
cameraTransform.worldToLocalMatrix* 
cameraTransform.projectionMatrix

Because it doesn't work if I use it in a vertex shader instead of glstate.matrix.mvp. The object disappears entirely and I can't find it any more.

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

3 Replies

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

Answer by Daniel-Brauer · Mar 12, 2010 at 05:19 AM

So it turns out I had two problems. The first should be obvious to any graphics programmer: my matrices were in reverse order. I didn't stop to think that even though I is called the MVP matrix, the actual order of the matrices in terms of matrix multiplication is PVM, as they are left-multiplied with any given vector.

The second problem was far more difficult to figure out: I needed to switch the sign on the third and fourth entries in the third row of the projection matrix. No idea why, but if you don't do this, your camera will be looking backwards and have its coordinate system flipped. This is on a Mac in OpenGL, by the way. I haven't tried it on Windows yet.

Update: I have since talked to Aras about exactly what to do and why. Here are the values I used. It works on both Mac OS and Windows.

Further Update: I made the following code into actual C# to avoid confusion.

bool d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
Matrix4x4 M = transform.localToWorldMatrix;
Matrix4x4 V = camera.worldToCameraMatrix;
Matrix4x4 P = camera.projectionMatrix;
if (d3d) {
    // Invert Y for rendering to a render texture
    for (int i = 0; i < 4; i++) {
        p[1,i] = -p[1,i];
    }
    // Scale and bias from OpenGL -> D3D depth range
    for (int i = 0; i < 4; i++) {
        p[2,i] = p[2,i]*0.5f + p[3,i]*0.5f;
    }
}
Matrix4x4 MVP = P*V*M;
Comment
Add comment · Show 3 · 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 taoa · Jan 06, 2011 at 04:16 PM 0
Share

Left-handed coordinate system when math functions are right handed?

avatar image bam93 · Jan 08, 2011 at 11:41 PM 0
Share

Thank you! I had to change it a bit to make it work without error, and I have a doubt how to use it properly (seems not to work). I updated my post below.

avatar image bret_alfieri · Jan 09, 2017 at 09:57 PM 0
Share

Surprisingly GL.GetGPUProjection$$anonymous$$atrix(camera.projection$$anonymous$$atrix) does not do this.

avatar image
0

Answer by bam93 · Jan 06, 2011 at 09:23 AM

Hi, could you please post the final working code? I have the very same problem and would appreciate it very much :)

Update: Using the posted code I did some tests. First, to make the code run/compile without errors I had to change it as follows:

d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
M = transform.localToWorldMatrix;
V = camera.worldToCameraMatrix;
P = camera.projectionMatrix;
if (d3d) {
// Invert Y for rendering to a render texture
for ( i = 0; i < 4; i++) { P[1,i] = -P[1,i]; }
// Scale and bias from OpenGL -> D3D depth range
for ( i = 0; i < 4; i++) { P[2,i] = P[2,i]*0.5 + P[3,i]*0.5;}
}
MVP = P*V*M;

Just let me briefly explain what I am trying to do. In order to make a Cg shader work properly in Unity, I absolutely need the ModelViewProjection.Inverse matrix. It is not available in Unity (for compatibility reasons with D3D if I understand correctly). So what I am trying to do is to calculate this matrix in a script and then provide it to my shader. The shader has a simple cube as input mesh and ray-casts a sphere from this. So my precise question is, what has transform and camera to be in my case. I tried the following, but I do not get the expected result (eg the sphere, which works fine outside of unity using mvp.inverse):

function Update () {
d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
M = GameObject.Find("Cube1").transform.localToWorldMatrix;
V = Camera.main.worldToCameraMatrix;
P = Camera.main.projectionMatrix;
if (d3d) {
// Invert Y for rendering to a render texture
for ( i = 0; i < 4; i++) { P[1,i] = -P[1,i]; }
// Scale and bias from OpenGL -> D3D depth range
for ( i = 0; i < 4; i++) { P[2,i] = P[2,i]*0.5 + P[3,i]*0.5;}
}
MVP = P*V*M;
Shader.SetGlobalMatrix("_matMVPI", MVP.inverse);
}

This code is run in a script attached to the main camera, but I guess it doesn't matter to what I attach the script. The Cg shader accesses _matMVPI in its vertex shader using

float4x4 ModelViewProjI = _matMVPI;

I wonder whether I use the right M, V and P matrices, eg M from the cube's transform and V, P from the main camera. Of course it's always difficult to debug what's going on inside the shader. I tried to multiply mvp and its (supposed) inverse, and change the color if eg the 1st matrix element is 1.0, but that doesn't work out (which is in agreement with the fact that the sphere doesn't render as it should).

Comment
Add comment · Show 2 · 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 Daniel-Brauer · Jan 07, 2011 at 10:22 PM 0
Share

I've updated my answer above.

avatar image The_Morten · Jun 12, 2014 at 07:04 AM 0
Share

You should be able to call inverse(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP) to get the inverse model view matrix directly in the shader?

EDIT: This of course assumes you are using CGPROGRA$$anonymous$$ for the shader, not ShaderLab.

avatar image
0

Answer by MUGIK · May 26 at 03:07 PM

This worked for me:

 Matrix4x4 GetVPMatrix ( Camera cam )
     => GL.GetGPUProjectionMatrix(cam.projectionMatrix,true) * cam.worldToCameraMatrix;
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Third person Camera stop it from going lower than the terrian 0 Answers

Snap Back? 1 Answer

How would i rewrite this script with a Character Controller? C# 1 Answer

Selecting transform in FBX hierarchy csharp script 1 Answer

respawn delay 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges