Pick return option<Vec3>
@@ -23,7 +23,7 @@ pub trait Pickable {
&self,
render_states: RenderStates,
viewport: Viewport,
- camera: &Camera
+ camera: &Camera,
) -> Result<(), Error>;
}
@@ -239,14 +239,28 @@ impl Camera {
return true;
}
- pub fn pick(&self, position: Vec3, direction: Vec3, max_depth: f32, objects: &[&dyn Pickable]) -> Result<Vec3, Error> {
+ pub fn pick(
+ &self,
+ position: Vec3,
+ direction: Vec3,
+ max_depth: f32,
+ objects: &[&dyn Pickable],
+ ) -> Result<Option<Vec3>, Error> {
let viewport = Viewport::new_at_origo(1, 1);
let up = if direction.dot(vec3(1.0, 0.0, 0.0)).abs() > 0.99 {
direction.cross(vec3(0.0, 1.0, 0.0))
} else {
direction.cross(vec3(1.0, 0.0, 0.0))
};
- let camera = Camera::new_orthographic(&self.context, position, position + direction * max_depth, up, 0.01, 0.01, max_depth)?;
+ let camera = Camera::new_orthographic(
+ &self.context,
+ position,
+ position + direction * max_depth,
+ up,
+ 0.01,
+ 0.01,
+ max_depth,
+ )?;
let texture = ColorTargetTexture2D::new(
&self.context,
viewport.width,
@@ -269,18 +283,33 @@ impl Camera {
let render_target = RenderTarget::new(&self.context, &texture, &depth_texture)?;
let render_states = RenderStates {
- write_mask: WriteMask {red: true, depth: true, ..WriteMask::NONE},
+ write_mask: WriteMask {
+ red: true,
+ depth: true,
+ ..WriteMask::NONE
+ },
depth_test: DepthTestType::Less,
..Default::default()
};
- render_target.write(&ClearState {red: Some(1.0), depth: Some(1.0), .. ClearState::none()}, || {
+ render_target.write(
+ &ClearState {
+ red: Some(1.0),
+ depth: Some(1.0),
+ ..ClearState::none()
+ },
+ || {
for object in objects {
object.pick(render_states, viewport, &camera)?;
}
Ok(())
- })?;
+ },
+ )?;
let depth = render_target.read_color_with_f32(viewport)?[0];
- Ok(position + direction * depth * max_depth)
+ Ok(if depth == 1.0 {
+ None
+ } else {
+ Some(position + direction * depth * max_depth)
+ })
}
///
