Skip to main content

Mesh

Struct Mesh 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn id(&self) -> u64

A number that no other mesh made in this process has. Renderers keep a mesh’s buffers under it.

Source

pub fn positions(&self) -> &[[f32; 3]]

The vertex positions.

Source

pub fn normals(&self) -> &[[f32; 3]]

The vertex normals, one for each position, each of length one.

Source

pub fn uvs(&self) -> &[[f32; 2]]

The vertex texture coordinates, one for each position.

Source

pub fn indices(&self) -> &[u32]

The vertex indices of the triangles’ corners, three to a triangle.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn bounds(&self) -> (Vec3, Vec3)

The smallest box along the axes that holds every vertex: its lowest and highest corners.

Source

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.

Source

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

Source

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

Source

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§

Source§

impl Debug for Mesh

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Mesh

Source§

fn eq(&self, other: &Mesh) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnyEq for T
where T: Any + PartialEq,

Source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> LayoutData for T
where T: Any + Debug + PartialEq,

Source§

fn same_as(&self, other: &(dyn LayoutData + 'static)) -> bool

Whether other holds the same data, so a parent can keep its last measure.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

The data as Any, to read it by its type.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.