Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Surface shaders and UNITY_MATRIX_MVP

Discussion in 'Shaders' started by ivkoni, Mar 13, 2011.

  1. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Hi friends,

    Where is the multiplication by UNITY_MATRIX_MVP done in surface shaders?
    I would like to overwrite it. Is there a way I can do this in a surface shader?
    If not possible, does someone still have the old shaders source code?

    http://unity3d.com/support/old-resources/assets/built-in-shaders has the new shaders only :(
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    you can have your own vertex function, so you can output whatever you want for your surface frag yourself. No need to mess with other stuff.
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Thank you Aubergine, but since there is no example, I don't know how to do it :)
    I know that I can have my own vertex function, but unity will multiply by UNITY_MATRIX_MVP behind the scenes it seems?
    Ideally, I want to multiply by my own matrix and not UNITY_MATRIX_MVP.
    I guess I could manipulate the vertices by something like inverse of UNITY_MATRIX_MPV * MATRIX_I_WANT_TO_USE
    if I want to use surface shaders.
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    what exactly do you want to do?
     
  5. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    In a vertex shader, I want to say:
    Code (csharp):
    1.  
    2. v2f vert(a2v IN)
    3. {
    4.    v2f OUT;
    5.     OUT.pos = mul(_ProjMatrix, IN.vertex);
    6.     return OUT;
    7. }
    8.  
    where _ProjMatrix is my own MVP matrix that I supply through script.

    Can I do this in a surface shader?
    I know that in a surface shader I can manipulate the vertices like so:
    Code (csharp):
    1.  
    2.   void vert (inout appdata_full v) {
    3.     v.vertex = mul(_ProjMatrix, v.vertex);
    4.       }
    5.  
    but unity will then multiply by UNITY_MATRIX_MPV after that.
    Like I said above, I can make _ProjMatrix be (Inverse of UNITY_MATRIX_MPV) * MATRIX_I_WANT, so when unity mulpilies it will end up using MATRIX_I_WANT, but I don't want to do this, since I am afraid I will run into precision problems by taking the inverse.

    I hope this is clear.
     
  6. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    I'm trying something similar. I'm not there yet. The closest I've gotten is setting up a script on a camera, and in OnPreRender, I have::


    Code (CSharp):
    1.       Matrix4x4 world = transform.localToWorldMatrix;
    2.         Matrix4x4 view = _cam.worldToCameraMatrix;
    3.         Matrix4x4 proj = _cam.projectionMatrix;
    4.  
    5.  
    6.         Matrix4x4 gpuProj = GL.GetGPUProjectionMatrix(proj, renderTextureIsTarget);
    7.         Matrix4x4 gpuMV = view * world; // UNITY_MATRIX_MV
    8.         Matrix4x4 gpuMVP = gpuProj * view *world; // UNITY_MATRIX_MVP
    9.  
    10.    
    11.         _inverseMVP = gpuMVP.inverse;
    12.         Shader.SetGlobalMatrix(_inverseMVPHash, _inverseMVP);
    [/INDENT]

    But unfortunately, when I use this in a surface shader:

    Code (CSharp):
    1.  
    2.         void vert (inout appdata_full v) {
    3.                  float4 modelToCamera =  mul(UNITY_MATRIX_MVP, v.vertex);
    4.                 float4 camAndBackAgain = mul(_InverseMVP, modelToCamera);
    5.              
    6.                 v.vertex = camAndBackAgain;
    7.              
    8.         }
    (This just applies MVP and then unapplies it, I hope? It's just a sanity check)

    It seems to add a couple of 90 degree turns somewhere?
     
    Last edited: Sep 27, 2016