1use makepad_tinyserde::*;
2
3#[derive(Clone, Copy, Default, Debug, PartialEq, SerRon, DeRon)]
4pub struct Rect{
5 pub x:f32,
6 pub y:f32,
7 pub w:f32,
8 pub h:f32
9}
10
11impl Rect{
12
13 pub fn contains(&self, x:f32, y:f32)->bool{
14 return x >= self.x && x <= self.x + self.w &&
15 y >= self.y && y <= self.y + self.h;
16 }
17 pub fn intersects(&self, r:Rect)->bool{
18 !(
19 r.x > self.x + self.w ||
20 r.x + r.w < self.x ||
21 r.y > self.y + self.h ||
22 r.y + r.h < self.y
23 )
24 }
25}
26
27#[derive(Clone, Copy, Default, Debug)]
28pub struct Mat4{
29 pub v: [f32; 16],
30}
31
32#[derive(Clone, Copy, Default, Debug, PartialEq, SerRon, DeRon)]
33pub struct Vec2{
34 pub x: f32,
35 pub y: f32,
36}
37
38impl Vec2{
39
40 pub fn distance(&self, other:&Vec2)->f32{
41 let dx = self.x - other.x;
42 let dy = self.y - other.y;
43 (dx*dx+dy*dy).sqrt()
44 }
45}
46#[derive(Clone, Copy, Default, Debug)]
52pub struct Vec3{
53 pub x: f32,
54 pub y: f32,
55 pub z: f32
56}
57
58#[derive(Clone, Copy, Default, Debug)]
64pub struct Vec4{
65 pub x: f32,
66 pub y: f32,
67 pub z: f32,
68 pub w: f32
69}
70
71
72#[derive(Clone, Copy, Default, Debug)]
73pub struct Color{
74 pub r: f32,
75 pub g: f32,
76 pub b: f32,
77 pub a: f32
78}
79
80pub fn mix(a:Color, b:Color, f:f32)->Color{
81 let nf = 1.0 - f;
82 return Color{
83 r: nf * a.r + f * b.r,
84 g: nf * a.g + f * b.g,
85 b: nf * a.b + f * b.b,
86 a: nf * a.a + f * b.a,
87 }
88}
89
90impl Mat4{
97 pub fn identity() -> Mat4{
98 return Mat4{v:[
99 1.0,0.0,0.0,0.0,
100 0.0,1.0,0.0,0.0,
101 0.0,0.0,1.0,0.0,
102 0.0,0.0,0.0,1.0
103 ]}
104 }
105
106 pub fn rotate_tsrt(t1: Vec3, s: Vec3, r: Vec3, t2: Vec3) -> Mat4{
107 let cx = f32::cos(r.x);
108 let cy = f32::cos(r.y);
109 let cz = f32::cos(r.z);
110 let sx = f32::sin(r.x);
111 let sy = f32::sin(r.y);
112 let sz = f32::sin(r.z);
113 let m0 = s.x * (cy * cz + sx * sy * sz);
114 let m1 = s.y * (-sz * cy + cz * sx * sy);
115 let m2 = s.z * (sy * cx);
116 let m4 = s.x * (sz * cx);
117 let m5 = s.y * (cx * cz);
118 let m6 = s.z * (-sx);
119 let m8 = s.x * (-sy * cz + cy * sx * sz);
120 let m9 = s.y * (sy * sz + cy * sx * cz);
121 let m10 = s.z * (cx * cy);
122 return Mat4{v:[
123 m0, m4, m8, 0.0,
124 m1, m5, m9, 0.0,
125 m2, m6, m10, 0.0,
126 t2.x + (m0 * t1.x + m1 * t1.y + m1 * t1.z),
127 t2.y + (m4 * t1.x + m5 * t1.y + m6 * t1.z),
128 t2.z + (m8 * t1.x + m9 * t1.y + m10 * t1.z),
129 1.0
130 ]}
131 }
132
133 pub fn perspective(fov_y:f32, aspect:f32, near:f32, far:f32) -> Mat4{
134 let f = 1.0 / f32::tan(fov_y / 2.0);
135 let nf = 1.0 / (near - far);
136 return Mat4{v:[
137 f / aspect, 0.0, 0.0, 0.0,
138 0.0, f , 0.0, 0.0,
139 0.0, 0.0, (far + near) * nf, -1.0,
140 0.0, 0.0, (2.0 * far * near) * nf, 0.0
141 ]}
142 }
143
144 pub fn scale_translate(sx:f32, sy:f32, sz:f32, x:f32, y:f32, z:f32)->Mat4{
145 return Mat4{v:[
146 sx,0.0,0.0,0.0,
147 0.0,sy,0.0,0.0,
148 0.0,0.0,sz,0.0,
149 x,y,z,1.0
150 ]}
151
152 }
153
154 pub fn ortho(left:f32, right:f32, top:f32, bottom:f32, near:f32, far:f32, scalex:f32, scaley:f32) -> Mat4{
155 let lr = 1.0 / (left - right);
156 let bt = 1.0 / (bottom - top);
157 let nf = 1.0 / (near - far);
158 return Mat4{v:[
165 -2.0 * lr * scalex, 0.0, 0.0, 0.0,
166 0.0, -2.0 * bt * scaley, 0.0, 0.0,
167 0.0, 0.0, -1.0 * nf, 0.0,
168 (left+right) * lr, (top+bottom) * bt, 0.5+(far+near)*nf, 1.0
169 ]}
170 }
171
172 pub fn transform_vec4(&self, v:Vec4)->Vec4{
173 let m = &self.v;
174 Vec4{
175 x:m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12] * v.w,
176 y:m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13] * v.w,
177 z:m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w,
178 w:m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w
179 }
180 }
181}