diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2023-05-14 00:02:58 +0200 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2023-05-14 00:02:58 +0200 |
commit | e71add5ae10121f7a821456e7f6960ea96210131 (patch) | |
tree | bb988d444f9ad0d7dcbf9388067ab64facb7a94b /src | |
parent | f2019713decb9dd1b486e4576eccb1bbac2c1d61 (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index 24b706f..9e43468 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,11 @@ use std::collections::HashMap; use itertools::Itertools; -use bevy::prelude::*; +use bevy::{ + prelude::*, + reflect::TypeUuid, + render::render_resource::{AsBindGroup, ShaderRef}, +}; use polyhedron_ops as p_ops; use smooth_bevy_cameras::{ controllers::orbit::{OrbitCameraBundle, OrbitCameraController, OrbitCameraPlugin}, @@ -17,6 +21,7 @@ fn main() { .add_plugin(OrbitCameraPlugin::default()) .add_startup_system(setup) .add_system(bevy::window::close_on_esc) + .add_plugin(MaterialPlugin::<EnemyMaterial>::default()) .run(); } @@ -30,7 +35,6 @@ impl ShapeMap { let mut sm = ShapeMap { shapes: HashMap::new(), }; - for var in (1..=depth as usize).flat_map(|n| { std::iter::repeat(ops.chars()) .take(n) @@ -50,18 +54,22 @@ impl ShapeMap { fn setup( mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, - mut materials: ResMut<Assets<StandardMaterial>>, + mut base_materials: ResMut<Assets<StandardMaterial>>, + mut enemy_materials: ResMut<Assets<EnemyMaterial>>, ) { let depth = 1; - let sm = ShapeMap::new("T", "asdk", depth); + let sm = ShapeMap::new("T", "skad", depth); let mut x = 0; let mut y = 0; for (_, s) in sm.shapes.iter() { let pos = Vec3::new(0.0, x as f32 * 2.0, y as f32 * 2.0); - commands.spawn(PbrBundle { + commands.spawn(MaterialMeshBundle { mesh: meshes.add(Mesh::from(s.clone())), - material: materials.add(Color::rgb(0.4, 0.35, 0.3).into()), + material: enemy_materials.add(EnemyMaterial { + color: Color::rgba(1.0, 1.0, 1.0, 1.0), + alpha_mode: AlphaMode::Opaque, + }), transform: Transform::from_translation(pos), ..default() }); @@ -72,6 +80,14 @@ fn setup( } } + // reference cube + commands.spawn(MaterialMeshBundle { + mesh: meshes.add(Mesh::from(shape::Cube::default())), + material: base_materials.add(Color::BLUE.into()), + transform: Transform::from_translation(Vec3::new(-2.0, 0.0, 0.0)), + ..default() + }); + commands // light .spawn(DirectionalLightBundle { @@ -93,3 +109,21 @@ fn setup( Vec3::new(0., -1., 0.), )); } + +impl Material for EnemyMaterial { + fn fragment_shader() -> ShaderRef { + "shaders/enemy_material.wgsl".into() + } + + fn alpha_mode(&self) -> AlphaMode { + self.alpha_mode + } +} + +#[derive(AsBindGroup, TypeUuid, Debug, Clone)] +#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"] +pub struct EnemyMaterial { + #[uniform(0)] + color: Color, + alpha_mode: AlphaMode, +} |