Skip to content

Commit

Permalink
- working omni shadow and fixes for cubemap cameras and the dynamic c…
Browse files Browse the repository at this point in the history
…ubemap example
  • Loading branch information
polymonster committed May 28, 2023
1 parent 8101ead commit 45221f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hotline-data
44 changes: 43 additions & 1 deletion plugins/ecs_examples/src/omni_shadow_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn omni_shadow_map(client: &mut Client<gfx_platform::Device, os_platform::Ap
"setup_omni_shadow_map"
],
update: systems![
"animate_omni_shadow",
"batch_lights"
],
render_graph: "mesh_lit_omni_shadow_map",
Expand Down Expand Up @@ -47,6 +48,7 @@ pub fn setup_omni_shadow_map(
let light_radius = 256.0;
commands.spawn((
Position(light_pos),
Velocity(Vec3f::unit_z()),
Colour(vec4f(0.5, 0.25, 0.125, 1.0)),
LightComponent {
light_type: LightType::Point,
Expand Down Expand Up @@ -98,7 +100,47 @@ pub fn setup_omni_shadow_map(
WorldMatrix(Mat34f::identity())
));

pmfx.update_cubemap_camera_constants("omni_shadow_camera", -light_pos, 0.1, light_radius * 2.0);
pmfx.update_cubemap_camera_constants("omni_shadow_camera", light_pos, 0.1, light_radius * 2.0);

Ok(())
}

#[no_mangle]
#[export_update_fn]
pub fn animate_omni_shadow (
time: Res<TimeRes>,
mut pmfx: ResMut<PmfxRes>,
mut light_query: Query<(&mut Position, &mut Velocity, &LightComponent)>) -> Result<(), hotline_rs::Error> {

let dim = 32;
let dim2 = dim / 2;
let tile_size = 10.0;
let spacing = 16.0;

let extent = (tile_size + spacing) * 3.0 * 6.0;

for (mut position, mut velocity, component) in &mut light_query {

position.0 += velocity.0 * time.delta * 400.0;

if position.z > extent {
velocity.0 = Vec3f::unit_x();
}

if position.x > extent {
velocity.0 = -Vec3f::unit_z();
}

if position.z < -extent {
velocity.0 = -Vec3f::unit_x();
}

if position.x < -extent {
velocity.0 = Vec3f::unit_z();
}

pmfx.update_cubemap_camera_constants("omni_shadow_camera", position.0, 0.1, component.radius * 2.0);
}

Ok(())
}
17 changes: 9 additions & 8 deletions src/pmfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,17 +686,17 @@ pub struct WorldBufferInfo {

pub fn cubemap_camera_face(face: usize, pos: Vec3f, near: f32, far: f32) -> CameraConstants {
let at = [
vec3f(-1.0, 0.0, 0.0), //+x
vec3f(1.0, 0.0, 0.0), //-x
vec3f(0.0, -1.0, 0.0), //+y
vec3f(0.0, 1.0, 0.0), //-y
vec3f(0.0, 0.0, 1.0), //+z
vec3f(0.0, 0.0, -1.0) //-z
vec3f(1.0, 0.0, 0.0), //+x
vec3f(-1.0, 0.0, 0.0), //-x
vec3f(0.0, 1.0, 0.0), //+y
vec3f(0.0, -1.0, 0.0), //-y
vec3f(0.0, 0.0, 1.0), //+z
vec3f(0.0, 0.0, -1.0) //-z
];

let right = [
vec3f(0.0, 0.0, 1.0),
vec3f(0.0, 0.0, -1.0),
vec3f(0.0, 0.0, 1.0),
vec3f(1.0, 0.0, 0.0),
vec3f(1.0, 0.0, 0.0),
vec3f(1.0, 0.0, 0.0),
Expand All @@ -706,8 +706,8 @@ pub fn cubemap_camera_face(face: usize, pos: Vec3f, near: f32, far: f32) -> Came
let up = [
vec3f(0.0, 1.0, 0.0),
vec3f(0.0, 1.0, 0.0),
vec3f(0.0, 0.0, 1.0),
vec3f(0.0, 0.0, -1.0),
vec3f(0.0, 0.0, 1.0),
vec3f(0.0, 1.0, 0.0),
vec3f(0.0, 1.0, 0.0)
];
Expand All @@ -721,6 +721,7 @@ pub fn cubemap_camera_face(face: usize, pos: Vec3f, near: f32, far: f32) -> Came

let proj = Mat4f::create_perspective_projection_lh_yup(deg_to_rad(90.0), 1.0, near, far);

let view = view.inverse();
CameraConstants {
view_matrix: view,
view_projection_matrix: proj * view,
Expand Down

0 comments on commit 45221f2

Please sign in to comment.