neo_babylon/
math.rs

1use wasm_bindgen::prelude::wasm_bindgen;
2use js_sys::Reflect;
3use wasm_bindgen::JsValue;
4use std::ops;
5
6use impl_ops::*;
7
8use crate::get_set_jsvalue;
9
10#[wasm_bindgen]
11extern "C" {
12    /// A three-dimensional vector
13    pub type Vector3;
14
15    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
16    pub fn new(x: f64, y: f64, z: f64) -> Vector3;
17
18    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector3)]
19    pub fn One() -> Vector3;
20
21    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector3)]
22    pub fn Zero() -> Vector3;
23
24    #[wasm_bindgen(method, getter)]
25    pub fn x(this: &Vector3) -> f64;
26
27    #[wasm_bindgen(method, setter)]
28    pub fn set_x(this: &Vector3, value: f64);
29
30    #[wasm_bindgen(method, getter)]
31    pub fn y(this: &Vector3) -> f64;
32
33    #[wasm_bindgen(method, setter)]
34    pub fn set_y(this: &Vector3, value: f64);
35
36    #[wasm_bindgen(method, getter)]
37    pub fn z(this: &Vector3) -> f64;
38
39    #[wasm_bindgen(method, setter)]
40    pub fn set_z(this: &Vector3, value: f64);
41}
42
43impl<T> From<(T, T, T)> for Vector3 
44where T: Into<f64> + Copy {
45    fn from(value: (T, T, T)) -> Self {
46        Vector3::new(value.0.into(), value.1.into(), value.2.into())
47    }
48}
49
50impl Vector3 {
51    pub fn set(&self, x: f64, y: f64, z: f64) {
52        self.set_x(x);
53        self.set_y(y);
54        self.set_z(z);
55    }
56}
57
58impl_op_ex!(+ |a: &Vector3, b: &Vector3| -> Vector3 { Vector3::new(a.x() + b.x(), a.y() + b.y(), a.z() + b.z()) });
59impl_op_ex!(- |a: &Vector3, b: &Vector3| -> Vector3 { Vector3::new(a.x() - b.x(), a.y() - b.y(), a.z() - b.z()) });
60impl_op_ex!(* |a: &Vector3, b: f64| -> Vector3 { Vector3::new(a.x() * b, a.y() * b, a.z() * b) });
61impl_op_ex!(/ |a: &Vector3, b: f64| -> Vector3 { Vector3::new(a.x() / b, a.y() / b, a.z() / b) });
62
63#[wasm_bindgen]
64extern "C" {
65    /// A four-dimensional vector
66    pub type Vector4;
67
68    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
69    pub fn new(x: f64, y: f64, z: f64, w: f64) -> Vector4;
70
71    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector4)]
72    pub fn One() -> Vector4;
73
74    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector4)]
75    pub fn Zero() -> Vector4;
76
77    #[wasm_bindgen(method, getter)]
78    pub fn x(this: &Vector4) -> f64;
79
80    #[wasm_bindgen(method, setter)]
81    pub fn set_x(this: &Vector4, value: f64);
82
83    #[wasm_bindgen(method, getter)]
84    pub fn y(this: &Vector4) -> f64;
85
86    #[wasm_bindgen(method, setter)]
87    pub fn set_y(this: &Vector4, value: f64);
88
89    #[wasm_bindgen(method, getter)]
90    pub fn z(this: &Vector4) -> f64;
91
92    #[wasm_bindgen(method, setter)]
93    pub fn set_z(this: &Vector4, value: f64);
94
95    #[wasm_bindgen(method, getter)]
96    pub fn w(this: &Vector4) -> f64;
97
98    #[wasm_bindgen(method, setter)]
99    pub fn set_w(this: &Vector4, value: f64);
100}
101
102impl_op_ex!(+ |a: &Vector4, b: &Vector4| -> Vector4 { Vector4::new(a.x() + b.x(), a.y() + b.y(), a.z() + b.z(), a.w() + b.w()) });
103impl_op_ex!(- |a: &Vector4, b: &Vector4| -> Vector4 { Vector4::new(a.x() - b.x(), a.y() - b.y(), a.z() - b.z(), a.w() - b.w()) });
104impl_op_ex!(* |a: &Vector4, b: f64| -> Vector4 { Vector4::new(a.x() * b, a.y() * b, a.z() * b, a.w() * b) });
105impl_op_ex!(/ |a: &Vector4, b: f64| -> Vector4 { Vector4::new(a.x() / b, a.y() / b, a.z() / b, a.w() / b) });
106
107#[wasm_bindgen]
108extern "C" {
109    /// An RGB color
110    pub type Color3;
111
112    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
113    pub fn new(r: f64, g: f64, b: f64) -> Color3;
114}
115
116impl Color3 {
117    get_set_jsvalue!(r, set_r, "r", f64);
118    get_set_jsvalue!(g, set_g, "g", f64);
119    get_set_jsvalue!(b, set_b, "b", f64);
120}
121
122impl<T> From<(T, T, T)> for Color3 
123where T: Into<f64> + Copy {
124    fn from(value: (T, T, T)) -> Self {
125        Color3::new(value.0.into(), value.1.into(), value.2.into())
126    }
127}
128
129#[wasm_bindgen]
130extern "C" {
131    /// An RGBA color
132    pub type Color4;
133
134    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
135    pub fn new(r: f64, g: f64, b: f64, a: f64) -> Color4;
136}
137
138impl Color4 {
139    get_set_jsvalue!(r, set_r, "r", f64);
140    get_set_jsvalue!(g, set_g, "g", f64);
141    get_set_jsvalue!(b, set_b, "b", f64);
142    get_set_jsvalue!(a, set_a, "a", f64);
143}
144
145impl From<Color3> for Color4 {
146    fn from(value: Color3) -> Self {
147        Color4::new(value.r().into(), value.g().into(), value.b().into(), 1.0)
148    }
149}
150
151impl<T> From<(T, T, T, T)> for Color4
152where T: Into<f64> + Copy {
153    fn from(value: (T, T, T, T)) -> Self {
154        Color4::new(value.0.into(), value.1.into(), value.2.into(), value.3.into())
155    }
156}
157
158impl<T> From<(T, T, T, T)> for Vector4
159where T: Into<f64> + Copy {
160    fn from(value: (T, T, T, T)) -> Self {
161        Vector4::new(value.0.into(), value.1.into(), value.2.into(), value.3.into())
162    }
163}
164
165#[wasm_bindgen]
166extern "C" {
167    pub type Quaternion;
168
169    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
170    pub fn new(x: f64, y: f64, z: f64, w: f64) -> Quaternion;
171}