From e71add5ae10121f7a821456e7f6960ea96210131 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Sun, 14 May 2023 00:02:58 +0200 Subject: wgsl shader testing --- src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'src') 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::::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>, - mut materials: ResMut>, + mut base_materials: ResMut>, + mut enemy_materials: ResMut>, ) { 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, +} -- cgit v1.2.3-54-g00ecf