VR Billboard Shader

This shader fixes VR billboard issues, keeping billboards properly aligned for a natural VR experience.

Add as shader material override to your sprite or mesh and it should work. 

Shader code
shader_type spatial;
render_mode unshaded, depth_draw_opaque, cull_disabled;

uniform sampler2D albedo_texture : source_color;

void vertex() {
    // Get the model position in world space
    vec3 model_pos = MODEL_MATRIX[3].xyz;

    // For VR, we'll use the eye position directly
    vec3 camera_pos = CAMERA_POSITION_WORLD;
    vec3 to_camera = normalize(camera_pos - model_pos);

    // Calculate the right vector
    vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), to_camera));
    // Keep up vector strictly vertical
    vec3 up = vec3(0.0, 1.0, 0.0);
    // Recalculate forward vector to ensure orthogonality
    vec3 forward = normalize(cross(right, up));

    // Build the billboard matrix
    mat4 billboard_matrix = mat4(
        vec4(right, 0.0),
        vec4(up, 0.0),
        vec4(forward, 0.0),
        vec4(model_pos, 1.0)
    );

    MODELVIEW_MATRIX = VIEW_MATRIX * billboard_matrix;
}

void fragment() {
    vec4 albedo = texture(albedo_texture, UV);
    ALBEDO = albedo.rgb;
    ALPHA = albedo.a;
    ALPHA_SCISSOR_THRESHOLD = 0.5;
}
Tags
3d sprite, billboard, billboards, VR, XR, y axis
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Local Space Y-Billboard Shader

Spatial View-Depending Directional Billboard

Billboard Sprite3D Sway (Godot 4.0)

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments