Skip to content

Commit db0d769

Browse files
committed
Change From<Icosphere> to TryFrom<Icosphere> (#6484)
# Objective - Fixes #6476 ## Solution - Return error instead of panic through `TryFrom` - ~~Add `.except()` in examples~~ - Add `.unwrap()` in examples
1 parent 4de4e54 commit db0d769

File tree

12 files changed

+107
-60
lines changed

12 files changed

+107
-60
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::mesh::{Indices, Mesh};
22
use hexasphere::shapes::IcoSphere;
3+
use thiserror::Error;
34
use wgpu::PrimitiveTopology;
45

56
/// A sphere made from a subdivided Icosahedron.
@@ -20,8 +21,19 @@ impl Default for Icosphere {
2021
}
2122
}
2223

23-
impl From<Icosphere> for Mesh {
24-
fn from(sphere: Icosphere) -> Self {
24+
#[derive(Debug, Clone, Error)]
25+
pub enum FromIcosphereError {
26+
#[error("Cannot create an icosphere of {subdivisions} subdivisions due to there being too many vertices being generated: {number_of_resulting_points}. (Limited to 65535 vertices or 79 subdivisions)")]
27+
TooManyVertices {
28+
subdivisions: usize,
29+
number_of_resulting_points: usize,
30+
},
31+
}
32+
33+
impl TryFrom<Icosphere> for Mesh {
34+
type Error = FromIcosphereError;
35+
36+
fn try_from(sphere: Icosphere) -> Result<Self, Self::Error> {
2537
if sphere.subdivisions >= 80 {
2638
/*
2739
Number of triangles:
@@ -53,12 +65,10 @@ impl From<Icosphere> for Mesh {
5365
*/
5466
let temp = sphere.subdivisions + 1;
5567
let number_of_resulting_points = temp * temp * 10 + 2;
56-
57-
panic!(
58-
"Cannot create an icosphere of {} subdivisions due to there being too many vertices being generated: {}. (Limited to 65535 vertices or 79 subdivisions)",
59-
sphere.subdivisions,
60-
number_of_resulting_points
61-
);
68+
return Err(FromIcosphereError::TooManyVertices {
69+
subdivisions: sphere.subdivisions,
70+
number_of_resulting_points,
71+
});
6272
}
6373
let generated = IcoSphere::new(sphere.subdivisions, |point| {
6474
let inclination = point.y.acos();
@@ -98,6 +108,6 @@ impl From<Icosphere> for Mesh {
98108
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, points);
99109
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
100110
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
101-
mesh
111+
Ok(mesh)
102112
}
103113
}

examples/3d/3d_shapes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn setup(
3838
meshes.add(shape::Box::default().into()),
3939
meshes.add(shape::Capsule::default().into()),
4040
meshes.add(shape::Torus::default().into()),
41-
meshes.add(shape::Icosphere::default().into()),
41+
meshes.add(shape::Icosphere::default().try_into().unwrap()),
4242
meshes.add(shape::UVSphere::default().into()),
4343
];
4444

examples/3d/bloom.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ fn setup_scene(
4747
radius: 0.5,
4848
subdivisions: 5,
4949
}
50-
.into(),
50+
.try_into()
51+
.unwrap(),
5152
);
5253

5354
for x in -10..10 {

examples/3d/pbr.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ fn setup(
2222
let y01 = (y + 2) as f32 / 4.0;
2323
// sphere
2424
commands.spawn(PbrBundle {
25-
mesh: meshes.add(Mesh::from(shape::Icosphere {
26-
radius: 0.45,
27-
subdivisions: 32,
28-
})),
25+
mesh: meshes.add(
26+
Mesh::try_from(shape::Icosphere {
27+
radius: 0.45,
28+
subdivisions: 32,
29+
})
30+
.unwrap(),
31+
),
2932
material: materials.add(StandardMaterial {
3033
base_color: Color::hex("ffd891").unwrap(),
3134
// vary key PBR parameters on a grid of spheres to show the effect
@@ -40,10 +43,13 @@ fn setup(
4043
}
4144
// unlit sphere
4245
commands.spawn(PbrBundle {
43-
mesh: meshes.add(Mesh::from(shape::Icosphere {
44-
radius: 0.45,
45-
subdivisions: 32,
46-
})),
46+
mesh: meshes.add(
47+
Mesh::try_from(shape::Icosphere {
48+
radius: 0.45,
49+
subdivisions: 32,
50+
})
51+
.unwrap(),
52+
),
4753
material: materials.add(StandardMaterial {
4854
base_color: Color::hex("ffd891").unwrap(),
4955
// vary key PBR parameters on a grid of spheres to show the effect

examples/3d/shadow_biases.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ fn setup(
4242
perceptual_roughness: 1.0,
4343
..default()
4444
});
45-
let sphere_handle = meshes.add(Mesh::from(shape::Icosphere {
46-
radius: sphere_radius,
47-
..default()
48-
}));
45+
let sphere_handle = meshes.add(
46+
Mesh::try_from(shape::Icosphere {
47+
radius: sphere_radius,
48+
..default()
49+
})
50+
.unwrap(),
51+
);
4952

5053
println!("Using DirectionalLight");
5154

examples/3d/shadow_caster_receiver.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ fn setup(
3737
perceptual_roughness: 1.0,
3838
..default()
3939
});
40-
let sphere_handle = meshes.add(Mesh::from(shape::Icosphere {
41-
radius: sphere_radius,
42-
..default()
43-
}));
40+
let sphere_handle = meshes.add(
41+
Mesh::try_from(shape::Icosphere {
42+
radius: sphere_radius,
43+
..default()
44+
})
45+
.unwrap(),
46+
);
4447

4548
// sphere - initially a caster
4649
commands.spawn(PbrBundle {

examples/3d/transparency_3d.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ fn setup(
2626
});
2727
// transparent sphere, uses `alpha_mode: Mask(f32)`
2828
commands.spawn(PbrBundle {
29-
mesh: meshes.add(Mesh::from(shape::Icosphere {
30-
radius: 0.5,
31-
subdivisions: 3,
32-
})),
29+
mesh: meshes.add(
30+
Mesh::try_from(shape::Icosphere {
31+
radius: 0.5,
32+
subdivisions: 3,
33+
})
34+
.unwrap(),
35+
),
3336
material: materials.add(StandardMaterial {
3437
// Alpha channel of the color controls transparency.
3538
// We set it to 0.0 here, because it will be changed over time in the
@@ -46,10 +49,13 @@ fn setup(
4649
});
4750
// transparent unlit sphere, uses `alpha_mode: Mask(f32)`
4851
commands.spawn(PbrBundle {
49-
mesh: meshes.add(Mesh::from(shape::Icosphere {
50-
radius: 0.5,
51-
subdivisions: 3,
52-
})),
52+
mesh: meshes.add(
53+
Mesh::try_from(shape::Icosphere {
54+
radius: 0.5,
55+
subdivisions: 3,
56+
})
57+
.unwrap(),
58+
),
5359
material: materials.add(StandardMaterial {
5460
base_color: Color::rgba(0.2, 0.7, 0.1, 0.0),
5561
alpha_mode: AlphaMode::Mask(0.5),
@@ -71,10 +77,13 @@ fn setup(
7177
});
7278
// opaque sphere
7379
commands.spawn(PbrBundle {
74-
mesh: meshes.add(Mesh::from(shape::Icosphere {
75-
radius: 0.5,
76-
subdivisions: 3,
77-
})),
80+
mesh: meshes.add(
81+
Mesh::try_from(shape::Icosphere {
82+
radius: 0.5,
83+
subdivisions: 3,
84+
})
85+
.unwrap(),
86+
),
7887
material: materials.add(Color::rgb(0.7, 0.2, 0.1).into()),
7988
transform: Transform::from_xyz(0.0, 0.5, -1.5),
8089
..default()

examples/animation/animated_transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn setup(
118118
commands
119119
.spawn((
120120
PbrBundle {
121-
mesh: meshes.add(Mesh::from(shape::Icosphere::default())),
121+
mesh: meshes.add(Mesh::try_from(shape::Icosphere::default()).unwrap()),
122122
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
123123
..default()
124124
},

examples/ecs/iter_combinations.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ fn generate_bodies(
5454
mut meshes: ResMut<Assets<Mesh>>,
5555
mut materials: ResMut<Assets<StandardMaterial>>,
5656
) {
57-
let mesh = meshes.add(Mesh::from(shape::Icosphere {
58-
radius: 1.0,
59-
subdivisions: 3,
60-
}));
57+
let mesh = meshes.add(
58+
Mesh::try_from(shape::Icosphere {
59+
radius: 1.0,
60+
subdivisions: 3,
61+
})
62+
.unwrap(),
63+
);
6164

6265
let color_range = 0.5..1.0;
6366
let vel_range = -0.5..0.5;
@@ -114,10 +117,13 @@ fn generate_bodies(
114117
BodyBundle {
115118
pbr: PbrBundle {
116119
transform: Transform::from_scale(Vec3::splat(star_radius)),
117-
mesh: meshes.add(Mesh::from(shape::Icosphere {
118-
radius: 1.0,
119-
subdivisions: 5,
120-
})),
120+
mesh: meshes.add(
121+
Mesh::try_from(shape::Icosphere {
122+
radius: 1.0,
123+
subdivisions: 5,
124+
})
125+
.unwrap(),
126+
),
121127
material: materials.add(StandardMaterial {
122128
base_color: Color::ORANGE_RED,
123129
emissive: (Color::ORANGE_RED * 2.),

examples/ios/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ fn setup_scene(
6767
});
6868
// sphere
6969
commands.spawn(PbrBundle {
70-
mesh: meshes.add(Mesh::from(shape::Icosphere {
71-
subdivisions: 4,
72-
radius: 0.5,
73-
})),
70+
mesh: meshes.add(
71+
Mesh::try_from(shape::Icosphere {
72+
subdivisions: 4,
73+
radius: 0.5,
74+
})
75+
.unwrap(),
76+
),
7477
material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()),
7578
transform: Transform::from_xyz(1.5, 1.5, 1.5),
7679
..default()

0 commit comments

Comments
 (0)