Skip to content

Commit

Permalink
wip: combine arm with animation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Oct 29, 2024
1 parent 3a56986 commit 1f2401e
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 192 deletions.
4 changes: 0 additions & 4 deletions crates/beet_examples/src/scenes/spatial/inverse_kinematics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ pub fn spawn_ik_camera(mut commands: Commands) {

pub fn spawn_arm_with_keyboard_target(mut commands: Commands) {
let target = spawn_keyboard_target(&mut commands);
spawn_arm(&mut commands, target);
}

pub fn spawn_arm(commands: &mut Commands, target: Entity) {
commands.spawn((
Name::new("scene"),
BundlePlaceholder::Gltf("robot-arm/robot-arm.glb".into()),
Expand Down
17 changes: 5 additions & 12 deletions crates/beet_spatial/src/inverse_kinematics/ik_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ pub fn ik_spawner(
};

let Some(items) =
find_by_name_recursive(&child_nodes_query, &arm_root.3, vec![
"Base", "Segment1", "Segment2", "Segment3",
"Gripper",
// "Target",
// "Phone",
map_names_to_query_entries(&child_nodes_query, &arm_root.3, vec![
"Base", "Segment1", "Segment2", "Segment3", "Gripper",
])
else {
return;
Expand All @@ -64,9 +61,6 @@ pub fn ik_spawner(
let segment2 = items[2];
let segment3 = items[3];
let gripper = items[4];
// let target = items[5];
// let phone = items[6];


// hack until globaltransform calculated in sceneinstanceready
let scale = transform.scale.x;
Expand All @@ -83,8 +77,6 @@ pub fn ik_spawner(
IkSegment::DEG_360.with_len(segment3_to_gripper),
);

println!("ik: {:?}", ik);

let ik_transforms = IkArm4DofTransforms::new(
ik, **target, base.0, segment1.0, segment2.0, segment3.0,
);
Expand All @@ -99,8 +91,9 @@ pub fn ik_spawner(
// ));
}

/// Provided a list of names, each being a child of the previous, returns that list of entities.
fn find_by_name_recursive<'a>(
/// Provided a list of names, each being a child of the previous,
/// returns that list of entities.
fn map_names_to_query_entries<'a>(
query: &'a Query<(Entity, &Name, &Transform, &Children)>,
children: &Children,
names: Vec<&str>,
Expand Down
70 changes: 1 addition & 69 deletions crates/emby/examples/arm_animation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use beet::prelude::*;
use beet_examples::prelude::*;
use beetmash::prelude::*;
use bevy::color::palettes::tailwind;
use bevy::prelude::*;
use emby::prelude::*;
use forky::prelude::TransformExt;
use std::time::Duration;

pub fn main() {
App::new()
Expand All @@ -17,71 +12,8 @@ pub fn main() {
beetmash::core::scenes::ground_3d,
beet_examples::scenes::flow::beet_debug_start_and_stop,
beet_examples::scenes::spatial::spawn_ik_camera,
setup,
emby::scenes::emote_arm,
),
)
.run();
}


fn setup(mut commands: Commands) {
let mut target = Entity::PLACEHOLDER;
let pos_happy = Vec3::new(0., 3., 0.);
let pos_idle = Vec3::new(0., 2., 0.);

let transform_idle = Transform::from_translation(pos_idle)
.with_scale_value(2.)
.looking_to(Dir3::NEG_Y, Dir3::X);



let target_parent = commands
// target parent is used to define offset transform
.spawn((Name::new("Target Parent"), transform_idle))
.with_children(|parent| {
target = parent
.spawn((Name::new("Target"), BundlePlaceholder::Pbr {
mesh: Sphere::new(0.2).into(),
material: MaterialPlaceholder::unlit(tailwind::BLUE_500),
}))
.id();
})
.id();
commands
.spawn((
Name::new("Behavior"),
RunOnSpawn,
Repeat::default(),
SequenceFlow,
))
.with_children(|parent| {
parent.spawn((
Name::new("New Pos"),
InsertOnRun::new(transform_idle).with_target(target_parent),
TargetAgent(target),
SetCurveOnRun::default(),
PlayProceduralAnimation::default()
// .with_meter_per_second(1.),
.with_duration_secs(2.),
));
parent.spawn((
Name::new("Idle Pause"),
TriggerInDuration::with_range(
OnRunResult::success(),
Duration::from_secs(1)..Duration::from_secs(4),
),
));
parent.spawn((
Name::new("Happy"),
TargetAgent(target_parent),
SetCurveOnRun::PingPongPause {
target: pos_happy,
pause: 1.,
func: EaseFunction::CubicInOut,
},
PlayProceduralAnimation::default().with_duration_secs(4.),
));
});

beet_examples::scenes::spatial::spawn_arm(&mut commands, target);
}
21 changes: 21 additions & 0 deletions crates/emby/examples/emote_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use beet_examples::prelude::*;
use bevy::prelude::*;
use emby::prelude::*;

pub fn main() {
App::new()
.add_plugins((crate_test_beet_example_plugin, EmbyPlugin))
.add_systems(
Startup,
(
beetmash::core::scenes::lighting_3d,
beetmash::core::scenes::ground_3d,
beet_examples::scenes::flow::beet_debug_start_and_stop,
beet_examples::scenes::spatial::spawn_ik_camera,
emby::scenes::emote_arm,
emby::scenes::spawn_barbarian,
emby::scenes::phone_texture_camera_3d,
),
)
.run();
}
23 changes: 2 additions & 21 deletions crates/emby/examples/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,12 @@ fn main() {
emby::scenes::spawn_barbarian,
),
)
// .add_systems(Update,disable_barbarian)
.run();
}


fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0., 1.6, 5.), // .looking_at(Vec3::ZERO, Vec3::Y),
Transform::from_xyz(0., 1.6, 5.)
));

commands.insert_resource(EmojiMap::new(&asset_server));
}



// fn emoji(mut commands: Commands, asset_server: Res<AssetServer>) {
// commands.spawn((
// Name::new("PhoneTexture"),
// SpriteBundle {
// // transform: Transform::from_scale(Vec3::splat(10.)),
// // transform: Transform::default().with_scale(Vec3::splat(10.)),
// texture: asset_server.load(EmojiMap::file_path(EmojiMap::HAPPY)),
// ..default()
// },
// RenderLayers::layer(RENDER_TEXTURE_LAYER),
// ));
// }
15 changes: 4 additions & 11 deletions crates/emby/examples/render_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use beet::prelude::*;
use beet_examples::prelude::*;
use bevy::prelude::*;
use emby::prelude::*;
// use bevy::render::view::RenderLayers;


fn main() {
App::new()
Expand All @@ -17,18 +15,13 @@ fn main() {
beetmash::core::scenes::ground_3d,
beetmash::core::scenes::ui_terminal_input,
emby::scenes::spawn_barbarian,
emby::scenes::phone_texture_camera_3d,
),
)
// .add_systems(Update,disable_barbarian)
.add_observer(emby::scenes::add_phone_render_texture_to_arm)
.run();
}


fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0., 1.6, 5.), // .looking_at(Vec3::ZERO, Vec3::Y),
));

commands.insert_resource(EmojiMap::new(&asset_server));
fn setup(mut commands: Commands) {
commands.spawn((Camera3d::default(), Transform::from_xyz(0., 1.6, 5.)));
}
10 changes: 7 additions & 3 deletions crates/emby/src/emote_agent/render_texture.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use beetmash::prelude::HandleWrapper;
use bevy::prelude::*;
use bevy::render::render_resource::*;
use bevy::render::view::RenderLayers;
Expand All @@ -12,6 +11,10 @@ pub struct RenderTexture {
pub handle: Handle<StandardMaterial>,
}

impl RenderTexture {
pub fn new(handle: Handle<StandardMaterial>) -> Self { Self { handle } }
}

/// The layer used for rendering to a texture instead of the main camera.
pub const RENDER_TEXTURE_LAYER: usize = 1;

Expand Down Expand Up @@ -56,10 +59,11 @@ pub fn render_texture_bundle(
Camera {
order: -1,
target: image_handle.into(),
clear_color: Color::WHITE.into(),
clear_color: Color::srgba_u8(0, 0, 0, 0).into(),
// clear_color: Color::WHITE.into(),
..default()
},
RenderLayers::layer(RENDER_TEXTURE_LAYER),
HandleWrapper(material_handle),
RenderTexture::new(material_handle),
)
}
10 changes: 7 additions & 3 deletions crates/emby/src/plugins/emby_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ pub struct EmbyPlugin;

