-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.js
298 lines (249 loc) · 7.7 KB
/
code.js
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
// drawing pixel by pixel using GPU from https://stackoverflow.com/questions/35444202/draw-a-single-pixel-in-webgl-using-gl-points
/**
* Creates a program, attaches shaders, links the program.
* @param {WebGLShader[]} shaders. The shaders to attach.
*/
var createGLProgram = (gl, shaders) => {
var program = gl.createProgram();
for (var i = 0; i < shaders.length; i += 1) {
gl.attachShader(program, shaders[i]);
}
gl.linkProgram(program);
// Check the link status
var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!linked) {
// Something went wrong with the link
var lastError = gl.getProgramInfoLog(program);
window.console.error("Error in program linking: " + lastError);
gl.deleteProgram(program);
return null;
}
return program;
};
var myCreateShader = (gl, shaderScriptText, shaderType) => {
// Create the shader object
var shader = gl.createShader(shaderType);
// Load the shader source
gl.shaderSource(shader, shaderScriptText);
// Compile the shader
gl.compileShader(shader);
return shader;
};
// Get A WebGL context.window size divided by two and scaled using css
//Start canvas with
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth / 2;
canvas.height = window.innerHeight / 2;
var gl = canvas.getContext("webgl", {
antialias: true
})
var vertexShader = myCreateShader(gl,
`attribute vec2 a_position;
uniform vec2 u_resolution;
void main() {
// convert the rectangle from pixels to 0.0 to 1.0
vec2 zeroToOne = a_position / u_resolution;
// convert from 0 -> 1 to 0 -> 2
vec2 zeroToTwo = zeroToOne * 2.0;
// convert from 0 -> 2 to -1 -> +1 (clipspace)
vec2 clipSpace = zeroToTwo - 1.0;
// Flip 0,0 from bottom left to conventional 2D top left.
gl_PointSize = 1.0;
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
}`, gl.VERTEX_SHADER);
var fragmentShader = myCreateShader(gl,
`precision mediump float;
uniform vec4 u_color;
void main() {
gl_FragColor = u_color;
}`, gl.FRAGMENT_SHADER);
var program = createGLProgram(gl, [vertexShader, fragmentShader]);
gl.useProgram(program);
// Store color location.
var colorLocation = gl.getUniformLocation(program, "u_color");
// Look up where the vertex data needs to go.
var positionLocation = gl.getAttribLocation(program, "a_position");
// Set the resolution.
var resolutionLocation = gl.getUniformLocation(program, "u_resolution");
gl.uniform2f(resolutionLocation, canvas.width, canvas.height);
// Create a buffer.
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.enableVertexAttribArray(positionLocation);
// Send the vertex data to the shader program.
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Set color to black.
gl.uniform4f(colorLocation, 0, 0, 0, 1);
var drawOneBlackPixel = (gl, x, y) => {
// Fills the buffer with a single point?
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
x + 0.5, y + 0.5
]), gl.STATIC_DRAW);
// Draw one point.
gl.drawArrays(gl.POINTS, 0, 1);
}
var drawArray = (gl, data) => {
if (data && data.length) {
// Fills the buffer with a single point?
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
// Draw one point.
gl.drawArrays(gl.POINTS, 0, data.length / 2);
}
}
gl.clearColor(0.9, 0.9, 0.9, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// These tests are supposed to be x,y coordinates from top left.
var sx = canvas.width;
var sy = canvas.height;
//fill world with random -1 and 1
var world = []
for (var x = 0; x < sx; x++) {
world.push(new Array(sy).fill().map(e => Math.random() > 0.5 ? 1 : -1));
}
//GUI using DAT.gui version in Polymer paper elements
document.addEventListener('PaperGUIReady', e => {
var gui = new PaperGUI();
gui.open();
var SimParams = function() {
this.message = 'dat.gui';
this.T = 5
this.toHeat = f => tempGrad(1);
this.toCool = f => tempGrad(-1);
this.radius = 10;
this.invertDraw = false;
this.pause = false;
};
var params = new SimParams();
var tController = gui.add(params, 'T', 0.00, 5.00).step(0.01).name('Temperature'); // Mix and match
gui.add(params, 'toCool').name('Cool')
gui.add(params, 'toHeat').name('Heat')
gui.add(params, 'radius', 1, 100).name('Draw size'); // Mix and match
gui.add(params, 'invertDraw').name('Invert draw color'); // Mix and match
var simPause = gui.add(params, 'pause').name('Pause Simulation');
//Automatic Temperature Gradient
var isGradRunning = false;
var tempGrad = (type) => {
if (!isGradRunning) {
isGradRunning = true;
var initT = params.T;
var timerID = setInterval(timer => {
tController.sliderEl_.value = ((initT));
tController.sliderEl_.dispatchEvent(new Event('change'));
initT += type * 0.1;
if (initT <= 0.5 || initT >= 5.0) {
clearInterval(timerID);
isGradRunning = false;
}
}, 200);
}
}
//var Z = world.map(line => line.reduce((a, b) => a + b)).reduce((a, b) => a + b);
//Mouse and Touch events
var mousedragging = false;
var touchdragging = false;
canvas.addEventListener("mousedown", e => {
mousedragging = true;
});
canvas.addEventListener("touchstart", e => {
touchdragging = true;
});
canvas.addEventListener("mouseup", e => {
mousedragging = false;
});
canvas.addEventListener("touchend", e => {
touchdragging = false;
});
canvas.addEventListener("touchmove", e => {
if (touchdragging) {
drawCircle(e.touches[0].clientX / 2, e.touches[0].clientY / 2);
}
});
canvas.addEventListener("mousemove", e => {
if (mousedragging) {
drawCircle(e.clientX / 2, e.clientY / 2);
}
});
//Given a position and radius draws circle in the world matrix
var drawCircle = (posX, posY) => {
var p1, p2, esq, x, y, dir, down, up;
var p1 = parseInt(posX);
var p2 = parseInt(posY);
for (var r = 0; r < params.radius; r++) {
for (var a = 0; a <= 360; a++) {
x = parseInt(r * Math.cos(a)) + p1;
y = parseInt(r * Math.sin(a)) + p2;
if (x >= sx) {
x = sx - 1;
}
if (y >= sy) {
y = sy - 1;
}
if (x <= 0) {
x = 0;
}
if (y <= 0) {
y = 0;
}
if (!params.invertDraw) {
world[x][y] = -1;
} else {
world[x][y] = 1;
}
}
}
}
//Simulation function
var simulation = f => {
for (var count = 0; count <= sx * sy / 2; count++) {
var m = 0;
var r1 = parseInt(Math.random() * sx);
var r2 = parseInt(Math.random() * sy);
var esq = r1 - 1;
var dir = r1 + 1;
var down = r2 + 1;
var up = r2 - 1;
if (esq < 0) {
esq = sx - 1;
}
if (dir >= sx) {
dir = 0;
}
if (up < 0) {
up = sy - 1;
}
if (down >= sy) {
down = 0;
}
var E = 2 * world[r1][r2] * (world[dir][r2] + world[esq][r2] + world[r1][down] + world[r1][up]);
if (E <= 0) {
world[r1][r2] = -world[r1][r2];
}
if (E > 0) {
var p = Math.exp(-E / params.T);
var r = Math.random();
if (r <= p) world[r1][r2] = -world[r1][r2];
}
}
};
//simulation interval, unless the toggle is false
var simClock = setInterval(simulation, 1);
simPause.onChange(state => {
if (state) {
clearInterval(simClock);
} else {
simClock = setInterval(simulation, 1);
}
});
//draw Interval, always so the mouseclick drawing is rendered
setInterval(f => {
var data = [];
for (var x = 0; x < sx; x++) {
for (var y = 0; y < sy; y++) {
if (world[x][y] === -1) {
data.push(x + 0.5, y + 0.5);
}
}
}
drawArray(gl, data)
}, 1);
});