Fixing weapon clipping using shader causes shadow bug

Godot Version

4.3

Question

Hey everyone,
I’m working on an FPS game in Godot 4.3 and encountered a common issue: the weapon clipping into world geometry. Additionally, I need the weapon to remain unaffected by any changes to the camera’s FOV.

After researching solutions, I decided to use a ShaderMaterial for the weapon instead of a subviewport. While a subviewport could solve the problem, it introduces performance overhead and other complications that I’d like to avoid.

Here’s the vertex function I wrote for my ShaderMaterial:

void vertex() {
	UV = UV * uv1_scale.xy + uv1_offset.xy;
	
	float scale = 1.0 / tan(fov * 0.5 * PI / 180.0);
	PROJECTION_MATRIX[0][0] = scale / (VIEWPORT_SIZE.x / VIEWPORT_SIZE.y);
	PROJECTION_MATRIX[1][1] = -scale;

	POSITION = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX.xyz, 1.0);
	POSITION.z = mix(POSITION.z, 0, -0.99);
	
}

This approach successfully fixes the clipping issue and ensures that the weapon is decoupled from FOV changes. However, I’ve run into a new problem: the shadows cast by the weapon are now in incorrect positions. It seems that modifying the projection matrix in this way causes the shadow pass to behave incorrectly.
I appreciate any help! :grinning:

I can’t help, but if you provide a screenshot or two, it will help others to find a solution. :slight_smile: