Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created June 11, 2019 13:37
Show Gist options
  • Select an option

  • Save KrabCode/b320461a73ffb4cf1fdbc06ea7e0dd35 to your computer and use it in GitHub Desktop.

Select an option

Save KrabCode/b320461a73ffb4cf1fdbc06ea7e0dd35 to your computer and use it in GitHub Desktop.
float t;
float frqMult = 3;
float ampMult = .3;
float startFrq = 6;
int octaves = 3;
int count = 30;
int vertices = width;
int sunStartFreq = 10;
;
float sunFreqMult = 3;
float sunAmp = 4;
float sunAmpMult = .2;
int sunlitWater, darkWater;
void setup() {
size(800, 800, P2D);
sunlitWater = color(255, 100, 100);
darkWater = color(0, 0, 50);
}
void draw() {
t = radians(frameCount);
background(255);
drawSun();
drawWaves();
capture();
}
int frameStartedCapturing = -1;
void keyPressed() {
if (key == 'k') {
saveFrame("capture/####.jpg");
}
if(key == 'r'){
frameStartedCapturing = frameCount;
}
}
void capture(){
if(frameStartedCapturing > 0 && frameCount < frameStartedCapturing+300){
saveFrame("capture/####.jpg");
}
}
void drawWaves() {
for (int yi = 0; yi < count; yi++) {
float yN = map(yi, 0, count-1, 0, 1);
beginShape(TRIANGLE_STRIP);
float middleY = height*.4+yN*height*.6;
noStroke();
for (int xi = 0; xi < vertices; xi++) {
float xN = map(xi, 0, vertices-1, 0, 1);
float distanceFromMiddleX = abs(.5-xN)*2;
float sunlight = (1-distanceFromMiddleX)*(1-yN);
fill(lerpColor(darkWater, sunlitWater, sunlight));
float x = xN*width;
float y = middleY+waveFbm(xN+sin(yN*TWO_PI), 20*(.4+.6*yN), t*(.2+.8*yN));
vertex(x, y);
vertex(x, y+100);
}
endShape();
}
}
float waveFbm(float xN, float amp, float t) {
float sum = 0;
float frq = startFrq;
float ampCorrection = 0;
for (int i = 0; i < octaves; i++) {
sum += amp*(abs(sin(xN*frq-t)));
frq *= frqMult;
amp *= ampMult;
ampCorrection += amp*2;
}
return sum-ampCorrection;
}
void drawSun() {
int vertices = 800;
pushMatrix();
translate(width*.5, height*.45);
beginShape(TRIANGLE_STRIP);
noStroke();
for (int i = 0; i < vertices; i++) {
float iN = map(i, 0, vertices-1, 0, 1);
float a = iN*TWO_PI;
float r = 100-sunAmp*sunFbm(a, t);
fill(sunlitWater);
vertex(r*cos(a), r*sin(a));
fill(0);
vertex(width*.8*cos(a), width*.8*sin(a));
}
endShape(CLOSE);
popMatrix();
}
float sunFbm(float n, float t) {
float amp = 1;
float freq = sunStartFreq;
float sum = 0;
for (int i = 0; i < 4; i++) {
sum -= amp*(1-2*abs(sin(n*freq-t)));
amp *= sunAmpMult;
freq *= sunFreqMult;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment