Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_gltf/src/loader/gltf_ext/mod.rs
6598 views
1
//! Methods to access information from [`gltf`] types
2
3
pub mod material;
4
pub mod mesh;
5
pub mod scene;
6
pub mod texture;
7
8
use bevy_platform::collections::HashSet;
9
10
use fixedbitset::FixedBitSet;
11
use gltf::{Document, Gltf};
12
13
use super::GltfError;
14
15
use self::{material::extension_texture_index, scene::check_is_part_of_cycle};
16
17
#[cfg_attr(
18
not(target_arch = "wasm32"),
19
expect(
20
clippy::result_large_err,
21
reason = "need to be signature compatible with `load_gltf`"
22
)
23
)]
24
/// Checks all glTF nodes for cycles, starting at the scene root.
25
pub(crate) fn check_for_cycles(gltf: &Gltf) -> Result<(), GltfError> {
26
// Initialize with the scene roots.
27
let mut roots = FixedBitSet::with_capacity(gltf.nodes().len());
28
for root in gltf.scenes().flat_map(|scene| scene.nodes()) {
29
roots.insert(root.index());
30
}
31
32
// Check each one.
33
let mut visited = FixedBitSet::with_capacity(gltf.nodes().len());
34
for root in roots.ones() {
35
let Some(node) = gltf.nodes().nth(root) else {
36
unreachable!("Index of a root node should always exist.");
37
};
38
check_is_part_of_cycle(&node, &mut visited)?;
39
}
40
41
Ok(())
42
}
43
44
pub(crate) fn get_linear_textures(document: &Document) -> HashSet<usize> {
45
let mut linear_textures = HashSet::default();
46
47
for material in document.materials() {
48
if let Some(texture) = material.normal_texture() {
49
linear_textures.insert(texture.texture().index());
50
}
51
if let Some(texture) = material.occlusion_texture() {
52
linear_textures.insert(texture.texture().index());
53
}
54
if let Some(texture) = material
55
.pbr_metallic_roughness()
56
.metallic_roughness_texture()
57
{
58
linear_textures.insert(texture.texture().index());
59
}
60
if let Some(texture_index) =
61
extension_texture_index(&material, "KHR_materials_anisotropy", "anisotropyTexture")
62
{
63
linear_textures.insert(texture_index);
64
}
65
66
// None of the clearcoat maps should be loaded as sRGB.
67
#[cfg(feature = "pbr_multi_layer_material_textures")]
68
for texture_field_name in [
69
"clearcoatTexture",
70
"clearcoatRoughnessTexture",
71
"clearcoatNormalTexture",
72
] {
73
if let Some(texture_index) =
74
extension_texture_index(&material, "KHR_materials_clearcoat", texture_field_name)
75
{
76
linear_textures.insert(texture_index);
77
}
78
}
79
}
80
81
linear_textures
82
}
83
84