FPS weapon clipping viewport/camera setup is causing rendering issues

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By David Saltares

Hi ,

I am building a small FPS game and followed a tutorial to prevent weapons clipping through geometry. This involves having a ViewportContainer, Viewport and secondary Camera setup where the camera renders a different layer than the main camera. Weapons are then put in the layer the secondary camera renders.

However, the viewport does not mix well with the main rendering, causing all sorts of artefacts. It’s as if the colour mixing was totally wrong.

Do you know what could possibly be wrong ? I have attached a lot of info about the issue and project in case that helps.

Thanks .

Video of the issue: Loom | Free Screen & Video Recording Software | Loom
Node setup walkthrough: Loom | Free Screen & Video Recording Software | Loom
Project files: GitHub - dsaltares/100-days-gamedev at issue/weapon-clipping-fix

:bust_in_silhouette: Reply From: Lola


the reason you’re having this problem is because you have several directional lights.
It was reported as a bug already.

If you really want two suns you can work around that by using the method described (and explained) in the bug report.
Here is it step-by-step in case you need it:

  1. Create a new ShaderMaterial for the material property of the ViewportContainer (CanvasItem > Material > material)
  2. Create a new Shader in this material
  3. Open it and put the following code in it:
    shader_type canvas_item;

    void fragment() {
        COLOR = texture(TEXTURE, UV);
        COLOR.a = clamp(COLOR.a, 0.0, 1.0);
  1. That’s it!

That did it, thank you so much. I implemented your suggestion here fix(weapon): avoid clipping through walls · dsaltares/100-days-gamedev@ad54fd1 · GitHub and it works like a charm.

It looked like an Engine bug to me, but did not think to check GH issues ‍♂️.

David Saltares | 2021-10-07 09:01