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
//! Transform-only instances of a shared caller mesh.
use crate::MeshHandle;
use molgfx_math::Mat4;
/// One transform-only occurrence of a shared [`crate::Mesh`].
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct MeshInstance {
mesh: MeshHandle,
transform: Mat4,
visible: bool,
}
impl MeshInstance {
/// Creates a visible instance.
///
/// # Errors
///
/// Returns [`crate::CoreError::InvalidMesh`] for a non-finite transform.
pub fn new(mesh: MeshHandle, transform: Mat4) -> Result<Self, crate::CoreError> {
if !transform.is_finite() {
return Err(crate::CoreError::InvalidMesh {
reason: "mesh instance transforms must be finite",
});
}
Ok(Self {
mesh,
transform,
visible: true,
})
}
/// Shared source mesh.
#[must_use]
pub const fn mesh(&self) -> MeshHandle {
self.mesh
}
/// Instance-local transform, applied before structure placement.
#[must_use]
pub const fn transform(&self) -> Mat4 {
self.transform
}
/// Replaces the instance transform.
///
/// # Errors
///
/// Returns [`crate::CoreError::InvalidMesh`] for a non-finite transform.
pub fn set_transform(&mut self, transform: Mat4) -> Result<(), crate::CoreError> {
if !transform.is_finite() {
return Err(crate::CoreError::InvalidMesh {
reason: "mesh instance transforms must be finite",
});
}
self.transform = transform;
Ok(())
}
/// Whether this occurrence participates in its mesh batch.
#[must_use]
pub const fn visible(&self) -> bool {
self.visible
}
/// Shows or hides this occurrence without changing its stable handle.
pub const fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
}