Moon Shader 2D All Phases and Roughened Shadow

Uses faked distant point of illumination and dot product to determine shadows, enabling both crescent and gibbous shapes. Uses a noise texture to create a stippled shadow on the light/dark edge.

Sample image uses speed=0.5, distance=240, roughness=1.63.  

The distance should be large, but beyond a certain point it doesn’t seem to matter much.

Shader code
shader_type canvas_item;

uniform vec4 color : source_color = vec4(1.); 
uniform vec4 shaded_color: source_color = vec4(0., 0., 0., 1.);
uniform float speed : hint_range(0.0, 2.0, 0.01);
uniform float dist : hint_range(0., 240., .01);
uniform sampler2D noise_texture : source_color;
uniform float roughness : hint_range(0., 4., .01);

void fragment() {
  vec2 uv = 2.*(UV - vec2(.5));
  float height = (texture(noise_texture, UV).r-.5)*roughness;
	
  vec3 sphere_point = vec3(uv.x+height, uv.y, 1.-sqrt(uv.x*uv.x + uv.y*uv.y));
  vec3 light_point = dist*vec3(sin(PI*-TIME*.25*speed), uv.y/dist, cos(-TIME*.25*speed*PI));
  float dot_val = dot(sphere_point, (light_point-sphere_point)/length(light_point-sphere_point));
  float val  = smoothstep(-.05, 0., dot_val);
	
  COLOR = mix(color, shaded_color, val);
  COLOR.a *= smoothstep(1., .98, length(uv));
}
Tags
canvas_item, moon, moon phases
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.

More from sturm_trouper

Simple spirals demo

Spider Web

Simple Pie Chart

Related shaders

Sea and Moon

Useful Gradient Effects All-in-one Shader

All in one outline shader

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments