1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use wasm_bindgen::prelude::wasm_bindgen;
use js_sys::Reflect;
use wasm_bindgen::JsValue;
use std::ops;

use impl_ops::*;

use crate::get_set_jsvalue;

#[wasm_bindgen]
extern "C" {
    /// A three-dimensional vector
    pub type Vector3;

    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
    pub fn new(x: f64, y: f64, z: f64) -> Vector3;

    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector3)]
    pub fn One() -> Vector3;

    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector3)]
    pub fn Zero() -> Vector3;

    #[wasm_bindgen(method, getter)]
    pub fn x(this: &Vector3) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_x(this: &Vector3, value: f64);

    #[wasm_bindgen(method, getter)]
    pub fn y(this: &Vector3) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_y(this: &Vector3, value: f64);

    #[wasm_bindgen(method, getter)]
    pub fn z(this: &Vector3) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_z(this: &Vector3, value: f64);
}

impl<T> From<(T, T, T)> for Vector3 
where T: Into<f64> + Copy {
    fn from(value: (T, T, T)) -> Self {
        Vector3::new(value.0.into(), value.1.into(), value.2.into())
    }
}

impl Vector3 {
    pub fn set(&self, x: f64, y: f64, z: f64) {
        self.set_x(x);
        self.set_y(y);
        self.set_z(z);
    }
}

impl_op_ex!(+ |a: &Vector3, b: &Vector3| -> Vector3 { Vector3::new(a.x() + b.x(), a.y() + b.y(), a.z() + b.z()) });
impl_op_ex!(- |a: &Vector3, b: &Vector3| -> Vector3 { Vector3::new(a.x() - b.x(), a.y() - b.y(), a.z() - b.z()) });
impl_op_ex!(* |a: &Vector3, b: f64| -> Vector3 { Vector3::new(a.x() * b, a.y() * b, a.z() * b) });
impl_op_ex!(/ |a: &Vector3, b: f64| -> Vector3 { Vector3::new(a.x() / b, a.y() / b, a.z() / b) });

#[wasm_bindgen]
extern "C" {
    /// A four-dimensional vector
    pub type Vector4;

    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
    pub fn new(x: f64, y: f64, z: f64, w: f64) -> Vector4;

    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector4)]
    pub fn One() -> Vector4;

    #[wasm_bindgen(js_namespace = BABYLON, static_method_of = Vector4)]
    pub fn Zero() -> Vector4;

    #[wasm_bindgen(method, getter)]
    pub fn x(this: &Vector4) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_x(this: &Vector4, value: f64);

    #[wasm_bindgen(method, getter)]
    pub fn y(this: &Vector4) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_y(this: &Vector4, value: f64);

    #[wasm_bindgen(method, getter)]
    pub fn z(this: &Vector4) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_z(this: &Vector4, value: f64);

    #[wasm_bindgen(method, getter)]
    pub fn w(this: &Vector4) -> f64;

    #[wasm_bindgen(method, setter)]
    pub fn set_w(this: &Vector4, value: f64);
}

impl_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()) });
impl_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()) });
impl_op_ex!(* |a: &Vector4, b: f64| -> Vector4 { Vector4::new(a.x() * b, a.y() * b, a.z() * b, a.w() * b) });
impl_op_ex!(/ |a: &Vector4, b: f64| -> Vector4 { Vector4::new(a.x() / b, a.y() / b, a.z() / b, a.w() / b) });

#[wasm_bindgen]
extern "C" {
    /// An RGB color
    pub type Color3;

    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
    pub fn new(r: f64, g: f64, b: f64) -> Color3;
}

impl Color3 {
    get_set_jsvalue!(r, set_r, "r", f64);
    get_set_jsvalue!(g, set_g, "g", f64);
    get_set_jsvalue!(b, set_b, "b", f64);
}

impl<T> From<(T, T, T)> for Color3 
where T: Into<f64> + Copy {
    fn from(value: (T, T, T)) -> Self {
        Color3::new(value.0.into(), value.1.into(), value.2.into())
    }
}

#[wasm_bindgen]
extern "C" {
    /// An RGBA color
    pub type Color4;

    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
    pub fn new(r: f64, g: f64, b: f64, a: f64) -> Color4;
}

impl Color4 {
    get_set_jsvalue!(r, set_r, "r", f64);
    get_set_jsvalue!(g, set_g, "g", f64);
    get_set_jsvalue!(b, set_b, "b", f64);
    get_set_jsvalue!(a, set_a, "a", f64);
}

impl From<Color3> for Color4 {
    fn from(value: Color3) -> Self {
        Color4::new(value.r().into(), value.g().into(), value.b().into(), 1.0)
    }
}

impl<T> From<(T, T, T, T)> for Color4
where T: Into<f64> + Copy {
    fn from(value: (T, T, T, T)) -> Self {
        Color4::new(value.0.into(), value.1.into(), value.2.into(), value.3.into())
    }
}

impl<T> From<(T, T, T, T)> for Vector4
where T: Into<f64> + Copy {
    fn from(value: (T, T, T, T)) -> Self {
        Vector4::new(value.0.into(), value.1.into(), value.2.into(), value.3.into())
    }
}

#[wasm_bindgen]
extern "C" {
    pub type Quaternion;

    #[wasm_bindgen(constructor, js_namespace = BABYLON)]
    pub fn new(x: f64, y: f64, z: f64, w: f64) -> Quaternion;
}