Search Unity

Question How to calculate whether or not a certain object is placed "in between" the camera and the player?

Discussion in 'Shader Graph' started by DAPPSD, Sep 19, 2019.

  1. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    I want to check in my Shader if the object is placed between the player and the camera (meaning that the player is hidden or partially hidden behind the object) and I want it to work on all the axis (meaning that it'll work even if the camera is pointing up and is in Y = 0 and the object is in Y = 1 and the player is in Y = 2)...

    I already know how to get all these objects' positions info inside my shader, what I need is a simple way of calculating everything... I tried playing around with dot product and normalizing stuff but nothing really worked...
    BTW I don't want to use raycasts...

    I made a simple 2D thingy in Microsoft paint to make my question clearer...
    Basically I want the know if the object is colliding with the dotted rectangle..
    BTW it's 2D image but I want it to work in 3D space..
     
    Last edited: Sep 19, 2019
    keeponshading likes this.
  2. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    I tried to compare and see if CamToObject distance is less than the PlayerToCam distance, but for some reason it always true...
    BTW I get the player's position via script, the camera position from the camera node, and the object's position from the position node, and to find the distance between vectors I use the distance node, and to compare the distances I use the comparison node... am I doing everything right?
     
    Last edited: Sep 20, 2019
  3. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    help? :(
     
  4. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi, I'm pretty sure you've got the logic and this is what I would do. Is this not working ? Could you post some screenshots and script if that did not work ?
     
  5. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    No it doesn't work...
    Here's the graph:


    It's supposed to change the objects' colors to red if they are closer to the camera than the player, but they are almost always true and red even if they are behind the player...
     
  6. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi,

    Meybe there is a simpler way, and I'm sure there will be more expert advice, but in the meantime, could you try this and see if it suits you?

    I used a View Direction node to calculate two individual targets (Player & Target (your Object)) instead of only one. The I compared both outputs in the comparison nodes.

    upload_2019-9-22_22-5-15.png
     
    deXter_969 likes this.
  7. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    Thanks, that would be a great solution, but not for my specific case, in my case there are a lot of targets not just one... (almost all the objects in the scene will have that shader, that's one of the reasons why I want to do this calculation and color change in the shader rather than the script)
     
  8. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    still need help :)
     
  9. FlorentFal

    FlorentFal

    Joined:
    Nov 20, 2015
    Posts:
    55
    Hi, what about this solution ?
    • In a MonoBehaviorScript doing on update (or fixed updat) a Physic.RayCastAll from the camera to you player
    • Process the "hit" list to get a list of renderer
    • Then on each renderer use the Material.SetInt() API to set the value of a "betweenCameraAndPlayer" property (1: object is between camera and player, 0: object is not between camera and player)
    • use the betweenCameraAndPlayer value in the shader to change the rendering
    To set back the betweenCameraAndPlayer property value to 0 when an object is no more between the camera and the player, the Monobehavior script would keep in memory the list of hit renderer and when a renderer does not exist anymore in the new hit list, set its "betweenCameraAndPlayer" prop to 0.
     
    Last edited: Oct 4, 2019
    deXter_969 likes this.
  10. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    That's a great solution, but I want to make this work only with the shader graph and with no raycasts whatsoever..
     
  11. dansitnick

    dansitnick

    Joined:
    Jun 9, 2016
    Posts:
    14
    Do you want the whole object to have the same color? If so, then you have to use raycasts, and there's really no good reason to avoid them. @FlorentFal suggested a highly performant solution.
     
  12. Chewico

    Chewico

    Joined:
    May 9, 2020
    Posts:
    2
    Hello, a solution can be:
    if you have the difference between the camera and the player as: Player - Camera, and divide x / y
    then calculate the difference between the object and the camera as: objec - camera (all in vectors) and divide x / y of the object, and compare it, if it is close to it it will mean that they are in the same direction

    upload_2020-5-26_14-39-59.png
    So disable the effect if the magnitude of the object is greater than the magnitude of the player