1
\$\begingroup\$

I'm currently learning about Unreal Engine 4 by making an FPS game.

How can I render the player character's hands and gun on top of everything?

Currently when I walk up to a wall, the hands and gun can clip into the wall like this:

Image of gun clipping into wall

I want it to look like this, where even when the player is facing into a wall, the hands and gun stay on top:

Image of hands and gun displayed fully, with no clipping

How can I achieve this?

Update: Found this solution:

Render the meshes at the end of the depth-only prepass with depth testing disabled and tagging those pixels in the stencil buffer. Then enable stencil test in the base pass to not touch those pixels. At the end of the base pass you turn off stencil test and render your foreground meshes with depth testing enabled to get their properties in the gbuffer."

\$\endgroup\$
5
  • \$\begingroup\$ If you make their materials transluscent, youcan then uncheck 'disbale depth test'. \$\endgroup\$
    – Rotem
    Aug 23 '20 at 21:45
  • \$\begingroup\$ @Rotem this won't work well on weapons with complex shapes, because it will prevent depth-testing from keeping the far parts of the weapon from drawing over the near parts. Generally you want to render them with depth-testing enabled, but in a second pass, after you've cleared the previous depth buffer or swapped it out. \$\endgroup\$
    – DMGregory
    Aug 24 '20 at 2:49
  • \$\begingroup\$ @DMGregory correct, but I dont think Unreal supports multiple passes out of the box. You'd need some fancy hack setup with SceneCapture2d \$\endgroup\$
    – Rotem
    Aug 24 '20 at 11:06
  • \$\begingroup\$ Have you considered not allowing the player to come so close to the wall that their weapons and hands would push through it? That would be more realistic and avoid rendering issues at the same time. \$\endgroup\$
    – G. Sliepen
    Sep 9 '20 at 8:40
  • \$\begingroup\$ This question is more about how to tame Unreal Engine rendering system than how to make this work. \$\endgroup\$ Sep 17 '20 at 16:12
1
\$\begingroup\$

Unfortunately I can’t answer in terms of unreal engine, as I do not have experience with it, but I’m guessing that my experience with general game development and Unity specifically may transfer in this particular case.

In Unity, I would set up the hands and weapon to be rendered separately from everything else, by setting their layer to be something other than the default.

Then, the main camera would be set up to not render things on this layer.

Finally, I would add another camera as a child of the first camera. It would need to:

  • render after the main camera (done by giving it a different priority)
  • clear the depth buffer (and only the depth buffer)
  • and render only the things on the new layer.

I’m sure there are also ways to do this using a shader for more performance, but this would not be a big performance hit, and also I’m unable to give anything close to good advice on how to write unreal engine shaders.

Again, I’m not familiar enough with unreal engine, I so if this is entirely off the mark, I apologize.

\$\endgroup\$
5
  • \$\begingroup\$ Thank you for your reply! Unfortunately, Unreal does not support multiple passes or render layers as Unity does. \$\endgroup\$ Aug 24 '20 at 18:53
  • \$\begingroup\$ I’m guessing that it does support that (seeing as how unreal is a very advanced engine), but it may not be simple to do. \$\endgroup\$
    – Ed Marty
    Aug 24 '20 at 18:56
  • \$\begingroup\$ I guess adding custom pass will require making a custom engine version, which is not the easiest thing to do and can break all other passes (cuz deferred). I'm just guessing is there a way to do this, because Unreal is really complex engine, as you've mentioned. Has to be. \$\endgroup\$ Aug 24 '20 at 19:05
  • 1
    \$\begingroup\$ @EluThingol do you have the ability to render to an off-screen buffer? If so, you could draw the hands/weapon to a texture, then display that texture in front of the main camera. \$\endgroup\$
    – DMGregory
    Aug 24 '20 at 19:10
  • \$\begingroup\$ @DMGregory I can only mask object using Custom Depth Stencil in Post Process Material. \$\endgroup\$ Aug 25 '20 at 6:45
1
\$\begingroup\$

Render the meshes at the end of the depth-only prepass with depth testing disabled and tagging those pixels in the stencil buffer. Then enable stencil test in the base pass to not touch those pixels. At the end of the base pass you turn off stencil test and render your foreground meshes with depth testing enabled to get their properties in the gbuffer.

\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.