impl Plugin for EmbyPlugin {
fn build(&self, app: &mut App) {
/*-*/

app.add_plugins(emote_agent_plugin);
app.add_plugins(emote_agent_plugin)
.add_systems(Startup, setup)
.add_observer(crate::scenes::add_phone_render_texture_to_arm);
}
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.insert_resource(EmojiMap::new(&asset_server));
}
40 changes: 0 additions & 40 deletions crates/emby/src/scenes/barbarian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,6 @@ pub fn spawn_barbarian(mut commands: Commands) {





// pub fn disable_barbarian(
// mut commands: Commands,
// mut events: EventReader<AssetEvent<Scene>>,
// query: Populated<(Entity, &SceneRoot)>,
// names: Query<&Name>,
// children: Query<&Children>,
// ) {
// for ev in events.read() {
// let AssetEvent::LoadedWithDependencies { id } = ev else {
// continue;
// };
// let Some((entity, _)) =
// query.iter().find(|(_, scene)| scene.id() == *id)
// else {
// continue;
// };

// let to_disable: HashSet<&'static str> =
// vec!["Mug"].into_iter().collect();

// for entity in children.iter_descendants(entity) {
// if let Ok(name) = names.get(entity) {
// println!("it has name: {}", name);
// if to_disable.contains(name.as_str()) {
// commands.entity(entity).insert(Visibility::Hidden);
// }
// }else{
// println!("it has no name");
// }
// }
// }
// }






/*
# meshes
Expand Down
77 changes: 77 additions & 0 deletions crates/emby/src/scenes/emote_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use beet::prelude::*;
use beetmash::prelude::*;
use bevy::color::palettes::tailwind;
use bevy::prelude::*;
use forky::prelude::TransformExt;
use std::time::Duration;


pub fn emote_arm(mut commands: Commands) {
let mut target = Entity::PLACEHOLDER;
let pos_happy = Vec3::new(0., 3., 0.);
let pos_idle = Vec3::new(0., 2., 0.);

let transform_idle = Transform::from_translation(pos_idle)
.with_scale_value(2.)
.looking_to(Dir3::NEG_Y, Dir3::X);



let target_parent = commands
// target parent is used to define offset transform
.spawn((Name::new("Target Parent"), transform_idle))
.with_children(|parent| {
target = parent
.spawn((Name::new("Target"), BundlePlaceholder::Pbr {
mesh: Sphere::new(0.2).into(),
material: MaterialPlaceholder::unlit(tailwind::BLUE_500),
}))
.id();
})
.id();


commands.spawn((
Name::new("Emote Arm"),
BundlePlaceholder::Gltf("robot-arm/robot-arm-phone.glb".into()),
Transform::from_scale(Vec3::splat(10.)),
TargetAgent(target),
IkSpawner::default(),
));

commands
.spawn((
Name::new("Behavior"),
RunOnSpawn,
Repeat::default(),
SequenceFlow,
))
.with_children(|parent| {
parent.spawn((
Name::new("New Pos"),
InsertOnRun::new(transform_idle).with_target(target_parent),
TargetAgent(target),
SetCurveOnRun::default(),
PlayProceduralAnimation::default()
// .with_meter_per_second(1.),
.with_duration_secs(2.),
));
parent.spawn((
Name::new("Idle Pause"),
TriggerInDuration::with_range(
OnRunResult::success(),
Duration::from_secs(1)..Duration::from_secs(4),
),
));
parent.spawn((
Name::new("Happy"),
TargetAgent(target_parent),
SetCurveOnRun::PingPongPause {
target: pos_happy,
pause: 1.,
func: EaseFunction::CubicInOut,
},
PlayProceduralAnimation::default().with_duration_secs(4.),
));
});
}
Loading

0 comments on commit 1f2401e

Please sign in to comment.