Skip to content
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

run skinned shadow wireframe example in ci #4370

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/example-run/skinned_mesh_wireframe_shadow.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(
exit_after: Some(100)
)
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ path = "examples/3d/lighting.rs"
name = "load_gltf"
path = "examples/3d/load_gltf.rs"

[[example]]
name = "manual_gltf_animation_player"
path = "examples/3d/manual_gltf_animation_player.rs"

[[example]]
name = "many_cubes"
path = "examples/3d/many_cubes.rs"
Expand Down Expand Up @@ -244,6 +240,14 @@ path = "examples/animation/custom_skinned_mesh.rs"
name = "gltf_skinned_mesh"
path = "examples/animation/gltf_skinned_mesh.rs"

[[example]]
name = "manual_gltf_animation_player"
path = "examples/animation/manual_gltf_animation_player.rs"

[[example]]
name = "skinned_mesh_wireframe_shadow"
path = "examples/animation/skinned_mesh_wireframe_shadow.rs"

# Application
[[example]]
name = "custom_loop"
Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ Example | File | Description
`3d_scene` | [`3d/3d_scene.rs`](./3d/3d_scene.rs) | Simple 3D scene with basic shapes and lighting
`lighting` | [`3d/lighting.rs`](./3d/lighting.rs) | Illustrates various lighting options in a simple scene
`load_gltf` | [`3d/load_gltf.rs`](./3d/load_gltf.rs) | Loads and renders a gltf file as a scene
`manual_gltf_animation_player` | [`3d/manual_gltf_animation_player.rs`](./3d/manual_gltf_animation_player.rs) | Loads and manually renders a gltf file with animations
`many_cubes` | [`3d/many_cubes.rs`](./3d/many_cubes.rs) | Simple benchmark to test per-entity draw overhead
`msaa` | [`3d/msaa.rs`](./3d/msaa.rs) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges
`orthographic` | [`3d/orthographic.rs`](./3d/orthographic.rs) | Shows how to create a 3D orthographic view (for isometric-look games or CAD applications)
Expand All @@ -125,6 +124,8 @@ Example | File | Description
--- | --- | ---
`custom_skinned_mesh` | [`animation/custom_skinned_mesh.rs`](./animation/custom_skinned_mesh.rs) | Skinned mesh example with mesh and joints data defined in code.
`gltf_skinned_mesh` | [`animation/gltf_skinned_mesh.rs`](./animation/gltf_skinned_mesh.rs) | Skinned mesh example with mesh and joints data loaded from a glTF file.
`manual_gltf_animation_player` | [`animation/manual_gltf_animation_player.rs`](./animation/manual_gltf_animation_player.rs) | Loads and manually renders a gltf file with animations
`skinned_mesh_wireframe_shadow` | [`animation/skinned_mesh_wireframe_shadow.rs`](./animation/skinned_mesh_wireframe_shadow.rs) | Shadow and wireframe on a skinned mesh.

## Application

Expand Down
118 changes: 118 additions & 0 deletions examples/animation/skinned_mesh_wireframe_shadow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::f32::consts::PI;

use bevy::{
pbr::{
wireframe::{WireframeConfig, WireframePlugin},
AmbientLight,
},
prelude::*,
render::{mesh::skinning::SkinnedMesh, render_resource::WgpuFeatures, settings::WgpuSettings},
};

/// Skinned mesh example with mesh and joints data loaded from a glTF file.
/// Example taken from <https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md>
fn main() {
App::new()
.insert_resource(WgpuSettings {
features: WgpuFeatures::POLYGON_MODE_LINE,
..default()
})
.add_plugins(DefaultPlugins)
.insert_resource(WireframeConfig { global: true })
.add_plugin(WireframePlugin)
.insert_resource(AmbientLight {
brightness: 1.0,
..Default::default()
})
.add_startup_system(setup)
.add_system(joint_animation)
.run();
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Create a camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});

// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 50.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});

// Spawn the first scene in `models/SimpleSkin/SimpleSkin.gltf`
commands.spawn_scene(asset_server.load::<Scene, _>("models/SimpleSkin/SimpleSkin.gltf#Scene0"));

commands.spawn_bundle(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::ZYX,
0.0,
1.0,
-std::f32::consts::FRAC_PI_4,
)),
directional_light: DirectionalLight {
shadow_projection: OrthographicProjection {
left: -10.0,
right: 10.0,
bottom: -10.0,
top: 10.0,
near: -10.0,
far: 10.0,
..default()
},
shadows_enabled: true,
..default()
},
..default()
});
}

/// The scene hierachy currently looks somewhat like this:
///
/// ```ignore
/// <Parent entity>
/// + Mesh node (without `PbrBundle` or `SkinnedMesh` component)
/// + Skinned mesh entity (with `PbrBundle` and `SkinnedMesh` component, created by glTF loader)
/// + First joint
/// + Second joint
/// ```
///
/// In this example, we want to get and animate the second joint.
/// It is similar to the animation defined in `models/SimpleSkin/SimpleSkin.gltf`.
fn joint_animation(
time: Res<Time>,
parent_query: Query<&Parent, With<SkinnedMesh>>,
children_query: Query<&Children>,
mut transform_query: Query<&mut Transform>,
) {
// Iter skinned mesh entity
for skinned_mesh_parent in parent_query.iter() {
// Mesh node is the parent of the skinned mesh entity.
let mesh_node_entity = skinned_mesh_parent.0;
// Get `Children` in the mesh node.
let mesh_node_children = children_query.get(mesh_node_entity).unwrap();

// First joint is the second child of the mesh node.
let first_joint_entity = mesh_node_children[1];
// Get `Children` in the first joint.
let first_joint_children = children_query.get(first_joint_entity).unwrap();

// Second joint is the first child of the first joint.
let second_joint_entity = first_joint_children[0];
// Get `Transform` in the second joint.
let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap();

second_joint_transform.rotation = Quat::from_axis_angle(
Vec3::Z,
0.5 * PI * time.time_since_startup().as_secs_f32().sin(),
);
}
}