How to stop 3D weapon clipping? (SOLVED)

Hello everyone. I am currently working on my 3D game and trying to make weapon rendered on top of other models to prevent clipping. I was able to achieve a somewhat acceptable result but I am curious if a perfect (or better) result can be achieved. The version I came up with renders weapon and hands on top for the points that are clipping but it is inconsistent within itself and depth is not applied properly?

Here is the part on the render script that handles model, weapon and GUI draw:

-- Draw world
    render.enable_state(graphics.STATE_DEPTH_TEST)
    render.enable_state(graphics.STATE_CULL_FACE)
    render.draw(self.predicates.model, draw_options_world)
    render.disable_state(graphics.STATE_CULL_FACE)

    -- Draw weapon (always visible)
    render.set_depth_mask(false)
    render.disable_state(graphics.STATE_DEPTH_TEST)
    render.enable_state(graphics.STATE_CULL_FACE)
    render.draw(self.predicates.weapon, draw_options_world)
    render.disable_state(graphics.STATE_CULL_FACE)
    render.enable_state(graphics.STATE_DEPTH_TEST)
    render.set_depth_mask(true)

    -- Draw rest (particles, GUI, etc)
    render.enable_state(graphics.STATE_BLEND)
    render.draw(self.predicates.weapon, draw_options_world) -- This line ensures when not clipping, rendering is done correctly, otherwise the problem persists even when no clipping is done
    render.draw(predicates.tile, draw_options_world)
    render.draw(predicates.particle, draw_options_world)
    render.disable_state(graphics.STATE_DEPTH_TEST)
    render.draw_debug3d()
    reset_camera_world(state)

    render.draw_debug3d()

    reset_camera_world(state)

Any help/tip is appreciated :slight_smile:

1 Like

One thing I noticed is that the depth_mask is set to false.
My approach was a bit different since I’m using a separate camera to change the FOV, but it ended up looking something like this:

 -- render the other components: sprites, tilemaps, particles etc
 --
 
  -- Render the gun last
    render.clear({
        [graphics.BUFFER_TYPE_DEPTH_BIT] = 1, -- I don't remember why I set this probably because the 2 cameras
    })

    render.set_view(render_helper.gun_camera_view)
    render.set_projection(render_helper.gun_camera_projection)
    render.draw(predicates.gun_particle) -- smoke and fire pfx
    render.enable_state(graphics.STATE_DEPTH_TEST)
    render.set_depth_mask(true)
    render.enable_state(graphics.STATE_CULL_FACE)
    render.draw(predicates.gun)
    render.disable_state(graphics.STATE_CULL_FACE)
    render.disable_state(graphics.STATE_DEPTH_TEST)

    render.set_depth_mask(false)

    render.draw_debug3d()

    reset_camera_world(state)

    -- render GUI
    --

2 Likes

That just solved the issue. Thank you :slight_smile: . Clearing the depth buffer was the way

2 Likes