Search Unity

Help Wanted Unity Matrix P and matrix MV equlivant

Discussion in 'Shader Graph' started by animator84, Jun 11, 2019.

  1. animator84

    animator84

    Joined:
    Jan 10, 2018
    Posts:
    14
    Hi , I'd like to make a billboard shader with shader graph, i've come across various examples, but this one is probably closes to what i want

    https://en.wikibooks.org/wiki/Cg_Programming/Unity/Billboards

    But can i now what is the equlivant of "
    UNITY_MATRIX_P" and "
    UNITY_MATRIX_MV" in shader graph? and what are they in general?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,658
    https://docs.unity3d.com/Packages/com.unity.shadergraph@6.7/manual/Transformation-Matrix-Node.html

    _P is the projection matrix, or the matrix used to transform from view position to effectively the screen position (actually homogeneous clip space).

    _MV is the model view matrix, or the matrix that transforms from the local object space to view space. Unity doesn’t actually pass this matrix to the shader from the game code, so it’s created in the shader by multiplying the model (object to world) matrix and view matrix together. But it’s actually cheaper to multiply a position by the model and then then the view matrix separately.
     
  3. animator84

    animator84

    Joined:
    Jan 10, 2018
    Posts:
    14
    I tried to duplicate the setup on the wiki along with the docs you've sent me into this : But I still can't get the billboard setup. would you happen to know what is the problem here? (link for my full shader graph)

    https://monosnap.com/file/TyKlqS8BkYTnOC2KyOFW2dgAbijzkX

    I'm basically trying to re-create this :

    vertexOutput vert(vertexInput input)
    {
    vertexOutput output;

    output.pos = mul(UNITY_MATRIX_P,
    mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
    + float4(input.vertex.x, input.vertex.y, 0.0, 0.0)
    * float4(_ScaleX, _ScaleY, 1.0, 1.0));

    output.tex = input.tex;

    return output;
    }
     
  4. LizardBrain451

    LizardBrain451

    Joined:
    Apr 22, 2019
    Posts:
    10
    animator84,

    This should give you what you need. Credit bGolus for the answer, but my shader code and newness to shader graph combined to make me walk through it.

    I am uncertain whether it is the best answer, but it works.

    upload_2019-6-11_9-4-32.png
     
  5. animator84

    animator84

    Joined:
    Jan 10, 2018
    Posts:
    14
    Hi LizardBrain451, thank you very much for your reply, I've tried replicating your setup but i still couldn't get it to work.Maybe it is a shader graph difference, my "transform matrix" node doesn't have an "object to world" drop down:
    shadergraph01.png

    So after some googling, and incorporating Bgolus's reply, I got this, but the results were really wonky. Is there something that I did wrong here? Appreciate any advice ~~ shadergraph02.png
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,658
    Shader Graph’s position expects an object space position, so it’s taking the value you pass in and applying the ObjectToClipPos() function to it, transforming it from object to clip space. If the position is already in clip space, that’s going to be really weird looking.

    What you need to do is get the billboard position in world or view space, then transform it back into object space.

    The short version would be:
    Position in object space * scale, convert to Vector4 with a w (A) of 0.0. Multiply that by the Inverse View matrix (the matrix needs to be the A input, position as B). Add the Object node’s position. Then use the Transform node to convert from world to object.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,658
    upload_2019-6-11_23-38-27.png

    This might not work if you're using the HDRP, but I think it should since it's not using the Position node set to World Space or in the fragment shader stage, both of which cause some problems with those later matrix, transform, and object nodes.
     
  8. LizardBrain451

    LizardBrain451

    Joined:
    Apr 22, 2019
    Posts:
    10
    I am curious why you aren't seeing the same parameters for the transformation matrix node animator84? Which version are you running? I am using HDRP and I am running Unity 2018.2.2.

    but here are the results, with an image pipped in, the version on the right is scaled 2x

    upload_2019-6-12_9-3-13.png
     
    Last edited: Jun 12, 2019
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,658
    The object to world and world to object are alternatively names for the model and inverse model matrices. I believe they’ve changed the names used in different versions of Shader Graph... they’re both equally correct, though the former is more easy to understand.
     
  10. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    1,628
    Wondering if someone can please help me with this example. I tried to re-crate it but i just cant seem to get it working

    upload_2020-12-3_17-45-35.png
     
  11. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,747
    This was an old thread but I had problems using that kind of method.
    Trying to make a billboard shader with shader graph seems a bit more trouble than writing a simple shader in the 'old days'. Less examples for one thing
    You have awful nodes like 'Split' which do not even have xyz versions!
    Maybe some custom nodes could better combine some of the graph parts required and billboards should come as examples anyway.

    I made a billboard one, that takes a non-scaled quad or other flat xy-plane mesh, & makes it face the camera, not just the camera plane (can be better or worse - always points at camera position)
    The problem with changing vertex positions in the shader graph is the mesh bounds become wrong - would be nice if you can handle that somehow in the shader graph.

    bb.png
     
    Gekigengar likes this.
  12. Nethan2000

    Nethan2000

    Joined:
    Jul 26, 2019
    Posts:
    1
    This works wonderfully with normal materials, but when I'm trying to enable emission, the billboard only shines when viewed from the negative Z direction and transitions into a non-emissive material when displayed from the other side. Do you have any idea what causes it? All other approaches that I've tried have the same issue.

    Sorry for resurrecting the thread, but this seems to be the best place to ask. I'm using Unity 2020.3.7f1.

    EDIT: I just noticed you're using Unlit shader. It looks that I took some shader graph inspired by your post but adapted for Lit materials. I'm trying to use the billboard for stars with a visible halo, so I need emission. The 3D sphere with this material works great but I have problems when making a lower LOD version.
     
    Last edited: May 22, 2021
  13. Double-V

    Double-V

    Joined:
    Aug 13, 2016
    Posts:
    21
    How would one go about doing the same thing, except only rotate the object on the Y axis, but still make it so gameObject rotation doesn't mess with it? I succeeded in making a Y-only billboard shader in a completely different way, but it broke whenever the rotation of the gameObject wasn't 0, whereas here I think the transformation matrix node helps with canceling out the rotation in some way, but I don't know what to tweak here to make it only rotate on Y.
     
unityunity