You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.6 KiB
86 lines
2.6 KiB
extern crate easel3d;
|
|
|
|
mod easel3d_wasm;
|
|
use easel3d_wasm::{WasmCanvas, Color};
|
|
|
|
use easel3d::easel::canvas::Canvas;
|
|
use easel3d::easel::fillable::Fillable;
|
|
|
|
use easel3d::math::transform::TMatrix;
|
|
use easel3d::math::vector::Vector;
|
|
|
|
use easel3d::space::camera::Camera;
|
|
use easel3d::space::light::DirectLight;
|
|
use easel3d::space::polyeder::Polyeder;
|
|
use easel3d::space::primitives::Primitives;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|
// allocator.
|
|
#[cfg(feature = "wee_alloc")]
|
|
#[global_allocator]
|
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
|
|
#[wasm_bindgen]
|
|
pub struct View3d { canvas :WasmCanvas
|
|
, degree :i32
|
|
, tetrahedron :Polyeder<f64>
|
|
, cube :Polyeder<f64>
|
|
, camera :Camera<f64>
|
|
, light :DirectLight<f64> }
|
|
|
|
#[wasm_bindgen]
|
|
impl View3d {
|
|
pub fn new(width :u16, height :u16) -> Self {
|
|
let light_vector = Vector(0.0, 0.0, 1.0);
|
|
let canvas = WasmCanvas::new(width, height);
|
|
let camera = Camera::<f64>::new(&canvas, 45);
|
|
|
|
Self { canvas: canvas
|
|
, degree: 0
|
|
, tetrahedron: Polyeder::tetrahedron(100.0)
|
|
, cube: Polyeder::cube(56.25)
|
|
, camera: camera
|
|
, light: DirectLight::new(light_vector) }
|
|
}
|
|
|
|
pub fn width(&self) -> u16 {
|
|
self.canvas.width()
|
|
}
|
|
|
|
pub fn height(&self) -> u16 {
|
|
self.canvas.height()
|
|
}
|
|
|
|
pub fn update(&mut self) {
|
|
let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
|
|
let rz = TMatrix::rotate_z(self.degree);
|
|
let rx = TMatrix::rotate_x(-self.degree*2);
|
|
let ry = TMatrix::rotate_y(-self.degree*2);
|
|
|
|
let rot1 = TMatrix::combine(vec!(rz, rx, t));
|
|
let rot2 = TMatrix::combine(vec!(rz, ry, t));
|
|
|
|
let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
|
|
, ( self.cube.transform(&rot2), 0x0000FF) );
|
|
|
|
let rlx = TMatrix::rotate_x(-self.degree/4);
|
|
let rly = TMatrix::rotate_y(-self.degree/1);
|
|
let light = self.light.transform(&TMatrix::combine(vec!(rlx, rly)));
|
|
|
|
self.degree = (self.degree + 1) % (4*360);
|
|
|
|
self.canvas.clear();
|
|
|
|
for (o, color) in objects {
|
|
for (pg, c) in o.project(&self.camera, &light, color) {
|
|
(&pg).fill(&mut self.canvas, c);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn image(&self) -> *const Color {
|
|
self.canvas.image()
|
|
}
|
|
}
|