Skip to content

Commit dafd7a1

Browse files
committed
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
1 parent 3f42307 commit dafd7a1

File tree

1 file changed

+7
-11
lines changed
  • crates/bevy_render/src/mesh/shape

1 file changed

+7
-11
lines changed

crates/bevy_render/src/mesh/shape/torus.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,16 @@ impl From<Torus> for Mesh {
4141
for side in 0..=torus.subdivisions_sides {
4242
let phi = side_stride * side as f32;
4343

44-
let x = theta.cos() * (torus.radius + torus.ring_radius * phi.cos());
45-
let z = theta.sin() * (torus.radius + torus.ring_radius * phi.cos());
46-
let y = torus.ring_radius * phi.sin();
47-
48-
let tan_ring = Vec3::new(
49-
theta.cos() * phi.sin() * -1.0,
50-
theta.sin() * phi.sin() * -1.0,
51-
phi.cos(),
44+
let position = Vec3::new(
45+
theta.cos() * (torus.radius + torus.ring_radius * phi.cos()),
46+
torus.ring_radius * phi.sin(),
47+
theta.sin() * (torus.radius + torus.ring_radius * phi.cos()),
5248
);
53-
let tan = Vec3::new(theta.sin() * -1.0, theta.cos(), 0.0);
5449

55-
let normal = tan.cross(tan_ring).normalize();
50+
let center = Vec3::new(torus.radius * theta.cos(), 0., torus.radius * theta.sin());
51+
let normal = (position - center).normalize();
5652

57-
positions.push([x, y, z]);
53+
positions.push(position.into());
5854
normals.push(normal.into());
5955
uvs.push([
6056
segment as f32 / torus.subdivisions_segments as f32,

0 commit comments

Comments
 (0)