|
| 1 | +# Torus |
| 2 | + |
| 3 | +A [Torus](https://docs.rs/bevy/latest/bevy/prelude/shape/struct.Torus.html) is a donut-like shape. |
| 4 | + |
| 5 | +```rust |
| 6 | +commands.spawn(PbrBundle { |
| 7 | + mesh: meshes |
| 8 | + .add( |
| 9 | + Torus { |
| 10 | + radius: 0.5, |
| 11 | + ring_radius: 0.1, |
| 12 | + ..default() |
| 13 | + } |
| 14 | + .into(), |
| 15 | + ) |
| 16 | + .into(), |
| 17 | + ..default() |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +We can set the [radius](https://docs.rs/bevy/latest/bevy/prelude/shape/struct.Torus.html#structfield.radius) of the [Torus](https://docs.rs/bevy/latest/bevy/prelude/shape/struct.Torus.html) and the radius ([ring_radius](https://docs.rs/bevy/latest/bevy/prelude/shape/struct.Torus.html#structfield.ring_radius)) of the [Torus](https://docs.rs/bevy/latest/bevy/prelude/shape/struct.Torus.html)'s ring. |
| 22 | + |
| 23 | +We set our camera position to `(2, 1, 3)` and make it looking at the origin. |
| 24 | + |
| 25 | +The full code is as follows: |
| 26 | + |
| 27 | +```rust |
| 28 | +use bevy::{ |
| 29 | + app::{App, Startup}, |
| 30 | + asset::Assets, |
| 31 | + core_pipeline::core_3d::Camera3dBundle, |
| 32 | + ecs::system::{Commands, ResMut}, |
| 33 | + math::Vec3, |
| 34 | + pbr::{PbrBundle, PointLightBundle, StandardMaterial}, |
| 35 | + render::mesh::{shape::Torus, Mesh}, |
| 36 | + transform::components::Transform, |
| 37 | + utils::default, |
| 38 | + DefaultPlugins, |
| 39 | +}; |
| 40 | + |
| 41 | +fn main() { |
| 42 | + App::new() |
| 43 | + .add_plugins(DefaultPlugins) |
| 44 | + .add_systems(Startup, setup) |
| 45 | + .run(); |
| 46 | +} |
| 47 | + |
| 48 | +fn setup( |
| 49 | + mut commands: Commands, |
| 50 | + mut meshes: ResMut<Assets<Mesh>>, |
| 51 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 52 | +) { |
| 53 | + commands.spawn(Camera3dBundle { |
| 54 | + transform: Transform::from_xyz(2., 1., 3.).looking_at(Vec3::ZERO, Vec3::Y), |
| 55 | + ..default() |
| 56 | + }); |
| 57 | + |
| 58 | + commands.spawn(PbrBundle { |
| 59 | + mesh: meshes |
| 60 | + .add( |
| 61 | + Torus { |
| 62 | + radius: 0.5, |
| 63 | + ring_radius: 0.1, |
| 64 | + ..default() |
| 65 | + } |
| 66 | + .into(), |
| 67 | + ) |
| 68 | + .into(), |
| 69 | + material: materials.add(StandardMaterial::default()).into(), |
| 70 | + ..default() |
| 71 | + }); |
| 72 | + |
| 73 | + commands.spawn(PointLightBundle { |
| 74 | + transform: Transform::from_xyz(2., 2., 1.), |
| 75 | + ..default() |
| 76 | + }); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Result: |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +<!-- :arrow_right: Next: --> |
| 85 | + |
| 86 | +:blue_book: Back: [Table of contents](./../README.md) |
0 commit comments