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 linear gradient defined by two endpoints and a list of color stops.
69///
70/// Used to create smooth color transitions along a straight line
71/// for fill or stroke operations on the canvas.
72#[derive(Clone, Data, Debug, New, PartialEq)]
73pub struct LinearGradient {
74    /// The starting point of the gradient in world space.
75    #[get(type(copy))]
76    pub(crate) start: Vector2D,
77    /// The ending point of the gradient in world space.
78    #[get(type(copy))]
79    pub(crate) end: Vector2D,
80    /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
81    pub(crate) stops: Vec<(f64, String)>,
82}
83
84/// A radial gradient defined by inner and outer circles and a list of color stops.
85///
86/// Used to create smooth color transitions radiating outward from a center point
87/// for fill or stroke operations on the canvas.
88#[derive(Clone, Data, Debug, New, PartialEq)]
89pub struct RadialGradient {
90    /// The center of the inner circle of the gradient.
91    #[get(type(copy))]
92    pub(crate) inner_center: Vector2D,
93    /// The radius of the inner circle.
94    #[get(type(copy))]
95    pub(crate) inner_radius: f64,
96    /// The center of the outer circle of the gradient.
97    #[get(type(copy))]
98    pub(crate) outer_center: Vector2D,
99    /// The radius of the outer circle.
100    #[get(type(copy))]
101    pub(crate) outer_radius: f64,
102    /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
103    pub(crate) stops: Vec<(f64, String)>,
104}
105
106/// Shadow rendering configuration for drop shadow effects on canvas primitives.
107///
108/// When applied, all subsequent fill, stroke, and draw operations will cast
109/// a shadow with the specified color, blur radius, and offset.
110#[derive(Clone, Data, Debug, New, PartialEq, PartialOrd)]
111pub struct ShadowConfig {
112    /// The CSS color string of the shadow (e.g., `"rgba(0,0,0,0.5)"`).
113    #[get(type(clone))]
114    pub(crate) color: String,
115    /// The blur radius of the shadow in pixels.
116    #[get(type(copy))]
117    pub(crate) blur: f64,
118    /// The horizontal offset of the shadow in pixels.
119    #[get(type(copy))]
120    pub(crate) offset_x: f64,
121    /// The vertical offset of the shadow in pixels.
122    #[get(type(copy))]
123    pub(crate) offset_y: f64,
124}
125
126/// Represents the rendering priority layer for draw call ordering.
127///
128/// Higher z-index values are drawn on top of lower values,
129/// enabling correct visual layering of game objects.
130#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
131pub struct RenderLayer {
132    /// The z-index determining draw order. Higher values draw later (on top).
133    #[get(type(copy))]
134    pub(crate) z_index: i32,
135    /// Whether objects in this layer should be rendered.
136    #[get(type(copy))]
137    pub(crate) visible: bool,
138}
139
140/// A supersampling anti-aliasing (SSAA) canvas wrapper that renders at a higher
141/// resolution on an offscreen canvas and downscales to the display canvas for
142/// smoother polygon edges in software-rendered 3D scenes.
143///
144/// The offscreen context is scaled by `scale_factor` so that all drawing
145/// code can use logical pixel coordinates without modification. After
146/// rendering, call `present()` to draw the high-resolution buffer onto the
147/// visible canvas with high-quality image smoothing.
148#[derive(Clone, Data, New)]
149pub struct SsaaCanvas {
150    /// The display canvas element visible to the user.
151    pub(crate) display_canvas: HtmlCanvasElement,
152    /// The 2D rendering context of the display canvas used for final presentation.
153    pub(crate) display_context: CanvasRenderingContext2d,
154    /// The offscreen canvas used for high-resolution rendering.
155    pub(crate) offscreen_canvas: HtmlCanvasElement,
156    /// The 2D rendering context of the offscreen canvas, pre-scaled by `scale_factor`.
157    pub(crate) offscreen_context: CanvasRenderingContext2d,
158    /// The supersampling scale factor (e.g., 2.0 means 4x SSAA).
159    #[get(type(copy))]
160    pub(crate) scale_factor: f64,
161    /// The logical display width in CSS pixels.
162    #[get(type(copy))]
163    pub(crate) width: f64,
164    /// The logical display height in CSS pixels.
165    #[get(type(copy))]
166    pub(crate) height: f64,
167}