UE4 Preventing weapon clipping into walls. Vertex shader trick

Hi guys :slight_smile:

Not sure what i saw that solution somewhere (some one says its used in UnrealTournament), so here it is :slight_smile:
We simply offset vertices of weapon model into direction of camera.
Cause some vertices are already very close to camera near clip plane, we sampling depth and multiply our “offset value” by depth, so vertices near camera wouldn’t be moved, which will cause clipping.

Those solution mostly look like trouble free, but i cant guarantee that.
Known problem - shadow of weapon will be distorted, so its better way to make that offset in shader only if we really close to walls or other obstacles.

6 Likes

This is a really neat and simple trick! Thanks for sharing :slight_smile:

A little update:
Some guy on youtube claims what this method are doesnt work with hands.
So i noted in the description of the video what vertex offset are only used in weapon material.
Also i just copy&paste those code to material used for hands of the characters and it mostly look work fine. Character chest are too close to the camera so its cut off. Probably there need some separate adjustments of depth mask for weapons and character materials.