You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
martian-chess/examples/select.rs

168 lines
5.4 KiB
Rust

#![feature(iter_next_chunk)]
/// Example to illustrate selecting objects in 3d space
///
use bevy::{
prelude::*,
render::mesh::MeshVertexAttribute,
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
render::{mesh::VertexAttributeValues, render_resource::VertexFormat},
window::PrimaryWindow,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, startup)
.add_systems(Update, select)
.run();
}
fn startup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
) {
let debug_material = materials.add(StandardMaterial {
base_color_texture: Some(images.add(uv_debug_texture())),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, 5.0),
..default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::default().into()),
material: debug_material.clone(),
transform: Transform::from_xyz(1.0, 1.0, 1.0),
..default()
});
// commands.spawn(PbrBundle {
// mesh: meshes.add(shape::Cube::default().into()),
// material: debug_material.clone(),
// transform: Transform::from_xyz(0.0, 0.0, 0.0),
// ..default()
// });
// commands.spawn(PbrBundle {
// mesh: meshes.add(shape::Torus::default().into()),
// material: debug_material.clone(),
// transform: Transform::from_xyz(3.0, 0.0, 0.0),
// ..default()
// });
// commands.spawn(PbrBundle {
// mesh: meshes.add(shape::Icosphere::default().try_into().unwrap()),
// material: debug_material.clone(),
// transform: Transform::from_xyz(0.0, 3.0, 0.0),
// ..default()
// });
}
/// Creates a colorful test pattern
fn uv_debug_texture() -> Image {
const TEXTURE_SIZE: usize = 8;
let mut palette: [u8; 32] = [
255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
];
let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
for y in 0..TEXTURE_SIZE {
let offset = TEXTURE_SIZE * y * 4;
texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
palette.rotate_right(4);
}
Image::new_fill(
Extent3d {
width: TEXTURE_SIZE as u32,
height: TEXTURE_SIZE as u32,
depth_or_array_layers: 1,
},
TextureDimension::D2,
&texture_data,
TextureFormat::Rgba8UnormSrgb,
)
}
fn select(
query: Query<(Entity, &Handle<Mesh>, &GlobalTransform)>,
meshes: Res<Assets<Mesh>>,
cameras: Query<(&Camera, &GlobalTransform)>,
windows: Query<&Window, With<PrimaryWindow>>,
) {
// TODO:
// When mouse moves
// Ray trace to find object being selected
windows.iter().for_each(|window| {
if let Some(pos) = window.cursor_position() {
cameras.iter().for_each(|(camera, gt)| {
if let Some(ray) = camera.viewport_to_world(gt, pos) {
query
.iter()
.filter_map(|(entity, handle, gt)| {
meshes.get(handle).map(|mesh| (entity, mesh, gt))
})
.for_each(|(entity, mesh, gt)| {
let hit = intersects(&ray, &gt, mesh);
if hit.is_some() {
info!("We got a hit at {:?}", entity);
}
});
}
});
}
});
panic!("Lol");
}
fn intersects(ray: &Ray, gt: &GlobalTransform, mesh: &Mesh) -> Option<f32> {
info!("Ray: {:#?}", ray);
let attr = MeshVertexAttribute::new("Vertex_Position", 0, VertexFormat::Float32x3);
if let Some(verts) = mesh.attribute(attr) {
if let Some(idxs) = mesh.indices() {
match verts {
VertexAttributeValues::Float32x3(vals) => {
let mut itr = idxs.iter();
while let Ok(points) = itr
.next_chunk::<3>()
.map(|[x, y, z]| {
[
Vec3::from_array(vals[x]),
Vec3::from_array(vals[y]),
Vec3::from_array(vals[z]),
]
})
.map(|[a, b, c]| {
[
gt.transform_point(a),
gt.transform_point(b),
gt.transform_point(c),
]
})
{
info!("Does ray intersect with points: {:?}", points);
// OK so I have a ray with an origin and direction
// And I have a triangle made of 3 points
// How do I test if ray intersects with tri???
}
}
_ => warn!("Unrecognized indexes"),
}
}
}
None
}