activating shader

This commit is contained in:
Fredi 2024-07-07 17:20:24 +02:00
parent 3816dbac77
commit ffeb6524ce
5 changed files with 60 additions and 23 deletions

View file

@ -0,0 +1,5 @@
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let global_coord: vec3<f32> = in.world_position;
return vec4(in.color[3],in.color[3],in.color[3], in.color[3]);
}

View file

@ -41,18 +41,19 @@ impl HouseState {
} }
} }
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { pub fn draw(state: &crate::State, _ctx: &mut comfy::EngineContext<'_>) {
let Some(house) = state.house() else { let Some(house) = state.house() else {
error!("How can I render a house when I'm not in one?!?"); error!("How can I render a house when I'm not in one?!?");
return; return;
}; };
//Draw House //Draw House
house house.rooms.get(house.current_room_id).unwrap().draw(
.rooms house.human_layer,
.get(house.current_room_id) house.player.can_see_metal(0.1),
.unwrap() house.player.get_position(),
.draw(house.human_layer, house.player.can_see_metal(0.1)); _ctx
);
//Draw Grid //Draw Grid
//state.house.grid.draw(); //state.house.grid.draw();

View file

@ -32,6 +32,9 @@ impl Player {
} }
} }
pub fn get_position(&self) -> Vec2 {
self.position
}
pub fn draw(&self) { pub fn draw(&self) {
draw_circle(self.position, 0.5, RED, 0); draw_circle(self.position, 0.5, RED, 0);
} }

View file

@ -1,7 +1,7 @@
use super::{furniture::Furniture, grid::Grid}; use super::{furniture::Furniture, grid::Grid};
use crate::game; use crate::game;
use comfy::{ use comfy::{
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE create_reloadable_sprite_shader, draw_rect, draw_rect_outline, draw_sprite, error, hashmap, random_i32, set_uniform_f32, use_default_shader, use_shader, vec2, EngineContext, HashSet, Index, RandomRange as _, ReloadableShaderSource, UniformDef, Vec2, GREEN, PURPLE, RED, WHITE
}; };
use indexmap::IndexSet; use indexmap::IndexSet;
@ -88,7 +88,10 @@ impl Room {
empty_spots.swap_remove_index(random_idx) empty_spots.swap_remove_index(random_idx)
} }
fn random_empty_spot_size(empty_spots: &mut IndexSet<u8>, size: u8) -> Option<u8> { fn random_empty_spot_size(
empty_spots: &mut IndexSet<u8>,
size: u8
) -> Option<u8> {
let mut empty_spots_size = IndexSet::<u8>::new(); let mut empty_spots_size = IndexSet::<u8>::new();
for &index in empty_spots.iter() { for &index in empty_spots.iter() {
@ -281,7 +284,8 @@ impl Room {
} }
}, },
RoomType::LivingRoom => { _ => {
//RoomType::LivingRoom => {
let has_couch = match u8::gen_range(0, 2) { let has_couch = match u8::gen_range(0, 2) {
0 => false, 0 => false,
1 => true, 1 => true,
@ -296,7 +300,6 @@ impl Room {
z: 0 z: 0
}); });
} }
} }
if let Some(pos) = random_empty_spot(&mut empty_spots) { if let Some(pos) = random_empty_spot(&mut empty_spots) {
@ -316,10 +319,8 @@ impl Room {
z: 0 z: 0
}); });
} }
}
}, //_ => {}
_ => {}
} }
furnitures furnitures
@ -375,7 +376,13 @@ impl Room {
Grid::new(nodes, connections) Grid::new(nodes, connections)
} }
pub fn draw(&self, human_layer: bool, magnet_layer: bool) { pub fn draw(
&self,
human_layer: bool,
magnet_layer: bool,
position: Vec2,
_ctx: &mut comfy::EngineContext<'_>
) {
let (width, height) = self.size; let (width, height) = self.size;
draw_rect( draw_rect(
@ -416,7 +423,27 @@ impl Room {
} }
} }
if magnet_layer { if true {
let magnet_shader_id = Some(
create_reloadable_sprite_shader(
&mut _ctx.renderer.shaders.borrow_mut(),
"magnet-shader",
ReloadableShaderSource {
static_source: include_str!("magnet-shader.wgsl").to_string(),
path: "src/activities/house/magnet-shader.wgsl".to_string()
},
hashmap! {
// "px".to_string() => UniformDef::F32(None),
// "py".to_string() => UniformDef::F32(None),
}
)
.unwrap()
).unwrap();
use_shader(magnet_shader_id);
// set_uniform_f32("px", position.x);
// set_uniform_f32("py", position.y);
if let Some(texture) = tile.f.get_magnet_texture_handle() { if let Some(texture) = tile.f.get_magnet_texture_handle() {
draw_sprite( draw_sprite(
texture, texture,
@ -426,6 +453,7 @@ impl Room {
tile.size * SCALE tile.size * SCALE
); );
} }
use_default_shader();
} }
if tile.f.is_on() { if tile.f.is_on() {

View file

@ -103,7 +103,7 @@ pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
} }
} }
pub fn draw(state: &State, engine: &EngineContext<'_>) { pub fn draw(state: &State, engine: &mut EngineContext<'_>) {
match state.activity { match state.activity {
Activity::House(_) => house::draw(state, engine), Activity::House(_) => house::draw(state, engine),
Activity::Overworld => overworld::draw(state, engine) Activity::Overworld => overworld::draw(state, engine)