From d9a88964444b83472f8974171347a2cd85151e07 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Sun, 1 Sep 2024 14:24:22 -0700 Subject: [PATCH] move boid_mesh code --- src/boid_mesh.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 78 ++++-------------------------------------------- 2 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 src/boid_mesh.rs diff --git a/src/boid_mesh.rs b/src/boid_mesh.rs new file mode 100644 index 0000000..1e50871 --- /dev/null +++ b/src/boid_mesh.rs @@ -0,0 +1,71 @@ +use bevy::{ + prelude::*, + render::{ + mesh::{Indices, PrimitiveTopology}, + render_asset::RenderAssetUsages, + }, + sprite::Mesh2dHandle, +}; +use std::f32::consts::FRAC_PI_2; + +#[derive(Resource, Default)] +pub struct BoidMesh(pub Mesh2dHandle); + +pub fn build_boid_mesh(mut meshes: ResMut>, mut boid_mesh: ResMut) { + let mesh = Mesh2dHandle(meshes.add(make_boid_mesh())); + boid_mesh.0 = mesh; +} + +pub fn make_boid_mesh() -> Mesh { + const TRIANGLE_HEIGHT: f32 = 10.; + const TRIANGLE_WIDTH: f32 = 5.; + const STROKE_WIDTH: f32 = 0.25; + + const HALF_HEIGHT: f32 = TRIANGLE_HEIGHT / 2.0; + const HALF_WIDTH: f32 = TRIANGLE_WIDTH / 2.0; + + let w2 = (HALF_WIDTH * HALF_WIDTH + TRIANGLE_HEIGHT * TRIANGLE_HEIGHT).sqrt(); + let c = STROKE_WIDTH / (HALF_WIDTH / w2); + + let alpha: f32 = (TRIANGLE_HEIGHT / HALF_WIDTH).atan(); + let b: f32 = STROKE_WIDTH * (FRAC_PI_2 - (alpha / 2.)).tan(); + + let mut boid_mesh = Mesh::new( + PrimitiveTopology::TriangleList, + RenderAssetUsages::RENDER_WORLD, + ); + + #[rustfmt::skip] + let verts = vec![ + [0.0, HALF_HEIGHT - c, 0.0], + [0.0, HALF_HEIGHT, 0.0], + [ + HALF_WIDTH - b, + -HALF_HEIGHT + STROKE_WIDTH, + 0.0, + ], + [HALF_WIDTH, -HALF_HEIGHT, 0.0], + [ + -HALF_WIDTH + b, + -HALF_HEIGHT + STROKE_WIDTH, + 0.0, + ], + [-HALF_WIDTH, -HALF_HEIGHT, 0.0], + ]; + + boid_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, verts); + + #[rustfmt::skip] + let indices = vec![ + 0, 1, 5, + 0, 2, 1, + 1, 2, 3, + 0, 5, 4, + 3, 4, 5, + 2, 4, 3 + ]; + + boid_mesh.insert_indices(Indices::U32(indices)); + + boid_mesh +} diff --git a/src/main.rs b/src/main.rs index c35c347..53fddd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,17 @@ -use bevy::{ - color::palettes::tailwind, - prelude::*, - render::{ - mesh::{Indices, PrimitiveTopology}, - render_asset::RenderAssetUsages, - }, - sprite::{MaterialMesh2dBundle, Mesh2dHandle}, -}; -use std::f32::consts::FRAC_PI_2; +mod boid_mesh; + +use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle}; +use boid_mesh::BoidMesh; fn main() { App::new() .init_resource::() .add_plugins(DefaultPlugins) - .add_systems(Startup, (build_boid_mesh, setup).chain()) + .add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain()) .add_systems(Update, move_boids) .run(); } -#[derive(Resource, Default)] -struct BoidMesh(Mesh2dHandle); - #[derive(Component, Default)] struct Boid; @@ -40,11 +31,6 @@ fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With>) { }); } -fn build_boid_mesh(mut meshes: ResMut>, mut boid_mesh: ResMut) { - let mesh = Mesh2dHandle(meshes.add(make_boid_mesh())); - boid_mesh.0 = mesh; -} - fn setup( mut commands: Commands, mut materials: ResMut>, @@ -65,57 +51,3 @@ fn setup( ..default() }); } - -fn make_boid_mesh() -> Mesh { - const TRIANGLE_HEIGHT: f32 = 10.; - const TRIANGLE_WIDTH: f32 = 5.; - const STROKE_WIDTH: f32 = 0.25; - - const HALF_HEIGHT: f32 = TRIANGLE_HEIGHT / 2.0; - const HALF_WIDTH: f32 = TRIANGLE_WIDTH / 2.0; - - let w2 = (HALF_WIDTH * HALF_WIDTH + TRIANGLE_HEIGHT * TRIANGLE_HEIGHT).sqrt(); - let c = STROKE_WIDTH / (HALF_WIDTH / w2); - - let alpha: f32 = (TRIANGLE_HEIGHT / HALF_WIDTH).atan(); - let b: f32 = STROKE_WIDTH * (FRAC_PI_2 - (alpha / 2.)).tan(); - - let mut boid_mesh = Mesh::new( - PrimitiveTopology::TriangleList, - RenderAssetUsages::RENDER_WORLD, - ); - - #[rustfmt::skip] - let verts = vec![ - [0.0, HALF_HEIGHT - c, 0.0], - [0.0, HALF_HEIGHT, 0.0], - [ - HALF_WIDTH - b, - -HALF_HEIGHT + STROKE_WIDTH, - 0.0, - ], - [HALF_WIDTH, -HALF_HEIGHT, 0.0], - [ - -HALF_WIDTH + b, - -HALF_HEIGHT + STROKE_WIDTH, - 0.0, - ], - [-HALF_WIDTH, -HALF_HEIGHT, 0.0], - ]; - - boid_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, verts); - - #[rustfmt::skip] - let indices = vec![ - 0, 1, 5, - 0, 2, 1, - 1, 2, 3, - 0, 5, 4, - 3, 4, 5, - 2, 4, 3 - ]; - - boid_mesh.insert_indices(Indices::U32(indices)); - - boid_mesh -}