Path: blob/main/crates/bevy_mesh/src/primitives/dim3/segment3d.rs
6598 views
use crate::{Indices, Mesh, MeshBuilder, Meshable, PrimitiveTopology};1use bevy_asset::RenderAssetUsages;2use bevy_math::primitives::Segment3d;3use bevy_reflect::prelude::*;45/// A builder used for creating a [`Mesh`] with a [`Segment3d`] shape.6#[derive(Clone, Copy, Debug, Default, Reflect)]7#[reflect(Default, Debug, Clone)]8pub struct Segment3dMeshBuilder {9segment: Segment3d,10}1112impl MeshBuilder for Segment3dMeshBuilder {13fn build(&self) -> Mesh {14let positions: Vec<_> = self.segment.vertices.into();15let indices = Indices::U32(vec![0, 1]);1617Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default())18.with_inserted_indices(indices)19.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)20}21}2223impl Meshable for Segment3d {24type Output = Segment3dMeshBuilder;2526fn mesh(&self) -> Self::Output {27Segment3dMeshBuilder { segment: *self }28}29}3031impl From<Segment3d> for Mesh {32fn from(segment: Segment3d) -> Self {33segment.mesh().build()34}35}3637#[cfg(test)]38mod tests {39use super::*;40use crate::Meshable;41use bevy_math::Vec3;4243#[test]44fn segment3d_mesh_builder() {45let segment = Segment3d::new(Vec3::ZERO, Vec3::X);46let mesh = segment.mesh().build();47assert_eq!(mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap().len(), 2);48assert_eq!(mesh.indices().unwrap().len(), 2);49}50}515253