From f9d8886fbac489cf4dbaf5a6cfa916c7242e5e31 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 9 Aug 2019 21:34:39 +0200 Subject: [PATCH] improve plotting of fields --- main.js | 14 ++++++++++---- src/lib.rs | 20 +++++--------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/main.js b/main.js index c26ad68..f0b416b 100644 --- a/main.js +++ b/main.js @@ -43,10 +43,10 @@ async function run() { mediump float b = 0.0; if (uChosenField == 1) { r = texture2D(uSamplerEX, vVertexPosition).a; - r = (r + 1.0)/2.0; + r += 0.5; } else if (uChosenField == 3) { b = texture2D(uSamplerEY, vVertexPosition).a; - b = (b + 1.0)/2.0; + b += 0.5; } else if (uChosenField == 2) { g = texture2D(uSamplerHZ, vVertexPosition).a; g = (g + 1.0)/2.0; @@ -122,7 +122,7 @@ async function run() { const chosen_field = { uLocation: gl.getUniformLocation(shaderProgram, 'uChosenField'), - value: 3, + value: 1, cycle: function() { if (this.value == 1) { this.value = 2; @@ -153,7 +153,7 @@ async function run() { const TIMEFACTOR = 1.0/7000; let t = performance.now()*TIMEFACTOR; - universes[0].set_initial(t, "exp"); + universes[0].set_initial(0.5, 0.5); function drawMe(t_draw) { gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); @@ -215,6 +215,12 @@ async function run() { chosen_field.cycle(); } }, {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(); window.requestAnimationFrame(drawMe); } diff --git a/src/lib.rs b/src/lib.rs index 2b4929f..8e8a816 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,15 +21,10 @@ pub fn set_panic_hook() { 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; - (2.0 * f32::consts::PI * (x + y) - t).sin() -} - -fn exponential(x: f32, y: f32, _t: f32) -> f32 { - use std::f32; - let x = x - 0.5; - let y = y - 0.5; + let x = x - x0; + let y = y - y0; let sigma = 0.05; @@ -47,12 +42,7 @@ impl Universe { Universe { ex, ey, hz } } - pub fn set_initial(&mut self, t: f32, selected_function: &str) { - let func = match selected_function { - "sin+cos" => sin_plus_cos, - "exp" => exponential, - _ => |_x: f32, _y: f32, _t: f32| 0.0, - }; + pub fn set_initial(&mut self, x0: f32, y0: f32) { let nx = self.ex.shape()[1]; let ny = self.ex.shape()[0]; for j in 0..ny { @@ -63,7 +53,7 @@ impl Universe { let y = j as f32 / ny as f32; self.ex[(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; } } }