Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/load_gltf_extras.rs
6592 views
1
//! Loads and renders a glTF file as a scene, and list all the different `gltf_extras`.
2
3
use bevy::{
4
gltf::{GltfExtras, GltfMaterialExtras, GltfMeshExtras, GltfSceneExtras},
5
prelude::*,
6
};
7
8
fn main() {
9
App::new()
10
.add_plugins(DefaultPlugins)
11
.add_systems(Startup, setup)
12
.add_systems(Update, check_for_gltf_extras)
13
.run();
14
}
15
16
#[derive(Component)]
17
struct ExampleDisplay;
18
19
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
20
commands.spawn((
21
Camera3d::default(),
22
Transform::from_xyz(2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
23
));
24
25
commands.spawn(DirectionalLight {
26
shadows_enabled: true,
27
..default()
28
});
29
30
// a barebones scene containing one of each gltf_extra type
31
commands.spawn(SceneRoot(asset_server.load(
32
GltfAssetLabel::Scene(0).from_asset("models/extras/gltf_extras.glb"),
33
)));
34
35
// a place to display the extras on screen
36
commands.spawn((
37
Text::default(),
38
TextFont {
39
font_size: 15.,
40
..default()
41
},
42
Node {
43
position_type: PositionType::Absolute,
44
top: px(12),
45
left: px(12),
46
..default()
47
},
48
ExampleDisplay,
49
));
50
}
51
52
fn check_for_gltf_extras(
53
gltf_extras_per_entity: Query<(
54
Entity,
55
Option<&Name>,
56
Option<&GltfSceneExtras>,
57
Option<&GltfExtras>,
58
Option<&GltfMeshExtras>,
59
Option<&GltfMaterialExtras>,
60
)>,
61
mut display: Single<&mut Text, With<ExampleDisplay>>,
62
) {
63
let mut gltf_extra_infos_lines: Vec<String> = vec![];
64
65
for (id, name, scene_extras, extras, mesh_extras, material_extras) in
66
gltf_extras_per_entity.iter()
67
{
68
if scene_extras.is_some()
69
|| extras.is_some()
70
|| mesh_extras.is_some()
71
|| material_extras.is_some()
72
{
73
let formatted_extras = format!(
74
"Extras per entity {} ('Name: {}'):
75
- scene extras: {:?}
76
- primitive extras: {:?}
77
- mesh extras: {:?}
78
- material extras: {:?}
79
",
80
id,
81
name.unwrap_or(&Name::default()),
82
scene_extras,
83
extras,
84
mesh_extras,
85
material_extras
86
);
87
gltf_extra_infos_lines.push(formatted_extras);
88
}
89
display.0 = gltf_extra_infos_lines.join("\n");
90
}
91
}
92
93