Search Unity

Question Trying to overlay UI on main camera

Discussion in 'VR' started by PenProd, Jan 18, 2023.

  1. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    So I'm trying to do something fairly simple. When a player "clicks" a specific object, a keypad should appear as a UI item which he can then interact with.

    First thing I tried, was create a Canvas, add a panel (color black, alpha set to 50%) and also added a 3D object of a keypad to the canvas. I set the render mode of the canvas to "Screen Space - Camera" and dragged my main camera to the "Render Camera" field of the canvas.

    This works but has a few disadvantages. The main disadvantage being that the canvas and its child objects receive lighting from the scene. And I always want it to be fully lit.

    So instead, I created a second camera. I set my canvas' layer to "UI". I set the Culling Mask of the new camera to UI only, and remove UI from the Culling mask of the main camera. So the new camera sees the UI objects only, and my main camera sees everything but the UI object. According to just about every tutorial I've found on the interwebs, this should work. It doesn't.

    When I run the scene, I only see the UI. I can change the values of the priority field, but I always get to see either camera, never both.
     
  2. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    Apparently, all tutorials out there on this subject are out-dated. I just found out about camera-stacking, and although that works, it is a huge performance hit. My frame rate on VR halves whenever I stack cameras. So I'm hoping I'm doing something wrong. So if anyone cares to explain what the proper way of doing this is, that would be greatly appreciated.
     
  3. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    Having read a bit more, stacking cameras is not the way to go on Android/Mobile devices due to the huge performance hit. So allow me to repeat what I want:

    When a player clicks on a specific object, the scene should get darker and a large keypad should appear that stays fixed in the center of the player's view.

    This method works without any drop in performance. But it has two major disadvantages. The first being that the keypad receives lighting from the scene (while I want the keypad to be always fully lit), and second, the entire canvas clips through walls and objects while I always want it to be rendered on top of everything else.

    So what's the proper way to do this?
     
    Last edited: Jan 19, 2023
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Have you tried this?

     
  5. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    Yes. Unfortunately, this is no longer applicable with Unity 2021+. I ended up adding RenderObjects to the shader where it skips depth checks (for this layer only). I had to do that for all shaders used in the keyboard. This did not work for the text on the display though since the shaders for that (TextMeshPro) don't offer the ability to add RenderObjects (since that's a URP feature only?). Fortunately, TextMesh Pro offers an overlay shader, so I just used that. So now it works.
     
  6. slimshader

    slimshader

    Joined:
    Jun 11, 2013
    Posts:
    187
    Fo other that will get here: It is totally applicable in Unity 2022 and even works well in VR