Last active
January 12, 2026 03:39
-
-
Save partybusiness/519083d02124d6c2f173fca76ac963da to your computer and use it in GitHub Desktop.
Godot sky shader that displays the moon based on light direction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| shader_type sky; | |
| uniform sampler2D moon_texture:source_color, repeat_disable; | |
| // updirection of moon texture will try to align with this | |
| uniform vec3 north = vec3(0, 0, -1); | |
| void sky() { | |
| vec3 light_dir = normalize(-LIGHT0_DIRECTION); | |
| vec3 side_dir = normalize(cross(light_dir, north)); | |
| vec3 up_dir = normalize(cross(light_dir, side_dir)); | |
| vec3 diff = normalize(EYEDIR) + light_dir; //difference between view dir and moon dir | |
| vec2 moon_uv = (vec2(dot(side_dir, diff), dot(up_dir, diff)) * 5.0) + 0.5; | |
| vec4 moon = texture(moon_texture, moon_uv); | |
| moon.rgb = moon.rgb * clamp(dot(normalize(EYEDIR), light_dir) * -2.0, 0.0, 1.0); // make sure only one moon | |
| COLOR = vec3(0, 0, 0) + moon.rgb; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| shader_type sky; | |
| uniform sampler2D moon_texture:source_color, repeat_disable; | |
| /* direction of the moon */ | |
| uniform vec3 moon_direction = vec3(0.4, 0.5, 0.0); | |
| /* controls the size of the moon */ | |
| uniform float moon_size = 0.1; | |
| /* controls the size of the halo / gradient around the moon */ | |
| uniform float moon_gradient_size = 2.0; | |
| /* Gradient centred around moon to make a halo or something */ | |
| uniform sampler2D moon_gradient:hint_default_black, repeat_disable, source_color; | |
| /* Gradient from bottom to top of the sky, can be just a GradienTexture1D */ | |
| uniform sampler2D vertical_gradient:hint_default_black, repeat_disable, source_color; | |
| void sky() { | |
| vec3 light_dir = normalize(-moon_direction); | |
| vec3 up_dir = normalize(cross(light_dir, vec3(0.0, 1.0, 0.0))); | |
| vec3 side_dir = normalize(cross(light_dir, up_dir)); | |
| vec3 diff = normalize(EYEDIR) + light_dir; //difference between view dir and moon dir | |
| vec2 moon_rel = vec2(dot(side_dir, diff), dot(up_dir, diff)) * 1.0 / moon_size; | |
| vec2 moon_uv = moon_rel + 0.5; | |
| vec4 moon = texture(moon_texture, moon_uv); | |
| float aligned = clamp(dot(normalize(EYEDIR), light_dir) * -2.0, 0.0, 1.0); | |
| vec3 gradient_colour = texture(moon_gradient, vec2(length(moon_rel) * 1.0 / moon_gradient_size, 0.5)).rgb; | |
| moon.rgb += gradient_colour; | |
| moon.rgb = moon.rgb * aligned; // make sure only one moon | |
| vec3 sky_colour = texture(vertical_gradient, vec2(EYEDIR.y * 0.5 + 0.5, 0.5)).rgb; | |
| COLOR = sky_colour + moon.rgb; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment