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;
}