Skip to content

Instantly share code, notes, and snippets.

@ndugger
Created September 22, 2020 00:51
Show Gist options
  • Save ndugger/642a29d29f4905770118fa18a9c798f6 to your computer and use it in GitHub Desktop.
Save ndugger/642a29d29f4905770118fa18a9c798f6 to your computer and use it in GitHub Desktop.
use wasm_bindgen::prelude::*;
/// Engine
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type Engine;
#[wasm_bindgen(constructor)]
pub fn new(canvas: &web_sys::Element) -> Engine;
#[wasm_bindgen(method, js_name = "runRenderLoop")]
pub fn run_render_loop(this: &Engine, render_closure: &JsValue);
}
/// Vector3
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type Vector3;
#[wasm_bindgen(constructor)]
pub fn new(x: f32, y: f32, z: f32) -> Vector3;
#[wasm_bindgen(method, setter)]
pub fn set_y(this: &Vector3, y: f32);
#[wasm_bindgen(static_method_of = Vector3, js_name = "Zero")]
pub fn zero() -> Vector3;
}
/// Scene
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type Scene;
#[wasm_bindgen(constructor)]
pub fn new(engine: &Engine) -> Scene;
#[wasm_bindgen(method)]
pub fn render(this: &Scene);
}
/// FreeCamera
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type FreeCamera;
#[wasm_bindgen(constructor)]
pub fn new(name: &str, position: Vector3, scene: &Scene) -> FreeCamera;
#[wasm_bindgen(method, js_name = "setTarget")]
pub fn set_target(this: &FreeCamera, target: Vector3);
#[wasm_bindgen(method, js_name = "attachControl")]
pub fn attach_control(this: &FreeCamera, canvas: &web_sys::Element, allow_default_actions: bool);
}
/// HemisphericLight
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type HemisphericLight;
#[wasm_bindgen(constructor)]
pub fn new(name: &str, position: Vector3, scene: &Scene) -> HemisphericLight;
#[wasm_bindgen(method, setter, js_name = "intensity")]
pub fn set_intensity(this: &HemisphericLight, intensity: f32);
}
/// Mesh
#[wasm_bindgen(module = "@babylonjs/core")]
extern "C" {
pub type Mesh;
#[wasm_bindgen(method, getter, js_name = "position")]
pub fn get_position(this: &Mesh) -> Vector3;
#[wasm_bindgen(static_method_of = Mesh, js_name = "CreateSphere")]
pub fn create_sphere(name: &str, segments: u32, diameter: f32, scene: &Scene, updatable: bool, side_orientation: u32) -> Mesh;
#[wasm_bindgen(static_method_of = Mesh, js_name = "CreateGround")]
pub fn create_ground(name: &str, width: f32, depth: f32, subdivisions: u32, scene: &Scene) -> Mesh;
}
#[macro_use]
extern crate lazy_static;
use babylon::{
Engine,
FreeCamera,
HemisphericLight,
Mesh,
Scene,
Vector3
};
use wasm_bindgen::{
closure::Closure,
prelude::*,
__rt::core::cell::RefCell,
};
use web_sys::{
Element,
window
};
pub mod babylon;
struct Application {
canvas: RefCell<Element>,
engine: RefCell<Engine>,
scene: RefCell<Scene>
}
#[wasm_bindgen(start)]
impl Application {
fn new() -> Self {
let environment = window().unwrap();
let elements = environment.document().unwrap();
let canvas = elements.get_element_by_id("canvas").unwrap();
let engine = Engine::new(&canvas);
let scene = Scene::new(&engine);
Application {
canvas: RefCell::new(canvas),
engine: RefCell::new(engine),
scene: RefCell::new(scene)
}
}
}
unsafe impl Send for Application {}
unsafe impl Sync for Application {}
lazy_static! {
static ref APPLICATION: Application = Application::new();
}
#[wasm_bindgen(start)]
pub fn main() {
let canvas = &APPLICATION.canvas.borrow();
let engine = &APPLICATION.engine.borrow();
let scene = &APPLICATION.scene.borrow();
let camera = FreeCamera::new("camera", Vector3::new(0.0, 5.0, -10.0), &scene);
camera.set_target(Vector3::zero());
camera.attach_control(&canvas, true);
let light = HemisphericLight::new("light", Vector3::new(0.0, 1.0, 0.0), &scene);
light.set_intensity(0.7);
let sphere = Mesh::create_sphere("sphere", 16, 2.0, &scene, false, 0);
sphere.get_position().set_y(2.0);
Mesh::create_ground("ground", 6.0, 6.0, 2, &scene);
let render_callback = Closure::wrap(Box::new(move || {
&APPLICATION.scene.borrow().render();
}) as Box<dyn FnMut()>);
engine.run_render_loop(render_callback.as_ref());
render_callback.forget();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment