pub struct Mesh { /* private fields */ }Expand description
Triangles in 3D that a composable can be drawn on.
Each vertex has a position, a normal and a texture coordinate. A
composable drawn on the mesh shows its content point (x, y) where the
texture coordinate is (x / width, y / height): (0, 0) is the content’s
top left corner and (1, 1) its bottom right.
use cranpose_ui_graphics::space::{Mesh, Vec3};
let sphere = Mesh::sphere(32, 16);
// The middle of the texture faces the viewer.
let front = sphere
.point_at([0.5, 0.5])
.expect("the sphere covers its texture");
assert!((front - Vec3::new(0.0, 0.0, -1.0)).length() < 1e-3);Two meshes are equal when they are the same mesh: a mesh does not change once made.
Implementations§
Source§impl Mesh
impl Mesh
Sourcepub fn new(
positions: Vec<[f32; 3]>,
uvs: Vec<[f32; 2]>,
normals: Option<Vec<[f32; 3]>>,
indices: Vec<u32>,
) -> Result<Mesh, MeshError>
pub fn new( positions: Vec<[f32; 3]>, uvs: Vec<[f32; 2]>, normals: Option<Vec<[f32; 3]>>, indices: Vec<u32>, ) -> Result<Mesh, MeshError>
A mesh from its vertices and the indices of its triangles’ corners,
three to a triangle. Without normals, each vertex takes the mean
direction of the triangles around it.
Sourcepub fn plane() -> Mesh
pub fn plane() -> Mesh
The unit square from (0, 0, 0) to (1, 1, 0), facing the viewer,
with the texture coordinate equal to the position. Scaled by a
composable’s size it shows the composable as it is laid out.
Sourcepub fn cuboid() -> Mesh
pub fn cuboid() -> Mesh
The unit cube from (-0.5, -0.5, -0.5) to (0.5, 0.5, 0.5). Each
face shows the whole texture, upright when seen from outside; the top
and bottom faces have the texture’s top toward the back.
Sourcepub fn sphere(segments: u32, rings: u32) -> Mesh
pub fn sphere(segments: u32, rings: u32) -> Mesh
The sphere of radius one about the origin, with segments slices
around and rings from top to bottom. The texture wraps around it
once: its left and right edges meet at the back, its top and bottom
edges are the poles, and its middle faces the viewer.
Sourcepub fn id(&self) -> u64
pub fn id(&self) -> u64
A number that no other mesh made in this process has. Renderers keep a mesh’s buffers under it.
Sourcepub fn normals(&self) -> &[[f32; 3]]
pub fn normals(&self) -> &[[f32; 3]]
The vertex normals, one for each position, each of length one.
Sourcepub fn indices(&self) -> &[u32]
pub fn indices(&self) -> &[u32]
The vertex indices of the triangles’ corners, three to a triangle.
Sourcepub fn cast(&self, origin: Vec3, direction: Vec3) -> Option<MeshHit>
pub fn cast(&self, origin: Vec3, direction: Vec3) -> Option<MeshHit>
The nearest place in front of origin where the ray along
direction meets a triangle, from either side.
Sourcepub fn cast_plane(
&self,
origin: Vec3,
direction: Vec3,
triangle: u32,
) -> Option<[f32; 2]>
pub fn cast_plane( &self, origin: Vec3, direction: Vec3, triangle: u32, ) -> Option<[f32; 2]>
Where the ray meets the plane of triangle, as a texture coordinate
carried on from the triangle’s corners: it lies outside the triangle
when the ray misses it. None when the ray runs along the plane or
meets it behind origin, or for a triangle that does not exist.
Sourcepub fn point_at(&self, uv: [f32; 2]) -> Option<Vec3>
pub fn point_at(&self, uv: [f32; 2]) -> Option<Vec3>
The position where the texture coordinate uv lies on the mesh, from
the first triangle that covers it; None when none does.
Sourcepub fn nearest(&self, point: Vec3, transform: &Mat4) -> Option<MeshContact>
pub fn nearest(&self, point: Vec3, transform: &Mat4) -> Option<MeshContact>
The point of the mesh, placed by transform, nearest point, with
the texture coordinate there; None for a mesh without triangles.
Sourcepub fn bounds(&self) -> (Vec3, Vec3)
pub fn bounds(&self) -> (Vec3, Vec3)
The smallest box along the axes that holds every vertex: its lowest and highest corners.
Sourcepub fn bounds_in(&self, transform: &Mat4) -> (Vec3, Vec3)
pub fn bounds_in(&self, transform: &Mat4) -> (Vec3, Vec3)
The smallest box along the axes that holds the corners of
Self::bounds that transform maps: its lowest and highest
corners.
Sourcepub fn texture_plane(&self) -> Option<TexturePlane>
pub fn texture_plane(&self) -> Option<TexturePlane>
The plane that carries texture coordinates onto the mesh, worked out
once: the plane of a flat mesh, or the one that touches a curved mesh
at the texture’s middle. None when the texture’s middle does not lie
on the mesh, or the mesh stretches it to a line there.
Source§impl Mesh
impl Mesh
Sourcepub fn from_obj(source: &str) -> Result<Mesh, ObjError>
pub fn from_obj(source: &str) -> Result<Mesh, ObjError>
A mesh read from the text of a Wavefront OBJ file.
It reads the positions (v), texture coordinates (vt), normals
(vn) and faces (f, split into triangles) of every object in the
file, and skips the other statements, such as materials and groups.
OBJ counts the texture’s v from the bottom; the mesh flips it, so a
model textured for an image shows a composable upright. A face
without texture coordinates takes (0, 0), and one without normals
takes smooth ones.
Source§impl Mesh
impl Mesh
Sourcepub fn from_sdf(
field: impl Fn(Vec3) -> f32,
bounds: (Vec3, Vec3),
resolution: u32,
) -> Result<Mesh, MeshError>
pub fn from_sdf( field: impl Fn(Vec3) -> f32, bounds: (Vec3, Vec3), resolution: u32, ) -> Result<Mesh, MeshError>
The surface where field is zero within the box from bounds.0 to
bounds.1: a signed distance field, negative inside the shape,
sampled on a grid of resolution cells along each side of the box.
The normals follow the field’s gradient, and the texture wraps the
shape as it wraps Mesh::sphere, seen from the middle of the box:
its middle faces the viewer, its left and right edges meet at the
back and its top and bottom edges are the poles. A field that changes
gives a shape that changes: build the mesh again for each new shape.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Mesh
impl RefUnwindSafe for Mesh
impl Send for Mesh
impl Sync for Mesh
impl Unpin for Mesh
impl UnsafeUnpin for Mesh
impl UnwindSafe for Mesh
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more