mirror of
https://github.com/bevyengine/bevy.git
synced 2026-06-30 15:55:32 -04:00
3f401d1507
# Objective Fix #19101. (Kinda, this is not the exact solution proposed in that issue, but it still shortens most of the examples) ## Solution Most of the images created in examples are for use in render targets. `Image::new_target_texture` is made for exactly that and significantly shortens the image creation process. ## Testing I tested all the examples I changed and they seem to work fine. --- btw for some reason most of the examples use `TextureFormat::Bgra8UnormSrgb` while the documentation for `Image::new_target_texture` recommends `TextureFormat::Rgba8UnormSrgb` for SDR images. What's up with that? --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
117 lines
3.6 KiB
Rust
117 lines
3.6 KiB
Rust
//! A simple scene to demonstrate spawning a viewport widget. The example will demonstrate how to
|
|
//! pick entities visible in the widget's view.
|
|
|
|
use bevy::{
|
|
camera::RenderTarget, picking::pointer::PointerInteraction, prelude::*,
|
|
render::render_resource::TextureFormat, ui::widget::ViewportNode,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((DefaultPlugins, MeshPickingPlugin))
|
|
.add_systems(Startup, test)
|
|
.add_systems(Update, draw_mesh_intersections)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component, Reflect, Debug)]
|
|
#[reflect(Component)]
|
|
struct Shape;
|
|
|
|
fn test(
|
|
mut commands: Commands,
|
|
mut images: ResMut<Assets<Image>>,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
// Spawn a UI camera
|
|
commands.spawn(Camera3d::default());
|
|
|
|
// Set up an texture for the 3D camera to render to.
|
|
// The size of the texture will be based on the viewport's ui size.
|
|
let image = Image::new_target_texture(0, 0, TextureFormat::Bgra8UnormSrgb, None);
|
|
let image_handle = images.add(image);
|
|
|
|
// Spawn the 3D camera
|
|
let camera = commands
|
|
.spawn((
|
|
Camera3d::default(),
|
|
Camera {
|
|
// Render this camera before our UI camera
|
|
order: -1,
|
|
..default()
|
|
},
|
|
RenderTarget::Image(image_handle.clone().into()),
|
|
))
|
|
.id();
|
|
|
|
// Spawn something for the 3D camera to look at
|
|
commands
|
|
.spawn((
|
|
Mesh3d(meshes.add(Cuboid::new(5.0, 5.0, 5.0))),
|
|
MeshMaterial3d(materials.add(Color::WHITE)),
|
|
Transform::from_xyz(0.0, 0.0, -10.0),
|
|
Shape,
|
|
))
|
|
// We can observe pointer events on our objects as normal, the
|
|
// `bevy::ui::widget::viewport_picking` system will take care of ensuring our viewport
|
|
// clicks pass through
|
|
.observe(on_drag_cuboid);
|
|
|
|
// Spawn our viewport widget
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
top: px(50),
|
|
left: px(50),
|
|
width: px(200),
|
|
height: px(200),
|
|
border: UiRect::all(px(5)),
|
|
..default()
|
|
},
|
|
BorderColor::all(Color::WHITE),
|
|
ViewportNode::new(camera),
|
|
))
|
|
.observe(on_drag_viewport);
|
|
}
|
|
|
|
fn on_drag_viewport(drag: On<Pointer<Drag>>, mut node_query: Query<&mut Node>) {
|
|
if matches!(drag.button, PointerButton::Secondary) {
|
|
let mut node = node_query.get_mut(drag.entity).unwrap();
|
|
|
|
if let (Val::Px(top), Val::Px(left)) = (node.top, node.left) {
|
|
node.left = px(left + drag.delta.x);
|
|
node.top = px(top + drag.delta.y);
|
|
};
|
|
}
|
|
}
|
|
|
|
fn on_drag_cuboid(drag: On<Pointer<Drag>>, mut transform_query: Query<&mut Transform>) {
|
|
if matches!(drag.button, PointerButton::Primary) {
|
|
let mut transform = transform_query.get_mut(drag.entity).unwrap();
|
|
transform.rotate_y(drag.delta.x * 0.02);
|
|
transform.rotate_x(drag.delta.y * 0.02);
|
|
}
|
|
}
|
|
|
|
fn draw_mesh_intersections(
|
|
pointers: Query<&PointerInteraction>,
|
|
untargetable: Query<Entity, Without<Shape>>,
|
|
mut gizmos: Gizmos,
|
|
) {
|
|
for (point, normal) in pointers
|
|
.iter()
|
|
.flat_map(|interaction| interaction.iter())
|
|
.filter_map(|(entity, hit)| {
|
|
if !untargetable.contains(*entity) {
|
|
hit.position.zip(hit.normal)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
{
|
|
gizmos.arrow(point, point + normal.normalize() * 0.5, Color::WHITE);
|
|
}
|
|
}
|