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
use std::sync::mpsc::Sender;
use crate::event::{Action, Key, MouseButton, WindowEvent};
use crate::window::WgpuCanvas;
use image::{GenericImage, Pixel};
use winit::window::WindowAttributes;
/// The possible number of samples for multisample anti-aliasing.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NumSamples {
/// Multisampling disabled.
Zero = 0,
/// One sample
One = 1,
/// Two samples
Two = 2,
/// Four samples
Four = 4,
/// Eight samples
Eight = 8,
/// Sixteen samples
Sixteen = 16,
}
impl NumSamples {
/// Create a `NumSamples` from a number.
/// Returns `None` if `i` is invalid.
pub fn from_u32(i: u32) -> Option<NumSamples> {
match i {
0 => Some(NumSamples::Zero),
1 => Some(NumSamples::One),
2 => Some(NumSamples::Two),
4 => Some(NumSamples::Four),
8 => Some(NumSamples::Eight),
16 => Some(NumSamples::Sixteen),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Canvas options.
pub struct CanvasSetup {
/// Is vsync enabled?
pub vsync: bool,
/// Number of AA samples.
pub samples: NumSamples,
/// The id of the canvas element to use (WASM only).
/// Defaults to `"canvas"`. If an element with this id exists in the DOM,
/// it will be used; otherwise a new canvas element with this id is created.
///
/// # Example
/// ```no_run
/// # use kiss3d::window::{Window, CanvasSetup};
/// let setup = CanvasSetup {
/// canvas_id: "my-3d-canvas".to_string(),
/// ..Default::default()
/// };
/// let mut window = Window::new_with_setup("Title", 800, 600, setup);
/// ```
pub canvas_id: String,
}
impl Default for CanvasSetup {
fn default() -> Self {
CanvasSetup {
vsync: true,
samples: NumSamples::Zero,
canvas_id: "canvas".to_string(),
}
}
}
/// An abstract structure representing a window for native applications, and a canvas for web applications.
pub struct Canvas {
canvas: WgpuCanvas,
}
impl Canvas {
/// Open a new window, and initialize the wgpu context.
pub async fn open(
window_attrs: WindowAttributes,
canvas_setup: Option<CanvasSetup>,
out_events: Sender<WindowEvent>,
) -> Self {
Canvas {
canvas: WgpuCanvas::open(window_attrs, canvas_setup, out_events).await,
}
}
/// Open a headless canvas (no window) for off-screen rendering.
#[cfg(not(target_arch = "wasm32"))]
pub async fn open_headless(
width: u32,
height: u32,
canvas_setup: Option<CanvasSetup>,
out_events: Sender<WindowEvent>,
) -> Self {
Canvas {
canvas: WgpuCanvas::open_headless(width, height, canvas_setup, out_events).await,
}
}
/// Poll all events that occurred since the last call to this method.
pub fn poll_events(&mut self) {
self.canvas.poll_events()
}
/// Resizes the canvas render targets.
pub fn resize(&mut self, width: u32, height: u32) {
self.canvas.resize(width, height)
}
/// Gets the current surface texture for rendering.
pub fn get_current_texture(&self) -> Option<wgpu::SurfaceTexture> {
self.canvas.get_current_texture()
}
/// Presents the current frame.
pub fn present(&self, frame: wgpu::SurfaceTexture) {
self.canvas.present(frame)
}
/// Gets the depth texture view for rendering.
pub fn depth_view(&self) -> &wgpu::TextureView {
self.canvas.depth_view()
}
/// Gets the MSAA texture view if MSAA is enabled.
pub fn msaa_view(&self) -> Option<&wgpu::TextureView> {
self.canvas.msaa_view()
}
/// Gets the sample count for MSAA.
pub fn sample_count(&self) -> u32 {
self.canvas.sample_count()
}
/// Gets the surface format.
pub fn surface_format(&self) -> wgpu::TextureFormat {
self.canvas.surface_format()
}
/// The size of the window.
pub fn size(&self) -> (u32, u32) {
self.canvas.size()
}
/// The current position of the cursor, if known.
///
/// This position may not be known if, e.g., the cursor has not been moved since the
/// window was open.
pub fn cursor_pos(&self) -> Option<(f64, f64)> {
self.canvas.cursor_pos()
}
/// The scale factor.
pub fn scale_factor(&self) -> f64 {
self.canvas.scale_factor()
}
/// Set the window title.
pub fn set_title(&mut self, title: &str) {
self.canvas.set_title(title)
}
/// Set the window icon. See `Window::set_icon` for details.
pub fn set_icon(&mut self, icon: impl GenericImage<Pixel = impl Pixel<Subpixel = u8>>) {
self.canvas.set_icon(icon)
}
/// Set the cursor grabbing behaviour.
pub fn set_cursor_grab(&self, grab: bool) {
self.canvas.set_cursor_grab(grab);
}
/// Set the cursor position.
pub fn set_cursor_position(&self, x: f64, y: f64) {
self.canvas.set_cursor_position(x, y);
}
/// Toggle the cursor visibility.
pub fn hide_cursor(&self, hide: bool) {
self.canvas.hide_cursor(hide);
}
/// Hide the window.
pub fn hide(&mut self) {
self.canvas.hide()
}
/// Show the window.
pub fn show(&mut self) {
self.canvas.show()
}
/// The state of a mouse button.
pub fn get_mouse_button(&self, button: MouseButton) -> Action {
self.canvas.get_mouse_button(button)
}
/// The state of a key.
pub fn get_key(&self, key: Key) -> Action {
self.canvas.get_key(key)
}
/// Copies the frame texture to the readback texture for later reading.
pub fn copy_frame_to_readback(&self, frame: &wgpu::SurfaceTexture) {
self.canvas.copy_frame_to_readback(frame)
}
/// Copies an arbitrary texture into the readback texture used by `read_pixels`.
///
/// The source texture must have `COPY_SRC` usage and the same size and
/// format as the surface.
pub fn copy_texture_to_readback(&self, src: &wgpu::Texture) {
self.canvas.copy_texture_to_readback(src)
}
/// Reads pixels from the readback texture into the provided buffer.
/// Returns RGB data (3 bytes per pixel).
pub fn read_pixels(&self, out: &mut Vec<u8>, x: usize, y: usize, width: usize, height: usize) {
self.canvas.read_pixels(out, x, y, width, height)
}
}