2024-08-21 13:01:33 -07:00
|
|
|
// based on the WebGL example in the `wasm-bindgen` guide
|
|
|
|
//
|
|
|
|
// https://rustwasm.github.io/wasm-bindgen/examples/webgl.html
|
|
|
|
//
|
2024-08-21 17:31:17 -07:00
|
|
|
// and this StackOverflow answer by wangdq
|
|
|
|
//
|
|
|
|
// https://stackoverflow.com/a/39684775
|
|
|
|
//
|
2024-08-21 13:01:33 -07:00
|
|
|
|
|
|
|
extern crate js_sys;
|
2024-08-25 21:40:46 -07:00
|
|
|
use core::array;
|
2024-09-09 19:41:15 -07:00
|
|
|
use nalgebra::{DMatrix, DVector, Rotation3, Vector3};
|
2024-08-26 16:06:37 -07:00
|
|
|
use sycamore::{prelude::*, motion::create_raf, rt::{JsCast, JsValue}};
|
2024-09-09 19:41:15 -07:00
|
|
|
use web_sys::{
|
|
|
|
console,
|
|
|
|
window,
|
|
|
|
KeyboardEvent,
|
|
|
|
WebGl2RenderingContext,
|
|
|
|
WebGlProgram,
|
|
|
|
WebGlShader,
|
|
|
|
WebGlUniformLocation
|
|
|
|
};
|
2024-08-21 13:01:33 -07:00
|
|
|
|
2024-08-25 21:40:46 -07:00
|
|
|
mod engine;
|
|
|
|
|
2024-08-21 13:01:33 -07:00
|
|
|
fn compile_shader(
|
|
|
|
context: &WebGl2RenderingContext,
|
|
|
|
shader_type: u32,
|
|
|
|
source: &str,
|
|
|
|
) -> WebGlShader {
|
|
|
|
let shader = context.create_shader(shader_type).unwrap();
|
|
|
|
context.shader_source(&shader, source);
|
|
|
|
context.compile_shader(&shader);
|
|
|
|
shader
|
|
|
|
}
|
|
|
|
|
2024-08-25 22:22:14 -07:00
|
|
|
fn get_uniform_array_locations<const N: usize>(
|
|
|
|
context: &WebGl2RenderingContext,
|
|
|
|
program: &WebGlProgram,
|
|
|
|
var_name: &str,
|
2024-08-26 13:41:34 -07:00
|
|
|
member_name_opt: Option<&str>
|
2024-08-25 22:22:14 -07:00
|
|
|
) -> [Option<WebGlUniformLocation>; N] {
|
|
|
|
array::from_fn(|n| {
|
2024-08-26 13:41:34 -07:00
|
|
|
let name = match member_name_opt {
|
|
|
|
Some(member_name) => format!("{var_name}[{n}].{member_name}"),
|
|
|
|
None => format!("{var_name}[{n}]")
|
|
|
|
};
|
2024-08-25 22:22:14 -07:00
|
|
|
context.get_uniform_location(&program, name.as_str())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-21 17:31:17 -07:00
|
|
|
// load the given data into the vertex input of the given name
|
|
|
|
fn bind_vertex_attrib(
|
|
|
|
context: &WebGl2RenderingContext,
|
2024-08-21 22:36:56 -07:00
|
|
|
index: u32,
|
2024-08-21 17:31:17 -07:00
|
|
|
size: i32,
|
|
|
|
data: &[f32]
|
|
|
|
) {
|
|
|
|
// create a data buffer and bind it to ARRAY_BUFFER
|
|
|
|
let buffer = context.create_buffer().unwrap();
|
|
|
|
context.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(&buffer));
|
|
|
|
|
|
|
|
// load the given data into the buffer. the function `Float32Array::view`
|
|
|
|
// creates a raw view into our module's `WebAssembly.Memory` buffer.
|
|
|
|
// allocating more memory will change the buffer, invalidating the view.
|
|
|
|
// that means we have to make sure we don't allocate any memory until the
|
|
|
|
// view is dropped
|
|
|
|
unsafe {
|
|
|
|
context.buffer_data_with_array_buffer_view(
|
|
|
|
WebGl2RenderingContext::ARRAY_BUFFER,
|
|
|
|
&js_sys::Float32Array::view(&data),
|
|
|
|
WebGl2RenderingContext::STATIC_DRAW,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow the target attribute to be used
|
2024-08-21 22:36:56 -07:00
|
|
|
context.enable_vertex_attrib_array(index);
|
2024-08-21 17:31:17 -07:00
|
|
|
|
|
|
|
// take whatever's bound to ARRAY_BUFFER---here, the data buffer created
|
|
|
|
// above---and bind it to the target attribute
|
|
|
|
//
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer
|
|
|
|
//
|
|
|
|
context.vertex_attrib_pointer_with_i32(
|
2024-08-21 22:36:56 -07:00
|
|
|
index,
|
2024-08-21 17:31:17 -07:00
|
|
|
size,
|
|
|
|
WebGl2RenderingContext::FLOAT,
|
|
|
|
false, // don't normalize
|
|
|
|
0, // zero stride
|
|
|
|
0, // zero offset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-04 16:27:28 -07:00
|
|
|
fn push_gen_construction(
|
2024-09-04 12:58:55 -07:00
|
|
|
sphere_vec: &mut Vec<DVector<f64>>,
|
2024-09-06 18:57:59 -07:00
|
|
|
color_vec: &mut Vec<[f32; 3]>,
|
2024-09-04 12:58:55 -07:00
|
|
|
construction_to_world: &DMatrix<f64>,
|
|
|
|
ctrl_x: f64,
|
|
|
|
ctrl_y: f64,
|
|
|
|
radius_x: f64,
|
|
|
|
radius_y: f64
|
|
|
|
) {
|
2024-09-06 18:57:59 -07:00
|
|
|
// push spheres
|
2024-09-04 12:58:55 -07:00
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(0.5, 0.5, ctrl_x, radius_x));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(-0.5, -0.5, ctrl_y, radius_y));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(-0.5, 0.5, 0.0, 0.75));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(0.5, -0.5, 0.0, 0.5));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(0.0, 0.15, 1.0, 0.25));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(0.0, -0.15, -1.0, 0.25));
|
2024-09-06 18:57:59 -07:00
|
|
|
|
|
|
|
// push colors
|
|
|
|
color_vec.push([1.00_f32, 0.25_f32, 0.00_f32]);
|
|
|
|
color_vec.push([0.00_f32, 0.25_f32, 1.00_f32]);
|
|
|
|
color_vec.push([0.25_f32, 0.00_f32, 1.00_f32]);
|
|
|
|
color_vec.push([0.25_f32, 1.00_f32, 0.00_f32]);
|
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.00_f32]);
|
|
|
|
color_vec.push([0.00_f32, 0.75_f32, 0.50_f32]);
|
2024-09-04 12:58:55 -07:00
|
|
|
}
|
|
|
|
|
2024-09-04 16:27:28 -07:00
|
|
|
fn push_low_curv_construction(
|
|
|
|
sphere_vec: &mut Vec<DVector<f64>>,
|
2024-09-06 18:57:59 -07:00
|
|
|
color_vec: &mut Vec<[f32; 3]>,
|
2024-09-04 16:27:28 -07:00
|
|
|
construction_to_world: &DMatrix<f64>,
|
2024-09-06 18:57:59 -07:00
|
|
|
off1: f64,
|
|
|
|
off2: f64,
|
|
|
|
off3: f64,
|
|
|
|
curv1: f64,
|
|
|
|
curv2: f64,
|
|
|
|
curv3: f64,
|
2024-09-04 16:27:28 -07:00
|
|
|
) {
|
2024-09-06 18:57:59 -07:00
|
|
|
// push spheres
|
|
|
|
let a = 0.75_f64.sqrt();
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(0.0, 0.0, 0.0, 1.0));
|
2024-09-08 23:43:26 -07:00
|
|
|
sphere_vec.push(construction_to_world * engine::sphere_with_offset(0.0, 0.0, 1.0, 0.0, 0.0));
|
2024-09-06 18:57:59 -07:00
|
|
|
sphere_vec.push(construction_to_world * engine::sphere_with_offset(1.0, 0.0, 0.0, off1, curv1));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere_with_offset(-0.5, a, 0.0, off2, curv2));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere_with_offset(-0.5, -a, 0.0, off3, curv3));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(-4.0/3.0, 0.0, 0.0, 1.0/3.0));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(2.0/3.0, -4.0/3.0 * a, 0.0, 1.0/3.0));
|
|
|
|
sphere_vec.push(construction_to_world * engine::sphere(2.0/3.0, 4.0/3.0 * a, 0.0, 1.0/3.0));
|
|
|
|
|
|
|
|
// push colors
|
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
|
2024-09-08 23:43:26 -07:00
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
|
2024-09-06 18:57:59 -07:00
|
|
|
color_vec.push([1.00_f32, 0.00_f32, 0.25_f32]);
|
|
|
|
color_vec.push([0.25_f32, 1.00_f32, 0.00_f32]);
|
|
|
|
color_vec.push([0.00_f32, 0.25_f32, 1.00_f32]);
|
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
|
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
|
|
|
|
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
|
2024-09-04 16:27:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
enum Tab {
|
|
|
|
GenTab,
|
|
|
|
LowCurvTab
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:01:33 -07:00
|
|
|
fn main() {
|
|
|
|
// set up a config option that forwards panic messages to `console.error`
|
|
|
|
#[cfg(feature = "console_error_panic_hook")]
|
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
|
|
|
|
sycamore::render(|| {
|
2024-09-04 16:27:28 -07:00
|
|
|
// tab selection
|
|
|
|
let tab_selection = create_signal(Tab::GenTab);
|
|
|
|
|
2024-09-09 19:41:15 -07:00
|
|
|
// navigation
|
|
|
|
let pitch_up = create_signal(0.0);
|
|
|
|
let pitch_down = create_signal(0.0);
|
|
|
|
let yaw_right = create_signal(0.0);
|
|
|
|
let yaw_left = create_signal(0.0);
|
|
|
|
|
2024-09-04 16:27:28 -07:00
|
|
|
// controls for general example
|
|
|
|
let gen_controls = create_node_ref();
|
2024-08-22 22:08:34 -07:00
|
|
|
let ctrl_x = create_signal(0.0);
|
|
|
|
let ctrl_y = create_signal(0.0);
|
2024-08-24 01:38:06 -07:00
|
|
|
let radius_x = create_signal(1.0);
|
|
|
|
let radius_y = create_signal(1.0);
|
2024-09-04 16:27:28 -07:00
|
|
|
|
|
|
|
// controls for low-curvature example
|
|
|
|
let low_curv_controls = create_node_ref();
|
2024-09-06 18:57:59 -07:00
|
|
|
let curv1 = create_signal(0.0);
|
|
|
|
let curv2 = create_signal(0.0);
|
|
|
|
let curv3 = create_signal(0.0);
|
|
|
|
let off1 = create_signal(1.0);
|
|
|
|
let off2 = create_signal(1.0);
|
|
|
|
let off3 = create_signal(1.0);
|
2024-09-04 16:27:28 -07:00
|
|
|
|
|
|
|
// shared controls
|
2024-08-24 01:01:13 -07:00
|
|
|
let opacity = create_signal(0.5);
|
2024-08-24 11:00:50 -07:00
|
|
|
let highlight = create_signal(0.2);
|
2024-08-27 18:39:58 -07:00
|
|
|
let turntable = create_signal(false);
|
|
|
|
let layer_threshold = create_signal(0.0); /* DEBUG */
|
|
|
|
let debug_mode = create_signal(false); /* DEBUG */
|
2024-08-25 21:40:46 -07:00
|
|
|
|
2024-08-26 23:39:51 -07:00
|
|
|
/* INSTRUMENTS */
|
2024-08-28 01:47:38 -07:00
|
|
|
const SAMPLE_PERIOD: i32 = 60;
|
2024-08-27 18:39:58 -07:00
|
|
|
let mut last_sample_time = 0.0;
|
2024-08-26 23:39:51 -07:00
|
|
|
let mut frames_since_last_sample = 0;
|
2024-08-27 18:39:58 -07:00
|
|
|
let mean_frame_interval = create_signal(0.0);
|
2024-08-26 23:39:51 -07:00
|
|
|
|
2024-08-25 21:40:46 -07:00
|
|
|
// display
|
2024-08-21 13:01:33 -07:00
|
|
|
let display = create_node_ref();
|
|
|
|
|
2024-08-27 13:55:08 -07:00
|
|
|
// change listener
|
|
|
|
let scene_changed = create_signal(true);
|
|
|
|
create_effect(move || {
|
2024-09-04 16:27:28 -07:00
|
|
|
// track tab selection
|
|
|
|
tab_selection.track();
|
|
|
|
|
|
|
|
// track controls for general example
|
2024-08-27 13:55:08 -07:00
|
|
|
ctrl_x.track();
|
|
|
|
ctrl_y.track();
|
|
|
|
radius_x.track();
|
|
|
|
radius_y.track();
|
2024-09-04 16:27:28 -07:00
|
|
|
|
|
|
|
// track controls for low-curvature example
|
2024-09-06 18:57:59 -07:00
|
|
|
curv1.track();
|
|
|
|
curv2.track();
|
|
|
|
curv3.track();
|
|
|
|
off1.track();
|
|
|
|
off2.track();
|
|
|
|
off3.track();
|
2024-09-04 16:27:28 -07:00
|
|
|
|
|
|
|
// track shared controls
|
2024-08-27 13:55:08 -07:00
|
|
|
opacity.track();
|
|
|
|
highlight.track();
|
2024-08-27 18:39:58 -07:00
|
|
|
turntable.track();
|
2024-08-27 13:55:08 -07:00
|
|
|
layer_threshold.track();
|
|
|
|
debug_mode.track();
|
|
|
|
|
|
|
|
scene_changed.set(true);
|
|
|
|
});
|
|
|
|
|
2024-08-21 13:01:33 -07:00
|
|
|
on_mount(move || {
|
2024-09-04 16:27:28 -07:00
|
|
|
// tab listener
|
|
|
|
create_effect(move || {
|
|
|
|
// get the control panel nodes
|
|
|
|
let gen_controls_node = gen_controls.get::<DomNode>();
|
|
|
|
let low_curv_controls_node = low_curv_controls.get::<DomNode>();
|
|
|
|
|
|
|
|
// hide all the control panels
|
|
|
|
gen_controls_node.add_class("hidden");
|
|
|
|
low_curv_controls_node.add_class("hidden");
|
|
|
|
|
|
|
|
// show the selected control panel
|
|
|
|
match tab_selection.get() {
|
|
|
|
Tab::GenTab => gen_controls_node.remove_class("hidden"),
|
|
|
|
Tab::LowCurvTab => low_curv_controls_node.remove_class("hidden")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-06 18:57:59 -07:00
|
|
|
// create list of construction elements
|
2024-08-28 16:06:33 -07:00
|
|
|
const SPHERE_MAX: usize = 200;
|
2024-08-25 21:40:46 -07:00
|
|
|
let mut sphere_vec = Vec::<DVector<f64>>::new();
|
2024-09-06 18:57:59 -07:00
|
|
|
let mut color_vec = Vec::<[f32; 3]>::new();
|
2024-08-25 21:40:46 -07:00
|
|
|
|
2024-08-27 18:39:58 -07:00
|
|
|
// timing
|
|
|
|
let mut last_time = 0.0;
|
|
|
|
|
|
|
|
// scene parameters
|
2024-09-09 19:41:15 -07:00
|
|
|
const NAV_SPEED: f64 = 0.4; // in radians per second
|
|
|
|
const TURNTABLE_SPEED: f64 = 0.1; // in radians per second
|
|
|
|
let mut orientation = DMatrix::<f64>::identity(5, 5);
|
|
|
|
let mut rotation = DMatrix::<f64>::identity(5, 5);
|
|
|
|
let location = {
|
|
|
|
const LEN: f64 = -5.0;
|
|
|
|
const LEN_SQ: f64 = LEN*LEN;
|
|
|
|
DMatrix::from_column_slice(5, 5, &[
|
|
|
|
1.0, 0.0, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0, 0.0, LEN,
|
|
|
|
0.0, 0.0, 2.0*LEN, 1.0, LEN_SQ,
|
|
|
|
0.0, 0.0, 0.0, 0.0, 1.0
|
|
|
|
])
|
|
|
|
};
|
2024-08-27 18:39:58 -07:00
|
|
|
|
2024-08-26 23:39:51 -07:00
|
|
|
/* INSTRUMENTS */
|
|
|
|
let performance = window().unwrap().performance().unwrap();
|
|
|
|
|
2024-08-21 13:01:33 -07:00
|
|
|
// get the display canvas
|
|
|
|
let canvas = display
|
|
|
|
.get::<DomNode>()
|
|
|
|
.unchecked_into::<web_sys::HtmlCanvasElement>();
|
|
|
|
let ctx = canvas
|
|
|
|
.get_context("webgl2")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<WebGl2RenderingContext>()
|
|
|
|
.unwrap();
|
|
|
|
|
2024-08-21 17:31:17 -07:00
|
|
|
// compile and attach the vertex and fragment shaders
|
2024-08-21 13:01:33 -07:00
|
|
|
let vertex_shader = compile_shader(
|
|
|
|
&ctx,
|
|
|
|
WebGl2RenderingContext::VERTEX_SHADER,
|
2024-08-24 11:26:53 -07:00
|
|
|
include_str!("identity.vert"),
|
2024-08-21 13:01:33 -07:00
|
|
|
);
|
|
|
|
let fragment_shader = compile_shader(
|
|
|
|
&ctx,
|
|
|
|
WebGl2RenderingContext::FRAGMENT_SHADER,
|
2024-08-24 11:26:53 -07:00
|
|
|
include_str!("inversive.frag"),
|
2024-08-21 13:01:33 -07:00
|
|
|
);
|
|
|
|
let program = ctx.create_program().unwrap();
|
|
|
|
ctx.attach_shader(&program, &vertex_shader);
|
|
|
|
ctx.attach_shader(&program, &fragment_shader);
|
|
|
|
ctx.link_program(&program);
|
|
|
|
let link_status = ctx
|
|
|
|
.get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
|
|
|
|
.as_bool()
|
|
|
|
.unwrap();
|
|
|
|
let link_msg = if link_status {
|
|
|
|
"Linked successfully"
|
|
|
|
} else {
|
|
|
|
"Linking failed"
|
|
|
|
};
|
|
|
|
console::log_1(&JsValue::from(link_msg));
|
|
|
|
ctx.use_program(Some(&program));
|
|
|
|
|
2024-08-26 00:43:42 -07:00
|
|
|
/* DEBUG */
|
|
|
|
// print the maximum number of vectors that can be passed as
|
|
|
|
// uniforms to a fragment shader. the OpenGL ES 3.0 standard
|
|
|
|
// requires this maximum to be at least 224, as discussed in the
|
|
|
|
// documentation of the GL_MAX_FRAGMENT_UNIFORM_VECTORS parameter
|
|
|
|
// here:
|
|
|
|
//
|
|
|
|
// https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glGet.xhtml
|
|
|
|
//
|
|
|
|
// there are also other size limits. for example, on Aaron's
|
|
|
|
// machine, the the length of a float or genType array seems to be
|
|
|
|
// capped at 1024 elements
|
|
|
|
console::log_2(
|
|
|
|
&ctx.get_parameter(WebGl2RenderingContext::MAX_FRAGMENT_UNIFORM_VECTORS).unwrap(),
|
|
|
|
&JsValue::from("uniform vectors available")
|
|
|
|
);
|
|
|
|
|
2024-08-21 23:07:14 -07:00
|
|
|
// find indices of vertex attributes and uniforms
|
2024-08-26 00:43:42 -07:00
|
|
|
let position_index = ctx.get_attrib_location(&program, "position") as u32;
|
|
|
|
let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt");
|
2024-08-25 22:22:14 -07:00
|
|
|
let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
|
2024-08-26 13:41:34 -07:00
|
|
|
&ctx, &program, "sphere_list", Some("sp")
|
2024-08-25 21:40:46 -07:00
|
|
|
);
|
2024-08-25 22:22:14 -07:00
|
|
|
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>(
|
2024-08-26 13:41:34 -07:00
|
|
|
&ctx, &program, "sphere_list", Some("lt")
|
|
|
|
);
|
|
|
|
let color_locs = get_uniform_array_locations::<SPHERE_MAX>(
|
|
|
|
&ctx, &program, "color_list", None
|
2024-08-25 21:40:46 -07:00
|
|
|
);
|
2024-08-22 22:08:34 -07:00
|
|
|
let resolution_loc = ctx.get_uniform_location(&program, "resolution");
|
|
|
|
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
|
2024-08-25 21:40:46 -07:00
|
|
|
let ctrl_loc = ctx.get_uniform_location(&program, "ctrl"); /* DEBUG */
|
|
|
|
let radius_loc = ctx.get_uniform_location(&program, "radius"); /* DEBUG */
|
2024-08-23 00:16:41 -07:00
|
|
|
let opacity_loc = ctx.get_uniform_location(&program, "opacity");
|
2024-08-24 11:00:50 -07:00
|
|
|
let highlight_loc = ctx.get_uniform_location(&program, "highlight");
|
2024-08-23 12:56:54 -07:00
|
|
|
let layer_threshold_loc = ctx.get_uniform_location(&program, "layer_threshold");
|
2024-08-26 13:51:01 -07:00
|
|
|
let debug_mode_loc = ctx.get_uniform_location(&program, "debug_mode");
|
2024-08-21 22:36:56 -07:00
|
|
|
|
2024-08-21 17:31:17 -07:00
|
|
|
// create a vertex array and bind it to the graphics context
|
|
|
|
let vertex_array = ctx.create_vertex_array().unwrap();
|
|
|
|
ctx.bind_vertex_array(Some(&vertex_array));
|
|
|
|
|
2024-08-22 22:08:34 -07:00
|
|
|
// set the vertex positions
|
|
|
|
const VERTEX_CNT: usize = 6;
|
|
|
|
let positions: [f32; 3*VERTEX_CNT] = [
|
|
|
|
// northwest triangle
|
|
|
|
-1.0, -1.0, 0.0,
|
|
|
|
-1.0, 1.0, 0.0,
|
|
|
|
1.0, 1.0, 0.0,
|
|
|
|
// southeast triangle
|
|
|
|
-1.0, -1.0, 0.0,
|
|
|
|
1.0, 1.0, 0.0,
|
|
|
|
1.0, -1.0, 0.0
|
2024-08-21 23:07:14 -07:00
|
|
|
];
|
2024-08-22 22:08:34 -07:00
|
|
|
bind_vertex_attrib(&ctx, position_index, 3, &positions);
|
|
|
|
|
2024-08-21 13:01:33 -07:00
|
|
|
// set up a repainting routine
|
2024-08-27 13:55:08 -07:00
|
|
|
let (_, start_animation_loop, _) = create_raf(move || {
|
2024-08-27 18:39:58 -07:00
|
|
|
// get the time step
|
|
|
|
let time = performance.now();
|
|
|
|
let time_step = 0.001*(time - last_time);
|
|
|
|
last_time = time;
|
|
|
|
|
2024-09-09 19:41:15 -07:00
|
|
|
// get the navigation state
|
|
|
|
let pitch_up_val = pitch_up.get();
|
|
|
|
let pitch_down_val = pitch_down.get();
|
|
|
|
let yaw_right_val = yaw_right.get();
|
|
|
|
let yaw_left_val = yaw_left.get();
|
2024-08-27 18:39:58 -07:00
|
|
|
let turntable_val = turntable.get();
|
2024-09-09 19:41:15 -07:00
|
|
|
|
|
|
|
// update the construction's orientation
|
|
|
|
let ang_vel = {
|
|
|
|
let pitch = pitch_up_val - pitch_down_val;
|
|
|
|
let yaw = yaw_right_val - yaw_left_val;
|
|
|
|
let ang_vel_from_keyboard =
|
|
|
|
if pitch != 0.0 || yaw != 0.0 {
|
|
|
|
NAV_SPEED * Vector3::new(-pitch, yaw, 0.0).normalize()
|
|
|
|
} else {
|
|
|
|
Vector3::zeros()
|
|
|
|
};
|
|
|
|
let ang_vel_from_turntable =
|
|
|
|
if turntable_val {
|
|
|
|
Vector3::new(0.0, TURNTABLE_SPEED, 0.0)
|
|
|
|
} else {
|
|
|
|
Vector3::zeros()
|
|
|
|
};
|
|
|
|
ang_vel_from_keyboard + ang_vel_from_turntable
|
|
|
|
};
|
|
|
|
let mut rotation_sp = rotation.fixed_view_mut::<3, 3>(0, 0);
|
|
|
|
rotation_sp.copy_from(
|
|
|
|
Rotation3::from_scaled_axis(time_step * ang_vel).matrix()
|
|
|
|
);
|
|
|
|
orientation = &rotation * &orientation;
|
2024-08-27 18:39:58 -07:00
|
|
|
|
2024-08-27 13:55:08 -07:00
|
|
|
if scene_changed.get() {
|
|
|
|
/* INSTRUMENTS */
|
2024-08-27 18:39:58 -07:00
|
|
|
// measure mean frame interval
|
2024-08-27 13:55:08 -07:00
|
|
|
frames_since_last_sample += 1;
|
|
|
|
if frames_since_last_sample >= SAMPLE_PERIOD {
|
2024-08-27 18:39:58 -07:00
|
|
|
mean_frame_interval.set((time - last_sample_time) / (SAMPLE_PERIOD as f64));
|
|
|
|
last_sample_time = time;
|
2024-08-27 13:55:08 -07:00
|
|
|
frames_since_last_sample = 0;
|
|
|
|
}
|
|
|
|
|
2024-09-09 19:41:15 -07:00
|
|
|
// find the map from construction space to world space
|
|
|
|
let construction_to_world = &location * &orientation;
|
2024-08-27 18:39:58 -07:00
|
|
|
|
2024-08-27 13:55:08 -07:00
|
|
|
// update the construction
|
|
|
|
sphere_vec.clear();
|
2024-09-06 18:57:59 -07:00
|
|
|
color_vec.clear();
|
2024-09-04 16:27:28 -07:00
|
|
|
match tab_selection.get() {
|
|
|
|
Tab::GenTab => push_gen_construction(
|
|
|
|
&mut sphere_vec,
|
2024-09-06 18:57:59 -07:00
|
|
|
&mut color_vec,
|
2024-09-04 16:27:28 -07:00
|
|
|
&construction_to_world,
|
|
|
|
ctrl_x.get(), ctrl_y.get(),
|
|
|
|
radius_x.get(), radius_y.get()
|
|
|
|
),
|
|
|
|
Tab::LowCurvTab => push_low_curv_construction(
|
|
|
|
&mut sphere_vec,
|
2024-09-06 18:57:59 -07:00
|
|
|
&mut color_vec,
|
2024-09-04 16:27:28 -07:00
|
|
|
&construction_to_world,
|
2024-09-06 18:57:59 -07:00
|
|
|
off1.get(), off2.get(), off3.get(),
|
|
|
|
curv1.get(), curv2.get(), curv3.get(),
|
2024-09-04 16:27:28 -07:00
|
|
|
)
|
|
|
|
};
|
2024-08-27 13:55:08 -07:00
|
|
|
|
|
|
|
// set the resolution
|
|
|
|
let width = canvas.width() as f32;
|
|
|
|
let height = canvas.height() as f32;
|
|
|
|
ctx.uniform2f(resolution_loc.as_ref(), width, height);
|
|
|
|
ctx.uniform1f(shortdim_loc.as_ref(), width.min(height));
|
|
|
|
|
|
|
|
// pass the construction
|
|
|
|
ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_vec.len() as i32);
|
|
|
|
for n in 0..sphere_vec.len() {
|
|
|
|
let v = &sphere_vec[n];
|
|
|
|
ctx.uniform3f(
|
|
|
|
sphere_sp_locs[n].as_ref(),
|
|
|
|
v[0] as f32, v[1] as f32, v[2] as f32
|
|
|
|
);
|
|
|
|
ctx.uniform2f(
|
|
|
|
sphere_lt_locs[n].as_ref(),
|
|
|
|
v[3] as f32, v[4] as f32
|
|
|
|
);
|
|
|
|
ctx.uniform3fv_with_f32_array(
|
|
|
|
color_locs[n].as_ref(),
|
|
|
|
&color_vec[n]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the control parameters
|
|
|
|
ctx.uniform2f(ctrl_loc.as_ref(), ctrl_x.get() as f32, ctrl_y.get() as f32); /* DEBUG */
|
|
|
|
ctx.uniform2f(radius_loc.as_ref(), radius_x.get() as f32, radius_y.get() as f32); /* DEBUG */
|
|
|
|
ctx.uniform1f(opacity_loc.as_ref(), opacity.get() as f32);
|
|
|
|
ctx.uniform1f(highlight_loc.as_ref(), highlight.get() as f32);
|
|
|
|
ctx.uniform1i(layer_threshold_loc.as_ref(), layer_threshold.get() as i32);
|
|
|
|
ctx.uniform1i(debug_mode_loc.as_ref(), debug_mode.get() as i32);
|
|
|
|
|
|
|
|
// draw the scene
|
|
|
|
ctx.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, VERTEX_CNT as i32);
|
|
|
|
|
|
|
|
// clear scene change flag
|
2024-09-09 19:41:15 -07:00
|
|
|
scene_changed.set(
|
|
|
|
pitch_up_val != 0.0
|
|
|
|
|| pitch_down_val != 0.0
|
|
|
|
|| yaw_left_val != 0.0
|
|
|
|
|| yaw_right_val != 0.0
|
|
|
|
|| turntable_val
|
|
|
|
);
|
2024-08-27 13:55:08 -07:00
|
|
|
} else {
|
2024-08-26 23:39:51 -07:00
|
|
|
frames_since_last_sample = 0;
|
2024-08-27 18:39:58 -07:00
|
|
|
mean_frame_interval.set(-1.0);
|
2024-08-26 23:39:51 -07:00
|
|
|
}
|
2024-08-21 13:01:33 -07:00
|
|
|
});
|
2024-08-27 13:55:08 -07:00
|
|
|
start_animation_loop();
|
2024-08-21 13:01:33 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
view! {
|
|
|
|
div(id="app") {
|
2024-09-04 16:27:28 -07:00
|
|
|
div(class="tab-pane") {
|
|
|
|
label {
|
|
|
|
"General"
|
|
|
|
input(
|
|
|
|
type="radio",
|
|
|
|
name="tab",
|
|
|
|
prop:checked=tab_selection.get() == Tab::GenTab,
|
|
|
|
on:click=move |_| tab_selection.set(Tab::GenTab)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
"Low curvature"
|
|
|
|
input(
|
|
|
|
type="radio",
|
|
|
|
name="tab",
|
|
|
|
prop:checked=tab_selection.get() == Tab::LowCurvTab,
|
|
|
|
on:change=move |_| tab_selection.set(Tab::LowCurvTab)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-08-27 18:39:58 -07:00
|
|
|
div { "Mean frame interval: " (mean_frame_interval.get()) " ms" }
|
2024-09-09 19:41:15 -07:00
|
|
|
canvas(
|
|
|
|
ref=display,
|
|
|
|
width=600,
|
|
|
|
height=600,
|
|
|
|
tabindex=0,
|
|
|
|
on:keydown=move |event: KeyboardEvent| {
|
|
|
|
let mut navigating = true;
|
|
|
|
match event.key().as_str() {
|
|
|
|
"ArrowUp" => pitch_up.set(1.0),
|
|
|
|
"ArrowDown" => pitch_down.set(1.0),
|
|
|
|
"ArrowRight" => yaw_right.set(1.0),
|
|
|
|
"ArrowLeft" => yaw_left.set(1.0),
|
|
|
|
_ => navigating = false
|
|
|
|
};
|
|
|
|
if navigating {
|
|
|
|
scene_changed.set(true);
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
on:keyup=move |event: KeyboardEvent| {
|
|
|
|
let mut navigating = true;
|
|
|
|
match event.key().as_str() {
|
|
|
|
"ArrowUp" => pitch_up.set(0.0),
|
|
|
|
"ArrowDown" => pitch_down.set(0.0),
|
|
|
|
"ArrowRight" => yaw_right.set(0.0),
|
|
|
|
"ArrowLeft" => yaw_left.set(0.0),
|
|
|
|
_ => navigating = false
|
|
|
|
};
|
|
|
|
if navigating {
|
|
|
|
scene_changed.set(true);
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
on:blur=move |_| {
|
|
|
|
pitch_up.set(0.0);
|
|
|
|
pitch_down.set(0.0);
|
|
|
|
yaw_right.set(0.0);
|
|
|
|
yaw_left.set(0.0);
|
|
|
|
}
|
|
|
|
)
|
2024-09-04 16:27:28 -07:00
|
|
|
div(ref=gen_controls) {
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 0 depth" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=-1.0,
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=ctrl_x
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 1 depth" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=-1.0,
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=ctrl_y
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 0 radius" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.5,
|
|
|
|
max=1.5,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=radius_x
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 1 radius" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.5,
|
|
|
|
max=1.5,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=radius_y
|
|
|
|
)
|
|
|
|
}
|
2024-08-26 01:47:53 -07:00
|
|
|
}
|
2024-09-04 16:27:28 -07:00
|
|
|
div(ref=low_curv_controls) {
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 1 offset" }
|
2024-09-06 18:57:59 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=-1.0,
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=off1
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 2 offset" }
|
2024-09-06 18:57:59 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=-1.0,
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=off2
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 3 offset" }
|
2024-09-06 18:57:59 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=-1.0,
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=off3
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 1 curvature" }
|
2024-09-06 18:57:59 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.0,
|
|
|
|
max=2.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=curv1
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 2 curvature" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.0,
|
|
|
|
max=2.0,
|
|
|
|
step=0.001,
|
2024-09-06 18:57:59 -07:00
|
|
|
bind:valueAsNumber=curv2
|
2024-09-04 16:27:28 -07:00
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Sphere 3 curvature" }
|
2024-09-04 16:27:28 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.0,
|
|
|
|
max=2.0,
|
|
|
|
step=0.001,
|
2024-09-06 18:57:59 -07:00
|
|
|
bind:valueAsNumber=curv3
|
2024-09-04 16:27:28 -07:00
|
|
|
)
|
|
|
|
}
|
2024-08-26 01:47:53 -07:00
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Opacity" }
|
2024-08-26 01:47:53 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=opacity
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Highlight" }
|
2024-08-26 01:47:53 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=highlight
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Turntable" }
|
2024-08-27 18:39:58 -07:00
|
|
|
input(
|
|
|
|
type="checkbox",
|
|
|
|
bind:checked=turntable
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Layer threshold" }
|
2024-08-26 01:47:53 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=5.0,
|
|
|
|
step=1.0,
|
|
|
|
bind:valueAsNumber=layer_threshold
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 00:32:29 -07:00
|
|
|
label(class="control") {
|
|
|
|
span { "Debug mode" }
|
2024-08-26 01:47:53 -07:00
|
|
|
input(
|
|
|
|
type="checkbox",
|
2024-08-26 13:51:01 -07:00
|
|
|
bind:checked=debug_mode
|
2024-08-26 01:47:53 -07:00
|
|
|
)
|
|
|
|
}
|
2024-08-21 13:01:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|