• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by raja-bala · Apr 01, 2015 at 12:24 PM · shaderrenderinggraphicsmatrixmatrix4x4

Convention of matrices passed to shaders

I'm seeing some funny results when passing matrices to shaders manually versus using the built-in shader variables. Unity stores matrices in column-major format, but I'm confused if they continue to be that way when used in the shader.

Let's take _CameraToWorld for instance. When declared in the shader, Unity automatically sets it up. Does the convention of _CameraToWorld differ when using CG/HLSL v/s GLSL?

I wouldn't think so.

I bring this up, because I only noticed today that Unity recommends using gameobject.renderer.localToWorldMatrix instead of gameobject.transform.localToWorldMatrix. I have no idea how my code even works! Does this mean the resulting matrix (using renderer) will be row-major for HLSL/CG and column-major for GLSL?

While we're at it, what's with the weird documentation for Camera.localToWorldMatrix?

Note that camera space matches OpenGL convention: camera's forward is the negative Z axis. This is different from Unity's convention, where forward is the positive Z axis.

Screen space in DX has X going right, Y down and Z into the display, so its RHS (with origin at top left)

Screen space in GL, if I'm not mistaken, is X going right, Y up, and Z into display, so its LHS (with origin at bottom left).

Is Unity talking about some other convention here?

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by spalmer · Jun 30, 2015 at 04:22 PM

Matrix packing convention doesn't differ between platforms in Unity. Now it's been a few weeks since I messed with this so bear with me if I'm in error. It seems their builtin matrices are row-major but not transposed on the CPU, so literally the same matrix on CPU and GPU from code perspective if you used array indexing. Usually most shader examples and systems I've seen require transposition of the matrices on the CPU before passing to the GPU so that the matrix multiply works out one way in shader code, something like mul(matrix, vector). In Unity they don't do the transpose, and they don't declare the matrices in the shader column-major either, so the transformations must be done like mul(vector, matrix) for the desired result. It's very confusing but actually pretty efficient and makes sense once you get used to it.

Camera.localToWorldMatrix is like it says: it uses OGL's LHS system, with +Z coming OUT of the display, towards viewer, versus the RHS convention used in the Unity Editor and for world space, object space, etc.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Set shader builtin matrices by script 0 Answers

[Unity SRP] How do I clear 3d RW RenderTexture? 1 Answer

Unity 5 Custom Deferred shader 0 Answers

How do I make a boat that doesn't let water enter?,How do I make a boat that water cant get in to 2 Answers

Object Emision while behind a black glass 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