From 3cc9c83f1a8e949f02a01bc8b790f334dea6931e Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 18 Apr 2022 08:49:20 -0700 Subject: [PATCH 1/2] Fix torus normals --- crates/bevy_render/src/mesh/shape/torus.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/shape/torus.rs b/crates/bevy_render/src/mesh/shape/torus.rs index 4fdc9b13181d5..0effdfb30ef8a 100644 --- a/crates/bevy_render/src/mesh/shape/torus.rs +++ b/crates/bevy_render/src/mesh/shape/torus.rs @@ -47,12 +47,12 @@ impl From for Mesh { let tan_ring = Vec3::new( theta.cos() * phi.sin() * -1.0, - theta.sin() * phi.sin() * -1.0, phi.cos(), + theta.sin() * phi.sin() * -1.0, ); - let tan = Vec3::new(theta.sin() * -1.0, theta.cos(), 0.0); + let tan = Vec3::new(theta.sin() * -1.0, 0.0, theta.cos()); - let normal = tan.cross(tan_ring).normalize(); + let normal = tan_ring.cross(tan).normalize(); positions.push([x, y, z]); normals.push(normal.into()); From e0c6ab9a17c8d27ebca7fac03e8ed02d54f625d6 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 18 Apr 2022 11:53:01 -0700 Subject: [PATCH 2/2] Simplify math --- crates/bevy_render/src/mesh/shape/torus.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/bevy_render/src/mesh/shape/torus.rs b/crates/bevy_render/src/mesh/shape/torus.rs index 0effdfb30ef8a..f22b3c7571114 100644 --- a/crates/bevy_render/src/mesh/shape/torus.rs +++ b/crates/bevy_render/src/mesh/shape/torus.rs @@ -41,20 +41,16 @@ impl From for Mesh { for side in 0..=torus.subdivisions_sides { let phi = side_stride * side as f32; - let x = theta.cos() * (torus.radius + torus.ring_radius * phi.cos()); - let z = theta.sin() * (torus.radius + torus.ring_radius * phi.cos()); - let y = torus.ring_radius * phi.sin(); - - let tan_ring = Vec3::new( - theta.cos() * phi.sin() * -1.0, - phi.cos(), - theta.sin() * phi.sin() * -1.0, + let position = Vec3::new( + theta.cos() * (torus.radius + torus.ring_radius * phi.cos()), + torus.ring_radius * phi.sin(), + theta.sin() * (torus.radius + torus.ring_radius * phi.cos()), ); - let tan = Vec3::new(theta.sin() * -1.0, 0.0, theta.cos()); - let normal = tan_ring.cross(tan).normalize(); + let center = Vec3::new(torus.radius * theta.cos(), 0., torus.radius * theta.sin()); + let normal = (position - center).normalize(); - positions.push([x, y, z]); + positions.push(position.into()); normals.push(normal.into()); uvs.push([ segment as f32 / torus.subdivisions_segments as f32,