move boid_mesh code
This commit is contained in:
parent
e41ff1cf61
commit
d9a8896444
2 changed files with 76 additions and 73 deletions
71
src/boid_mesh.rs
Normal file
71
src/boid_mesh.rs
Normal file
|
@ -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<Assets<Mesh>>, mut boid_mesh: ResMut<BoidMesh>) {
|
||||||
|
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
|
||||||
|
}
|
78
src/main.rs
78
src/main.rs
|
@ -1,26 +1,17 @@
|
||||||
use bevy::{
|
mod boid_mesh;
|
||||||
color::palettes::tailwind,
|
|
||||||
prelude::*,
|
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
|
||||||
render::{
|
use boid_mesh::BoidMesh;
|
||||||
mesh::{Indices, PrimitiveTopology},
|
|
||||||
render_asset::RenderAssetUsages,
|
|
||||||
},
|
|
||||||
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
|
||||||
};
|
|
||||||
use std::f32::consts::FRAC_PI_2;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.init_resource::<BoidMesh>()
|
.init_resource::<BoidMesh>()
|
||||||
.add_plugins(DefaultPlugins)
|
.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)
|
.add_systems(Update, move_boids)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
|
||||||
struct BoidMesh(Mesh2dHandle);
|
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct Boid;
|
struct Boid;
|
||||||
|
|
||||||
|
@ -40,11 +31,6 @@ fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_boid_mesh(mut meshes: ResMut<Assets<Mesh>>, mut boid_mesh: ResMut<BoidMesh>) {
|
|
||||||
let mesh = Mesh2dHandle(meshes.add(make_boid_mesh()));
|
|
||||||
boid_mesh.0 = mesh;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup(
|
fn setup(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
|
@ -65,57 +51,3 @@ fn setup(
|
||||||
..default()
|
..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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue