Skip to content

Commit

Permalink
wip: 3d render texture
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Oct 27, 2024
1 parent be49967 commit b9596c2
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 64 deletions.
32 changes: 32 additions & 0 deletions crates/beet_examples/examples/render_texture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use beet_examples::prelude::*;
use beet_flow::prelude::*;
use bevy::prelude::*;


fn main() {
App::new()
.add_plugins((crate_test_beet_example_plugin, plugin_ml))
.insert_resource(BeetDebugConfig::default())
.add_systems(
Startup,
(
setup,
beetmash::core::scenes::lighting_3d,
beetmash::core::scenes::ground_3d,
beetmash::core::scenes::ui_terminal_input,
beet_examples::emote_agent::scenes::spawn_barbarian,
),
)
// .add_systems(Update,disable_barbarian)
.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));
}
24 changes: 24 additions & 0 deletions crates/beet_examples/src/emote_agent/apply_render_layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use bevy::prelude::*;
use bevy::render::view::RenderLayers;
use bevy::scene::SceneInstanceReady;


/// Currently [`RenderLayers`] are not applied to children of a scene.
/// This [`SceneInstanceReady`] observer applies the [`RenderLayers`]
/// of a [`SceneRoot`] to all children with a [`Transform`].
pub fn apply_render_layers_to_children(
trigger: Trigger<SceneInstanceReady>,
mut commands: Commands,
children: Query<&Children>,
transforms: Query<&Transform>,
query: Populated<(Entity, &RenderLayers)>,
) {
let Ok((parent, render_layers)) = query.get(trigger.entity()) else {
return;
};
children.iter_descendants(parent).for_each(|entity| {
if transforms.contains(entity) {
commands.entity(entity).insert(render_layers.clone());
}
});
}
16 changes: 16 additions & 0 deletions crates/beet_examples/src/emote_agent/emote_agent_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::prelude::*;
use bevy::prelude::*;



pub fn emote_agent_plugin(app: &mut App) {
app.add_observer(apply_render_layers_to_children)
.add_systems(
Update,
(
ik_spawner.never_param_warn(),
update_emoji_swapper.never_param_warn(),
),
)
.register_type::<IkSpawner>();
}
6 changes: 6 additions & 0 deletions crates/beet_examples/src/emote_agent/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod apply_render_layers;
#[allow(unused_imports)]
pub use self::apply_render_layers::*;
pub mod emoji;
#[allow(unused_imports)]
pub use self::emoji::*;
Expand All @@ -7,6 +10,9 @@ pub use self::emoji_map::*;
pub mod emoji_swapper;
#[allow(unused_imports)]
pub use self::emoji_swapper::*;
pub mod emote_agent_plugin;
#[allow(unused_imports)]
pub use self::emote_agent_plugin::*;
pub mod emote_bubble;
#[allow(unused_imports)]
pub use self::emote_bubble::*;
Expand Down
10 changes: 5 additions & 5 deletions crates/beet_examples/src/emote_agent/scenes/barbarian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::prelude::*;
use beetmash::prelude::*;
use bevy::animation::RepeatAnimation;
use bevy::prelude::*;
use bevy::render::view::RenderLayers;

pub fn spawn_barbarian(mut commands: Commands) {
let mut graph = AnimationGraphPlaceholder::default();
Expand Down Expand Up @@ -30,13 +31,15 @@ pub fn spawn_barbarian(mut commands: Commands) {
AssetLoadBlockAppReady,
graph,
AnimationTransitions::default(),
// RenderLayers::layer(RENDER_TEXTURE_LAYER),
))
.with_children(|parent| {
let agent = parent.parent_entity();

let emote_bubble = spawn_emote_bubble(&mut parent.spawn((
Transform::from_xyz(0.5, 2.5, 0.5),
Visibility::Hidden,
RenderLayers::layer(RENDER_TEXTURE_LAYER),
)));

idle_behavior = parent
Expand All @@ -52,12 +55,9 @@ pub fn spawn_barbarian(mut commands: Commands) {
EndOnRun::success().with_target(idle_behavior),
InsertSentenceOnUserInput::default(),
RunOnInsertSentence::default(),
InsertOnTrigger::<OnRun, Visibility>::new(Visibility::Visible)
InsertOnRun::new(Visibility::Visible).with_target(emote_bubble),
InsertOnRunResult::new(Visibility::Hidden)
.with_target(emote_bubble),
InsertOnTrigger::<OnRunResult, Visibility>::new(
Visibility::Hidden,
)
.with_target(emote_bubble),
TargetAgent(agent),
cheer_animation_bundle,
RunOnRunResult::new_with_target(idle_behavior),
Expand Down
14 changes: 12 additions & 2 deletions crates/beet_examples/src/emote_agent/scenes/phone_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use bevy::prelude::*;
use bevy::render::view::RenderLayers;

pub fn phone_texture(
pub fn phone_texture_emoji(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
Expand All @@ -23,7 +23,7 @@ pub fn phone_texture(
));
}

pub fn phone_texture_camera(
pub fn phone_texture_camera_2d(
mut commands: Commands,
mut images: ResMut<Assets<Image>>,
mut materials: ResMut<Assets<StandardMaterial>>,
Expand All @@ -34,3 +34,13 @@ pub fn phone_texture_camera(
Camera2d,
));
}
pub fn phone_texture_camera_3d(
mut commands: Commands,
mut images: ResMut<Assets<Image>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
render_texture_bundle(&mut images, &mut materials),
Camera3d::default(),
));
}
15 changes: 6 additions & 9 deletions crates/beet_examples/src/plugins/beet_example_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn beet_example_plugin(app: &mut App) {
DefaultReplicatePlugin,
temp_patches,
))
.add_plugins((plugin_spatial, plugin_2d, plugin_3d))
.add_plugins((plugin_spatial, plugin_2d, plugin_3d, emote_agent_plugin))
.register_type::<Collectable>();
}

