-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject3.html
More file actions
395 lines (333 loc) · 12.5 KB
/
Copy pathproject3.html
File metadata and controls
395 lines (333 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!doctype html>
<html lang='en'>
<head>
<style>body{ margin:0; background:black; }</style>
</head>
<body>
<canvas id='gl'></canvas>
</body>
<script src="https://cdn.jsdelivr.net/npm/tweakpane@3.0.7/dist/tweakpane.min.js"></script>
<!-- vertex shader, as simple as possible -->
<script id='vertex' type='x-shader/x-vertex'>
attribute vec2 a_position;
void main() {
gl_Position = vec4( a_position, 0, 1 );
}
</script>
<!-- game of life simulation fragment shader -->
<script id='simulation' type='x-shader/x-fragment'>
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 resolution;
uniform sampler2D state;
uniform float dA;
uniform float dB;
uniform float feedRate;
uniform float killRate;
uniform float size;
// look up individual cell values
float getA(int x, int y) {
return texture2D( state, ( gl_FragCoord.xy + vec2(x, y) ) / resolution ).r;
}
float getB(int x, int y) {
return texture2D( state, ( gl_FragCoord.xy + vec2(x, y) ) / resolution ).a;
}
float laplaceA() {
float sumA = (getA( 0, 0) * -1.0) +
(getA(-1, -1) * 0.05) +
(getA(-1, 0) * 0.2) +
(getA(-1, 1) * 0.05) +
(getA( 0, -1) * 0.2) +
(getA( 0, 1) * 0.2) +
(getA( 1, -1) * 0.05) +
(getA( 1, 0) * 0.2) +
(getA( 1, 1) * 0.05);
return sumA;
}
float laplaceB() {
float sumB = (getB( 0, 0) * -1.0) +
(getB(-1, -1) * 0.05) +
(getB(-1, 0) * 0.2) +
(getB(-1, 1) * 0.05) +
(getB( 0, -1) * 0.2) +
(getB( 0, 1) * 0.2) +
(getB( 1, -1) * 0.05) +
(getB( 1, 0) * 0.2) +
(getB( 1, 1) * 0.05);
return sumB;
}
void main() {
// get sum of surrounding cell values
float a = getA(0,0);
float b = getB(0,0);
float nextA = a + (dA * laplaceA() * size - (a * b * b) + (feedRate * (1.0 - a)));
float nextB = b + (dB * laplaceB() * size + (a * b * b) - ((killRate + feedRate) * b));
gl_FragColor = vec4(nextA, 0.5, 0.5, nextB );
}
</script>
<!-- render to screen shader -->
<script id='render' type='x-shader/x-fragment'>
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D uSampler;
uniform vec2 resolution;
void main() {
gl_FragColor = vec4(texture2D( uSampler, gl_FragCoord.xy / resolution ).rgb, 1.);
}
</script>
<script type='text/javascript'>
let gl, framebuffer,
simulationProgram, drawProgram,
uTime, uSimulationState,
textureBack, textureFront,
dimensions = {width:null, height:null}
let dA, dB, fRate, kRate, size
let udA, udB, uFRate, uKRate, usize
window.onload = function() {
const pane = new Tweakpane.Pane();
const PARAMS = {
dA: 1,
dB: 0.40,
fRate: 0.067,
kRate: 0.062,
size: 0.5
}
const dAInput = pane.addInput(
PARAMS, 'dA',
{min: 0, max: 1}
)
const dBInput = pane.addInput(
PARAMS, 'dB',
{min: 0, max: 1}
)
const feedInput = pane.addInput(
PARAMS, 'fRate',
{min: 0, max: 1}
)
const killInput = pane.addInput(
PARAMS, 'kRate',
{min: 0, max: 1}
)
const sizeInput = pane.addInput(
PARAMS, 'size',
{min: 0, max: 1}
)
dAInput.on('change', function(ev){
dA = ev.value
})
dBInput.on('change', function(ev){
dB = ev.value
})
feedInput.on('change', function(ev){
fRate = ev.value
})
killInput.on('change', function(ev){
kRate = ev.value
})
sizeInput.on('change', function(ev){
size = ev.value
})
dA = PARAMS['dA']
dB = PARAMS['dB']
fRate = PARAMS['fRate']
kRate = PARAMS['kRate']
size = PARAMS['size']
const canvas = document.getElementById('gl')
gl = canvas.getContext('webgl2')
canvas.width = dimensions.width = window.innerWidth
canvas.height = dimensions.height = window.innerHeight
// define drawing area of webgl canvas. bottom corner, width / height
gl.viewport( 0,0, gl.drawingBufferWidth, gl.drawingBufferHeight )
gl.getExtension('EXT_color_buffer_float')
makeBuffer()
makeShaders()
makeTextures()
setInitialState()
}
function makeBuffer() {
// create a buffer object to store vertices
const buffer = gl.createBuffer()
// point buffer at graphic context's ARRAY_BUFFER
gl.bindBuffer( gl.ARRAY_BUFFER, buffer )
const triangles = new Float32Array([
-1, -1,
1, -1,
-1, 1,
-1, 1,
1, -1,
1, 1
])
// initialize memory for buffer and populate it. Give
// open gl hint contents will not change dynamically.
gl.bufferData(gl.ARRAY_BUFFER, triangles, gl.STATIC_DRAW)
}
function makeShaders() {
// create vertex shader
let shaderScript = document.getElementById('vertex')
let shaderSource = shaderScript.text
const vertexShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertexShader, shaderSource)
gl.compileShader(vertexShader)
// create fragment shader
shaderScript = document.getElementById('render')
shaderSource = shaderScript.text
const drawFragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(drawFragmentShader, shaderSource)
gl.compileShader(drawFragmentShader)
// create render program that draws to screen
drawProgram = gl.createProgram()
gl.attachShader(drawProgram, vertexShader)
gl.attachShader(drawProgram, drawFragmentShader)
gl.linkProgram(drawProgram)
gl.useProgram(drawProgram)
uRes = gl.getUniformLocation(drawProgram, 'resolution')
gl.uniform2f(uRes, gl.drawingBufferWidth, gl.drawingBufferHeight)
// get position attribute location in shader
let position = gl.getAttribLocation(drawProgram, 'a_position')
// enable the attribute
gl.enableVertexAttribArray(position)
// this will point to the vertices in the last bound array buffer.
// In this example, we only use one array buffer, where we're storing
// our vertices
gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0,0)
shaderScript = document.getElementById('simulation')
shaderSource = shaderScript.text
const simulationFragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(simulationFragmentShader, shaderSource)
gl.compileShader(simulationFragmentShader)
// create simulation program
simulationProgram = gl.createProgram()
gl.attachShader(simulationProgram, vertexShader)
gl.attachShader(simulationProgram, simulationFragmentShader)
gl.linkProgram(simulationProgram)
gl.useProgram(simulationProgram)
uRes = gl.getUniformLocation(simulationProgram, 'resolution')
gl.uniform2f(uRes, gl.drawingBufferWidth, gl.drawingBufferHeight)
// find a pointer to the uniform "time" in our fragment shader
uTime = gl.getUniformLocation(simulationProgram, 'time')
udA = gl.getUniformLocation(simulationProgram, 'dA')
udB = gl.getUniformLocation(simulationProgram, 'dB' )
uFRate = gl.getUniformLocation(simulationProgram, 'feedRate')
uKRate = gl.getUniformLocation(simulationProgram, 'killRate')
usize = gl.getUniformLocation(simulationProgram, 'size')
//uSimulationState = gl.getUniformLocation( simulationProgram, 'state' )
position = gl.getAttribLocation(simulationProgram, 'a_position')
gl.enableVertexAttribArray(simulationProgram)
gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0,0)
}
function makeTextures() {
textureBack = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, textureBack)
// these two lines are needed for non-power-of-2 textures
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// how to map when texture element is less than one pixel
// use gl.NEAREST to avoid linear interpolation
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
// how to map when texture element is more than one pixel
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, dimensions.width, dimensions.height, 0, gl.RGBA, gl.FLOAT, null)
textureFront = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, textureFront)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, dimensions.width, dimensions.height, 0, gl.RGBA, gl.FLOAT, null)
// Create a framebuffer and attach the texture.
framebuffer = gl.createFramebuffer()
// textures loaded, now ready to render
render()
}
function poke(x, y, a, b, texture) {
gl.bindTexture( gl.TEXTURE_2D, texture)
gl.texSubImage2D(
gl.TEXTURE_2D, 0,
// x offset, y offset, width, height
x, y, 1, 1,
gl.RGBA, gl.FLOAT,
// is supposed to be a typed array
new Float32Array([ a, 0.0, 0.0, b ])
)
}
function setInitialState() {
initialize();
setSquares(2, 1, 100);
setSquares(5, 2, 250)
setSquares(1, 8, 370)
}
function initialize(){
for( i = 0; i < dimensions.width; i++ ) {
for( j = 0; j < dimensions.height; j++ ) {
tempA = 1.0, tempB = 0.0
poke(i, j, 1.0, 0.0, textureBack)
}
}
}
function setSquares(x, y, area){
let tempA = 1.0, tempB = 0.0
for(i = 0; i < dimensions.width; i++ ) {
for(j = 0; j < dimensions.height; j++) {
if(i > Math.floor((dimensions.width / x) - area)
&& i < Math.floor((dimensions.width / x) + area)
&& j > Math.floor((dimensions.height / y) - area)
&& j < Math.floor((dimensions.height / y) + area )){
tempA = 1.0
tempB = 1.0
poke(i, j, tempA, tempB, textureBack)
}
}
}
}
// keep track of time via incremental frame counter
let time = 0
function render() {
// schedules render to be called the next time the video card requests
// a frame of video
window.requestAnimationFrame(render)
// use our simulation shader
gl.useProgram(simulationProgram)
// update time on CPU and GPU
time++
gl.uniform1f(uTime, time)
gl.uniform1f(udA, dA)
gl.uniform1f(udB, dB)
gl.uniform1f(uFRate, fRate)
gl.uniform1f(uKRate, kRate);
gl.uniform1f(usize, size)
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
// use the framebuffer to write to our texFront texture
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureFront, 0)
// set viewport to be the size of our state (game of life simulation)
// here, this represents the size that will be drawn onto our texture
gl.viewport(0, 0, dimensions.width,dimensions.height)
// in our shaders, read from texBack, which is where we poked to
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, textureBack)
gl.uniform1i(uSimulationState, 0)
// run shader
gl.drawArrays(gl.TRIANGLES, 0, 6)
// swap our front and back textures
let tmp = textureFront
textureFront = textureBack
textureBack = tmp
// use the default framebuffer object by passing null
gl.bindFramebuffer( gl.FRAMEBUFFER, null )
// set our viewport to be the size of our canvas
// so that it will fill it entirely
gl.viewport(0, 0, dimensions.width,dimensions.height )
// select the texture we would like to draw to the screen.
// note that webgl does not allow you to write to / read from the
// same texture in a single render pass. Because of the swap, we're
// displaying the state of our simulation ****before**** this render pass (frame)
gl.bindTexture( gl.TEXTURE_2D, textureFront )
// use our drawing (copy) shader
gl.useProgram( drawProgram )
// put simulation on screen
gl.drawArrays( gl.TRIANGLES, 0, 6 )
}
</script>
</html>