Skip to content

Commit

Permalink
feat: support grid view of cameras /w auto-yaw, position samplers (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosure authored Jul 11, 2024
1 parent a3819cd commit e0e8756
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 95 deletions.
1 change: 1 addition & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: create a ZeroverseCamera abstraction to simplify viewer
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::prelude::*;

pub mod camera;
pub mod manifold;
pub mod material;
pub mod mesh;
pub mod plucker;
pub mod primitive;
pub mod render;
pub mod scene;
// pub mod scene;


pub struct BevyZeroversePlugin;
Expand All @@ -19,6 +20,7 @@ impl Plugin for BevyZeroversePlugin {
material::ZeroverseMaterialPlugin,
primitive::ZeroversePrimitivePlugin,
render::RenderPlugin,
// scene::ZeroverseScenePlugin,
));

#[cfg(feature = "plucker")]
Expand Down
4 changes: 2 additions & 2 deletions src/plucker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ impl Node for PluckerNode {
)),
);

if let Some((
for (
plucker_bindings,
view_offset,
view,
)) = self.prepared_plucker.iter_manual(world).next() {
) in self.prepared_plucker.iter_manual(world) {
let pipeline = pipeline_cache.get_compute_pipeline(pipeline.pipeline_id).unwrap();

{
Expand Down
62 changes: 36 additions & 26 deletions src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use bevy::{
prelude::*,
math::primitives::{
Capsule3d,
Cone,
ConicalFrustum,
Cuboid,
Cylinder,
Sphere,
Tetrahedron,
Torus,
math::{
primitives::{
Capsule3d,
Cone,
ConicalFrustum,
Cuboid,
Cylinder,
Sphere,
Tetrahedron,
Torus,
},
sampling::ShapeSample,
},
pbr::{
TransmittedShadowReceiver,
Expand Down Expand Up @@ -59,6 +62,7 @@ pub enum ZeroversePrimitives {
Torus,
}

// TODO: support scale and rotation pdfs via https://github.com/villor/bevy_lookup_curve
#[derive(Clone, Component, Debug, Reflect, Resource)]
#[reflect(Resource)]
pub struct PrimitiveSettings {
Expand All @@ -73,22 +77,13 @@ pub struct PrimitiveSettings {
pub rotation_upper_bound: Vec3,
pub scale_lower_bound: Vec3,
pub scale_upper_bound: Vec3,
pub position_bound: Vec3,
}

impl PrimitiveSettings {
pub fn count(n: usize) -> PrimitiveSettings {
PrimitiveSettings {
components: n,
..Default::default()
}
}
pub position_sampler: PositionSampler,
}

impl Default for PrimitiveSettings {
fn default() -> PrimitiveSettings {
PrimitiveSettings {
components: 25,
components: 5,
available_types: ZeroversePrimitives::iter().collect(),
available_operations: ManifoldOperations::iter().collect(),
wireframe_probability: 0.0,
Expand All @@ -99,7 +94,26 @@ impl Default for PrimitiveSettings {
rotation_upper_bound: Vec3::splat(std::f32::consts::PI),
scale_lower_bound: Vec3::splat(0.05),
scale_upper_bound: Vec3::splat(1.0),
position_bound: Vec3::splat(1.5),
position_sampler: PositionSampler::Cube(Vec3::splat(0.5)),
}
}
}

#[derive(Clone, Debug, Reflect)]
pub enum PositionSampler {
Capsule(f32, f32),
Cube(Vec3),
Cylinder(f32, f32),
Sphere(f32),
}

impl PositionSampler {
pub fn sample(&self, rng: &mut impl Rng) -> Vec3 {
match *self {
PositionSampler::Capsule(radius, length) => Capsule3d::new(radius, length).sample_interior(rng),
PositionSampler::Cube(extents) => Cuboid::from_size(extents).sample_interior(rng),
PositionSampler::Cylinder(radius, height) => Cylinder::new(radius, height).sample_interior(rng),
PositionSampler::Sphere(radius) => Sphere::new(radius).sample_interior(rng),
}
}
}
Expand Down Expand Up @@ -138,11 +152,7 @@ fn build_primitive(
.collect::<Vec<_>>();

let positions = (0..settings.components)
.map(|_| Vec3::new(
rng.gen_range(-settings.position_bound.x..settings.position_bound.x),
rng.gen_range(-settings.position_bound.y..settings.position_bound.y),
rng.gen_range(-settings.position_bound.z..settings.position_bound.z),
))
.map(|_| settings.position_sampler.sample(rng))
.collect::<Vec<_>>();

let rotations = (0..settings.components)
Expand Down
13 changes: 13 additions & 0 deletions src/scene/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
use bevy::prelude::*;

pub mod room;


pub struct ZeroverseScenePlugin;

impl Plugin for ZeroverseScenePlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
room::ZeroverseRoomPlugin,
));
}
}
80 changes: 80 additions & 0 deletions src/scene/room.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use bevy::prelude::*;

use crate::{
material::ZeroverseMaterials,
primitive::PrimitiveSettings,
};


pub struct ZeroverseRoomPlugin;
impl Plugin for ZeroverseRoomPlugin {
fn build(&self, app: &mut App) {
app.register_type::<RoomSettings>();

app.add_systems(Update, process_rooms);
}
}


#[derive(Clone, Component, Debug, Reflect, Resource)]
#[reflect(Resource)]
pub struct RoomSettings {
pub base_settings: PrimitiveSettings,
}

impl Default for RoomSettings {
fn default() -> RoomSettings {
RoomSettings {
base_settings: PrimitiveSettings::default(),
}
}
}


#[derive(Bundle, Default, Debug)]
pub struct RoomBundle {
pub settings: RoomSettings,
pub spatial: SpatialBundle,
}


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


fn build_room(
commands: &mut ChildBuilder,
settings: &RoomSettings,
meshes: &mut ResMut<Assets<Mesh>>,
zeroverse_materials: &Res<ZeroverseMaterials>,
) {
let rng = &mut rand::thread_rng();

}


fn process_rooms(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
zeroverse_materials: Res<ZeroverseMaterials>,
primitives: Query<
(
Entity,
&RoomSettings,
),
Without<ZeroverseRoom>,
>,
) {
for (entity, settings) in primitives.iter() {
commands.entity(entity)
.insert(ZeroverseRoom)
.with_children(|subcommands| {
build_room(
subcommands,
settings,
&mut meshes,
&zeroverse_materials,
);
});
}
}
Loading

0 comments on commit e0e8756

Please sign in to comment.