-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffraction.html
More file actions
324 lines (290 loc) · 10.1 KB
/
diffraction.html
File metadata and controls
324 lines (290 loc) · 10.1 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
<!DOCTYPE html>
<html>
<head>
<title>Diffraction Simulation</title>
</head>
<body>
<div class="container">
<table class="labels">
<tr>
<td class="label-name">λ (nm)</td>
<td class="label-value" id="lambda"></td>
</tr>
<tr>
<td class="label-name">z (mm)</td>
<td class="label-value" id="z"></td>
</tr>
<tr>
<td class="label-name">Slit size (mm)</td>
<td class="label-value">
<span id="slit_width" data-decimal="3"></span>×<span id="slit_height" data-decimal="3"></span>
</td>
</tr>
<tr>
<td class="label-name">Array size</td>
<td class="label-value">
<span id="array_width"></span>×<span id="array_height"></span>
</td>
</tr>
<tr>
<td class="label-name">Array pitch (mm)</td>
<td class="label-value" id="pitch" data-decimal="3"></td>
</tr>
<tr>
<td class="label-name">Image size (pixels)</td>
<td class="label-value">
<span id="canvas_width"></span>×<span id="canvas_height"></span>
</td>
</tr>
<tr>
<td class="label-name">Pixel size (mm)</td>
<td class="label-value" id="pixel_size" data-decimal="3"></td>
</tr>
<tr>
<td class="label-name">Frames</td>
<td class="label-value" id="frames"></td>
</tr>
<tr>
<td class="label-name">U2 (%)</td>
<td class="label-value" id="u2" data-decimal="2"></td>
</tr>
</table>
<div class="canvas-wrapper">
<canvas id="canvas" width="100" height="100"></canvas>
<canvas id="canvas2" width="100" height="100"></canvas>
</div>
</div>
<style>
canvas {
zoom: 400%;
display: inline-block;
margin-right: 10px;
}
.canvas-wrapper {
display: inline-flex;
justify-content: center;
}
.container {
background-color: black;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
position: relative;
}
.labels {
position: absolute;
top: 0;
left: 0;
padding-top: 10px;
padding-left: 10px;
color: white;
}
.label-value {
padding-left: 10px;
}
</style>
<script>
function updateLabels(meta) {
for (const key in meta) {
const labelElement = document.getElementById(key);
if (labelElement) {
const decimalPlacesAttr = labelElement.getAttribute('data-decimal');
const decimalPlaces = decimalPlacesAttr !== null ? parseInt(decimalPlacesAttr, 10) : 0;
labelElement.textContent = meta[key].toFixed(decimalPlaces);
}
}
}
</script>
<script>
let storedPixels = []; // Array to store pixel data
function updateCanvas2FromCanvas1(gl, ctx2) {
// Get dimensions from the WebGL canvas
const canvas1 = gl.canvas;
const width = canvas1.width;
const height = canvas1.height;
// Create a buffer to read pixel data
const pixels = new Uint8Array(width * height * 4);
// Read pixels from WebGL canvas
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
// Add read pixels to the storedPixels array and update maxPixelValue
let maxPixelValue = 0;
let minPixelValue = Number.MAX_VALUE;
for (let i in pixels) {
storedPixels[i] = (storedPixels[i] || 0) + pixels[i];
maxPixelValue = Math.max(maxPixelValue, storedPixels[i]);
minPixelValue = Math.min(minPixelValue, storedPixels[i]);
}
// Create a normalized pixel array
const normalizedPixels = new Uint8ClampedArray(pixels.length);
for (let i in storedPixels) {
normalizedPixels[i] = (storedPixels[i] / maxPixelValue) * 255;
}
// Create new ImageData and update canvas2
const imgData = new ImageData(normalizedPixels, width, height);
ctx2.putImageData(imgData, 0, 0);
return 100.0 * minPixelValue / maxPixelValue;
}
</script>
<script>
// Get canvas and WebGL context
const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");
// Enable float textures
// const ext = gl.getExtension("OES_texture_float");
// if (!ext) {
// console.log("OES_texture_float is not available");
// }
const canvas2 = document.getElementById("canvas2");
const ctx2 = canvas2.getContext("2d");
// Shader source code
const vertexShaderSrc = `
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
`;
const N = 20;
const M = 8;
const MM = 2*M + 1;
const X_MID = (canvas.width / 2).toFixed(1);
const Y_MID = (canvas.height / 2).toFixed(1);
const fragmentShaderSrc = `
precision highp float;
uniform float k;
uniform float z;
uniform float pixel_size;
uniform float slit_delta;
uniform float pitch;
uniform float random_phase[${MM*MM}];
vec2 rotate(vec2 v, float theta) {
float cos_theta = cos(theta);
float sin_theta = sin(theta);
return vec2(v.x*cos_theta - v.y*sin_theta,
v.x*sin_theta + v.y*cos_theta);
}
float mag(vec2 v) {
return v.x*v.x + v.y*v.y;
}
vec2 slit(vec2 p_img, vec2 p_slit, float r_phase) {
// 2d vector from centre of the slit to position in the image.
vec2 p_d_img = p_img - p_slit;
float mag_p_d_img = mag(p_d_img);
float k_over_z = k / z;
vec2 e = vec2(0.0, 0.0);
for (int col = -${N}; col <= ${N}; ++col) {
// The position on the slit relative to p_slit (the centre of the slit).
vec2 p = vec2(float(col) * slit_delta, 0.0);
for (int row = -${N}; row <= ${N}; ++row) {
p.y = float(row) * slit_delta;
float phase = -k_over_z * dot(p, p_d_img);
e.x += cos(phase);
e.y += sin(phase);
}
}
e /= sqrt(z*z + mag_p_d_img);
return rotate(e, r_phase + k_over_z * mag_p_d_img / 2.0);
}
void main() {
// The image point (in the x-y plane at z-coordinate z).
vec2 p_img = vec2((gl_FragCoord.x - ${X_MID}) * pixel_size,
(gl_FragCoord.y - ${Y_MID}) * pixel_size);
// The electric field at point p_img.
vec2 e = vec2(0.0, 0.0);
for (int col = 0; col <= ${MM}; ++col) {
// The center of the slit (in the x-y plane at z-coordinate 0).
vec2 p_slit = vec2(float(col - ${M}) * pitch, 0.0);
for (int row = 0; row <= ${MM}; ++row) {
p_slit.y = float(row - ${M}) * pitch;
e += slit(p_img, p_slit, random_phase[col*${MM} + row]);
}
}
float e_m = mag(e);
e_m *= 0.0005;
float over = e_m > 1.0 ? 0.05 * (e_m - 1.0) : 0.0;
gl_FragColor = vec4(e_m, over, 0.0, 1.0);
}
`;
// Compile shaders and link program
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSrc);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
console.log('Vertex shader failed to compile: ' + gl.getShaderInfoLog(vertexShader));
}
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSrc);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
console.log('Fragment shader failed to compile: ' + gl.getShaderInfoLog(fragmentShader));
}
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log('Program failed to link: ' + gl.getProgramInfoLog(program));
}
// Use the program
gl.useProgram(program);
// Create a square to draw the image on
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
1.0, 1.0
]), gl.STATIC_DRAW);
// Link buffer to shader attribute
const position = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(position);
gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
// Bind CPU to GPU variables
const gpu_k = gl.getUniformLocation(program, "k");
const gpu_z = gl.getUniformLocation(program, "z");
const gpu_pixel_size = gl.getUniformLocation(program, "pixel_size");
const gpu_slit_delta = gl.getUniformLocation(program, "slit_delta");
const gpu_pitch = gl.getUniformLocation(program, "pitch");
const gpu_random_phase = gl.getUniformLocation(program, "random_phase");
const random_phase = new Float32Array(MM*MM);
const meta = {};
meta.lambda = 1500.0; // nm.
meta.z = 500.0; // mm
meta.pixel_size = 0.018; // mm
meta.slit_width = 0.02; // mm
meta.slit_height = meta.slit_width;
meta.slit_delta = meta.slit_width/(2 * N);
meta.pitch = 0.94; // mm
meta.array_width = 2*M + 1;
meta.array_height = meta.array_width;
meta.canvas_width = canvas.width;
meta.canvas_height = canvas.height;
meta.frames = 0;
meta.u2 = 0;
const num_frames = 5;
animate = () => {
requestAnimationFrame((currentTime) => {
gl.uniform1f(gpu_k, 2.0 * Math.PI / (meta.lambda * 1E-6));
gl.uniform1f(gpu_z, meta.z);
gl.uniform1f(gpu_pixel_size, meta.pixel_size);
gl.uniform1f(gpu_slit_delta, meta.slit_delta);
gl.uniform1f(gpu_pitch, meta.pitch);
for (let i in random_phase) {
random_phase[i] = 2 * Math.PI * Math.random();
}
gl.uniform1fv(gpu_random_phase, new Float32Array(random_phase));
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
//meta.u2 = updateCanvas2FromCanvas1(gl, ctx2);
meta.frames++;
updateLabels(meta);
if (meta.frames < num_frames) {
//meta.pitch = 0.94 + frames*0.0002;
animate();
}
});
};
animate();
</script>
</body>
</html>