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;
}
Live Preview
Tags
FPS
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Comments 6

  1. Thanks! Btw there is the function 'radians()' to convert from degrees to radians ;)

    1
  2. tower

    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

    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;
    }
    
    1
  3. Belzecue
    shader_type spatial;
    
    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;
    	POSITION = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX, 1.0);
    	POSITION.z = 0.1;
    }
    

    This avoids setting DEPTH in frag func, which is a performance killer (see https://twitter.com/reduzio/status/1748831423690133831)

    1
  4. driftware

    this makes my model appear without textures for some reason.

    0
    1. Anaxim

      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)

      0
  5. Li Fuch Yu

    doesn't work nice going retard

    -9