Expand All @@ -52,10 +52,10 @@ pub fn beet_example_plugin(app: &mut App) {
fn plugin_spatial(app: &mut App) {
app
.add_plugins(ActionPlugin::<(
RemoveOnTrigger<OnRunResult, SteerTarget>,
RemoveOnTrigger<OnRunResult, Velocity>,
InsertOnTrigger<OnRun, Velocity>,
RemoveOnTrigger<OnRun, Velocity>,
RemoveOnRunResult<SteerTarget>,
RemoveOnRunResult<Velocity>,
InsertOnRun<Velocity>,
RemoveOnRun<Velocity>,
)>::default())
/*-*/;
}
Expand All @@ -75,7 +75,7 @@ pub fn plugin_ml(app: &mut App) {
// fetch
.add_plugins(ActionPlugin::<(
InsertSentenceSteerTarget<Collectable>,
RemoveOnTrigger<OnRunResult, Sentence>,
RemoveOnRunResult<Sentence>,
)>::default())
/*-*/;
}
Expand Down Expand Up @@ -106,10 +106,7 @@ fn plugin_3d(app: &mut App) {
camera_distance,
rotate_collectables,
keyboard_controller,
ik_spawner.never_param_warn(),
update_emoji_swapper.never_param_warn()
))
.register_type::<IkSpawner>()
.register_type::<FollowCursor3d>()
.register_type::<KeyboardController>()
.register_type::<CameraDistance>()
Expand Down
10 changes: 6 additions & 4 deletions crates/beet_examples/src/scenes/ml/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn fetch_npc(mut commands: Commands) {
RunOnSteerTargetInsert::new_with_source(agent),
RunOnSteerTargetRemove::new_with_source(agent),
ScoreFlow::default(),
RemoveOnTrigger::<OnRunResult, Sentence>::default(),
RemoveOnRunResult::<Sentence>::default(),
))
.with_children(|parent| {
parent.spawn((
Expand All @@ -57,11 +57,13 @@ pub fn fetch_npc(mut commands: Commands) {
max_radius: 10.,
},
PlayAnimation::new(walk_index).repeat_forever(),
InsertOnTrigger::<OnRun, Velocity>::new_with_target(agent),
InsertOnRun::<Velocity>::new_with_target(agent),
Seek::default(),
EndOnArrive::new(1.),
RemoveOnTrigger::<OnRunResult, SteerTarget>::new_with_target(agent),
RemoveOnTrigger::<OnRunResult, Velocity>::new_with_target(agent),
RemoveOnRunResult::<SteerTarget>::new_with_target(
agent,
),
RemoveOnRunResult::<Velocity>::new_with_target(agent),
));
});
});
Expand Down
8 changes: 2 additions & 6 deletions crates/beet_examples/src/scenes/spatial/seek_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ pub fn seek_3d(mut commands: Commands) {
.with_children(|parent| {
parent.spawn((
Name::new("Idle"),
RemoveOnTrigger::<OnRun, Velocity>::new_with_target(
agent,
),
RemoveOnRun::<Velocity>::new_with_target(agent),
TargetAgent(agent),
PlayAnimation::new(idle_index)
.with_transition_duration(transition_duration),
Expand All @@ -81,9 +79,7 @@ pub fn seek_3d(mut commands: Commands) {
Name::new("Seek"),
TargetAgent(agent),
Seek::default(),
InsertOnTrigger::<OnRun, Velocity>::new_with_target(
agent,
),
InsertOnRun::<Velocity>::new_with_target(agent),
PlayAnimation::new(walk_index)
.repeat_forever()
.with_transition_duration(transition_duration),
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_flow/src/actions/misc/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ use crate::prelude::*;

/// This does **not** trigger observers, making it safe from infinite loops
/// Reattaches the [`RunOnSpawn`] component whenever [`OnRunResult`] is called.
pub type Repeat = InsertOnTrigger<OnRunResult, RunOnSpawn>;
pub type Repeat = InsertOnRunResult<RunOnSpawn>;
8 changes: 8 additions & 0 deletions crates/beet_flow/src/actions/on_trigger/aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::prelude::*;


pub type InsertOnRun<T> = InsertOnTrigger<OnRun, T>;
pub type InsertOnRunResult<T> = InsertOnTrigger<OnRunResult, T>;

pub type RemoveOnRun<T> = RemoveOnTrigger<OnRun, T>;
pub type RemoveOnRunResult<T> = RemoveOnTrigger<OnRunResult, T>;
11 changes: 4 additions & 7 deletions crates/beet_flow/src/actions/on_trigger/continue_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bevy::prelude::*;
/// ```
#[derive(Debug, Default, Component, Reflect)]
#[reflect(Default, Component)]
#[require(RunTimer, InsertOnTrigger<OnRun, Running>, RemoveOnTrigger<OnRunResult, Running>)]
#[require(RunTimer, InsertOnRun<Running>, RemoveOnRunResult<Running>)]
pub struct ContinueRun;

#[cfg(test)]
Expand All @@ -39,15 +39,12 @@ mod test {
fn works() -> Result<()> {
let mut app = App::new();
app.add_plugins(ActionPlugin::<(
InsertOnTrigger<OnRun, Running>,
RemoveOnTrigger<OnRunResult, Running>,
InsertOnRun<Running>,
RemoveOnRunResult<Running>,
)>::default());
let world = app.world_mut();

let entity = world
.spawn(ContinueRun)
.flush_trigger(OnRun)
.id();
let entity = world.spawn(ContinueRun).flush_trigger(OnRun).id();
expect(world.entities().len()).to_be(3)?;
expect(&*world).to_have_component::<Running>(entity)?;
world
Expand Down
8 changes: 2 additions & 6 deletions crates/beet_flow/src/actions/on_trigger/insert_on_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use bevy::prelude::*;
use std::marker::PhantomData;


pub type InsertOnRun<T> = InsertOnTrigger<OnRun, T>;


/// Inserts the provided `Bundle` on the [`TriggerOnTrigger::target`] when
/// the `EventIn` is triggered on one of the [`TriggerOnTrigger::sources`].
Expand Down Expand Up @@ -48,13 +46,11 @@ mod test {
#[test]
fn works() -> Result<()> {
let mut app = App::new();
app.add_plugins(
ActionPlugin::<InsertOnTrigger<OnRun, Running>>::default(),
);
app.add_plugins(ActionPlugin::<InsertOnRun<Running>>::default());
let world = app.world_mut();

let entity = world
.spawn(InsertOnTrigger::<OnRun, Running>::default())
.spawn(InsertOnRun::<Running>::default())
.flush_trigger(OnRun)
.id();
expect(world.entities().len()).to_be(2)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/beet_flow/src/actions/on_trigger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use self::insert_on_trigger::*;
pub mod on_global_trigger;
#[allow(unused_imports)]
pub use self::on_global_trigger::*;
pub mod aliases;
#[allow(unused_imports)]
pub use self::aliases::*;
pub mod on_trigger_action;
#[allow(unused_imports)]
pub use self::on_trigger_action::*;
Expand Down
15 changes: 4 additions & 11 deletions crates/beet_flow/src/actions/on_trigger/on_trigger_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,11 @@ mod test {
#[test]
fn works() -> Result<()> {
let mut app = App::new();
app.add_plugins(
ActionPlugin::<InsertOnTrigger<OnRun, Running>>::default(),
);
app.add_plugins(ActionPlugin::<InsertOnRun<Running>>::default());
let world = app.world_mut();

let entity = world
.spawn(InsertOnTrigger::<OnRun, Running>::default())
.spawn(InsertOnRun::<Running>::default())
.flush_trigger(OnRun)
.id();
expect(world.entities().len()).to_be(2)?;
Expand All @@ -160,17 +158,12 @@ mod test {
#[test]
fn other_sources() -> Result<()> {
let mut app = App::new();
app.add_plugins(
ActionPlugin::<InsertOnTrigger<OnRun, Running>>::default(),
);
app.add_plugins(ActionPlugin::<InsertOnRun<Running>>::default());
let world = app.world_mut();

let source = world.spawn_empty().id();
let entity = world
.spawn(
InsertOnTrigger::<OnRun, Running>::default()
.with_source(source),
)
.spawn(InsertOnRun::<Running>::default().with_source(source))
.id();

world.entity_mut(source).flush_trigger(OnRun);
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_flow/src/lifecycle/lifecycle_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl Plugin for LifecyclePlugin {
ActionPlugin::<(
EndOnRun,
TriggerInDuration<OnRunResult>,
InsertOnTrigger<OnRun, Running>,
RemoveOnTrigger<OnRunResult, Running>,
InsertOnRun<Running>,
RemoveOnRunResult<Running>,
RunOnRunResult,
RunOnSceneReady,
)>::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_spatial/src/plugins/beet_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl PluginGroup for BeetSpatialPlugins {

pub fn spatial_observers_plugin(app: &mut App) {
app.add_plugins(ActionPlugin::<(
InsertOnTrigger<OnRun, Visibility>,
InsertOnTrigger<OnRunResult, Visibility>,
InsertOnRun<Visibility>,
InsertOnRunResult<Visibility>,
)>::default());
}
Loading

0 comments on commit b9596c2

Please sign in to comment.