 c2c19e5ae4
			
		
	
	
		c2c19e5ae4
		
			
		
	
	
	
	
		
			
			**Ready for review. Examples migration progress: 100%.** # Objective - Implement https://github.com/bevyengine/bevy/discussions/15014 ## Solution This implements [cart's proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459) faithfully except for one change. I separated `TextSpan` from `TextSpan2d` because `TextSpan` needs to require the `GhostNode` component, which is a `bevy_ui` component only usable by UI. Extra changes: - Added `EntityCommands::commands_mut` that returns a mutable reference. This is a blocker for extension methods that return something other than `self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable reference for this reason. ## Testing - [x] Text examples all work. --- ## Showcase TODO: showcase-worthy ## Migration Guide TODO: very breaking ### Accessing text spans by index Text sections are now text sections on different entities in a hierarchy, Use the new `TextReader` and `TextWriter` system parameters to access spans by index. Before: ```rust fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) { let text = query.single_mut(); text.sections[1].value = format_time(time.elapsed()); } ``` After: ```rust fn refresh_text( query: Query<Entity, With<TimeText>>, mut writer: UiTextWriter, time: Res<Time> ) { let entity = query.single(); *writer.text(entity, 1) = format_time(time.elapsed()); } ``` ### Iterating text spans Text spans are now entities in a hierarchy, so the new `UiTextReader` and `UiTextWriter` system parameters provide ways to iterate that hierarchy. The `UiTextReader::iter` method will give you a normal iterator over spans, and `UiTextWriter::for_each` lets you visit each of the spans. --------- Co-authored-by: ickshonpe <david.curthoys@googlemail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
		
			
				
	
	
		
			148 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			148 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Shows how to orbit camera around a static scene using pitch, yaw, and roll.
 | |
| //!
 | |
| //! See also: `first_person_view_model` example, which does something similar but as a first-person
 | |
| //! camera view.
 | |
| 
 | |
| use std::{f32::consts::FRAC_PI_2, ops::Range};
 | |
| 
 | |
| use bevy::{input::mouse::AccumulatedMouseMotion, prelude::*};
 | |
| 
 | |
| #[derive(Debug, Resource)]
 | |
| struct CameraSettings {
 | |
|     pub orbit_distance: f32,
 | |
|     pub pitch_speed: f32,
 | |
|     // Clamp pitch to this range
 | |
|     pub pitch_range: Range<f32>,
 | |
|     pub roll_speed: f32,
 | |
|     pub yaw_speed: f32,
 | |
| }
 | |
| 
 | |
