Path: blob/main/crates/bevy_mesh/src/primitives/dim3/cuboid.rs
6598 views
use crate::{Indices, Mesh, MeshBuilder, Meshable, PrimitiveTopology};1use bevy_asset::RenderAssetUsages;2use bevy_math::{primitives::Cuboid, Vec3};3use bevy_reflect::prelude::*;45/// A builder used for creating a [`Mesh`] with a [`Cuboid`] shape.6#[derive(Clone, Copy, Debug, Reflect)]7#[reflect(Default, Debug, Clone)]8pub struct CuboidMeshBuilder {9half_size: Vec3,10}1112impl Default for CuboidMeshBuilder {13/// Returns the default [`CuboidMeshBuilder`] with a width, height, and depth of `1.0`.14fn default() -> Self {15Self {16half_size: Vec3::splat(0.5),17}18}19}2021impl MeshBuilder for CuboidMeshBuilder {22fn build(&self) -> Mesh {23let min = -self.half_size;24let max = self.half_size;2526// Suppose Y-up right hand, and camera look from +Z to -Z27let vertices = &[28// Front29([min.x, min.y, max.z], [0.0, 0.0, 1.0], [0.0, 0.0]),30([max.x, min.y, max.z], [0.0, 0.0, 1.0], [1.0, 0.0]),31([max.x, max.y, max.z], [0.0, 0.0, 1.0], [1.0, 1.0]),32([min.x, max.y, max.z], [0.0, 0.0, 1.0], [0.0, 1.0]),33// Back34([min.x, max.y, min.z], [0.0, 0.0, -1.0], [1.0, 0.0]),35([max.x, max.y, min.z], [0.0, 0.0, -1.0], [0.0, 0.0]),36([max.x, min.y, min.z], [0.0, 0.0, -1.0], [0.0, 1.0]),37([min.x, min.y, min.z], [0.0, 0.0, -1.0], [1.0, 1.0]),38// Right39([max.x, min.y, min.z], [1.0, 0.0, 0.0], [0.0, 0.0]),40([max.x, max.y, min.z], [1.0, 0.0, 0.0], [1.0, 0.0]),41([max.x, max.y, max.z], [1.0, 0.0, 0.0], [1.0, 1.0]),42([max.x, min.y, max.z], [1.0, 0.0, 0.0], [0.0, 1.0]),43// Left44([min.x, min.y, max.z], [-1.0, 0.0, 0.0], [1.0, 0.0]),45([min.x, max.y, max.z], [-1.0, 0.0, 0.0], [0.0, 0.0]),46([min.x, max.y, min.z], [-1.0, 0.0, 0.0], [0.0, 1.0]),47([min.x, min.y, min.z], [-1.0, 0.0, 0.0], [1.0, 1.0]),48// Top49([max.x, max.y, min.z], [0.0, 1.0, 0.0], [1.0, 0.0]),50([min.x, max.y, min.z], [0.0, 1.0, 0.0], [0.0, 0.0]),51([min.x, max.y, max.z], [0.0, 1.0, 0.0], [0.0, 1.0]),52([max.x, max.y, max.z], [0.0, 1.0, 0.0], [1.0, 1.0]),53// Bottom54([max.x, min.y, max.z], [0.0, -1.0, 0.0], [0.0, 0.0]),55([min.x, min.y, max.z], [0.0, -1.0, 0.0], [1.0, 0.0]),56([min.x, min.y, min.z], [0.0, -1.0, 0.0], [1.0, 1.0]),57([max.x, min.y, min.z], [0.0, -1.0, 0.0], [0.0, 1.0]),58];5960let positions: Vec<_> = vertices.iter().map(|(p, _, _)| *p).collect();61let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();62let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();6364let indices = Indices::U32(vec![650, 1, 2, 2, 3, 0, // front664, 5, 6, 6, 7, 4, // back678, 9, 10, 10, 11, 8, // right6812, 13, 14, 14, 15, 12, // left6916, 17, 18, 18, 19, 16, // top7020, 21, 22, 22, 23, 20, // bottom71]);7273Mesh::new(74PrimitiveTopology::TriangleList,75RenderAssetUsages::default(),76)77.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)78.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)79.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)80.with_inserted_indices(indices)81}82}8384impl Meshable for Cuboid {85type Output = CuboidMeshBuilder;8687fn mesh(&self) -> Self::Output {88CuboidMeshBuilder {89half_size: self.half_size,90}91}92}9394impl From<Cuboid> for Mesh {95fn from(cuboid: Cuboid) -> Self {96cuboid.mesh().build()97}98}99100101