eldiron-shared 0.9.0

Shared code and common types for the Eldiron applications.
Documentation
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::prelude::*;
use core::f32;
use std::f32::consts::PI;
use theframework::prelude::*;

pub struct PrerenderedCamera {
    pub ratio: f32,
    pub pixel_size: Vec2f,
    pub half_width: f32,
    pub half_height: f32,
    pub w: Vec3f,
    pub u: Vec3f,
    pub v: Vec3f,
}

/// Camera
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Camera {
    pub origin: Vec3f,
    pub center: Vec3f,
    pub fov: f32,

    // For orbit
    pub distance: f32,

    pub forward: Vec3f,
    pub up: Vec3f,
    pub right: Vec3f,

    pub orbit_x: f32,
    pub orbit_y: f32,
}

impl Camera {
    pub fn new(origin: Vec3f, center: Vec3f, fov: f32) -> Self {
        Self {
            origin,
            center,
            fov,

            distance: 2.0,

            forward: Vec3f::new(0.0, 0.0, -1.0),
            up: Vec3f::new(0.0, 1.0, 0.0),
            right: Vec3f::new(1.0, 0.0, 0.0),

            orbit_x: 0.0,
            orbit_y: -90.0,
        }
    }

    /// Set the camera's origin and center based on the top-down angle (in degrees)
    pub fn set_top_down_angle(&mut self, angle_deg: f32, distance: f32, look_at: Vec3f) {
        let angle_rad = angle_deg.to_radians();
        let height = distance * angle_rad.sin();
        let horizontal_distance = distance * angle_rad.cos();

        self.center = look_at;

        // Assuming the camera looks along the negative z-axis by default
        self.origin = Vec3f {
            x: look_at.x,
            y: look_at.y + height,
            z: look_at.z - horizontal_distance,
        };
    }

    /// Zoom the camera by a given factor
    pub fn zoom(&mut self, delta: f32) {
        let direction = normalize(self.center - self.origin);

        self.origin += direction * delta;
        self.center += direction * delta;
    }

    // Move the camera by a given displacement
    pub fn move_by(&mut self, x_offset: f32, y_offset: f32) {
        // self.origin += Vec3f::new(x_offset, y_offset, 0.0);
        // self.center += Vec3f::new(x_offset, y_offset, 0.0);

        let direction = normalize(self.center - self.origin);
        let up_vector = vec3f(0.0, 1.0, 0.0);
        let right_vector = cross(direction, up_vector);

        let displacement = right_vector * x_offset + up_vector * y_offset;

        self.origin += displacement;
        self.center += displacement;

        /*
        let direction = normalize(self.center - self.origin);
        let up_vector = vec3f(0.0, 1.0, 0.0);
        let right_vector = cross(direction, up_vector);

        self.origin += direction * y_offset + right_vector * x_offset;
        self.center += direction * y_offset + right_vector * x_offset;*/
    }

    /// Pan the camera horizontally and vertically
    pub fn pan(&mut self, horizontal: f32, vertical: f32) {
        let w = normalize(self.origin - self.center);
        let up_vector = vec3f(0.0, 1.0, 0.0);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        self.center += u * horizontal + v * vertical;
    }

    /// Rotate the camera around its center
    pub fn rotate(&mut self, yaw: f32, pitch: f32) {
        fn magnitude(vec: Vec3f) -> f32 {
            (vec.x.powi(2) + vec.y.powi(2) + vec.z.powi(2)).sqrt()
        }

        let radius = magnitude(self.origin - self.center);

        let mut theta = ((self.origin.z - self.center.z) / radius).acos();
        let mut phi = ((self.origin.x - self.center.x) / (radius * theta.sin())).acos();

        theta += pitch.to_radians();
        phi += yaw.to_radians();

        theta = theta.clamp(0.1, PI - 0.1); //theta.max(0.1).min(PI - 0.1);

        self.origin.x = self.center.x + radius * theta.sin() * phi.cos();
        self.origin.y = self.center.y + radius * theta.cos();
        self.origin.z = self.center.z + radius * theta.sin() * phi.sin();
    }

