Created
November 7, 2024 23:39
-
-
Save KrabCode/72ac569ebaf72a202819d19122d3826e to your computer and use it in GitHub Desktop.
Animated 2D moon with debug visuals. Uses LazyGui.
This file contains 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
import com.krab.lazy.*; | |
LazyGui gui; | |
PGraphics pg; | |
float t; | |
float phase; | |
float lightPhase; | |
boolean growing; | |
float r; | |
float angle0; | |
float edge0; | |
float angle1; | |
float edge1; | |
int frame = 1; | |
void setup() { | |
size(1000, 500, P3D); | |
colorMode(HSB, 1, 1, 1, 1); | |
smooth(8); | |
frameRate(60); | |
gui = new LazyGui(this); | |
pg = createGraphics(width, height, P2D); | |
pg.smooth(8); | |
} | |
void draw() { | |
if (t > 30 && t <= 31) { | |
// save("out6/" + nf(frame++, 3, 0) + ".jpg"); | |
} | |
background(0); | |
pg.beginDraw(); | |
pg.colorMode(HSB, 1, 1, 1, 1); | |
pg.clear(); | |
hint(ENABLE_DEPTH_TEST); | |
t = gui.slider("time"); | |
r = gui.slider("radius", 200); | |
float speed = gui.slider("speed", 0.5); | |
gui.sliderSet("time", t + radians(speed)); | |
phase = (t * TAU) % TAU; | |
lightPhase = (phase + HALF_PI); | |
growing = cos(lightPhase) < 0; | |
angle0 = phase; | |
edge0 = cos(angle0); | |
angle1 = phase + PI; | |
edge1 = cos(angle1); | |
drawReference(); | |
drawArcMinimap(); | |
drawCurve(); | |
drawMoon2D(); | |
pg.endDraw(); | |
hint(DISABLE_DEPTH_TEST); | |
ortho(); | |
noLights(); | |
image(pg, 0, 0); | |
} | |
void drawReference() { | |
if (!gui.toggle("reference", true)) return; | |
push(); | |
translate(width/2, height/2); | |
translate(gui.plotXY("ref pos").x, gui.plotXY("ref pos").y); | |
ortho(); | |
fill(0.8, 0, 1); | |
noStroke(); | |
directionalLight(1, 0, 0.8, cos(lightPhase), 0, sin(lightPhase)); | |
sphereDetail(40); | |
sphere(r); | |
pop(); | |
} | |
void drawArcMinimap() { | |
if (!gui.toggle("minimap", true)) return; | |
pg.push(); | |
pg.noStroke(); | |
pg.fill(0.8); | |
pg.translate(gui.plotXY("map pos", width - 50).x, gui.plotXY("map pos", height - 50).y); | |
pg.arc(0, 0, 40, 40, 0, phase); | |
pg.pop(); | |
} | |
void drawCurve() { | |
if (!gui.toggle("curve")) return; | |
pg.push(); | |
pg.translate(width/2, height/2); | |
pg.translate(gui.plotXY("ref pos").x, gui.plotXY("ref pos").y); | |
pg.strokeWeight(6); | |
pg.noFill(); | |
if (growing) { | |
// black back, white front | |
// new moon -> light growing -> full moon | |
// curved edge leading the light coming from the right | |
pg.stroke(0.35, 1, 1); | |
pg.arc(0, 0, 2*r*edge0, 2*r, -HALF_PI, HALF_PI); | |
} else { | |
// white back, black front | |
// full moon -> light shrinking -> no moon | |
// curved edge leading the darkness coming from the right | |
pg.stroke(0.0, 1, 1); | |
pg.arc(0, 0, 2*r*edge1, 2*r, -HALF_PI, HALF_PI); | |
} | |
pg.pop(); | |
} | |
void drawMoon2D() { | |
if (!gui.toggle("moon 2D")) return; | |
int fg = gui.colorPicker("foreground", color(0.6)).hex; | |
int bg = gui.colorPicker("background", color(0.2)).hex; | |
int outline = gui.colorPicker("outline", color(0.6)).hex; | |
pg.push(); | |
pg.translate(width/2, height/2); | |
pg.translate(gui.plotXY("moon pos").x, gui.plotXY("moon pos").y); | |
pg.scale(gui.slider("scale", 1)); | |
pg.fill(growing ? bg : fg); | |
pg.circle(0, 0, r*2); | |
pg.fill(!growing ? bg : fg); | |
pg.noStroke(); | |
float outlineWeight = gui.slider("outline weight", 3); | |
float rInner = r - outlineWeight * 0.5; | |
circleArc(growing ? rInner*edge0 : rInner*edge1, rInner); | |
pg.stroke(outline); | |
pg.noFill(); | |
pg.strokeWeight(outlineWeight); | |
pg.circle(0, 0, r*2); | |
pg.pop(); | |
} | |
void circleArc(float w, float h) { | |
int detail = gui.sliderInt("detail", 300); | |
pg.beginShape(); | |
// outer circle | |
for (int i = 0; i < detail; i++) { | |
float n = map(i, 0, detail-1, -HALF_PI, HALF_PI); | |
float x = r * cos(n); | |
float y = r * sin(n); | |
pg.vertex(x, y); | |
} | |
// inner arc | |
for (int i = 0; i < detail; i++) { | |
float n = - map(i, 0, detail-1, -HALF_PI, HALF_PI); | |
float x = w * cos(n); | |
float y = h * sin(n); | |
pg.vertex(x, y); | |
} | |
pg.endShape(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://i.imgur.com/YSQM753.mp4