From 73fa2f6dccf65b70d43d4b7921d925ca05cb04b4 Mon Sep 17 00:00:00 2001 From: Elham Aryanpur Date: Sat, 21 Jan 2023 19:17:44 +0300 Subject: [PATCH] refactor: added light and model loader --- Cargo.toml | 6 +++--- examples/basic_light.rs | 8 ++++---- src/definitions/light.rs | 16 +++++++--------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1213f40..bad757f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blue_engine_utilities" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["Elham Aryanpur "] description = "Utilities for Blue Engine" @@ -15,8 +15,8 @@ animation = ["dep:keyframe_derive", "dep:keyframe"] gltf = ["dep:gltf"] [dependencies] -# blue_engine = { version = "^0.4.22" } -blue_engine = { path = "../Blue Engine" } +blue_engine = { version = "^0.4.23" } +#blue_engine = { path = "../Blue Engine" } anyhow = "1.0" # Animation diff --git a/examples/basic_light.rs b/examples/basic_light.rs index 1086ccc..35fc9da 100644 --- a/examples/basic_light.rs +++ b/examples/basic_light.rs @@ -18,15 +18,15 @@ fn main() -> anyhow::Result<()> { .objects .get_mut("light sphere") .unwrap() - .set_color(1f32, 0f32, 0f32, 1f32); + .set_color(1f32, 0f32, 0f32, 1f32).expect("color couldn't change"); // load the monke - load_gltf("monke", "./resources/monkey.glb", &mut engine.renderer, &mut engine.objects); + load_gltf("monke", "./resources/monkey.glb", &mut engine.renderer, &mut engine.objects).expect("couldn't load the monke model"); engine .objects .get_mut("monke") .unwrap() - .set_color(0.051f32, 0.533f32, 0.898f32, 1f32); + .set_color(0.051f32, 0.533f32, 0.898f32, 1f32).expect("color couldn't change"); let mut light_manager = LightManager::new(); light_manager.set_object_as_light("light sphere".to_string()); @@ -35,7 +35,7 @@ fn main() -> anyhow::Result<()> { let start = std::time::SystemTime::now(); engine.update_loop(move |renderer, _, objects, _, camera, _| { - light_manager.update(objects, renderer, camera); + light_manager.update(objects, renderer, camera).expect("couldn't update the light manager"); let camx = start.elapsed().unwrap().as_secs_f32().sin() * radius; let camy = start.elapsed().unwrap().as_secs_f32().sin() * radius; diff --git a/src/definitions/light.rs b/src/definitions/light.rs index 6e0376a..59e332e 100644 --- a/src/definitions/light.rs +++ b/src/definitions/light.rs @@ -32,6 +32,7 @@ impl crate::LightManager { camera: &blue_engine::Camera, ) -> anyhow::Result<()> { let light_keys: Vec = self.light_objects.keys().map(|x| x.clone()).collect(); + let shader_content = include_str!("./light_shader.wgsl").to_string(); for i in objects.iter_mut() { let i = i.1; @@ -73,25 +74,22 @@ impl crate::LightManager { i.update_uniform_buffer(renderer)?; - let shader_content = include_str!("./light_shader.wgsl").to_string(); - i.shader_builder.shader = shader_content; + let mut shader_content = shader_content.clone(); if !self.affected_objects.contains(&i.name) { - let mut changes = Vec::<(&str, &str)>::new(); if i.camera_effect { - changes.push(("//@CAMERASTRUCT", r#" + shader_content = shader_content.replace("//@CAMERASTRUCT", r#" struct CameraUniforms { camera_matrix: mat4x4, }; @group(1) @binding(0) -var camera_uniform: CameraUniforms;"#)); - changes.push(("//@CAMERAOUT", "out.position = camera_uniform.camera_matrix * (transform_uniform.transform_matrix * vec4(input.position, 1.0));")); +var camera_uniform: CameraUniforms;"#); + shader_content = shader_content.replace("//@CAMERAOUT", "out.position = camera_uniform.camera_matrix * (transform_uniform.transform_matrix * vec4(input.position, 1.0));"); } else { - changes.push(("//@CAMERAOUT","out.position = transform_uniform.transform_matrix * vec4(input.position, 1.0);")); + shader_content = shader_content.replace("//@CAMERAOUT","out.position = transform_uniform.transform_matrix * vec4(input.position, 1.0);"); } - i.shader_builder.parse(changes); - println!("{}", i.shader_builder.shader); + i.shader_builder.shader = shader_content; i.update_shader(renderer)?; self.affected_objects.push(i.name.clone());