Skip to content

Commit

Permalink
feat: rotation augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mosure committed Sep 7, 2024
1 parent 6c4066f commit 1b21283
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 78 deletions.
9 changes: 9 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ pub struct BevyZeroverseConfig {
#[pyo3(get, set)]
#[arg(long, value_enum, default_value_t = ZeroverseSceneType::Object)]
pub scene_type: ZeroverseSceneType,

#[pyo3(get, set)]
#[arg(long, default_value = "true")]
pub rotation_augmentation: bool,
}

#[cfg(feature = "python")]
Expand Down Expand Up @@ -241,6 +245,9 @@ pub struct BevyZeroverseConfig {

#[arg(long, value_enum, default_value_t = ZeroverseSceneType::Object)]
pub scene_type: ZeroverseSceneType,

#[arg(long, default_value = "true")]
pub rotation_augmentation: bool,
}

impl Default for BevyZeroverseConfig {
Expand All @@ -261,6 +268,7 @@ impl Default for BevyZeroverseConfig {
yaw_speed: 0.0,
render_mode: Default::default(),
scene_type: Default::default(),
rotation_augmentation: true,
}
}
}
Expand Down Expand Up @@ -639,6 +647,7 @@ fn propagate_cli_settings(
plucker_settings.enabled = args.plucker_visualization;

scene_settings.num_cameras = args.num_cameras;
scene_settings.rotation_augmentation = args.rotation_augmentation;
scene_settings.scene_type = args.scene_type.clone();
}
}
Expand Down
62 changes: 33 additions & 29 deletions src/scene/cornell_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
Sphere,
},
pbr::{
CascadeShadowConfigBuilder,
// CascadeShadowConfigBuilder,
NotShadowCaster,
TransmittedShadowReceiver,
},
Expand All @@ -23,6 +23,7 @@ use crate::{
},
scene::{
RegenerateSceneEvent,
RotationAugment,
SceneLoadedEvent,
ZeroverseScene,
ZeroverseSceneRoot,
Expand Down Expand Up @@ -134,6 +135,7 @@ fn setup_scene(

commands.spawn((ZeroverseSceneRoot, ZeroverseScene))
.insert(Name::new("cornell_cube"))
.insert(RotationAugment)
.insert(SpatialBundle::default())
.with_children(|commands| {
{//cornell cube walls
Expand Down Expand Up @@ -194,7 +196,7 @@ fn setup_scene(
}
}

{// sphere on bottom
{// sphere on left
let mut mesh = Sphere::new(0.25)
.mesh()
.build();
Expand All @@ -210,34 +212,34 @@ fn setup_scene(
PbrBundle {
mesh: meshes.add(mesh),
material,
transform: Transform::from_translation(Vec3::new(0.0, -0.3, 0.0)),
transform: Transform::from_translation(Vec3::new(-0.8, 0.5, 0.0)),
..Default::default()
},
TransmittedShadowReceiver,
));
}
});

commands.spawn((
DirectionalLightBundle {
transform: Transform::from_xyz(1.0, -3.0, 2.0)
.looking_at(Vec3::ZERO, Vec3::Y),
directional_light: DirectionalLight {
illuminance: 800.0,
shadows_enabled: true,
..default()
},
cascade_shadow_config: CascadeShadowConfigBuilder {
first_cascade_far_bound: 4.0,
maximum_distance: 10.0,
..default()
}
.into(),
..default()
},
ZeroverseScene,
Name::new("directional_light"),
));
// commands.spawn((
// DirectionalLightBundle {
// transform: Transform::from_xyz(1.0, -3.0, 2.0)
// .looking_at(Vec3::ZERO, Vec3::Y),
// directional_light: DirectionalLight {
// illuminance: 800.0,
// shadows_enabled: true,
// ..default()
// },
// cascade_shadow_config: CascadeShadowConfigBuilder {
// first_cascade_far_bound: 4.0,
// maximum_distance: 10.0,
// ..default()
// }
// .into(),
// ..default()
// },
// ZeroverseScene,
// Name::new("directional_light"),
// ));

for _ in 0..scene_settings.num_cameras {
commands.spawn(ZeroverseCamera {
Expand All @@ -261,14 +263,16 @@ fn setup_cameras(
) {
for _ in 0..scene_settings.num_cameras {
commands.spawn(ZeroverseCamera {
sampler: CameraPositionSampler {
sampler_type: CameraPositionSamplerType::Sphere {
radius: 3.25,
sampler: CameraPositionSampler {
sampler_type: CameraPositionSamplerType::Sphere {
radius: 3.25,
},
..default()
},
..default()
},
..default()
}).insert(ZeroverseScene);
})
.insert(ZeroverseScene)
.insert(RotationAugment);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/scene/lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use bevy::{
};
use rand::Rng;

use crate::scene::ZeroverseScene;
use crate::scene::{
RotationAugment,
ZeroverseScene,
};


pub struct ZeroverseLightingPlugin;
Expand Down Expand Up @@ -70,6 +73,7 @@ pub fn setup_lighting(
.into(),
..default()
},
RotationAugment,
ZeroverseScene,
Name::new("directional_light"),
));
Expand Down
69 changes: 68 additions & 1 deletion src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_args::{
Serialize,
ValueEnum,
};
use rand::Rng;

#[cfg(feature = "python")]
use pyo3::prelude::*;
Expand All @@ -30,8 +31,12 @@ impl Plugin for ZeroverseScenePlugin {
app.add_event::<RegenerateSceneEvent>();
app.add_event::<SceneLoadedEvent>();

app.init_resource::<GlobalRotationAugment>();
app.register_type::<GlobalRotationAugment>();

app.init_resource::<ZeroverseSceneSettings>();
app.register_type::<ZeroverseSceneSettings>();

app.register_type::<SceneAabb>();

app.add_plugins((
Expand All @@ -42,7 +47,14 @@ impl Plugin for ZeroverseScenePlugin {
));

app.add_systems(PreUpdate, create_scene_aabb);
app.add_systems(Update, draw_scene_aabb);
app.add_systems(
Update,
(
draw_scene_aabb,
regenerate_rotation_augment,
),
);
app.add_systems(PostUpdate, rotation_augment);
}
}

Expand Down Expand Up @@ -76,6 +88,7 @@ pub enum ZeroverseSceneType {
#[reflect(Resource)]
pub struct ZeroverseSceneSettings {
pub num_cameras: usize,
pub rotation_augmentation: bool,
pub scene_type: ZeroverseSceneType,
}

Expand Down Expand Up @@ -200,3 +213,57 @@ fn draw_scene_aabb(
gizmos.cuboid(Transform::from(aabb), color);
}
}


#[derive(Debug, Component, Clone, Reflect)]
pub struct RotationAugment;

#[derive(Debug, Component, Clone, Reflect)]
pub struct RotationAugmented;

#[derive(Resource, Debug, Default, Reflect)]
pub struct GlobalRotationAugment {
pub rotation: Quat,
}

fn rotation_augment(
mut commands: Commands,
mut to_augment: Query<
(
Entity,
&mut Transform,
),
(
With<RotationAugment>,
Without<RotationAugmented>
),
>,
global_rotation: Res<GlobalRotationAugment>,
) {
for (entity, mut transform) in to_augment.iter_mut() {
commands.entity(entity)
.remove::<RotationAugment>()
.insert(RotationAugmented);

transform.rotation *= global_rotation.rotation;
}
}

fn regenerate_rotation_augment(
mut global_rotation: ResMut<GlobalRotationAugment>,
mut regenerate_events: EventReader<RegenerateSceneEvent>,
scene_settings: Res<ZeroverseSceneSettings>,
) {
if regenerate_events.is_empty() {
return;
}
regenerate_events.clear();

if scene_settings.rotation_augmentation {
let mut rng = rand::thread_rng();
let random_rotation = Quat::from_rotation_y(rng.gen_range(0.0..std::f32::consts::PI * 2.0));
global_rotation.rotation = random_rotation;
} else {
global_rotation.rotation = Quat::IDENTITY;
}
}
42 changes: 18 additions & 24 deletions src/scene/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
ZeroverseLightingSettings,
},
RegenerateSceneEvent,
RotationAugment,
SceneLoadedEvent,
ZeroverseScene,
ZeroverseSceneRoot,
Expand Down Expand Up @@ -40,37 +41,34 @@ fn setup_scene(
mut commands: Commands,
primitive_settings: Res<ZeroversePrimitiveSettings>,
mut load_event: EventWriter<SceneLoadedEvent>,
scene_settings: Res<ZeroverseSceneSettings>,
) {
commands
.spawn(PrimitiveBundle {
settings: primitive_settings.clone(),
..default()
})
.insert(RotationAugment)
.insert((ZeroverseScene, ZeroverseSceneRoot))
.insert(Name::new("zeroverse_object"));
.insert(Name::new("zeroverse_object"))
.with_children(|commands| {
for _ in 0..scene_settings.num_cameras {
commands.spawn(ZeroverseCamera {
sampler: CameraPositionSampler {
sampler_type: CameraPositionSamplerType::Sphere {
radius: 3.25,
},
..default()
},
..default()
});
}
});

load_event.send(SceneLoadedEvent);
}


fn setup_cameras(
mut commands: Commands,
scene_settings: Res<ZeroverseSceneSettings>,
) {
for _ in 0..scene_settings.num_cameras {
commands.spawn(ZeroverseCamera {
sampler: CameraPositionSampler {
sampler_type: CameraPositionSamplerType::Sphere {
radius: 3.25,
},
..default()
},
..default()
}).insert(ZeroverseScene);
}
}


fn regenerate_scene(
mut commands: Commands,
primitive_settings: Res<ZeroversePrimitiveSettings>,
Expand All @@ -93,11 +91,6 @@ fn regenerate_scene(
commands.entity(entity).despawn_recursive();
}

setup_cameras(
commands.reborrow(),
scene_settings,
);

setup_lighting(
commands.reborrow(),
lighting_settings,
Expand All @@ -107,5 +100,6 @@ fn regenerate_scene(
commands,
primitive_settings,
load_event,
scene_settings,
);
}
Loading

0 comments on commit 1b21283

Please sign in to comment.