From dafd7a14c9875f7ee31686746af44af75bca1b4c Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Tue, 26 Apr 2022 18:42:43 +0000 Subject: [PATCH] Fix torus normals (#4520) # Objective Fix wonky torus normals. ## Solution I attempted this previously in #3549, but it looks like I botched it. It seems like I mixed up the y/z axes. Somehow, the result looked okay from that particular camera angle. This video shows toruses generated with - [left, orange] original torus mesh code - [middle, pink] PR 3549 - [right, purple] This PR https://user-images.githubusercontent.com/200550/164093183-58a7647c-b436-4512-99cd-cf3b705cefb0.mov --- 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 4fdc9b13181d5..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, - theta.sin() * phi.sin() * -1.0, - phi.cos(), + 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, theta.cos(), 0.0); - let normal = tan.cross(tan_ring).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,