Skip to content

Commit

Permalink
refactor: added light and model loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Jan 21, 2023
1 parent 98f1da7 commit 73fa2f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blue_engine_utilities"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["Elham Aryanpur <[email protected]>"]
description = "Utilities for Blue Engine"
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/basic_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions src/definitions/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl crate::LightManager {
camera: &blue_engine::Camera,
) -> anyhow::Result<()> {
let light_keys: Vec<String> = 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;
Expand Down Expand Up @@ -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<f32>,
};
@group(1) @binding(0)
var<uniform> camera_uniform: CameraUniforms;"#));
changes.push(("//@CAMERAOUT", "out.position = camera_uniform.camera_matrix * (transform_uniform.transform_matrix * vec4<f32>(input.position, 1.0));"));
var<uniform> camera_uniform: CameraUniforms;"#);
shader_content = shader_content.replace("//@CAMERAOUT", "out.position = camera_uniform.camera_matrix * (transform_uniform.transform_matrix * vec4<f32>(input.position, 1.0));");
} else {
changes.push(("//@CAMERAOUT","out.position = transform_uniform.transform_matrix * vec4<f32>(input.position, 1.0);"));
shader_content = shader_content.replace("//@CAMERAOUT","out.position = transform_uniform.transform_matrix * vec4<f32>(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());
Expand Down

0 comments on commit 73fa2f6

Please sign in to comment.