Initial Commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// GLSL version of Conway's game of life, ported from GLSL sandbox:
|
||||
// http://glsl.heroku.com/e#207.3
|
||||
// Exemplifies the use of the ppixels uniform in the shader, that gives
|
||||
// access to the pixels of the previous frame.
|
||||
PShader conway;
|
||||
PGraphics pg;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, P3D);
|
||||
pg = createGraphics(400, 400, P2D);
|
||||
pg.noSmooth();
|
||||
conway = loadShader("conway.glsl");
|
||||
conway.set("resolution", float(pg.width), float(pg.height));
|
||||
}
|
||||
|
||||
void draw() {
|
||||
conway.set("time", millis()/1000.0);
|
||||
float x = map(mouseX, 0, width, 0, 1);
|
||||
float y = map(mouseY, 0, height, 1, 0);
|
||||
conway.set("mouse", x, y);
|
||||
pg.beginDraw();
|
||||
pg.background(0);
|
||||
pg.shader(conway);
|
||||
pg.rect(0, 0, pg.width, pg.height);
|
||||
pg.endDraw();
|
||||
image(pg, 0, 0, width, height);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Conway's game of life
|
||||
|
||||
#ifdef GL_ES
|
||||
precision highp float;
|
||||
#endif
|
||||
|
||||
#define PROCESSING_COLOR_SHADER
|
||||
|
||||
uniform float time;
|
||||
uniform vec2 mouse;
|
||||
uniform vec2 resolution;
|
||||
uniform sampler2D ppixels;
|
||||
|
||||
vec4 live = vec4(0.5,1.0,0.7,1.);
|
||||
vec4 dead = vec4(0.,0.,0.,1.);
|
||||
vec4 blue = vec4(0.,0.,1.,1.);
|
||||
|
||||
void main( void ) {
|
||||
vec2 position = ( gl_FragCoord.xy / resolution.xy );
|
||||
vec2 pixel = 1./resolution;
|
||||
|
||||
if (length(position-mouse) < 0.01) {
|
||||
float rnd1 = mod(fract(sin(dot(position + time * 0.001, vec2(14.9898,78.233))) * 43758.5453), 1.0);
|
||||
if (rnd1 > 0.5) {
|
||||
gl_FragColor = live;
|
||||
} else {
|
||||
gl_FragColor = blue;
|
||||
}
|
||||
} else {
|
||||
float sum = 0.;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(-1., -1.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(-1., 0.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(-1., 1.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(1., -1.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(1., 0.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(1., 1.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(0., -1.)).g;
|
||||
sum += texture2D(ppixels, position + pixel * vec2(0., 1.)).g;
|
||||
vec4 me = texture2D(ppixels, position);
|
||||
|
||||
if (me.g <= 0.1) {
|
||||
if ((sum >= 2.9) && (sum <= 3.1)) {
|
||||
gl_FragColor = live;
|
||||
} else if (me.b > 0.004) {
|
||||
gl_FragColor = vec4(0., 0., max(me.b - 0.004, 0.25), 0.);
|
||||
} else {
|
||||
gl_FragColor = dead;
|
||||
}
|
||||
} else {
|
||||
if ((sum >= 1.9) && (sum <= 3.1)) {
|
||||
gl_FragColor = live;
|
||||
} else {
|
||||
gl_FragColor = blue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user