Skip to main content

euv_engine/renderer/
struct.rs

1use crate::*;
2
3/// A 2D camera that defines the viewport into the game world.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
5pub struct Camera2D {
6    /// The world-space position of the camera center.
7    #[get(type(copy))]
8    pub(crate) position: Vector2D,
9    /// The zoom factor (1.0 = no zoom, 2.0 = 2x magnification).
10    #[get(type(copy))]
11    pub(crate) zoom: f64,
12    /// The rotation angle in radians.
13    #[get(type(copy))]
14    pub(crate) rotation: f64,
15    /// The viewport width in screen pixels.
16    #[get(type(copy))]
17    pub(crate) viewport_width: f64,
18    /// The viewport height in screen pixels.
19    #[get(type(copy))]
20    pub(crate) viewport_height: f64,
21}
22
23/// A 3D camera that defines the viewport into a 3D world using perspective
24/// or orthographic projection.
25#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
26pub struct Camera3D {
27    /// The world-space position of the camera (eye).
28    #[get(type(copy))]
29    pub(crate) position: Vector3D,
30    /// The point the camera is looking at (target).
31    #[get(type(copy))]
32    pub(crate) target: Vector3D,
33    /// The up direction for the camera.
34    #[get(type(copy))]
35    #[new(skip)]
36    pub(crate) up: Vector3D,
37    /// The vertical field of view in radians.
38    #[get(type(copy))]
39    #[new(skip)]
40    pub(crate) fov: f64,
41    /// The near clipping plane distance.
42    #[get(type(copy))]
43    #[new(skip)]
44    pub(crate) near: f64,
45    /// The far clipping plane distance.
46    #[get(type(copy))]
47    #[new(skip)]
48    pub(crate) far: f64,
49    /// The viewport width in pixels.
50    #[get(type(copy))]
51    pub(crate) viewport_width: f64,
52    /// The viewport height in pixels.
53    #[get(type(copy))]
54    pub(crate) viewport_height: f64,
55}
56
57/// A wrapper around `CanvasRenderingContext2d` providing convenience
58/// drawing methods and camera management for the game engine.
59#[derive(Clone, Data, New)]
60pub struct CanvasRenderer {
61    /// The underlying canvas 2D rendering context.
62    pub(crate) context: CanvasRenderingContext2d,
63    /// The active camera controlling the viewport.
64    #[get(type(copy))]
65    pub(crate) camera: Camera2D,
66}
67
68/// A supersampling anti-aliasing (SSAA) canvas wrapper that renders at a higher
69/// resolution on an offscreen canvas and downscales to the display canvas for
70/// smoother polygon edges in software-rendered 3D scenes.
71///
72/// The offscreen context is scaled by `scale_factor` so that all drawing
73/// code can use logical pixel coordinates without modification. After
74/// rendering, call `present()` to draw the high-resolution buffer onto the
75/// visible canvas with high-quality image smoothing.
76#[derive(Clone, Data, New)]
77pub struct SsaaCanvas {
78    /// The display canvas element visible to the user.
79    pub(crate) display_canvas: HtmlCanvasElement,
80    /// The 2D rendering context of the display canvas used for final presentation.
81    pub(crate) display_context: CanvasRenderingContext2d,
82    /// The offscreen canvas used for high-resolution rendering.
83    pub(crate) offscreen_canvas: HtmlCanvasElement,
84    /// The 2D rendering context of the offscreen canvas, pre-scaled by `scale_factor`.
85    pub(crate) offscreen_context: CanvasRenderingContext2d,
86    /// The supersampling scale factor (e.g., 2.0 means 4x SSAA).
87    #[get(type(copy))]
88    pub(crate) scale_factor: f64,
89    /// The logical display width in CSS pixels.
90    #[get(type(copy))]
91    pub(crate) width: f64,
92    /// The logical display height in CSS pixels.
93    #[get(type(copy))]
94    pub(crate) height: f64,
95}