Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active January 9, 2025 18:53
Show Gist options
  • Save partybusiness/54d83e6d514c9f2e6cba240c2fd6083c to your computer and use it in GitHub Desktop.
Save partybusiness/54d83e6d514c9f2e6cba240c2fd6083c to your computer and use it in GitHub Desktop.
Displays a fake ground plane that doesn't need to correspond to geometry. Useful for creating a ground plane that extends to the horizon without requiring infinite geometry.
shader_type spatial;
uniform sampler2D ground_texture:source_color, repeat_enable, filter_linear_mipmap;
uniform float ground_height = -1.0;
uniform float scale = 2.0;
varying vec3 world_position;
varying vec3 cam_pos;
void vertex() {
NORMAL =vec3(0.0, 1.0, 0.0); // normal should always face up
world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
cam_pos = CAMERA_POSITION_WORLD + EYE_OFFSET;
}
void fragment() {
vec3 view_dir = normalize(world_position - cam_pos);
if (view_dir.y > 0.0) // only show it below the horizon
discard;
vec2 ground_uv = (vec2(view_dir.x, view_dir.z) * abs(cam_pos.y - ground_height) / view_dir.y - vec2(cam_pos.x, cam_pos.z)) / scale;
ALBEDO = texture(ground_texture, ground_uv).rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment