Skip to content

Commit c812d23

Browse files
committed
Make BRDF example more intresting
1 parent e3a0120 commit c812d23

File tree

1 file changed

+67
-79
lines changed

1 file changed

+67
-79
lines changed

crates/engine/examples/brdf/main.rs

Lines changed: 67 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
use std::{path::Path, rc::Rc};
1+
use std::{
2+
cell::Cell,
3+
path::Path,
4+
rc::{Rc, Weak},
5+
};
26

37
/// Renders a brdf example
48
use gfx_maths::*;
59
use vulkan_engine::{
6-
core::engine::Engine,
10+
core::{engine::Engine, input::Input},
711
scene::{
812
component::{
913
camera_component::CameraComponent, debug_movement_component::DebugMovementComponent,
10-
light_component::LightComponent, renderer::RendererComponent,
14+
light_component::LightComponent, renderer::RendererComponent, Component,
1115
},
16+
entity::Entity,
1217
light::{DirectionalLight, PointLight},
1318
material::MaterialPipeline,
1419
model::Model,
@@ -112,43 +117,24 @@ fn setup(engine: &mut Engine) {
112117
}
113118
}
114119

115-
scene
116-
.new_entity_with_transform(
117-
"DirLight1".to_string(),
118-
Transform {
119-
position: Vec3::zero(),
120-
rotation: Quaternion::axis_angle(Vec3::new(1.0, 0.0, 0.0), -90.0f32.to_radians()),
121-
scale: Vec3::one(),
122-
},
123-
)
124-
.new_component::<LightComponent>()
125-
.light
126-
.set(
127-
DirectionalLight {
128-
direction: Vec4::zero(),
129-
illuminance: Vec4::new(10.1, 10.1, 10.1, 0.0),
130-
}
131-
.into(),
132-
);
120+
let sun = scene.new_entity_with_transform(
121+
"Sun".to_string(),
122+
Transform {
123+
position: Vec3::zero(),
124+
rotation: Quaternion::axis_angle(Vec3::new(1.0, 0.0, 0.0), -90.0f32.to_radians()),
125+
scale: Vec3::one(),
126+
},
127+
);
133128

134-
scene
135-
.new_entity_with_transform(
136-
"DirLight2".to_string(),
137-
Transform {
138-
position: Vec3::zero(),
139-
rotation: Quaternion::axis_angle(Vec3::new(1.0, 0.0, 0.0), 90.0f32.to_radians()),
140-
scale: Vec3::one(),
141-
},
142-
)
143-
.new_component::<LightComponent>()
144-
.light
145-
.set(
146-
DirectionalLight {
147-
direction: Vec4::zero(),
148-
illuminance: Vec4::new(1.6, 1.6, 1.6, 0.0),
149-
}
150-
.into(),
151-
);
129+
sun.new_component::<LightComponent>().light.set(
130+
DirectionalLight {
131+
direction: Vec4::zero(),
132+
illuminance: Vec4::new(239.0, 245.0, 218.0, 0.0) / 50.0,
133+
}
134+
.into(),
135+
);
136+
137+
sun.new_component::<RotationComponent>();
152138

153139
scene
154140
.new_entity_with_transform(
@@ -188,46 +174,9 @@ fn setup(engine: &mut Engine) {
188174
);
189175
scene
190176
.new_entity_with_transform(
191-
"PointLight White 3".to_string(),
192-
Transform {
193-
position: Vec3::new(0.1, -3.0, -3.0),
194-
rotation: Quaternion::identity(),
195-
scale: Vec3::one(),
196-
},
197-
)
198-
.new_component::<LightComponent>()
199-
.light
200-
.set(
201-
PointLight {
202-
position: Vec4::zero(),
203-
luminous_flux: Vec4::new(100.0, 100.0, 100.0, 0.0),
204-
}
205-
.into(),
206-
);
207-
scene
208-
.new_entity_with_transform(
209-
"PointLight White 4".to_string(),
210-
Transform {
211-
position: Vec3::new(0.1, -3.0, -3.0),
212-
rotation: Quaternion::identity(),
213-
scale: Vec3::one(),
214-
},
215-
)
216-
.new_component::<LightComponent>()
217-
.light
218-
.set(
219-
PointLight {
220-
position: Vec4::zero(),
221-
luminous_flux: Vec4::new(100.0, 100.0, 100.0, 0.0),
222-
}
223-
.into(),
224-
);
225-
226-
scene
227-
.new_entity_with_transform(
228-
"PointLight Red".to_string(),
177+
"PointLight Cyan 3".to_string(),
229178
Transform {
230-
position: Vec3::new(0.0, 0.0, -3.0),
179+
position: Vec3::new(0.0, 0.0, 8.0),
231180
rotation: Quaternion::identity(),
232181
scale: Vec3::one(),
233182
},
@@ -237,10 +186,49 @@ fn setup(engine: &mut Engine) {
237186
.set(
238187
PointLight {
239188
position: Vec4::zero(),
240-
luminous_flux: Vec4::new(100.0, 0.0, 0.0, 0.0),
189+
luminous_flux: Vec4::new(0.0, 160.0, 145.0, 0.0) * 2.0,
241190
}
242191
.into(),
243192
);
244193

245194
scene.load();
246195
}
196+
197+
#[derive(Debug)]
198+
struct RotationComponent {
199+
entity: Weak<Entity>,
200+
rotation_speed: Cell<f32>,
201+
}
202+
203+
impl Component for RotationComponent {
204+
fn create(entity: &Rc<Entity>) -> Rc<Self>
205+
where
206+
Self: Sized,
207+
{
208+
Rc::new(Self {
209+
entity: Rc::downgrade(entity),
210+
rotation_speed: Cell::new(30.0),
211+
})
212+
}
213+
214+
fn load(&self) {}
215+
216+
fn start(&self) {}
217+
218+
fn update(&self, _: &Input, delta: f32) {
219+
if let Some(entity) = self.entity.upgrade() {
220+
let mut transform = entity.transform.borrow_mut();
221+
222+
let mut rotation = transform.rotation;
223+
rotation = Quaternion::axis_angle(
224+
Vec3::new(0.0, 0.0, 1.0),
225+
self.rotation_speed.get().to_radians() * delta,
226+
) * rotation;
227+
transform.rotation = rotation;
228+
}
229+
}
230+
231+
fn inspector_name(&self) -> &'static str {
232+
"TimeOfDayComponent"
233+
}
234+
}

0 commit comments

Comments
 (0)