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}
120impl Default for VisLines {
122 fn default() -> VisLines {
123 VisLines {
124 show_lines: false,
125 line_color: na::Vector4::<f32>::new(1.0, 0.1, 0.1, 1.0),
126 line_width: 1.0,
127 color_type: LineColorType::Solid,
128 zbuffer: true,
129 antialias_edges: false,
130 added_automatically: false,
131 }
132 }
133}
134impl Default for VisWireframe {
135 fn default() -> VisWireframe {
136 VisWireframe {
137 show_wireframe: false,
138 wire_color: na::Vector4::<f32>::new(1.0, 0.0, 0.0, 1.0),
139 wire_width: 1.0,
140 added_automatically: false,
141 }
142 }
143}
144impl Default for VisNormals {
145 fn default() -> VisNormals {
146 VisNormals {
147 show_normals: false,
148 normals_color: na::Vector4::<f32>::new(1.0, 0.0, 0.0, 1.0),
149 normals_width: 1.0,
150 normals_scale: 1.0,
151 added_automatically: false,
152 }
153 }
154}
155impl Default for VisPoints {
156 fn default() -> VisPoints {
157 VisPoints {
158 show_points: false,
159 show_points_indices: false,
160 point_color: na::Vector4::<f32>::new(245.0 / 255.0, 175.0 / 255.0, 110.0 / 255.0, 1.0),
161 point_size: 1.0,
162 is_point_size_in_world_space: false,
163 color_type: PointColorType::Solid,
164 zbuffer: true,
165 added_automatically: false,
166 }
167 }
168}
169impl Default for VisMesh {
170 fn default() -> VisMesh {
171 VisMesh {
172 show_mesh: true,
173 solid_color: na::Vector4::<f32>::new(1.0, 206.0 / 255.0, 143.0 / 255.0, 1.0),
174 metalness: 0.0,
175 perceptual_roughness: 0.5,
176 roughness_black_lvl: 0.0,
177 uv_scale: 1.0,
178 opacity: 1.0,
179 needs_sss: false,
180 color_type: MeshColorType::Solid,
181 added_automatically: false,
182 }
183 }
184}
185
186#[derive(Clone)]
189pub struct ModelMatrix(pub na::SimilarityMatrix3<f32>); impl Default for ModelMatrix {
193 fn default() -> ModelMatrix {
194 ModelMatrix(na::SimilarityMatrix3::<f32>::identity())
195 }
196}
197impl ModelMatrix {
198 #[must_use]
199 pub fn with_translation(self, t: &na::Vector3<f32>) -> Self {
200 let mut mat = self;
201 mat.0.append_translation_mut(&na::Translation3::new(t[0], t[1], t[2]));
202 mat
203 }
204 #[must_use]
205 pub fn with_rotation_rot3(self, r: &na::Rotation3<f32>) -> Self {
206 let mut mat = self;
207 mat.0.append_rotation_mut(r);
208 mat
209 }
210 #[must_use]
211 pub fn with_rotation_axis_angle(self, v: &na::Vector3<f32>) -> Self {
212 let mut mat = self;
213 mat.0
214 .append_rotation_mut(&na::Rotation3::from_axis_angle(&na::UnitVector3::<f32>::new_normalize(*v), v.norm()));
215 mat
216 }
217 #[must_use]
218 pub fn with_rotation_euler(self, e: &na::Vector3<f32>) -> Self {
219 let mut mat = self;
220 mat.0.append_rotation_mut(&na::Rotation3::from_euler_angles(e.x, e.y, e.z));
221 mat
222 }
223}
224
225#[derive(Clone, Debug)]
227pub struct CamTrack(pub DMatrix<f32>);
228
229#[derive(Clone, Debug)]
230pub struct Verts(pub DynamicTensorFloat2D);
231
232#[derive(Clone, Debug)]
235pub struct EdgesV1(pub DynamicTensorFloat2D);
236#[derive(Clone, Debug)]
239pub struct EdgesV2(pub DynamicTensorFloat2D);
240
241#[derive(Clone)]
246pub struct Faces(pub DynamicTensorInt2D);
247
248#[derive(Clone, Debug)]
251pub struct Edges(pub DynamicTensorInt2D);
252
253#[derive(Clone)]
255pub struct UVs(pub DynamicTensorFloat2D);
256#[derive(Clone)]
262pub struct Normals(pub DynamicTensorFloat2D);
263
264#[derive(Clone)]
266pub struct Tangents(pub DynamicTensorFloat2D);
267
268#[derive(Clone)]
270pub struct Colors(pub DynamicTensorFloat2D);
271
272#[derive(Clone)]
273#[allow(clippy::struct_excessive_bools)]
274pub struct ImgConfig {
275 pub keep_on_cpu: bool,
276 pub fast_upload: bool, pub generate_mipmaps: bool,
278 pub mipmap_generation_cpu: bool,
279}
280impl Default for ImgConfig {
281 fn default() -> Self {
282 Self {
283 keep_on_cpu: true,
284 fast_upload: true,
285 generate_mipmaps: true,
286 mipmap_generation_cpu: false,
287 }
288 }
289}
290
291#[derive(Clone)]
294pub struct GenericImg {
295 pub path: Option<String>,
296 pub cpu_img: Option<DynImage>, pub config: ImgConfig,
298}
299impl GenericImg {
300 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
303 let cpu_img = Some(ImageReader::open(path).unwrap().decode().unwrap());
304
305 Self {
306 path: Some(path.to_string()),
307 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
308 config: config.clone(),
309 }
310 }
311
312 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
315 let reader = ImageReader::new(BufReader::new(FileLoader::open(path).await))
316 .with_guessed_format()
317 .expect("Cursor io never fails");
318
319 let cpu_img = Some(reader.decode().unwrap());
320
321 Self {
322 path: Some(path.to_string()),
323 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
324 config: config.clone(),
325 }
326 }
327
328 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
331 Self::new_from_reader(Cursor::new(buf), config)
332 }
333
334 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
337 let reader_img = ImageReader::new(BufReader::new(reader))
338 .with_guessed_format()
339 .expect("Format for image should be something known and valid");
340
341 let cpu_img = Some(reader_img.decode().unwrap());
342
343 Self {
344 path: None,
345 cpu_img: cpu_img.map(|v| v.try_into().unwrap()),
346 config: config.clone(),
347 }
348 }
349
350 pub fn img_ref(&self) -> &DynImage {
351 self.cpu_img.as_ref().unwrap()
352 }
353
354 pub fn img_ref_mut(&mut self) -> &mut DynImage {
355 self.cpu_img.as_mut().unwrap()
356 }
357}
358
359#[derive(Clone)]
361pub struct DiffuseImg {
362 pub generic_img: GenericImg,
363}
364
365impl DiffuseImg {
366 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
367 let generic_img = GenericImg::new_from_path(path, config);
368 Self { generic_img }
369 }
370
371 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
372 let generic_img = GenericImg::new_from_path_async(path, config).await;
373 Self { generic_img }
374 }
375
376 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
377 let generic_img = GenericImg::new_from_buf(buf, config);
378 Self { generic_img }
379 }
380
381 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
382 let generic_img = GenericImg::new_from_reader(reader, config);
383 Self { generic_img }
384 }
385}
386
387#[derive(Clone)]
389pub struct NormalImg {
390 pub generic_img: GenericImg,
391}
392
393impl NormalImg {
394 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
395 let generic_img = GenericImg::new_from_path(path, config);
396 Self { generic_img }
397 }
398
399 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
400 let generic_img = GenericImg::new_from_path_async(path, config).await;
401 Self { generic_img }
402 }
403
404 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
405 let generic_img = GenericImg::new_from_buf(buf, config);
406 Self { generic_img }
407 }
408
409 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
410 let generic_img = GenericImg::new_from_reader(reader, config);
411 Self { generic_img }
412 }
413}
414
415pub struct MetalnessImg {
417 pub generic_img: GenericImg,
418}
419impl MetalnessImg {
420 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
421 let generic_img = GenericImg::new_from_path(path, config);
422 Self { generic_img }
423 }
424
425 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
426 let generic_img = GenericImg::new_from_path_async(path, config).await;
427 Self { generic_img }
428 }
429
430 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
431 let generic_img = GenericImg::new_from_buf(buf, config);
432 Self { generic_img }
433 }
434
435 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
436 let generic_img = GenericImg::new_from_reader(reader, config);
437 Self { generic_img }
438 }
439}
440
441pub struct RoughnessImg {
444 pub generic_img: GenericImg,
445}
446impl RoughnessImg {
447 pub fn new_from_path(path: &str, config: &ImgConfig) -> Self {
448 let generic_img = GenericImg::new_from_path(path, config);
449 Self { generic_img }
450 }
451
452 pub async fn new_from_path_async(path: &str, config: &ImgConfig) -> Self {
453 let generic_img = GenericImg::new_from_path_async(path, config).await;
454 Self { generic_img }
455 }
456
457 pub fn new_from_buf(buf: &[u8], config: &ImgConfig) -> Self {
458 let generic_img = GenericImg::new_from_buf(buf, config);
459 Self { generic_img }
460 }
461
462 pub fn new_from_reader<R: Read + Seek>(reader: R, config: &ImgConfig) -> Self {
463 let generic_img = GenericImg::new_from_reader(reader, config);
464 Self { generic_img }
465 }
466}
467
468pub struct EnvironmentMap {
505 pub diffuse_path: String,
506 pub specular_path: String,
507}
508impl EnvironmentMap {
509 pub fn new_from_path(diffuse_path: &str, specular_path: &str) -> Self {
510 Self {
511 diffuse_path: String::from(diffuse_path),
512 specular_path: String::from(specular_path),
513 }
514 }
515}
516
517#[cfg(target_arch = "wasm32")]
522unsafe impl Send for ConfigChanges {}
523#[cfg(target_arch = "wasm32")]
524unsafe impl Sync for ConfigChanges {}
525#[cfg(target_arch = "wasm32")]
527unsafe impl Send for Verts {}
528#[cfg(target_arch = "wasm32")]
529unsafe impl Sync for Verts {}
530#[cfg(target_arch = "wasm32")]
532unsafe impl Send for EdgesV1 {}
533#[cfg(target_arch = "wasm32")]
534unsafe impl Sync for EdgesV1 {}
535#[cfg(target_arch = "wasm32")]
537unsafe impl Send for EdgesV2 {}
538#[cfg(target_arch = "wasm32")]
539unsafe impl Sync for EdgesV2 {}
540#[cfg(target_arch = "wasm32")]
542unsafe impl Send for Edges {}
543#[cfg(target_arch = "wasm32")]
544unsafe impl Sync for Edges {}
545#[cfg(target_arch = "wasm32")]
547unsafe impl Send for Faces {}
548#[cfg(target_arch = "wasm32")]
549unsafe impl Sync for Faces {}
550#[cfg(target_arch = "wasm32")]
552unsafe impl Send for UVs {}
553#[cfg(target_arch = "wasm32")]
554unsafe impl Sync for UVs {}
555#[cfg(target_arch = "wasm32")]
557unsafe impl Send for Normals {}
558#[cfg(target_arch = "wasm32")]
559unsafe impl Sync for Normals {}
560#[cfg(target_arch = "wasm32")]
562unsafe impl Send for Tangents {}
563#[cfg(target_arch = "wasm32")]
564unsafe impl Sync for Tangents {}
565#[cfg(target_arch = "wasm32")]
567unsafe impl Send for Colors {}
568#[cfg(target_arch = "wasm32")]
569unsafe impl Sync for Colors {}
570#[cfg(target_arch = "wasm32")]
572unsafe impl Send for DiffuseImg {}
573#[cfg(target_arch = "wasm32")]
574unsafe impl Sync for DiffuseImg {}
575#[cfg(target_arch = "wasm32")]
577unsafe impl Send for NormalImg {}
578#[cfg(target_arch = "wasm32")]
579unsafe impl Sync for NormalImg {}
580#[cfg(target_arch = "wasm32")]
582unsafe impl Send for MetalnessImg {}
583#[cfg(target_arch = "wasm32")]
584unsafe impl Sync for MetalnessImg {}
585#[cfg(target_arch = "wasm32")]
587unsafe impl Send for RoughnessImg {}
588#[cfg(target_arch = "wasm32")]
589unsafe impl Sync for RoughnessImg {}
590#[cfg(target_arch = "wasm32")]
592unsafe impl Send for EnvironmentMap {}
593#[cfg(target_arch = "wasm32")]
594unsafe impl Sync for EnvironmentMap {}