Search Unity

How to render an object if viewed from straight ahead but not the side?

Discussion in 'Editor & General Support' started by Dreamwriter, Dec 6, 2016.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    In my game the player is in a vehicle with a big windshield in front and smaller windows to the side. I have potentially large objects that I only want visible through the windshield but not the side windows. Any idea how I can do this?

    My first thought was to use a separate camera rendering specific layers and a rendertexture, but that doesn't work if the player's view within the vehicle moves (plus, the game has a VR mode, and the rendertexture would appear 2D).

    My second thought was a separate overlapping camera that only renders over the part of the screen where the windshield is, but I'm not really sure how to mask out a camera that way, and even then, the player's viewpoint shifts and suddenly the mask isn't matching up anymore.

    Does Unity maybe have any sort of 3D masking object I could put outside the side windows that would hide parts of certain objects that enter it?
     
  2. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    What I ended up doing was just using my one camera, and creating a stencil mask (the shape of the vehicle windshield) with a shader that doesn't render anything, just mark the pixels behind the mask. I then altered the shader on the objects to only render pixels at locations marked by the stencil mask. I'm not really a shader guy, but this was surprisingly super simple.