Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Regarding UNITY_MATRIX_MVP and UnityObjectToClipPos...

Discussion in '5.6 Beta' started by zeroyao, Mar 14, 2017.

  1. zeroyao


    Unity Technologies

    Mar 28, 2013
    Hey dear beta testers,

    TLDR; If you are still worrying or was worrying about manually replacing all the existing UNITY_MATRIX_MVPs with UnityObjectToClipPos calls, especially for those shaders you published on asset store, NO MORE!

    // longer version starts here

    Recently in 5.6 betas Unity scans your shaders and prints warnings on detection of any usage of UNITY_MATRIX_MVP, urging you to replace it with the built-in function UnityObjectToClipPos. This caused confusion and inconvenience especially to asset store authors.

    We do this new change because in 5.6 and future, UNITY_MATRIX_MVP is always calculated by multiplying two matrices, UNITY_MATRIX_VP and unity_ObjectToWorld (previously we only do this in stereo rendering and instancing shaders). If the purpose of using it is only to transform a vertex into clip space, it is cheaper to use UnityObjectToClipPos() function, which does 2 matrix-vector multiplications, instead of 1 matrix-matrix multiplication and 1 matrix-vector multiplication.

    This made the code hard to be clean and optimal in both 5.6 and before. To address this, we introduced an automatic shader upgrader in RC2 (not officially in yet, but is on track), replacing "mul(UNITY_MATRIX_MVP, vector)" with "UnityObjectToClipPos(vector)" (vector can be an arbitrary expression). With this change, any existing shaders will be automatically transformed to the most efficient form without special actions.

    P.S. We don't do the same thing for UNITY_MATRIX_MV because unfortunately UnityObjectToViewPos returns a float3 which doesn't guarantee a successful compile after the replacement.

    P.S.2 UnityObjectToClipPos now is made available all the time, even if UnityCG.cginc is not included.

    Sorry for the trouble!

  2. CDF


    Sep 14, 2013
    A thousand thanks
  3. Kronnect


    Nov 16, 2014
    Cool. I missed this post and had just requested something similar. Much appreciated!