Search code examples
unity-game-engineoculus

How to fix the rendering of laser with respect to the UI canvas


Added some UI elements inside a canvas in world-space render mode. There is an issue with the occlusion of UI elements with respect to the laser pointer. The pointer here is made from a line renderer. Here are the things that have been tried and still the pointer does not properly occlude the canvas.

  • changing the hierarchy
  • changing the z-order
  • changing the sorting order

The source code is from Unity and is available here. There is no related script that requires modification. Seems like a setting or shader change might only be required here.

Laser occlusion with UI


Solution

  • It is possible to write a custom shader to test for depth, but this would be a very expensive shader - and it looks like you want to use it on VR, and I would not recommend to use it like so.

    Over-engineered solution and still not very cheap but cheaper solution would be to render UI onto a camera that renders into RenderTarget and passes the input to the UI.

    1. Create a camera - with Redner Target output
    2. Create canvas that uses Screen Space - Camera
    3. Attach the camera to the Canvas
    4. Put Plane in world space and use RenderTarget as material
    5. Add a script that would allow passing RayCasts through RenderTarget to UI. Possible soltuion

    Few notes:

    • Camera needs to be disabled for as long as it is possible to save performance and only updated via Camera.Render() when UI is updated.
    • This solution would still be expensive
    • Adds a lot of complexity to a simple problem
    • Adds the possibility to "wrap" UI around meshes

    EDIT: After edit its much simpler problem!

    UI is rendered at RenderQueue = 3000 you probably have unlit material as a line-renderer which has RenderQueue = 2450.

    You need to create a material with RenderQueue >= 3000 for your line renderer

    If you don't want to create new material, you can change it from code

    // use this with caution! As this can affect other materials when run in editor!
    meshRenderer.material.renderQueue = 3100;
    

    EDIT 2: If you have URP installed you might need to change the default material to something from URP. Default material uses Legacy shader which causes issues with the Line Renderer and UI.