improve plotting of fields

This commit is contained in:
Magnus Ulimoen 2019-08-09 21:34:39 +02:00
parent 43f1088d66
commit f9d8886fba
2 changed files with 15 additions and 19 deletions

14
main.js
View File

@ -43,10 +43,10 @@ async function run() {
mediump float b = 0.0; mediump float b = 0.0;
if (uChosenField == 1) { if (uChosenField == 1) {
r = texture2D(uSamplerEX, vVertexPosition).a; r = texture2D(uSamplerEX, vVertexPosition).a;
r = (r + 1.0)/2.0; r += 0.5;
} else if (uChosenField == 3) { } else if (uChosenField == 3) {
b = texture2D(uSamplerEY, vVertexPosition).a; b = texture2D(uSamplerEY, vVertexPosition).a;
b = (b + 1.0)/2.0; b += 0.5;
} else if (uChosenField == 2) { } else if (uChosenField == 2) {
g = texture2D(uSamplerHZ, vVertexPosition).a; g = texture2D(uSamplerHZ, vVertexPosition).a;
g = (g + 1.0)/2.0; g = (g + 1.0)/2.0;
@ -122,7 +122,7 @@ async function run() {
const chosen_field = { const chosen_field = {
uLocation: gl.getUniformLocation(shaderProgram, 'uChosenField'), uLocation: gl.getUniformLocation(shaderProgram, 'uChosenField'),
value: 3, value: 1,
cycle: function() { cycle: function() {
if (this.value == 1) { if (this.value == 1) {
this.value = 2; this.value = 2;
@ -153,7 +153,7 @@ async function run() {
const TIMEFACTOR = 1.0/7000; const TIMEFACTOR = 1.0/7000;
let t = performance.now()*TIMEFACTOR; let t = performance.now()*TIMEFACTOR;
universes[0].set_initial(t, "exp"); universes[0].set_initial(0.5, 0.5);
function drawMe(t_draw) { function drawMe(t_draw) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
@ -215,6 +215,12 @@ async function run() {
chosen_field.cycle(); chosen_field.cycle();
} }
}, {passive: true}); }, {passive: true});
window.addEventListener('click', event => {
const mousex = event.clientX / window.innerWidth;
const mousey = event.clientY / window.innerHeight;
universes[0].set_initial(mousex, 1.0 - mousey);
}, {passive: true});
resizeCanvas(); resizeCanvas();
window.requestAnimationFrame(drawMe); window.requestAnimationFrame(drawMe);
} }

View File

@ -21,15 +21,10 @@ pub fn set_panic_hook() {
console_error_panic_hook::set_once(); console_error_panic_hook::set_once();
} }
fn sin_plus_cos(x: f32, y: f32, t: f32) -> f32 { fn gaussian(x: f32, x0: f32, y: f32, y0: f32) -> f32 {
use std::f32; use std::f32;
(2.0 * f32::consts::PI * (x + y) - t).sin() let x = x - x0;
} let y = y - y0;
fn exponential(x: f32, y: f32, _t: f32) -> f32 {
use std::f32;
let x = x - 0.5;
let y = y - 0.5;
let sigma = 0.05; let sigma = 0.05;
@ -47,12 +42,7 @@ impl Universe {
Universe { ex, ey, hz } Universe { ex, ey, hz }
} }
pub fn set_initial(&mut self, t: f32, selected_function: &str) { pub fn set_initial(&mut self, x0: f32, y0: f32) {
let func = match selected_function {
"sin+cos" => sin_plus_cos,
"exp" => exponential,
_ => |_x: f32, _y: f32, _t: f32| 0.0,
};
let nx = self.ex.shape()[1]; let nx = self.ex.shape()[1];
let ny = self.ex.shape()[0]; let ny = self.ex.shape()[0];
for j in 0..ny { for j in 0..ny {
@ -63,7 +53,7 @@ impl Universe {
let y = j as f32 / ny as f32; let y = j as f32 / ny as f32;
self.ex[(j, i)] = 0.0; self.ex[(j, i)] = 0.0;
self.ey[(j, i)] = 0.0; self.ey[(j, i)] = 0.0;
self.hz[(j, i)] = func(x, y, t); self.hz[(j, i)] = gaussian(x, x0, y, y0) / 32.0;
} }
} }
} }