Search Unity

View Matrix for cg shaders in Unity

Discussion in 'Shaders' started by Red Phoenix, Apr 6, 2009.

  1. Red Phoenix

    Red Phoenix

    Joined:
    Apr 4, 2009
    Posts:
    21
    Hello guys, how can i read/access the view matrix using shaderlab/cg in unity?

    i found the world matrix and the projection one, i haven't found this one.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    As most of Unity's rendering comes from dark corners of OpenGL (back in the day it was the only renderer), there's no separate view matrix. Just like in OpenGL, model (a.k.a. world) matrix and view matrix are combined into glstate.matrix.modelview[0]
     
  3. Red Phoenix

    Red Phoenix

    Joined:
    Apr 4, 2009
    Posts:
    21
    thx

    indeed you are correct, i just checked the glstate list in nvidia's cgfx manual and i didnt find any separate view matrix listed.

    k, great, i guess i can't do much then if i work with imported cgfx files that use the following missing semantics WORLDVIEWPROJECTION, VIEWINVERSE :x

    i was thinking of writing an inverse 4x4 function then use it to create VIEWINVERSE then do _Object2World*(view matrix)*glstate.matrix.projection for WORLDVIEWPROJECTION.

    oh well


    i guess i can't
     
  4. MuseGames

    MuseGames

    Joined:
    Mar 19, 2008
    Posts:
    98
  5. Red Phoenix

    Red Phoenix

    Joined:
    Apr 4, 2009
    Posts:
    21
    cool man, thx, i'll try it and let u know :)
     
  6. Red Phoenix

    Red Phoenix

    Joined:
    Apr 4, 2009
    Posts:
    21
    ok, i set up the update section of the camera attached script to send to the shader any custom matrix i want, it's working but i'm still not sure which combinations of matrices i should send to have the correct visual shading effect.

    i need this view matrix to import correctly a shader from fxcomposer.

    sending camera.worldToCameraMatrix * camera.projectionMatrix is giving way off target results, sending the following gives me much closer visuals to the shader preview i see in fxcomposer :

    in the camera script :

    function Update () {
    var Iview=camera.cameraToWorldMatrix.inverse;

    Shader.SetGlobalMatrix("_MyIview", Iview.inverse);
    }

    in the shader cg :

    uniform float4x4 _MyIview;

    float4x4 wvp = glstate.matrix.mvp;
    float4x4 worldI = _World2Object;
    float4x4 viewInv = _MyIview;
    float4x4 world = _Object2World;

    ----

    can't any Unity3d developer clarify if what i'm doing is possible to begin with and if so what do i need to send exactly here to get the view matrix into the shader?
     
  7. sendel76

    sendel76

    Joined:
    Mar 17, 2010
    Posts:
    36
    You should try
    gl_ProjectionMatrix * camera.worldToCameraMatrix
    instead of
    camera.worldToCameraMatrix * gl_ProjectionMatrix
     
  8. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @sendel76 , what part of the code are you referencing when you say to try that?
     
  9. foxes

    foxes

    Joined:
    Oct 9, 2015
    Posts:
    10
    I use UNITY_MATRIX_V and UNITY_MATRIX_P separately in the calculations for DrawProceduralIndirect but they do not correspond to the UNITY_MATRIX_VP multiplication. (Unity5)