| impl Default for CameraSettings {
 | |
|     fn default() -> Self {
 | |
|         // Limiting pitch stops some unexpected rotation past 90° up or down.
 | |
|         let pitch_limit = FRAC_PI_2 - 0.01;
 | |
|         Self {
 | |
|             // These values are completely arbitrary, chosen because they seem to produce
 | |
|             // "sensible" results for this example. Adjust as required.
 | |
|             orbit_distance: 20.0,
 | |
|             pitch_speed: 0.003,
 | |
|             pitch_range: -pitch_limit..pitch_limit,
 | |
|             roll_speed: 1.0,
 | |
|             yaw_speed: 0.004,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .init_resource::<CameraSettings>()
 | |
|         .add_systems(Startup, (setup, instructions))
 | |
|         .add_systems(Update, orbit)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| /// Set up a simple 3D scene
 | |
| fn setup(
 | |
|     mut commands: Commands,
 | |
|     mut meshes: ResMut<Assets<Mesh>>,
 | |
|     mut materials: ResMut<Assets<StandardMaterial>>,
 | |
| ) {
 | |
|     commands.spawn((
 | |
|         Name::new("Camera"),
 | |
|         Camera3d::default(),
 | |
|         Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
 | |
|     ));
 | |
| 
 | |
|     commands.spawn((
 | |
|         Name::new("Plane"),
 | |
|         Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
 | |
|         MeshMaterial3d(materials.add(StandardMaterial {
 | |
|             base_color: Color::srgb(0.3, 0.5, 0.3),
 | |
|             // Turning off culling keeps the plane visible when viewed from beneath.
 | |
|             cull_mode: None,
 | |
|             ..default()
 | |
|         })),
 | |
|     ));
 | |
| 
 | |
|     commands.spawn((
 | |
|         Name::new("Cube"),
 | |
|         Mesh3d(meshes.add(Cuboid::default())),
 | |
|         MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
 | |
|         Transform::from_xyz(1.5, 0.51, 1.5),
 | |
|     ));
 | |
| 
 | |
|     commands.spawn((
 | |
|         Name::new("Light"),
 | |
|         PointLight::default(),
 | |
|         Transform::from_xyz(3.0, 8.0, 5.0),
 | |
|     ));
 | |
| }
 | |
| 
 | |
| fn instructions(mut commands: Commands) {
 | |
|     commands
 | |
|         .spawn((
 | |
|             Name::new("Instructions"),
 | |
|             NodeBundle {
 | |
|                 style: Style {
 | |
|                     align_items: AlignItems::Start,
 | |
|                     flex_direction: FlexDirection::Column,
 | |
|                     justify_content: JustifyContent::Start,
 | |
|                     width: Val::Percent(100.),
 | |
|                     ..default()
 | |
|                 },
 | |
|                 ..default()
 | |
|             },
 | |
|         ))
 | |
|         .with_children(|parent| {
 | |
|             parent.spawn(Text::new("Mouse up or down: pitch"));
 | |
|             parent.spawn(Text::new("Mouse left or right: yaw"));
 | |
|             parent.spawn(Text::new("Mouse buttons: roll"));
 | |
|         });
 | |
| }
 | |
| 
 | |
| fn orbit(
 | |
|     mut camera: Query<&mut Transform, With<Camera>>,
 | |
|     camera_settings: Res<CameraSettings>,
 | |
|     mouse_buttons: Res<ButtonInput<MouseButton>>,
 | |
|     mouse_motion: Res<AccumulatedMouseMotion>,
 | |
|     time: Res<Time>,
 | |
| ) {
 | |
|     let mut transform = camera.single_mut();
 | |
|     let delta = mouse_motion.delta;
 | |
|     let mut delta_roll = 0.0;
 | |
| 
 | |
|     if mouse_buttons.pressed(MouseButton::Left) {
 | |
|         delta_roll -= 1.0;
 | |
|     }
 | |
|     if mouse_buttons.pressed(MouseButton::Right) {
 | |
|         delta_roll += 1.0;
 | |
|     }
 | |
| 
 | |
|     // Mouse motion is one of the few inputs that should not be multiplied by delta time,
 | |
|     // as we are already receiving the full movement since the last frame was rendered. Multiplying
 | |
|     // by delta time here would make the movement slower that it should be.
 | |
|     let delta_pitch = delta.y * camera_settings.pitch_speed;
 | |
|     let delta_yaw = delta.x * camera_settings.yaw_speed;
 | |
| 
 | |
|     // Conversely, we DO need to factor in delta time for mouse button inputs.
 | |
|     delta_roll *= camera_settings.roll_speed * time.delta_seconds();
 | |
| 
 | |
|     // Obtain the existing pitch, yaw, and roll values from the transform.
 | |
|     let (yaw, pitch, roll) = transform.rotation.to_euler(EulerRot::YXZ);
 | |
| 
 | |
|     // Establish the new yaw and pitch, preventing the pitch value from exceeding our limits.
 | |
|     let pitch = (pitch + delta_pitch).clamp(
 | |
|         camera_settings.pitch_range.start,
 | |
|         camera_settings.pitch_range.end,
 | |
|     );
 | |
|     let roll = roll + delta_roll;
 | |
|     let yaw = yaw + delta_yaw;
 | |
|     transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, roll);
 | |
| 
 | |
|     // Adjust the translation to maintain the correct orientation toward the orbit target.
 | |
|     // In our example it's a static target, but this could easily be customised.
 | |
|     let target = Vec3::ZERO;
 | |
|     transform.translation = target - transform.forward() * camera_settings.orbit_distance;
 | |
| }
 |