-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Add 3d shapes example #4613
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7df6f69
Add 3d shapes example
rparrett ac22413
Add shapes example to README
rparrett be509bd
Ditch the texture
rparrett aa0baa9
Clippy
rparrett 1c9c069
Re-adjust example description
rparrett 9737886
Add some comments
rparrett df0d47c
Fix non-doc comment that should be doc comment
rparrett d848ba7
Use less potentially ambiguous example name
rparrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(Msaa { samples: 4 }) | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(rotate) | ||
.run(); | ||
} | ||
|
||
#[derive(Component)] | ||
struct Shape; | ||
|
||
const X_EXTENT: f32 = 14.; | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
asset_server: ResMut<AssetServer>, | ||
) { | ||
let debug_material = materials.add(StandardMaterial { | ||
base_color_texture: Some(asset_server.load("textures/uv_debug.png")), | ||
..default() | ||
}); | ||
|
||
let shapes = [ | ||
meshes.add(shape::Cube::default().into()), | ||
meshes.add(shape::Box::default().into()), | ||
meshes.add(shape::Capsule::default().into()), | ||
meshes.add(shape::Torus::default().into()), | ||
meshes.add(shape::Icosphere::default().into()), | ||
meshes.add(shape::UVSphere::default().into()), | ||
]; | ||
|
||
let num_shapes = shapes.len(); | ||
|
||
for (i, shape) in shapes.into_iter().enumerate() { | ||
commands | ||
.spawn_bundle(PbrBundle { | ||
mesh: shape, | ||
material: debug_material.clone(), | ||
transform: Transform { | ||
translation: Vec3::new( | ||
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT, | ||
2.0, | ||
0.0, | ||
), | ||
..default() | ||
}, | ||
..Default::default() | ||
}) | ||
.insert(Shape); | ||
} | ||
|
||
commands.spawn_bundle(PointLightBundle { | ||
point_light: PointLight { | ||
intensity: 9000.0, | ||
range: 100., | ||
shadows_enabled: true, | ||
..Default::default() | ||
}, | ||
transform: Transform::from_xyz(8.0, 16.0, 8.0), | ||
..Default::default() | ||
}); | ||
|
||
// ground plane | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(shape::Plane { size: 50. }.into()), | ||
material: materials.add(Color::SILVER.into()), | ||
..Default::default() | ||
}); | ||
|
||
commands.spawn_bundle(PerspectiveCameraBundle { | ||
transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y), | ||
..Default::default() | ||
}); | ||
} | ||
|
||
fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) { | ||
for mut transform in query.iter_mut() { | ||
transform.rotation = Quat::from_rotation_y(time.seconds_since_startup() as f32 / 2.) | ||
* Quat::from_rotation_x(-std::f32::consts::PI / 4.) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this is that if a user sees in the file tree
shapes.rs
thinks that the example name isshapes
.Remember to rename the file and change the reference to the README if you accept this.