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;
|
|
|
|
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
|
2024-08-21 23:07:14 -07:00
|
|
|
use web_sys::{console, WebGl2RenderingContext, WebGlShader};
|
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-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-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-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-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-23 12:56:54 -07:00
|
|
|
let layer_threshold = create_signal(0.0);
|
2024-08-21 13:01:33 -07:00
|
|
|
let display = create_node_ref();
|
|
|
|
|
|
|
|
on_mount(move || {
|
|
|
|
// 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-21 23:07:14 -07:00
|
|
|
// find indices of vertex attributes and uniforms
|
2024-08-21 22:36:56 -07:00
|
|
|
let position_index = ctx.get_attrib_location(&program, "position") as u32;
|
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");
|
|
|
|
let ctrl_loc = ctx.get_uniform_location(&program, "ctrl");
|
2024-08-24 01:38:06 -07:00
|
|
|
let radius_loc = ctx.get_uniform_location(&program, "radius");
|
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-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
|
|
|
|
create_effect(move || {
|
2024-08-23 00:16:41 -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));
|
|
|
|
|
2024-08-22 22:08:34 -07:00
|
|
|
// pass the control parameters
|
|
|
|
ctx.uniform2f(ctrl_loc.as_ref(), ctrl_x.get() as f32, ctrl_y.get() as f32);
|
2024-08-24 01:38:06 -07:00
|
|
|
ctx.uniform2f(radius_loc.as_ref(), radius_x.get() as f32, radius_y.get() as f32);
|
2024-08-23 00:16:41 -07:00
|
|
|
ctx.uniform1f(opacity_loc.as_ref(), opacity.get() as f32);
|
2024-08-24 11:00:50 -07:00
|
|
|
ctx.uniform1f(highlight_loc.as_ref(), highlight.get() as f32);
|
2024-08-23 12:56:54 -07:00
|
|
|
ctx.uniform1i(layer_threshold_loc.as_ref(), layer_threshold.get() as i32);
|
2024-08-22 00:04:58 -07:00
|
|
|
|
2024-08-24 11:31:22 -07:00
|
|
|
// draw the scene
|
2024-08-21 17:31:17 -07:00
|
|
|
ctx.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, VERTEX_CNT as i32);
|
2024-08-21 13:01:33 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
view! {
|
|
|
|
div(id="app") {
|
|
|
|
canvas(ref=display, width="600", height="600")
|
2024-08-22 00:04:58 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
2024-08-22 22:08:34 -07:00
|
|
|
min=-1.0,
|
2024-08-22 00:04:58 -07:00
|
|
|
max=1.0,
|
2024-08-23 12:56:54 -07:00
|
|
|
step=0.001,
|
2024-08-22 22:08:34 -07:00
|
|
|
bind:valueAsNumber=ctrl_x
|
2024-08-22 00:04:58 -07:00
|
|
|
)
|
2024-08-21 13:01:33 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
2024-08-22 22:08:34 -07:00
|
|
|
min=-1.0,
|
2024-08-21 13:01:33 -07:00
|
|
|
max=1.0,
|
2024-08-23 12:56:54 -07:00
|
|
|
step=0.001,
|
2024-08-22 22:08:34 -07:00
|
|
|
bind:valueAsNumber=ctrl_y
|
2024-08-21 13:01:33 -07:00
|
|
|
)
|
2024-08-24 01:38:06 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.5,
|
|
|
|
max=1.5,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=radius_x
|
|
|
|
)
|
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
min=0.5,
|
|
|
|
max=1.5,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=radius_y
|
|
|
|
)
|
2024-08-23 00:16:41 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=1.0,
|
2024-08-23 12:56:54 -07:00
|
|
|
step=0.001,
|
2024-08-23 00:16:41 -07:00
|
|
|
bind:valueAsNumber=opacity
|
|
|
|
)
|
2024-08-24 11:00:50 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=1.0,
|
|
|
|
step=0.001,
|
|
|
|
bind:valueAsNumber=highlight
|
|
|
|
)
|
2024-08-23 12:56:54 -07:00
|
|
|
input(
|
|
|
|
type="range",
|
|
|
|
max=3.0,
|
|
|
|
step=1.0,
|
|
|
|
bind:valueAsNumber=layer_threshold
|
|
|
|
)
|
2024-08-21 13:01:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|