forked from jpaasen/hos-webgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
380 lines (290 loc) · 8.47 KB
/
index.html
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
<html>
<header>
<title>Huygens' on Speed - WebGL/three.js</title>
<meta charset="utf-8">
<style type="text/css">
canvas {
width: 100%;
height: 100%;
}
#hos {
}
</style>
</header>
<body bgcolor=black oncontextmenu="return false;">
<script type="text/javascript"></script>
<script src="js/jquery.min.js"></script>
<script src="js/three.min.js"></script>
<script src="js/KeyboardJS/keyboard.js"></script>
<!--<script src="hos.js"></script>-->
<script src="js/stats.min.js"></script>
<script type="x-shader/x-vertex" id='vertShader'>
varying vec2 pixelPos;
void main() {
vec4 pos = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
gl_Position = pos;
pixelPos = vec2(pos.x, pos.y);
}
</script>
<script type="x-shader/x-fragment" id='fragShader'>
varying vec2 pixelPos;
uniform float time;
uniform float f;
uniform float dynamicRange;
uniform float observationAreaSize;
uniform int nSrc;
uniform float minDist;
uniform vec3 srcPos[250];
uniform float srcTimestamps[250];
uniform float steeringDelays[250];
void main() {
float c = 1540.0;
float pi = 3.14;
float totalRealPart = 0.0;
float totalImagPart = 0.0;
// Calc field value
for (int i = 0; i < 250; ++i) {
if (i < nSrc) {
vec3 obs = vec3(pixelPos.x*observationAreaSize, pixelPos.y*observationAreaSize, 0.0);
vec3 src = srcPos[i];
float dist = length(obs - src);
float travelTime = dist / c;
float srcFlightTime = time - (srcTimestamps[i] + steeringDelays[i]);
if (srcFlightTime > travelTime) {
if (dist < minDist) // Prevent high values close the src
dist = minDist;
float omega = 2.0 * pi * f * (travelTime - srcFlightTime);
totalRealPart += cos(omega) / dist;
totalImagPart += sin(omega) / dist;
}
}
}
float outValue = 0.0;
// Normalize and display
if (nSrc > 0) {
outValue = sqrt(totalRealPart*totalRealPart + totalImagPart*totalImagPart);
float maxValue = 1.0 / minDist;
if (abs(outValue) != 0.0) {
outValue /= (float(nSrc) * maxValue * sqrt(2.0));
outValue = 20.0 * log(outValue) / log(10.0);
if (outValue < dynamicRange)
outValue = dynamicRange;
outValue = (outValue - dynamicRange) / (-dynamicRange);
}
}
gl_FragColor = vec4(outValue, outValue, outValue, 1.0);
}
</script>
<div id='hos' onmousemove="mouse_move(event)" onmousedown="mouse_down(event)" onmouseup="mouse_up(event)">
</div>
<script>
var nSrcMax = 250;
var nSrc = 0;
var srcPos = [];
var srcTimestamps = [];
var steeringDelays = [];
init();
var leftMouseDown = false;
var rightMouseDown = false;
var mouseMoved = false;
var linearArrayMode = true;
var fromPoint;
var arrayPitch = 0.5; // in lambdas
var observationAreaSize = 0.1; // in meters
var minDist = 1e-4;
var f = 0.5e6;
var dynamicRange = -100.0; // dB
var c = 1540.0; // m/s
var timeStep = 5e-7;
var pause = false;
var uniforms = {
time: { type: 'f', value: 0.0 },
f: { type: 'f', value: f },
dynamicRange: {type: 'f', value: dynamicRange },
observationAreaSize: { type: 'f', value: observationAreaSize },
nSrc: { type: 'i', value: 0 },
minDist: { type: 'f', value: minDist},
srcPos: { type: 'v3v', value: srcPos },
srcTimestamps: {type: 'fv1', value: srcTimestamps },
steeringDelays: {type: 'fv1', value: steeringDelays }
};
var container = document.getElementById('hos');
var width = window.innerHeight * 0.8;
var height = window.innerHeight * 0.8;
var scene = new THREE.Scene();
var camera = new THREE.OrthographicCamera(-width/2, width/2, -height/2, height/2, 1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
container.appendChild( renderer.domElement );
var stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
var geometry = new THREE.BoxGeometry(width, height, 1);
var vertShader = document.getElementById('vertShader').textContent;
var fragShader = document.getElementById('fragShader').textContent;
var material = new THREE.ShaderMaterial({uniforms: uniforms, vertexShader: vertShader, fragmentShader: fragShader});
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
function init() {
nSrc = 0;
srcPos = [];
srcTimestamps = [];
steeringDelays = [];
for (i = 0; i < nSrcMax; ++i) {
srcPos[srcPos.length] = new THREE.Vector3(0,0,0);
srcTimestamps[srcTimestamps.length] = 0.0;
steeringDelays[steeringDelays.length] = 0.0;
}
}
function convertCoords(x, y) {
return new THREE.Vector3(
(x / document.getElementById('hos').offsetHeight - 0.5) * 2.0 * observationAreaSize,
(y / document.getElementById('hos').offsetHeight - 0.5) * -2.0 * observationAreaSize, 0.0);
}
function addLinearArray(pointFrom, pointTo, lambdaSpacing) {
var lambda = c / f;
var tmpVec = new THREE.Vector3();
tmpVec.subVectors(pointTo, pointFrom);
var arraySize = tmpVec.length();
var pitch = lambda * lambdaSpacing;
var nPoints = arraySize / pitch;
var pitchVec = tmpVec.setLength(pitch);
//var currentPoint = pointFrom;
var arrayTimeStamp = uniforms.time.value;
if (nPoints < nSrcMax) {
for (i = 0.0; i < nPoints; ++i) {
srcPos[nSrc] = new THREE.Vector3(pointFrom.x + pitchVec.x * i, pointFrom.y + pitchVec.y * i, 0.0);
srcTimestamps[nSrc] = arrayTimeStamp;
//console.log(srcPos[nSrc]);
nSrc++;
//currentPoint = currentPoint.add(pitchVec);
}
uniforms.nSrc.value = nSrc;
uniforms.srcPos.value = srcPos;
uniforms.srcTimestamps.value = srcTimestamps;
}
}
function updateSteering(x, y) {
var focus = convertCoords(x, y);
// find src with max time-of-fligh
var maxTimeOfFlight = 0.0;
for (i = 0; i < nSrc; ++i) {
var pos = new THREE.Vector3();
pos.subVectors(focus, srcPos[i]);
var lengthToFocus = pos.length();
var timeOfFlight = lengthToFocus / c;
if (timeOfFlight > maxTimeOfFlight) {
maxTimeOfFlight = timeOfFlight;
}
steeringDelays[i] = timeOfFlight;
}
// calc final delays
for (i = 0; i < nSrc; ++i) {
steeringDelays[i] = maxTimeOfFlight - steeringDelays[i];
}
uniforms.steeringDelays.value = steeringDelays;
}
function reset(event) {
uniforms.time.value = 0.0;
}
function addSrc(point) {
if (nSrc < nSrcMax) {
srcPos[nSrc] = point;
srcTimestamps[nSrc] = uniforms.time.value;
nSrc++;
//console.log(point);
uniforms.nSrc.value = nSrc;
uniforms.srcPos.value = srcPos;
uniforms.srcTimestamps.value = srcTimestamps;
}
}
function mouse_down(event) {
if (event.button == 0) {
if (linearArrayMode) {
fromPoint = convertCoords(event.clientX, event.clientY);
}
leftMouseDown = true;
} else if (event.button == 2) {
rightMouseDown = true;
} else {
}
}
function mouse_up(event) {
if (leftMouseDown) {
if (linearArrayMode && mouseMoved) {
addLinearArray(fromPoint, convertCoords(event.clientX, event.clientY), arrayPitch);
} else {
if (!mouseMoved) {
addSrc(convertCoords(event.clientX, event.clientY));
}
}
mouseMoved = false;
leftMouseDown = false;
} else if (rightMouseDown) {
rightMouseDown = false;
} else {
}
}
function mouse_move(event) {
if (leftMouseDown && !linearArrayMode) {
addSrc(convertCoords(event.clientX, event.clientY));
} else if (rightMouseDown) {
updateSteering(event.clientX, event.clientY);
} else {
}
mouseMoved = true;
}
KeyboardJS.on('m', function() {
linearArrayMode = !linearArrayMode;
});
KeyboardJS.on('p', function() {
pause = !pause;
});
KeyboardJS.on('c', function() {
init();
uniforms.nSrc.value = nSrc;
uniforms.srcPos.value = srcPos;
uniforms.srcTimestamps.value = srcTimestamps;
});
KeyboardJS.on('t', function() {
uniforms.time.value -= timeStep*3;
});
KeyboardJS.on('shift + t', function() {
uniforms.time.value += timeStep*3;
});
KeyboardJS.on('f', function() {
if (f > 1e5) {
f -= 1e5;
}
uniforms.f.value = f;
});
KeyboardJS.on('shift + f', function() {
f += 1e5;
uniforms.f.value = f;
});
KeyboardJS.on('d', function() {
uniforms.dynamicRange.value -= 5.0;
});
KeyboardJS.on('shift + d', function() {
uniforms.dynamicRange.value += 5.0;
});
KeyboardJS.on('l', function() {
arrayPitch -= 0.1;
});
KeyboardJS.on('shift + l', function() {
arrayPitch += 0.1;
});
function render() {
requestAnimationFrame(render);
stats.update();
if (!pause) {
uniforms.time.value += timeStep;
}
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>