Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

URP FPS-object camera clipping into walls? (official SRP tutorial)

Discussion in 'Universal Render Pipeline' started by mat108, Apr 1, 2021.

  1. mat108

    mat108

    Joined:
    Aug 24, 2018
    Posts:
    130
    So I followed the official URP tutorial on how to create a custom render pass for a specific camera layer, in order to create first person rendered objects (such as weapons in a first person shooter) which don't clip into walls/enemies etc. This technique avoids camera stacking (which works but has many drawbacks) and has many other benefits.

    Completely expectedly, it didn't work. While the FPS layer is being rendered (incredible!), it is not rendered ontop of level geometry. It is being rendered ontop of things that are further in the distance but things closer to the camera are being rendered on top of it. While this is indeed very very nice, practically all games have had this feature for probably 30 years. Scripting the scriptable render pipeline in order for it to be exactly the same as the nonscriptable rendering pipeline might seem like an exciting feature, but the possibilities remain somewhat limited.

    I'd propose Unity make it so that we can script the scriptable rendering pipeline in such a way that it becomes customisable, and thus surpasses the nonscriptable rendering pipeline (NSRP). If users were able to customise the rendering process via scripting, that would open up a tremendous amount of possibilities for customising the rendering process.

    In any case here's my settings:


    So I now desperately turn to the Unity forums in search of answers. Full disclosue: I might lack some knowledge on how to get it to work and maybe I'm also just plain stupid or missed a step in the video. I know stencil and depth might have something to do. I was able to (by randomly playing around with values and permutations thereof) get the fps layer NOT to clip into walls, but at the expense of weird invertedly rendered geometry in my FPS layer. While this is indeed very nice, it's not quite what I'm after. I'd much rather prefer my meshes to be rendered correctly, while not clipping walls.

    Would be incredibly grateful towards anyone who can help me out here.

    Video in Question:

    <iframe width="560" height="315" src="
    " title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
     
    Last edited: Apr 1, 2021
  2. mat108

    mat108

    Joined:
    Aug 24, 2018
    Posts:
    130
  3. mat108

    mat108

    Joined:
    Aug 24, 2018
    Posts:
    130
    500 years and 5 million hours of research later and I still don't know.
     
  4. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    @mat108 Hey, if you are still working on this I have a solution for you. If you draw the opaque render pass, the opaque parts of your gun, after opaques it will render them after all the other opaques, like walls and stuff. Then in the overrides section of that render pass go to depth and turn off depth testing. This will make is so it will always draw everything in this pass no matter what.

    The problem that you are having is when rendering the gun before other opaque objects, the depth of those gun pixels on the other side of the wall will be further away than the wall when it gets rendered. The way the rendering works is that pixels that are closer in the opaque render queue draw on top of pixels that are further away. So the wall keeps rendering over parts of the gun. But if you set the gun to render after the wall and it doesn't do depth testing it will just draw over the wall.

    The video suggests rendering the gun before opaques as to not have to redraw those pixels, but you will be fine doing a little redraw. The other option would be to create a custom render feature that can draw your gun right on the near plane of the camera before rendering other opaques and do nothing else. A pretty inexpensive render operation. Then other objects wouldn't render there and your gun that doesn't do depth testing could render there. But That is beyond a forum post to explain. Just use the first solution.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    I have it set up like this to prevent the first-person view objects from clipping into world geometry:
    upload_2024-2-28_15-29-41.png

    This works great when your environment is mostly solid geometry. However, it will look odd if you have an FPS where you can sneak through bushes and grass and your weapon always renders in front of all the branches and leaves. This could be solved with other render features and/or layering - no idea.

    But what would work for the latter case is not to use a ZWrite mask, but rather enable the Camera on the second RenderObjects feature, then reduce the FOV significantly as said in the video. The weapon model will have to be created with an artificially low FOV in mind however, so it won't necessarily look great with any weapon model.