Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Last active June 5, 2020 23:48
Show Gist options
  • Save marcedwards/621e622315cd8a24e37921c4ac6dc6d4 to your computer and use it in GitHub Desktop.
Save marcedwards/621e622315cd8a24e37921c4ac6dc6d4 to your computer and use it in GitHub Desktop.
Rainbow Sphere in Processing 3.5.4
//
// Rainbow Sphere.
// Created using Processing 3.5.4.
//
// Code by @marcedwards from @bjango.
//
// A GIF of this code can be seen here:
// https://twitter.com/marcedwards/status/1261625706326286336
//
PGraphics tex;
PShape globe;
void setup() {
size(400, 400, P3D);
frameRate(30);
smooth(4);
colorMode(RGB, 1);
noStroke();
sphereDetail(32);
globe = createShape(SPHERE, 120);
tex = createGraphics(width, height);
tex.colorMode(RGB, 1);
tex.fill(1);
tex.noStroke();
}
void draw() {
background(0);
drawTexture(timeLoop(60), 8);
image(tex, 0, 0);
for (int a = 0; a < TAU; a += TAU / 6) {
directionalLight(1, 1, 1, cos(a), sin(a), 0.5);
}
translate(width / 2, height / 2);
sphereDetail(48);
globe = createShape(SPHERE, 120);
globe.setTexture(tex);
shape(globe);
}
void drawTexture(float offset, float items) {
tex.beginDraw();
tex.background(0);
tex.noStroke();
tex.colorMode(HSB, 1);
tex.scale(1.5);
float step = tex.height / items;
for (float y = (1 - offset) * -step; y <= tex.height; y += step) {
float hue = y / tex.height;
tex.fill(hue, 0.6, 0.9);
tex.rect(0, y, tex.width, step / 2);
tex.fill(hue, 1, 0.3);
tex.rect(0, y + step / 2, tex.width, step / 2);
}
tex.endDraw();
}
//
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment