forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d934b97
commit 92045aa
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const regl = require('../regl')() | ||
|
||
const RADIUS = 512 | ||
const INITIAL_CONDITIONS = (Array(RADIUS * RADIUS * 4)).fill(0).map( | ||
() => Math.random() > 0.9 ? 255 : 0) | ||
|
||
const state = (Array(2)).fill().map(() => | ||
regl.framebuffer({ | ||
colorBuffer: regl.texture({ | ||
radius: RADIUS, | ||
data: INITIAL_CONDITIONS, | ||
wrap: 'repeat' | ||
}), | ||
depth: false | ||
})) | ||
|
||
const updateLife = regl({ | ||
frag: ` | ||
precision mediump float; | ||
uniform sampler2D prevState; | ||
varying vec2 uv; | ||
void main() { | ||
float n = 0.0; | ||
for(int dx=-1; dx<=1; ++dx) | ||
for(int dy=-1; dy<=1; ++dy) { | ||
n += texture2D(prevState, uv+vec2(dx,dy)/float(${RADIUS})).r; | ||
} | ||
float s = texture2D(prevState, uv).r; | ||
if(n > 3.0+s || n < 3.0) { | ||
gl_FragColor = vec4(0,0,0,1); | ||
} else { | ||
gl_FragColor = vec4(1,1,1,1); | ||
} | ||
}`, | ||
|
||
framebuffer: (args, id, params) => state[(params.count + 1) % 2] | ||
}) | ||
|
||
const setupQuad = regl({ | ||
frag: ` | ||
precision mediump float; | ||
uniform sampler2D prevState; | ||
varying vec2 uv; | ||
void main() { | ||
float state = texture2D(prevState, uv).r; | ||
gl_FragColor = vec4(vec3(state), 1); | ||
}`, | ||
|
||
vert: ` | ||
precision mediump float; | ||
attribute vec2 position; | ||
varying vec2 uv; | ||
void main() { | ||
uv = 0.5 * (position + 1.0); | ||
gl_Position = vec4(position, 0, 1); | ||
}`, | ||
|
||
attributes: { | ||
position: regl.buffer([ -4, -4, 4, -4, 0, 4 ]) | ||
}, | ||
|
||
uniforms: { | ||
prevState: (args, id, params) => state[params.count % 2].color[0] | ||
}, | ||
|
||
depth: { enable: false }, | ||
|
||
count: 3 | ||
}) | ||
|
||
regl.frame(() => { | ||
setupQuad(() => { | ||
regl.draw() | ||
updateLife() | ||
}) | ||
}) |