-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add an example for intersection plane #249
Changes from 4 commits
8b34ccb
ec13983
358e6b1
d06491b
4a7f833
8ac9df1
b7128e5
d88dd24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,156 @@ | ||||||
use macroquad::models::Vertex; | ||||||
use macroquad::prelude::*; | ||||||
use nalgebra::{Point3, UnitVector3, Vector3}; | ||||||
use parry3d::math::Real; | ||||||
use parry3d::query::IntersectResult; | ||||||
use parry3d::shape::{Cuboid, TriMesh}; | ||||||
|
||||||
#[macroquad::main("parry3d::query::PlaneIntersection")] | ||||||
async fn main() { | ||||||
let trimesh = Cuboid::new(Vector3::new(1.0, 1.0, 1.0)).to_trimesh(); | ||||||
|
||||||
let camera_pos = Vec3::new(-1.5f32, 2.5f32, -3f32); | ||||||
|
||||||
let mesh = mquad_mesh_from_points(&trimesh, camera_pos); | ||||||
let trimesh = TriMesh::new(trimesh.0, trimesh.1); | ||||||
|
||||||
for _ in 1.. { | ||||||
clear_background(BLACK); | ||||||
|
||||||
let elapsed_time = get_time(); | ||||||
|
||||||
// Animated rotation for the intersection plane. | ||||||
let bias = -1.2 * (elapsed_time as f32 / 3f32).sin(); | ||||||
let rotation = Quat::from_axis_angle(Vec3::Z, (elapsed_time as f32 * 40f32).to_radians()); | ||||||
let up_plane_vector = rotation * Vec3::Y; | ||||||
|
||||||
// Get the intersection polyline. | ||||||
let intersection_result = trimesh.intersection_with_local_plane( | ||||||
&UnitVector3::new_normalize(Vector3::<Real>::new( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the type parameter annotation really necessary?
Suggested change
|
||||||
up_plane_vector.x, | ||||||
up_plane_vector.y, | ||||||
up_plane_vector.z, | ||||||
)), | ||||||
bias, | ||||||
0.0005, | ||||||
); | ||||||
|
||||||
// Initialize 3D camera. | ||||||
set_camera(&Camera3D { | ||||||
position: camera_pos, | ||||||
up: Vec3::new(0f32, 1f32, 0f32), | ||||||
target: Vec3::new(0.5f32, 0f32, 0.5f32), | ||||||
..Default::default() | ||||||
}); | ||||||
|
||||||
// Draw involved shapes. | ||||||
let plane_center = up_plane_vector * bias; | ||||||
draw_line_3d(plane_center, plane_center + up_plane_vector, GREEN); | ||||||
draw_mesh(&mesh); | ||||||
draw_grid_ex(10, 0.333, BLUE, RED, plane_center, rotation); | ||||||
|
||||||
/* | ||||||
* | ||||||
* Render the intersection. | ||||||
* | ||||||
*/ | ||||||
match intersection_result { | ||||||
IntersectResult::Intersect(points) => { | ||||||
draw_polyline( | ||||||
points | ||||||
.segments() | ||||||
.map(|s| (mquad_from_na(s.a), mquad_from_na(s.b))) | ||||||
.collect(), | ||||||
Color::new(0f32, 1f32, 0f32, 1f32), | ||||||
); | ||||||
set_default_camera(); | ||||||
draw_text("Intersection found!"); | ||||||
} | ||||||
IntersectResult::Negative => { | ||||||
set_default_camera(); | ||||||
draw_text("No intersection found, the shape is below the plane."); | ||||||
} | ||||||
IntersectResult::Positive => { | ||||||
set_default_camera(); | ||||||
draw_text("No intersection found, the shape is above the plane."); | ||||||
} | ||||||
} | ||||||
next_frame().await | ||||||
} | ||||||
} | ||||||
|
||||||
fn mquad_mesh_from_points(trimesh: &(Vec<Point3<Real>>, Vec<[u32; 3]>), camera_pos: Vec3) -> Mesh { | ||||||
let (points, indices) = trimesh; | ||||||
// Transform the parry mesh into a mquad Mesh | ||||||
let (mquad_points, mquad_indices) = ( | ||||||
points | ||||||
.iter() | ||||||
.map(|p| Vertex { | ||||||
position: mquad_from_na(*p), | ||||||
uv: Vec2::new(p.x, p.y), | ||||||
color: DARKGRAY, | ||||||
}) | ||||||
.collect(), | ||||||
indices.iter().flatten().map(|v| *v as u16).collect(), | ||||||
); | ||||||
|
||||||
// Macroquad doesn´t support adding normals to vertices, so we'll bake a color into these vertices. | ||||||
// See https://github.com/not-fl3/macroquad/issues/321. | ||||||
|
||||||
// Compute the normal of each vertex, making them unique | ||||||
let vertices: Vec<Vertex> = mquad_compute_normals(&mquad_points, &mquad_indices, camera_pos); | ||||||
// Regenerate the index for each vertex. | ||||||
let indices: Vec<u16> = (0..vertices.len() * 3) | ||||||
.into_iter() | ||||||
.map(|i| i as u16) | ||||||
.collect(); | ||||||
let mesh = Mesh { | ||||||
vertices, | ||||||
indices, | ||||||
texture: None, | ||||||
}; | ||||||
mesh | ||||||
} | ||||||
|
||||||
fn mquad_compute_normals(points: &Vec<Vertex>, indices: &Vec<u16>, cam_pos: Vec3) -> Vec<Vertex> { | ||||||
let mut vertices: Vec<Vertex> = Vec::<Vertex>::new(); | ||||||
for indices in indices.chunks(3) { | ||||||
let v0 = &points[indices[0] as usize]; | ||||||
let v1 = &points[indices[1] as usize]; | ||||||
let v2 = &points[indices[2] as usize]; | ||||||
let normal = (v0.position - v2.position) | ||||||
.cross(v1.position - v2.position) | ||||||
.normalize(); | ||||||
let brightness_mod = 0.2 + (0.8 / 2.) * (normal.dot(cam_pos) + 1.); | ||||||
|
||||||
for &i in indices.iter() { | ||||||
let mut color = points[i as usize].color; | ||||||
color.r *= brightness_mod; | ||||||
color.g *= brightness_mod; | ||||||
color.b *= brightness_mod; | ||||||
|
||||||
vertices.push(Vertex { | ||||||
position: points[i as usize].position, | ||||||
uv: Vec2::ZERO, | ||||||
color: color, | ||||||
}); | ||||||
} | ||||||
} | ||||||
vertices | ||||||
} | ||||||
Comment on lines
+82
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all this is lengthy, but unfortunately there's not much way around it (see not-fl3/macroquad#321); it would be good to settle on a "common" library for all examples, but as it's the first 3d example this might be ok to keep it here, and we'll move it out with the next example, probably #250 |
||||||
|
||||||
fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { | ||||||
for i in 0..polygon.len() { | ||||||
let a = polygon[i].0; | ||||||
let b = polygon[i].1; | ||||||
draw_line_3d(a, b, color); | ||||||
} | ||||||
} | ||||||
|
||||||
fn mquad_from_na(a: Point3<Real>) -> Vec3 { | ||||||
Vec3::new(a.x, a.y, a.z) | ||||||
} | ||||||
|
||||||
fn draw_text(text: &str) { | ||||||
macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.