Code licensed under GPL-3.0.
Planet, Earth, and Moon textures by Solar System Scope, licensed CC BY 4.0.
The Moon doesn't change shape -- its near side always faces Earth. A phase is just how much of that near side the Sun happens to be lighting from this angle as the Moon orbits.