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

How to Get Angel between Two Vectos with atan2 in shader graph?

Discussion in 'General Discussion' started by funny123456, Mar 3, 2022.

  1. funny123456

    funny123456

    Joined:
    Sep 20, 2019
    Posts:
    14
    Hi everyone. I hope someone can give me an example of finding the angle between two vectos using arctangent 2 in shader graph. I would like to have the result in degree. This is for a billboard shader. Really appreciate your help!
     
  2. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Did you try searching for it in the nodes?
     
  3. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,801
    Wait, why? Why would you need degrees specifically?
     
  4. funny123456

    funny123456

    Joined:
    Sep 20, 2019
    Posts:
    14
    Actually radians is fine too. But the thing is I don't really know how to get the angle between two vectors in shader graph and that angle should have a range of 0-360 degree. So hoping to get some help here.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You don't need to use any angles to make a billboard shader. You can grab base vectors from camera or store them as parameers, then you can build object matrix using cross products.

    You also can't really measure angle with atan, and would need acos. And acos goes to 180 degrees only.

    To build a billboard matrix, you need Up vector, camera position, object position. Up vector could be extracted from thecamera (if it is screen aligned billboard), or it could be pointing up, if iti s something like a tree or grass tile.

    First you take vector from object to cam. With cross product
    right = Vector3.Cross(up, vecToCam)
    , you'll get a vector pointing right. Using another cross product you can get a vector pointing forward
    forward = Vector3.Cross(up, right)
    ;
    When you have position (of the billboard), up, right and forward, that forms a transformation matrix for that object.

    No angles invovled.
     
  6. funny123456

    funny123456

    Joined:
    Sep 20, 2019
    Posts:
    14
    Hey. You are awesome! Thank you so much. That's nice and clean. Don't know why I keep thinking using angle and rotation.
     
    neginfinity likes this.
  7. funny123456

    funny123456

    Joined:
    Sep 20, 2019
    Posts:
    14
    Using atan2 function is also a way to go.