forked from StudioInfinity/dyna3
refactor: Code formatting (#108)
Primarily, switch to using trailing commas. Also uniformizes commas with respect to switch branches, makes function call layout more consistent, line breaking more consistent, alphabetizes imports, uses the field init shorthand when possible, etc. Resolves #99. Co-authored-by: Aaron Fenyes <aaron.fenyes@fareycircles.ooo> Reviewed-on: StudioInfinity/dyna3#108 Co-authored-by: Vectornaut <vectornaut@nobody@nowhere.net> Co-committed-by: Vectornaut <vectornaut@nobody@nowhere.net>
This commit is contained in:
parent
2eba80fb69
commit
ef1a579ac0
12 changed files with 310 additions and 297 deletions
|
@ -12,12 +12,12 @@ use web_sys::{
|
|||
WebGlProgram,
|
||||
WebGlShader,
|
||||
WebGlUniformLocation,
|
||||
wasm_bindgen::{JsCast, JsValue}
|
||||
wasm_bindgen::{JsCast, JsValue},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
AppState,
|
||||
assembly::{Element, ElementColor, ElementMotion, Point, Sphere}
|
||||
assembly::{Element, ElementColor, ElementMotion, Point, Sphere},
|
||||
};
|
||||
|
||||
// --- color ---
|
||||
|
@ -37,15 +37,15 @@ fn combine_channels(color: ElementColor, opacity: f32) -> ColorWithOpacity {
|
|||
struct SceneSpheres {
|
||||
representations: Vec<DVector<f64>>,
|
||||
colors_with_opacity: Vec<ColorWithOpacity>,
|
||||
highlights: Vec<f32>
|
||||
highlights: Vec<f32>,
|
||||
}
|
||||
|
||||
impl SceneSpheres {
|
||||
fn new() -> SceneSpheres{
|
||||
fn new() -> SceneSpheres {
|
||||
SceneSpheres {
|
||||
representations: Vec::new(),
|
||||
colors_with_opacity: Vec::new(),
|
||||
highlights: Vec::new()
|
||||
highlights: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,10 @@ impl SceneSpheres {
|
|||
self.representations.len().try_into().expect("Number of spheres must fit in a 32-bit integer")
|
||||
}
|
||||
|
||||
fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32) {
|
||||
fn push(
|
||||
&mut self, representation: DVector<f64>,
|
||||
color: ElementColor, opacity: f32, highlight: f32,
|
||||
) {
|
||||
self.representations.push(representation);
|
||||
self.colors_with_opacity.push(combine_channels(color, opacity));
|
||||
self.highlights.push(highlight);
|
||||
|
@ -64,7 +67,7 @@ struct ScenePoints {
|
|||
representations: Vec<DVector<f64>>,
|
||||
colors_with_opacity: Vec<ColorWithOpacity>,
|
||||
highlights: Vec<f32>,
|
||||
selections: Vec<f32>
|
||||
selections: Vec<f32>,
|
||||
}
|
||||
|
||||
impl ScenePoints {
|
||||
|
@ -73,11 +76,14 @@ impl ScenePoints {
|
|||
representations: Vec::new(),
|
||||
colors_with_opacity: Vec::new(),
|
||||
highlights: Vec::new(),
|
||||
selections: Vec::new()
|
||||
selections: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32, selected: bool) {
|
||||
fn push(
|
||||
&mut self, representation: DVector<f64>,
|
||||
color: ElementColor, opacity: f32, highlight: f32, selected: bool,
|
||||
) {
|
||||
self.representations.push(representation);
|
||||
self.colors_with_opacity.push(combine_channels(color, opacity));
|
||||
self.highlights.push(highlight);
|
||||
|
@ -87,14 +93,14 @@ impl ScenePoints {
|
|||
|
||||
pub struct Scene {
|
||||
spheres: SceneSpheres,
|
||||
points: ScenePoints
|
||||
points: ScenePoints,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
fn new() -> Scene {
|
||||
Scene {
|
||||
spheres: SceneSpheres::new(),
|
||||
points: ScenePoints::new()
|
||||
points: ScenePoints::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +111,12 @@ pub trait DisplayItem {
|
|||
// the smallest positive depth, represented as a multiple of `dir`, where
|
||||
// the line generated by `dir` hits the element. returns `None` if the line
|
||||
// misses the element
|
||||
fn cast(&self, dir: Vector3<f64>, assembly_to_world: &DMatrix<f64>, pixel_size: f64) -> Option<f64>;
|
||||
fn cast(
|
||||
&self,
|
||||
dir: Vector3<f64>,
|
||||
assembly_to_world: &DMatrix<f64>,
|
||||
pixel_size: f64,
|
||||
) -> Option<f64>;
|
||||
}
|
||||
|
||||
impl DisplayItem for Sphere {
|
||||
|
@ -124,7 +135,12 @@ impl DisplayItem for Sphere {
|
|||
|
||||
// this method should be kept synchronized with `sphere_cast` in
|
||||
// `spheres.frag`, which does essentially the same thing on the GPU side
|
||||
fn cast(&self, dir: Vector3<f64>, assembly_to_world: &DMatrix<f64>, _pixel_size: f64) -> Option<f64> {
|
||||
fn cast(
|
||||
&self,
|
||||
dir: Vector3<f64>,
|
||||
assembly_to_world: &DMatrix<f64>,
|
||||
_pixel_size: f64,
|
||||
) -> Option<f64> {
|
||||
// if `a/b` is less than this threshold, we approximate
|
||||
// `a*u^2 + b*u + c` by the linear function `b*u + c`
|
||||
const DEG_THRESHOLD: f64 = 1e-9;
|
||||
|
@ -177,7 +193,12 @@ impl DisplayItem for Point {
|
|||
}
|
||||
|
||||
/* SCAFFOLDING */
|
||||
fn cast(&self, dir: Vector3<f64>, assembly_to_world: &DMatrix<f64>, pixel_size: f64) -> Option<f64> {
|
||||
fn cast(
|
||||
&self,
|
||||
dir: Vector3<f64>,
|
||||
assembly_to_world: &DMatrix<f64>,
|
||||
pixel_size: f64,
|
||||
) -> Option<f64> {
|
||||
let rep = self.representation.with_untracked(|rep| assembly_to_world * rep);
|
||||
if rep[2] < 0.0 {
|
||||
// this constant should be kept synchronized with `point.frag`
|
||||
|
@ -220,7 +241,7 @@ fn compile_shader(
|
|||
fn set_up_program(
|
||||
context: &WebGl2RenderingContext,
|
||||
vertex_shader_source: &str,
|
||||
fragment_shader_source: &str
|
||||
fragment_shader_source: &str,
|
||||
) -> WebGlProgram {
|
||||
// compile the shaders
|
||||
let vertex_shader = compile_shader(
|
||||
|
@ -260,12 +281,12 @@ fn get_uniform_array_locations<const N: usize>(
|
|||
context: &WebGl2RenderingContext,
|
||||
program: &WebGlProgram,
|
||||
var_name: &str,
|
||||
member_name_opt: Option<&str>
|
||||
member_name_opt: Option<&str>,
|
||||
) -> [Option<WebGlUniformLocation>; N] {
|
||||
array::from_fn(|n| {
|
||||
let name = match member_name_opt {
|
||||
Some(member_name) => format!("{var_name}[{n}].{member_name}"),
|
||||
None => format!("{var_name}[{n}]")
|
||||
None => format!("{var_name}[{n}]"),
|
||||
};
|
||||
context.get_uniform_location(&program, name.as_str())
|
||||
})
|
||||
|
@ -276,7 +297,7 @@ fn bind_to_attribute(
|
|||
context: &WebGl2RenderingContext,
|
||||
attr_index: u32,
|
||||
attr_size: i32,
|
||||
buffer: &Option<WebGlBuffer>
|
||||
buffer: &Option<WebGlBuffer>,
|
||||
) {
|
||||
context.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, buffer.as_ref());
|
||||
context.vertex_attrib_pointer_with_i32(
|
||||
|
@ -292,7 +313,7 @@ fn bind_to_attribute(
|
|||
// load the given data into a new vertex buffer object
|
||||
fn load_new_buffer(
|
||||
context: &WebGl2RenderingContext,
|
||||
data: &[f32]
|
||||
data: &[f32],
|
||||
) -> Option<WebGlBuffer> {
|
||||
// create a buffer and bind it to ARRAY_BUFFER
|
||||
let buffer = context.create_buffer();
|
||||
|
@ -319,7 +340,7 @@ fn bind_new_buffer_to_attribute(
|
|||
context: &WebGl2RenderingContext,
|
||||
attr_index: u32,
|
||||
attr_size: i32,
|
||||
data: &[f32]
|
||||
data: &[f32],
|
||||
) {
|
||||
let buffer = load_new_buffer(context, data);
|
||||
bind_to_attribute(context, attr_index, attr_size, &buffer);
|
||||
|
@ -341,9 +362,9 @@ fn event_dir(event: &MouseEvent) -> (Vector3<f64>, f64) {
|
|||
Vector3::new(
|
||||
FOCAL_SLOPE * (2.0*(f64::from(event.client_x()) - rect.left()) - width) / shortdim,
|
||||
FOCAL_SLOPE * (2.0*(rect.bottom() - f64::from(event.client_y())) - height) / shortdim,
|
||||
-1.0
|
||||
-1.0,
|
||||
),
|
||||
FOCAL_SLOPE * 2.0 / shortdim
|
||||
FOCAL_SLOPE * 2.0 / shortdim,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -443,14 +464,14 @@ pub fn Display() -> View {
|
|||
let sphere_program = set_up_program(
|
||||
&ctx,
|
||||
include_str!("identity.vert"),
|
||||
include_str!("spheres.frag")
|
||||
include_str!("spheres.frag"),
|
||||
);
|
||||
|
||||
// set up the point rendering program
|
||||
let point_program = set_up_program(
|
||||
&ctx,
|
||||
include_str!("point.vert"),
|
||||
include_str!("point.frag")
|
||||
include_str!("point.frag"),
|
||||
);
|
||||
|
||||
/* DEBUG */
|
||||
|
@ -467,7 +488,7 @@ pub fn Display() -> View {
|
|||
// capped at 1024 elements
|
||||
console::log_2(
|
||||
&ctx.get_parameter(WebGl2RenderingContext::MAX_FRAGMENT_UNIFORM_VECTORS).unwrap(),
|
||||
&JsValue::from("uniform vectors available")
|
||||
&JsValue::from("uniform vectors available"),
|
||||
);
|
||||
|
||||
// find the sphere program's vertex attribute
|
||||
|
@ -503,7 +524,7 @@ pub fn Display() -> View {
|
|||
// southeast triangle
|
||||
-1.0, -1.0, 0.0,
|
||||
1.0, 1.0, 0.0,
|
||||
1.0, -1.0, 0.0
|
||||
1.0, -1.0, 0.0,
|
||||
];
|
||||
let viewport_position_buffer = load_new_buffer(&ctx, &viewport_positions);
|
||||
|
||||
|
@ -596,7 +617,7 @@ pub fn Display() -> View {
|
|||
vec![
|
||||
ElementMotion {
|
||||
element: sel,
|
||||
velocity: elt_motion.as_view()
|
||||
velocity: elt_motion.as_view(),
|
||||
}
|
||||
]
|
||||
);
|
||||
|
@ -629,7 +650,7 @@ pub fn Display() -> View {
|
|||
0.0, 1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0, u,
|
||||
0.0, 0.0, 2.0*u, 1.0, u*u,
|
||||
0.0, 0.0, 0.0, 0.0, 1.0
|
||||
0.0, 0.0, 0.0, 0.0, 1.0,
|
||||
])
|
||||
};
|
||||
let asm_to_world = &location * &orientation;
|
||||
|
@ -668,19 +689,19 @@ pub fn Display() -> View {
|
|||
let v = &sphere_reps_world[n];
|
||||
ctx.uniform3fv_with_f32_array(
|
||||
sphere_sp_locs[n].as_ref(),
|
||||
v.rows(0, 3).as_slice()
|
||||
v.rows(0, 3).as_slice(),
|
||||
);
|
||||
ctx.uniform2fv_with_f32_array(
|
||||
sphere_lt_locs[n].as_ref(),
|
||||
v.rows(3, 2).as_slice()
|
||||
v.rows(3, 2).as_slice(),
|
||||
);
|
||||
ctx.uniform4fv_with_f32_array(
|
||||
sphere_color_locs[n].as_ref(),
|
||||
&scene.spheres.colors_with_opacity[n]
|
||||
&scene.spheres.colors_with_opacity[n],
|
||||
);
|
||||
ctx.uniform1f(
|
||||
sphere_highlight_locs[n].as_ref(),
|
||||
scene.spheres.highlights[n]
|
||||
scene.spheres.highlights[n],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -773,7 +794,7 @@ pub fn Display() -> View {
|
|||
"ArrowLeft" if shift => roll_ccw.set(value),
|
||||
"ArrowRight" => yaw_right.set(value),
|
||||
"ArrowLeft" => yaw_left.set(value),
|
||||
_ => navigating = false
|
||||
_ => navigating = false,
|
||||
};
|
||||
if navigating {
|
||||
scene_changed.set(true);
|
||||
|
@ -793,7 +814,7 @@ pub fn Display() -> View {
|
|||
"s" | "S" => translate_neg_y.set(value),
|
||||
"]" | "}" => shrink_neg.set(value),
|
||||
"[" | "{" => shrink_pos.set(value),
|
||||
_ => manipulating = false
|
||||
_ => manipulating = false,
|
||||
};
|
||||
if manipulating {
|
||||
event.prevent_default();
|
||||
|
@ -805,12 +826,12 @@ pub fn Display() -> View {
|
|||
// switch back to integer-valued parameters when that becomes possible
|
||||
// again
|
||||
canvas(
|
||||
ref=display,
|
||||
id="display",
|
||||
width="600",
|
||||
height="600",
|
||||
tabindex="0",
|
||||
on:keydown=move |event: KeyboardEvent| {
|
||||
ref = display,
|
||||
id = "display",
|
||||
width = "600",
|
||||
height = "600",
|
||||
tabindex = "0",
|
||||
on:keydown = move |event: KeyboardEvent| {
|
||||
if event.key() == "Shift" {
|
||||
// swap navigation inputs
|
||||
roll_cw.set(yaw_right.get());
|
||||
|
@ -836,7 +857,7 @@ pub fn Display() -> View {
|
|||
set_manip_signal(&event, 1.0);
|
||||
}
|
||||
},
|
||||
on:keyup=move |event: KeyboardEvent| {
|
||||
on:keyup = move |event: KeyboardEvent| {
|
||||
if event.key() == "Shift" {
|
||||
// swap navigation inputs
|
||||
yaw_right.set(roll_cw.get());
|
||||
|
@ -858,7 +879,7 @@ pub fn Display() -> View {
|
|||
set_manip_signal(&event, 0.0);
|
||||
}
|
||||
},
|
||||
on:blur=move |_| {
|
||||
on:blur = move |_| {
|
||||
pitch_up.set(0.0);
|
||||
pitch_down.set(0.0);
|
||||
yaw_right.set(0.0);
|
||||
|
@ -866,7 +887,7 @@ pub fn Display() -> View {
|
|||
roll_ccw.set(0.0);
|
||||
roll_cw.set(0.0);
|
||||
},
|
||||
on:click=move |event: MouseEvent| {
|
||||
on:click = move |event: MouseEvent| {
|
||||
// find the nearest element along the pointer direction
|
||||
let (dir, pixel_size) = event_dir(&event);
|
||||
console::log_1(&JsValue::from(dir.to_string()));
|
||||
|
@ -883,18 +904,18 @@ pub fn Display() -> View {
|
|||
clicked = Some((elt, depth))
|
||||
}
|
||||
},
|
||||
None => clicked = Some((elt, depth))
|
||||
}
|
||||
None => ()
|
||||
None => clicked = Some((elt, depth)),
|
||||
},
|
||||
None => (),
|
||||
};
|
||||
}
|
||||
|
||||
// if we clicked something, select it
|
||||
match clicked {
|
||||
Some((elt, _)) => state.select(&elt, event.shift_key()),
|
||||
None => state.selection.update(|sel| sel.clear())
|
||||
None => state.selection.update(|sel| sel.clear()),
|
||||
};
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue