First Person View Model Shader
Shader for rendering first person view models.
Similar to the method used by:
– Valve (TF2)
– Halo (Reach)
Shader code
// Riordan's (Kastor) FPS view shader (MIT)
shader_type spatial;
render_mode depth_draw_opaque,cull_back;
uniform float fov : hint_range(20, 120) = 40;
const float M_PI = 3.14159265359;
void vertex() {
// recreate the camera projection matrix with our custom fov value
float scale = 1.0 / tan(fov * 0.5 * M_PI / 180.0);
PROJECTION_MATRIX[0][0] = scale / (VIEWPORT_SIZE.x / VIEWPORT_SIZE.y);
PROJECTION_MATRIX[1][1] = scale;
}
void fragment() {
// Scale the depth value 70%, this prevents most clipping.
DEPTH = FRAGCOORD.z;
DEPTH = FRAGCOORD.z * 0.7;
}
Thanks! Btw there is the function ‘radians()’ to convert from degrees to radians 😉
Just a heads up, this shader as is results in inverted rendering in Godot 4.x, due to the screen-space y coordinate being flipped in Godot 4. Here’s some amended code for it to work in Godot 4.x
This avoids setting DEPTH in frag func, which is a performance killer (see https://twitter.com/reduzio/status/1748831423690133831)
this makes my model appear without textures for some reason.
And appears double right? For me the original model appears too + a second model with the shader FOV and no textures. (I cant find nowhere a proper FOV shader for Godot 4.2)