Hey, I'm currently working on a project where I'm trying to prevent a gun model from clipping through other objects. After researching, I came across suggestions regarding the use of the "no depth flag" in the mesh's material.

I attempted to implement this technique by using a ShaderMaterial3D in the material_override property of the MeshInstance3D. While it successfully prevents the gun from clipping, I encountered an issue where the model loses its textures and appears completely white.

Wrong

It should be like this instead:

Correct

To provide more context and facilitate troubleshooting, I have uploaded the project to GitHub. Here is the link: https://github.com/SmiteIsTrashBro/Godot-FPS

Any suggestion would be appreaciated, thanks in advance.

I have a very quick suggestion with no time/experience to elaborate:
A common technique I've seen in Godot is having the weapon exist inside its own viewport. You could look into it or wait for someone smarter than me to explain more in depth.
I hope this helps.

I'm not sure about the textures, but "no depth test" won't work on complex models, as they still need to test against themselves (other overlapping parts of the same model).

So there are two tricks that might work, but both have problems.

You can render the gun on another viewport (with transparency) and this will get the effect you want. However, it has weird interaction with environment light and shadow, so it won't exactly match the real scene (though you can sometimes fake it okay).

The other option is to render the gun with a special shader that distorts the perspective. For example, you can scale the gun to 10% on the Z axis, and then use a shader matrix to scale it back. If done right, it will draw to the depth buffer at 10% (so closer to the camera and not clipping the wall) but on the screen it will look normal. However, this also bends the normals in a way that the lighting is no longer correct. It may be fixable, I've only read about this technique, but I not it's possible in other engines.

The final thing, which will be correct and easier, is to just make an animation where the player lifts the gun up if it gets close to the wall. This is what most modern games do, as it looks more realistic.

this is my viewmodel shader, it prevents clipping and has a seperate FOV for viewmodels

shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;

uniform vec4 albedo : source_color;
uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;

uniform float roughness = 1.0;
uniform sampler2D texture_metallic : hint_default_white,filter_linear_mipmap,repeat_enable;
uniform sampler2D texture_roughness : hint_roughness_gray,filter_linear_mipmap,repeat_enable;
uniform float specular = 0.5;
uniform float metallic = 1.0;
uniform sampler2D texture_normal : hint_roughness_normal,filter_linear_mipmap,repeat_enable;
uniform float normal_scale = 1.0;
uniform vec3 uv1_scale = vec3(1.0,1.0,1.0);



uniform float FOV = 40.0;

void vertex() {
	UV=UV*uv1_scale.xy;
	
	float onetanfov = 1.0 / tan(0.5 * (FOV * PI / 180.0));
	float aspect = VIEWPORT_SIZE.x / VIEWPORT_SIZE.y;
	PROJECTION_MATRIX[1][1]= -onetanfov;
	PROJECTION_MATRIX[0][0] = onetanfov / aspect;
	POSITION = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX.xyz, 1.0);
	POSITION.z = mix(POSITION.z, 0, 0.997);
}

void fragment(){
	vec2 base_uv = UV;
	vec4 albedo_tex = texture(texture_albedo,base_uv);
	ALBEDO = albedo.rgb * albedo_tex.rgb;
	vec4 metallic_texture_channel =vec4(0.333333,0.333333,0.333333,0.0);
	float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
	METALLIC = metallic_tex * metallic;
	vec4 roughness_texture_channel = vec4(0.333333,0.333333,0.333333,0.0);
	float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;
	SPECULAR = specular;
	NORMAL_MAP = texture(texture_normal,base_uv).rgb;
	NORMAL_MAP_DEPTH = normal_scale;
}

note, u must disable shadows on your view models