    /// Create a pinhole ray
    pub fn create_ray(&self, uv: Vec2f, screen: Vec2f, offset: Vec2f) -> Ray {
        let ratio = screen.x / screen.y;
        let pixel_size = vec2f(1.0 / screen.x, 1.0 / screen.y);

        let half_width = (self.fov.to_radians() * 0.5).tan();
        let half_height = half_width / ratio;

        let up_vector = vec3f(0.0, 1.0, 0.0);

        let w = normalize(self.origin - self.center);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        let lower_left = self.origin - u * half_width - v * half_height - w;
        let horizontal = u * half_width * 2.0;
        let vertical = v * half_height * 2.0;
        let mut dir = lower_left - self.origin;

        dir += horizontal * (pixel_size.x * offset.x + uv.x);
        dir += vertical * (pixel_size.y * offset.y + uv.y);

        Ray::new(self.origin, normalize(dir))
    }

    pub fn create_ortho_ray(&self, uv: Vec2f, screen: Vec2f, offset: Vec2f) -> Ray {
        let ratio = screen.x / screen.y;
        let pixel_size = Vec2f::new(1.0 / screen.x, 1.0 / screen.y);

        let cam_origin = self.origin;
        let cam_look_at = self.center;

        let half_width = ((self.fov + 100.0).to_radians() * 0.5).tan();
        let half_height = half_width / ratio;

        let up_vector = Vec3f::new(0.0, 1.0, 0.0);

        let w = normalize(cam_origin - cam_look_at);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        let horizontal = u * half_width * 2.0;
        let vertical = v * half_height * 2.0;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (pixel_size.x * offset.x + uv.x - 0.5);
        out_origin += vertical * (pixel_size.y * offset.y + uv.y - 0.5);

        Ray::new(out_origin, normalize(-w))
    }

    pub fn create_ortho_ray2(
        &self,
        uv: Vec2f,
        screen: Vec2f,
        tiles: Vec2f,
        offset: Vec2f,
        scale_factor: f32,
    ) -> Ray {
        let pixel_size = Vec2f::new(1.0 / screen.x, 1.0 / screen.y);

        let cam_origin = self.origin;
        let cam_look_at = self.center;

        let half_width = tiles.x;
        let half_height = tiles.y;

        let up_vector = Vec3f::new(0.0, 1.0, 0.0);

        let w = normalize(cam_origin - cam_look_at);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        let horizontal = u * half_width * scale_factor;
        let vertical = v * half_height * scale_factor;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (pixel_size.x * offset.x + uv.x - 0.5);
        out_origin += vertical * (pixel_size.y * offset.y + uv.y - 0.5);

        Ray::new(out_origin, normalize(-w))
    }

    pub fn create_ortho_ray_prerendered(&self, uv: Vec2f, prerender: &PrerenderedCamera) -> Ray {
        let cam_origin = self.origin;

        let horizontal = prerender.u * prerender.half_width;
        let vertical = prerender.v * prerender.half_height;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (uv.x - 0.5);
        out_origin += vertical * (uv.y - 0.5);

        Ray::new(out_origin, -prerender.w)
    }

    pub fn create_tilted_isometric_ray(
        &self,
        uv: Vec2f,
        screen: Vec2f,
        offset: Vec2f,
        alignment: i32,
    ) -> Ray {
        let ratio = screen.x / screen.y;
        let pixel_size = Vec2f::new(1.0 / screen.x, 1.0 / screen.y);

        let cam_origin = self.origin;
        let cam_look_at = self.center;

        let half_width = ((self.fov + 100.0).to_radians() * 0.5).tan();
        let half_height = half_width / ratio;

        let up_vector = Vec3f::new(0.0, 1.0, 0.0);

        let w = normalize(cam_origin - cam_look_at);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        let horizontal = u * half_width * 2.0;
        let vertical = v * half_height * 2.0;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (pixel_size.x * offset.x + uv.x - 0.5);
        out_origin += vertical * (pixel_size.y * offset.y + uv.y - 0.5);
        out_origin.y = cam_origin.y;

        Ray::new(
            out_origin,
            normalize(vec3f(
                if alignment == 0 { -0.35 } else { 0.35 },
                -1.0,
                -0.35,
            )),
        )
    }

