Skip to content

Commit

Permalink
Set filters from js
Browse files Browse the repository at this point in the history
  • Loading branch information
bengosney committed Apr 24, 2023
1 parent 888d507 commit b3a5055
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 21 additions & 1 deletion wasm-lib/src/convolutions.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
use crate::vec3::Vec3;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}

#[derive(Clone, PartialEq)]
pub struct Kernel<T:Copy> {
pub struct Kernel<T> {
shape: usize,
data: Vec<T>,
half_range: usize,
normalize: bool
}

#[wasm_bindgen]
pub struct ImageFilter {
kernel: Kernel<i16>
}

#[wasm_bindgen]
impl ImageFilter {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<i16>, normalize: bool) -> Self {
Self { kernel: Kernel::new(data, normalize) }
}
}

impl ImageFilter {
pub fn get_kernel(&self) -> &Kernel<i16> {
&self.kernel
}
}

impl<T: Copy> Kernel<T> {
pub fn new(data: Vec<T>, normalize: bool) -> Self {
let shape = (data.len() as f32).sqrt().ceil() as usize;
Expand Down
5 changes: 5 additions & 0 deletions wasm-lib/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::Clamped;
use web_sys::{CanvasRenderingContext2d, ImageData};

use crate::convolutions::ImageFilter;
use crate::convolutions::Kernel;
use crate::rgb::RGB;
use crate::vec3::Ray;
Expand Down Expand Up @@ -64,6 +65,10 @@ impl Scene {
}
}

pub fn add_filter(&mut self, filter: ImageFilter) {
self.filters.push(filter.get_kernel().clone());
}

pub fn add_entity(&mut self, entity: Entity) {
self.entities.push(entity);
}
Expand Down

0 comments on commit b3a5055

Please sign in to comment.