Created
August 31, 2011 12:02
-
-
Save spoike/1183382 to your computer and use it in GitHub Desktop.
Processing Sketch - Flames!
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
/** | |
* Flames. Generates some flames by using gradients (with basic | |
* spline math) and Perlin noise (for generating the flame | |
* height). | |
*/ | |
/** Flame outer color */ | |
color startCol = color(100, 0, 210); | |
/** Flame inner color */ | |
color endCol = color(200, 150, 255); | |
void setup() { | |
int w = 150; | |
int h = 150; | |
frame.removeNotify(); | |
frame.setUndecorated(true); | |
frame.addNotify(); | |
size(w, h); | |
background(backCol); | |
pgCurrent = createGraphics(w, h, P3D); | |
pgPrevious = createGraphics(w, h, P3D); | |
gradient = new Gradient(startCol, endCol); | |
} | |
Gradient gradient; | |
PGraphics pgCurrent; | |
PGraphics pgPrevious; | |
int col = 255; | |
int fadeFalloff = 25; | |
float wFactor = 0.02; | |
float time = 0; | |
float timeScale = 0.05; | |
int backCol = 50; | |
int yOffset = -5; | |
void draw() { | |
drawFlames(pgCurrent, pgPrevious); | |
image(pgCurrent, 0, 0); | |
} | |
void drawFlames(PGraphics pg, PGraphics pgOld) { | |
pgOld.beginDraw(); | |
pgOld.image(pg, 0, 0); | |
pgOld.endDraw(); | |
pg.beginDraw(); | |
pg.image(pgOld, 1, yOffset); | |
pg.fill(backCol, fadeFalloff); | |
pg.noStroke(); | |
pg.rect(0, 0, width, height); | |
for(int w = 0; w < width; w++) { | |
float h = noise(w*wFactor, time)*height; | |
for (int y = (int)h; y<height; y++) { | |
float t = ((y)-h)/((float)height-h); | |
pg.stroke(gradient.getAt(t)); | |
pg.point(w, y); | |
} | |
} | |
time += timeScale; | |
pg.endDraw(); | |
} | |
class Gradient { | |
color start; | |
color end; | |
Gradient(color start, color end) { | |
this.start = start; | |
this.end = end; | |
} | |
public color getAt(float t) { | |
float r = spline(red(start), red(end), t); | |
float g = spline(green(start), green(end), t); | |
float b = spline(blue(start), blue(end), t); | |
return color(r, g, b); | |
} | |
private float spline(float a, float b, float t) { | |
return a+t*(b-a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might implement gradient from one color to another. But at the moment there are some really cool blue flames.