    pub fn create_tilted_isometric_ray2(
        &self,
        uv: Vec2f,
        screen: Vec2f,
        tiles: Vec2f,
        offset: Vec2f,
        alignment: i32,
        scale_factor: f32,
    ) -> Ray {
        let pixel_size = Vec2f::new(1.0 / screen.x, 1.0 / screen.y);

        let cam_origin = self.origin;
        let cam_look_at = self.center;

        let half_width = tiles.x;
        let half_height = tiles.y;

        let up_vector = Vec3f::new(0.0, 1.0, 0.0);

        let w = normalize(cam_origin - cam_look_at);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        let horizontal = u * half_width * scale_factor;
        let vertical = v * half_height * scale_factor;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (pixel_size.x * offset.x + uv.x - 0.5);
        out_origin += vertical * (pixel_size.y * offset.y + uv.y - 0.5);
        out_origin.y = cam_origin.y;

        Ray::new(
            out_origin,
            normalize(vec3f(
                if alignment == 0 { -0.35 } else { 0.35 },
                -1.0,
                -0.35,
            )),
        )
    }

    pub fn create_tilted_isometric_ray_prerendered(
        &self,
        uv: Vec2f,
        alignment: i32,
        prerender: &PrerenderedCamera,
    ) -> Ray {
        let cam_origin = self.origin;

        let horizontal = prerender.u * prerender.half_width;
        let vertical = prerender.v * prerender.half_height;

        let mut out_origin = cam_origin;
        out_origin += horizontal * (uv.x - 0.5);
        out_origin += vertical * (uv.y - 0.5);
        out_origin.y = cam_origin.y;

        Ray::new(
            out_origin,
            //normalize(
            vec3f(
                if alignment == 0 { -0.35 } else { 0.35 },
                -1.0,
                -0.35,
                //    )
            ),
        )
    }

    pub fn prerender(origin: Vec3f, center: Vec3f, screen: Vec2f, fov: f32) -> PrerenderedCamera {
        let ratio = screen.x / screen.y;
        let pixel_size = Vec2f::new(1.0 / screen.x, 1.0 / screen.y);

        let half_width = ((fov + 100.0).to_radians() * 0.5).tan();
        let half_height = half_width / ratio;

        let up_vector = Vec3f::new(0.0, 1.0, 0.0);

        let w = normalize(origin - center);
        let u = cross(up_vector, w);
        let v = cross(w, u);

        PrerenderedCamera {
            ratio,
            half_width: half_width * 2.0,
            half_height: half_height * 2.0,
            pixel_size,
            w,
            u,
            v,
        }
    }

    /// Computes the orbi camera vectors. Based on https://www.shadertoy.com/view/ttfyzN
    pub fn compute_orbit(&mut self, mouse_delta: Vec2f) {
        #[inline(always)]
        pub fn mix(a: &f32, b: &f32, v: f32) -> f32 {
            (1.0 - v) * a + b * v
        }

        let min_camera_angle = 0.01;
        let max_camera_angle = std::f32::consts::PI - 0.01;

        self.orbit_x += mouse_delta.x;
        self.orbit_y += mouse_delta.y;

        let angle_x = -self.orbit_x;
        let angle_y = mix(&min_camera_angle, &max_camera_angle, self.orbit_y);

        let mut camera_pos = Vec3f::zero();

        camera_pos.x = sin(angle_x) * sin(angle_y) * self.distance;
        camera_pos.y = -cos(angle_y) * self.distance;
        camera_pos.z = cos(angle_x) * sin(angle_y) * self.distance;

        camera_pos += self.center;

        self.origin = camera_pos;
        self.forward = normalize(self.center - camera_pos);
        self.right = normalize(cross(vec3f(0.0, 1.0, 0.0), -self.forward));
        self.up = normalize(cross(-self.forward, self.right));
    }

    /// Create an orbit camera ray
    pub fn create_orbit_ray(&self, uv: Vec2f, screen_dim: Vec2f, offset: Vec2f) -> Ray {
        let camera_pos = self.origin;
        let camera_fwd = self.forward;
        let camera_up = self.up;
        let camera_right = self.right;

        let uv_jittered = (uv * screen_dim + (offset - 0.5)) / screen_dim;
        let mut screen = uv_jittered * 2.0 - 1.0;

        let aspect_ratio = screen_dim.x / screen_dim.y;
        screen.y /= aspect_ratio;

        let camera_distance = tan(self.fov * 0.5 * std::f32::consts::PI / 180.0);
        let mut ray_dir = vec3f(screen.x, screen.y, camera_distance);
        ray_dir = normalize(Mat3f::from((camera_right, camera_up, camera_fwd)) * ray_dir);

        Ray::new(camera_pos, ray_dir)
    }
}