1extern crate nalgebra as na;
5use gloss_img::DynImage;
9use gloss_utils::{
10 io::FileLoader,
11 tensor::{DynamicTensorFloat2D, DynamicTensorInt2D},
12};
13use image::ImageReader;
14use na::DMatrix;
15use std::io::{BufReader, Cursor, Read, Seek};
16#[derive(Clone)]
20pub struct ConfigChanges {
21 pub new_distance_fade_center: na::Point3<f32>,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq)]
26pub enum PointColorType {
27 Solid = 0,
28 PerVert,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum LineColorType {
34 Solid = 0,
35 PerVert,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq)]
40pub enum MeshColorType {
41 Solid = 0,
42 PerVert,
43 Texture,
44 UV,
45 Normal,
46 NormalViewCoords,
47}
48
49#[derive(Clone)]
51#[allow(clippy::struct_excessive_bools)]
52pub struct VisLines {
53 pub show_lines: bool,
54 pub line_color: na::Vector4<f32>,
55 pub line_width: f32,
56 pub color_type: LineColorType,
57 pub zbuffer: bool,
58 pub antialias_edges: bool,
59 pub added_automatically: bool,
63}
64#[derive(Clone)]
66pub struct VisWireframe {
67 pub show_wireframe: bool,
68 pub wire_color: na::Vector4<f32>,
69 pub wire_width: f32,
70 pub added_automatically: bool,
74}
75#[derive(Clone)]
77pub struct VisNormals {
78 pub show_normals: bool,
79 pub normals_color: na::Vector4<f32>,
80 pub normals_width: f32,
81 pub normals_scale: f32, pub added_automatically: bool,
86}
87#[derive(Clone)]
89#[allow(clippy::struct_excessive_bools)]
90pub struct VisPoints {
91 pub show_points: bool,
92 pub show_points_indices: bool,
93 pub point_color: na::Vector4<f32>,
94 pub point_size: f32,
95 pub is_point_size_in_world_space: bool,
96 pub color_type: PointColorType,
97 pub zbuffer: bool,
98 pub added_automatically: bool,
102}
103#[derive(Clone)]
105pub struct VisMesh {
106 pub show_mesh: bool,
107 pub solid_color: na::Vector4<f32>,
108 pub metalness: f32,
109 pub perceptual_roughness: f32,
110 pub roughness_black_lvl: f32,
111 pub uv_scale: f32,
112 pub opacity: f32,
113 pub needs_sss: bool,
114 pub color_type: MeshColorType,
115 pub added_automatically: bool,
119}
120#[derive(Clone)]
122pub struct VisOutline {
123 pub show_outline: bool,
124 pub outline_color: na::Vector4<f32>,
125 pub outline_width: f32,
126 pub added_automatically: bool,
130}
131impl Default for VisLines {
133 fn default() -> VisLines {
134 VisLines {
135 show_lines: false,
136 line_color: na::Vector4::<f32>::new(1.0, 0.1, 0.1, 1.0),
137 line_width: 1.0,
138 color_type: LineColorType::Solid,
139 zbuffer: true,
140 antialias_edges: false,
141 added_automatically: false,
142 }
143 }
144}
145impl Default for VisWireframe {
146 fn default() -> VisWireframe {
147 VisWireframe {
148 show_wireframe: false,
149 wire_color: na::Vector4::<f32>::new(1.0, 0.0, 0.0, 1.0),
150 wire_width: 1.0,
151 added_automatically: false,
152 }
153 }
154}
155impl Default for VisNormals {
156 fn default() -> VisNormals {
157 VisNormals {
158 show_normals: false,
159 normals_color: na::Vector4::<f32>::new(1.0, 0.0, 0.0, 1.0),
160 normals_width: 1.0,
161 normals_scale: 1.0,
162 added_automatically: false,
163 }
164 }
165}
166impl Default for VisPoints {
167 fn default() -> VisPoints {
168 VisPoints {
169 show_points: false,
170 show_points_indices: false,
171 point_color: na::Vector4::<f32>::new(245.0 / 255.0, 175.0 / 255.0, 110.0 / 255.0, 1.0),
172 point_size: 1.0,
173 is_point_size_in_world_space: false,
174 color_type: PointColorType::Solid,
175 zbuffer: true,
176 added_automatically: false,
177 }
178 }
179}
180impl Default for VisMesh {
181 fn default() -> VisMesh {
182 VisMesh {
183 show_mesh: true,
184 solid_color: na::Vector4::<f32>::new(1.0, 206.0 / 255.0, 143.0 / 255.0, 1.0),
185 metalness: 0.0,
186 perceptual_roughness: 0.5,
187 roughness_black_lvl: 0.0,
188 uv_scale: 1.0,
189 opacity: 1.0,
190 needs_sss: false,
191 color_type: MeshColorType::Solid,
192 added_automatically: false,
193 }
194 }
195}
196impl Default for VisOutline {
197 fn default() -> VisOutline {
198 VisOutline {
199 show_outline: false,
200 outline_color: na::Vector4::<f32>::new(0.29, 0.82, 0.73, 1.0), outline_width: 4.0,
202 added_automatically: false,
203 }
204 }
205}
206
207#[derive(Clone)]
210pub struct ModelMatrix(pub na::SimilarityMatrix3<f32>); impl Default for ModelMatrix {
214 fn default() -> ModelMatrix {
215 ModelMatrix(na::SimilarityMatrix3::<f32>::identity())
216 }
217}
218impl ModelMatrix {
219 #[must_use]
220 pub fn with_translation(self, t: &na::Vector3<f32>) -> Self {
221 let mut mat = self;
222 mat.0.append_translation_mut(&na::Translation3::new(t[0], t[1], t[2]));
223 mat
224 }
225 #[must_use]
226 pub fn with_rotation_rot3(self, r: &na::Rotation3<f32>) -> Self {
227 let mut mat = self;
228 mat.0.append_rotation_mut(r);
229 mat
230 }
231 #[must_use]
232 pub fn with_rotation_axis_angle(self, v: &na::Vector3<f32>) -> Self {
233 let mut mat = self;
234 mat.0
235 .append_rotation_mut(&na::Rotation3::from_axis_angle(&na::UnitVector3::<f32>::new_normalize(*v), v.norm()));
236 mat
237 }
238 #[must_use]
239 pub fn with_rotation_euler(self, e: &na::Vector3<f32>) -> Self {
240 let mut mat = self;
241 mat.0.append_rotation_mut(&na::Rotation3::from_euler_angles(e.x, e.y, e.z));
242 mat
243 }
244}
245
246#[derive(Clone, Debug)]
248pub struct CamTrack(pub DMatrix<f32>);
249
250#[derive(Clone, Debug)]
251pub struct Verts(pub DynamicTensorFloat2D);
252
253#[derive(Clone, Debug)]
256pub struct EdgesV1(pub DynamicTensorFloat2D);
257#[derive(Clone, Debug)]
260pub struct EdgesV2(pub DynamicTensorFloat2D);
261
262#[derive(Clone)]
267pub struct Faces(pub DynamicTensorInt2D);
268
269#[derive(Clone, Debug)]
272pub struct Edges(pub DynamicTensorInt2D);
273
274#[derive(Clone)]
276pub struct UVs(pub DynamicTensorFloat2D);
277#[derive(Clone)]
283pub struct Normals(pub DynamicTensorFloat2D);
284
285#[derive(Clone)]
287pub struct Tangents(pub DynamicTensorFloat2D);
288
289#[derive(Clone)]
291pub struct Colors(pub DynamicTensorFloat2D);
292
293#[derive(Clone)]
294#[allow(clippy::struct_excessive_bools)]
295pub struct ImgConfig {
296 pub keep_on_cpu: bool,
297 pub fast_upload: bool, pub generate_mipmaps: bool,
299 pub mipmap_generation_cpu: bool,
300}
301impl Default for ImgConfig {
302 fn default() -> Self {
303 Self {
304 keep_on_cpu: true,
305 fast_upload: true,
306 generate_mipmaps: true,
307 mipmap_generation_cpu: false,
308 }
309 }
310}
311
312#[derive(Clone)]
315pub struct GenericImg {
316 pub path: Option<String>,
317 pub cpu_img: Option<DynImage>, pub config: ImgConfig,
319}
320impl GenericImg {
321 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
324 let cpu_img = Some(ImageReader::open(path).unwrap().decode().unwrap());
325
326 Self {
327 path: Some(path.to_string()),
328 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
329 config: config.clone(),
330 }
331 }
332
333 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
336 let reader = ImageReader::new(BufReader::new(FileLoader::open(path).await))
337 .with_guessed_format()
338 .expect("Cursor io never fails");
339
340 let cpu_img = Some(reader.decode().unwrap());
341
342 Self {
343 path: Some(path.to_string()),
344 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
345 config: config.clone(),
346 }
347 }
348
349 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
352 Self::new_from_reader(Cursor::new(buf), config)
353 }
354
355 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
358 let reader_img = ImageReader::new(BufReader::new(reader))
359 .with_guessed_format()
360 .expect("Format for image should be something known and valid");
361
362 let cpu_img = Some(reader_img.decode().unwrap());
363
364 Self {
365 path: None,
366 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
367 config: config.clone(),
368 }
369 }
370
371 pub fn img_ref(&self) -> &DynImage {
372 self.cpu_img.as_ref().unwrap()
373 }
374
375 pub fn img_ref_mut(&mut self) -> &mut DynImage {
376 self.cpu_img.as_mut().unwrap()
377 }
378}
379
380#[derive(Clone)]
382pub struct DiffuseImg {
383 pub generic_img: GenericImg,
384}
385
386impl DiffuseImg {
387 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
388 let generic_img = GenericImg::new_from_path(path, config);
389 Self { generic_img }
390 }
391
392 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
393 let generic_img = GenericImg::new_from_path_async(path, config).await;
394 Self { generic_img }
395 }
396
397 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
398 let generic_img = GenericImg::new_from_buf(buf, config);
399 Self { generic_img }
400 }
401
402 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
403 let generic_img = GenericImg::new_from_reader(reader, config);
404 Self { generic_img }
405 }
406}
407
408#[derive(Clone)]
410pub struct NormalImg {
411 pub generic_img: GenericImg,
412}
413
414impl NormalImg {
415 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
416 let generic_img = GenericImg::new_from_path(path, config);
417 Self { generic_img }
418 }
419
420 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
421 let generic_img = GenericImg::new_from_path_async(path, config).await;
422 Self { generic_img }
423 }
424
425 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
426 let generic_img = GenericImg::new_from_buf(buf, config);
427 Self { generic_img }
428 }
429
430 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
431 let generic_img = GenericImg::new_from_reader(reader, config);
432 Self { generic_img }
433 }
434}
435
436pub struct MetalnessImg {
438 pub generic_img: GenericImg,
439}
440impl MetalnessImg {
441 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
442 let generic_img = GenericImg::new_from_path(path, config);
443 Self { generic_img }
444 }
445
446 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
447 let generic_img = GenericImg::new_from_path_async(path, config).await;
448 Self { generic_img }
449 }
450
451 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
452 let generic_img = GenericImg::new_from_buf(buf, config);
453 Self { generic_img }
454 }
455
456 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
457 let generic_img = GenericImg::new_from_reader(reader, config);
458 Self { generic_img }
459 }
460}
461
462pub struct RoughnessImg {
465 pub generic_img: GenericImg,
466}
467impl RoughnessImg {
468 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
469 let generic_img = GenericImg::new_from_path(path, config);
470 Self { generic_img }
471 }
472
473 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
474 let generic_img = GenericImg::new_from_path_async(path, config).await;
475 Self { generic_img }
476 }
477
478 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
479 let generic_img = GenericImg::new_from_buf(buf, config);
480 Self { generic_img }
481 }
482
483 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
484 let generic_img = GenericImg::new_from_reader(reader, config);
485 Self { generic_img }
486 }
487}
488
489pub struct EnvironmentMap {
526 pub diffuse_path: String,
527 pub specular_path: String,
528}
529impl EnvironmentMap {
530 pub fn new_from_path(diffuse_path: &str, specular_path: &str) -> Self {
531 Self {
532 diffuse_path: String::from(diffuse_path),
533 specular_path: String::from(specular_path),
534 }
535 }
536}
537
538#[cfg(target_arch = "wasm32")]
543unsafe impl Send for ConfigChanges {}
544#[cfg(target_arch = "wasm32")]
545unsafe impl Sync for ConfigChanges {}
546#[cfg(target_arch = "wasm32")]
548unsafe impl Send for Verts {}
549#[cfg(target_arch = "wasm32")]
550unsafe impl Sync for Verts {}
551#[cfg(target_arch = "wasm32")]
553unsafe impl Send for EdgesV1 {}
554#[cfg(target_arch = "wasm32")]
555unsafe impl Sync for EdgesV1 {}
556#[cfg(target_arch = "wasm32")]
558unsafe impl Send for EdgesV2 {}
559#[cfg(target_arch = "wasm32")]
560unsafe impl Sync for EdgesV2 {}
561#[cfg(target_arch = "wasm32")]
563unsafe impl Send for Edges {}
564#[cfg(target_arch = "wasm32")]
565unsafe impl Sync for Edges {}
566#[cfg(target_arch = "wasm32")]
568unsafe impl Send for Faces {}
569#[cfg(target_arch = "wasm32")]
570unsafe impl Sync for Faces {}
571#[cfg(target_arch = "wasm32")]
573unsafe impl Send for UVs {}
574#[cfg(target_arch = "wasm32")]
575unsafe impl Sync for UVs {}
576#[cfg(target_arch = "wasm32")]
578unsafe impl Send for Normals {}
579#[cfg(target_arch = "wasm32")]
580unsafe impl Sync for Normals {}
581#[cfg(target_arch = "wasm32")]
583unsafe impl Send for Tangents {}
584#[cfg(target_arch = "wasm32")]
585unsafe impl Sync for Tangents {}
586#[cfg(target_arch = "wasm32")]
588unsafe impl Send for Colors {}
589#[cfg(target_arch = "wasm32")]
590unsafe impl Sync for Colors {}
591#[cfg(target_arch = "wasm32")]
593unsafe impl Send for DiffuseImg {}
594#[cfg(target_arch = "wasm32")]
595unsafe impl Sync for DiffuseImg {}
596#[cfg(target_arch = "wasm32")]
598unsafe impl Send for NormalImg {}
599#[cfg(target_arch = "wasm32")]
600unsafe impl Sync for NormalImg {}
601#[cfg(target_arch = "wasm32")]
603unsafe impl Send for MetalnessImg {}
604#[cfg(target_arch = "wasm32")]
605unsafe impl Sync for MetalnessImg {}
606#[cfg(target_arch = "wasm32")]
608unsafe impl Send for RoughnessImg {}
609#[cfg(target_arch = "wasm32")]
610unsafe impl Sync for RoughnessImg {}
611#[cfg(target_arch = "wasm32")]
613unsafe impl Send for EnvironmentMap {}
614#[cfg(target_arch = "wasm32")]
615unsafe impl Sync for EnvironmentMap {}