Skip to content

Commit 9d342e5

Browse files
authored
Move gltf stuff in pbr to its own file (#22905)
# Objective - Separation of concerns ## Solution - Move GLTF stuff in pbr to its own files ## Testing - `cargo run --example animated_mesh`
1 parent 0339de9 commit 9d342e5

File tree

2 files changed

+146
-136
lines changed

2 files changed

+146
-136
lines changed

crates/bevy_pbr/src/gltf.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
use bevy_gltf::{
2+
extensions::{GltfExtensionHandler, GltfExtensionHandlers},
3+
gltf, GltfAssetLabel, GltfMaterial,
4+
};
5+
6+
use crate::{MeshMaterial3d, StandardMaterial};
7+
use bevy_app::App;
8+
use bevy_asset::Handle;
9+
use bevy_ecs::prelude::*;
10+
11+
use bevy_asset::LoadContext;
12+
13+
pub(crate) fn add_gltf(app: &mut App) {
14+
#[cfg(target_family = "wasm")]
15+
bevy_tasks::block_on(async {
16+
app.world_mut()
17+
.resource_mut::<GltfExtensionHandlers>()
18+
.0
19+
.write()
20+
.await
21+
.push(Box::new(GltfExtensionHandlerPbr))
22+
});
23+
24+
#[cfg(not(target_family = "wasm"))]
25+
app.world_mut()
26+
.resource_mut::<GltfExtensionHandlers>()
27+
.0
28+
.write_blocking()
29+
.push(Box::new(GltfExtensionHandlerPbr));
30+
}
31+
32+
fn standard_material_from_gltf_material(material: &GltfMaterial) -> StandardMaterial {
33+
StandardMaterial {
34+
base_color: material.base_color,
35+
base_color_channel: material.base_color_channel.clone(),
36+
base_color_texture: material.base_color_texture.clone(),
37+
emissive: material.emissive,
38+
emissive_channel: material.emissive_channel.clone(),
39+
emissive_texture: material.emissive_texture.clone(),
40+
perceptual_roughness: material.perceptual_roughness,
41+
metallic: material.metallic,
42+
metallic_roughness_channel: material.metallic_roughness_channel.clone(),
43+
metallic_roughness_texture: material.metallic_roughness_texture.clone(),
44+
reflectance: material.reflectance,
45+
specular_tint: material.specular_tint,
46+
specular_transmission: material.specular_transmission,
47+
#[cfg(feature = "pbr_transmission_textures")]
48+
specular_transmission_channel: material.specular_transmission_channel.clone(),
49+
#[cfg(feature = "pbr_transmission_textures")]
50+
specular_transmission_texture: material.specular_transmission_texture.clone(),
51+
thickness: material.thickness,
52+
#[cfg(feature = "pbr_transmission_textures")]
53+
thickness_channel: material.thickness_channel.clone(),
54+
#[cfg(feature = "pbr_transmission_textures")]
55+
thickness_texture: material.thickness_texture.clone(),
56+
ior: material.ior,
57+
attenuation_distance: material.attenuation_distance,
58+
attenuation_color: material.attenuation_color,
59+
normal_map_channel: material.normal_map_channel.clone(),
60+
normal_map_texture: material.normal_map_texture.clone(),
61+
occlusion_channel: material.occlusion_channel.clone(),
62+
occlusion_texture: material.occlusion_texture.clone(),
63+
#[cfg(feature = "pbr_specular_textures")]
64+
specular_channel: material.specular_channel.clone(),
65+
#[cfg(feature = "pbr_specular_textures")]
66+
specular_texture: material.specular_texture.clone(),
67+
#[cfg(feature = "pbr_specular_textures")]
68+
specular_tint_channel: material.specular_tint_channel.clone(),
69+
#[cfg(feature = "pbr_specular_textures")]
70+
specular_tint_texture: material.specular_tint_texture.clone(),
71+
clearcoat: material.clearcoat,
72+
clearcoat_perceptual_roughness: material.clearcoat_perceptual_roughness,
73+
#[cfg(feature = "pbr_multi_layer_material_textures")]
74+
clearcoat_roughness_channel: material.clearcoat_roughness_channel.clone(),
75+
#[cfg(feature = "pbr_multi_layer_material_textures")]
76+
clearcoat_roughness_texture: material.clearcoat_roughness_texture.clone(),
77+
#[cfg(feature = "pbr_multi_layer_material_textures")]
78+
clearcoat_normal_channel: material.clearcoat_normal_channel.clone(),
79+
#[cfg(feature = "pbr_multi_layer_material_textures")]
80+
clearcoat_normal_texture: material.clearcoat_normal_texture.clone(),
81+
anisotropy_strength: material.anisotropy_strength,
82+
anisotropy_rotation: material.anisotropy_rotation,
83+
#[cfg(feature = "pbr_anisotropy_texture")]
84+
anisotropy_channel: material.anisotropy_channel.clone(),
85+
#[cfg(feature = "pbr_anisotropy_texture")]
86+
anisotropy_texture: material.anisotropy_texture.clone(),
87+
double_sided: material.double_sided,
88+
cull_mode: material.cull_mode,
89+
unlit: material.unlit,
90+
alpha_mode: material.alpha_mode,
91+
uv_transform: material.uv_transform,
92+
..Default::default()
93+
}
94+
}
95+
96+
#[derive(Default, Clone)]
97+
struct GltfExtensionHandlerPbr;
98+
99+
impl GltfExtensionHandler for GltfExtensionHandlerPbr {
100+
fn dyn_clone(&self) -> Box<dyn GltfExtensionHandler> {
101+
Box::new((*self).clone())
102+
}
103+
fn on_root(&mut self, load_context: &mut LoadContext<'_>, _gltf: &gltf::Gltf) {
104+
// create the `StandardMaterial` for the glTF `DefaultMaterial` so
105+
// it can be accessed when meshes don't have materials.
106+
let std_label = format!("{}#std", GltfAssetLabel::DefaultMaterial);
107+
108+
load_context.add_labeled_asset(
109+
std_label,
110+
standard_material_from_gltf_material(&GltfMaterial::default()),
111+
);
112+
}
113+
114+
fn on_material(
115+
&mut self,
116+
load_context: &mut LoadContext<'_>,
117+
_gltf_material: &gltf::Material,
118+
_material: Handle<GltfMaterial>,
119+
material_asset: &GltfMaterial,
120+
material_label: &str,
121+
) {
122+
let std_label = format!("{}#std", material_label);
123+
124+
load_context.add_labeled_asset(
125+
std_label,
126+
standard_material_from_gltf_material(material_asset),
127+
);
128+
}
129+
130+
fn on_spawn_mesh_and_material(
131+
&mut self,
132+
load_context: &mut LoadContext<'_>,
133+
_primitive: &gltf::Primitive,
134+
_mesh: &gltf::Mesh,
135+
_material: &gltf::Material,
136+
entity: &mut EntityWorldMut,
137+
material_label: &str,
138+
) {
139+
let std_label = format!("{}#std", material_label);
140+
let handle = load_context.get_label_handle::<StandardMaterial>(std_label);
141+
142+
entity.insert(MeshMaterial3d(handle));
143+
}
144+
}

crates/bevy_pbr/src/lib.rs

Lines changed: 2 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ mod atmosphere;
2828
mod cluster;
2929
mod components;
3030
pub mod contact_shadows;
31-
use bevy_gltf::{
32-
extensions::{GltfExtensionHandler, GltfExtensionHandlers},
33-
GltfAssetLabel,
34-
};
31+
mod gltf;
3532
use bevy_render::sync_component::SyncComponent;
3633
pub use contact_shadows::{
3734
ContactShadows, ContactShadowsBuffer, ContactShadowsPlugin, ContactShadowsUniform,
@@ -60,8 +57,6 @@ mod volumetric_fog;
6057
use bevy_color::{Color, LinearRgba};
6158

6259
pub use atmosphere::*;
63-
use bevy_asset::LoadContext;
64-
use bevy_gltf::{gltf, GltfMaterial};
6560
use bevy_light::{AmbientLight, DirectionalLight, PointLight, ShadowFilteringMethod, SpotLight};
6661
use bevy_shader::{load_shader_library, ShaderRef};
6762
pub use cluster::*;
@@ -230,22 +225,7 @@ impl Plugin for PbrPlugin {
230225
.add_plugins((ScatteringMediumPlugin, AtmospherePlugin));
231226

232227
if self.gltf_render_enabled {
233-
#[cfg(target_family = "wasm")]
234-
bevy_tasks::block_on(async {
235-
app.world_mut()
236-
.resource_mut::<GltfExtensionHandlers>()
237-
.0
238-
.write()
239-
.await
240-
.push(Box::new(GltfExtensionHandlerPbr))
241-
});
242-
243-
#[cfg(not(target_family = "wasm"))]
244-
app.world_mut()
245-
.resource_mut::<GltfExtensionHandlers>()
246-
.0
247-
.write_blocking()
248-
.push(Box::new(GltfExtensionHandlerPbr));
228+
gltf::add_gltf(app);
249229
}
250230

251231
if self.add_default_deferred_lighting_plugin {
@@ -385,120 +365,6 @@ pub fn stbn_placeholder() -> Image {
385365
}
386366
}
387367

388-
fn standard_material_from_gltf_material(material: &GltfMaterial) -> StandardMaterial {
389-
StandardMaterial {
390-
base_color: material.base_color,
391-
base_color_channel: material.base_color_channel.clone(),
392-
base_color_texture: material.base_color_texture.clone(),
393-
emissive: material.emissive,
394-
emissive_channel: material.emissive_channel.clone(),
395-
emissive_texture: material.emissive_texture.clone(),
396-
perceptual_roughness: material.perceptual_roughness,
397-
metallic: material.metallic,
398-
metallic_roughness_channel: material.metallic_roughness_channel.clone(),
399-
metallic_roughness_texture: material.metallic_roughness_texture.clone(),
400-
reflectance: material.reflectance,
401-
specular_tint: material.specular_tint,
402-
specular_transmission: material.specular_transmission,
403-
#[cfg(feature = "pbr_transmission_textures")]
404-
specular_transmission_channel: material.specular_transmission_channel.clone(),
405-
#[cfg(feature = "pbr_transmission_textures")]
406-
specular_transmission_texture: material.specular_transmission_texture.clone(),
407-
thickness: material.thickness,
408-
#[cfg(feature = "pbr_transmission_textures")]
409-
thickness_channel: material.thickness_channel.clone(),
410-
#[cfg(feature = "pbr_transmission_textures")]
411-
thickness_texture: material.thickness_texture.clone(),
412-
ior: material.ior,
413-
attenuation_distance: material.attenuation_distance,
414-
attenuation_color: material.attenuation_color,
415-
normal_map_channel: material.normal_map_channel.clone(),
416-
normal_map_texture: material.normal_map_texture.clone(),
417-
occlusion_channel: material.occlusion_channel.clone(),
418-
occlusion_texture: material.occlusion_texture.clone(),
419-
#[cfg(feature = "pbr_specular_textures")]
420-
specular_channel: material.specular_channel.clone(),
421-
#[cfg(feature = "pbr_specular_textures")]
422-
specular_texture: material.specular_texture.clone(),
423-
#[cfg(feature = "pbr_specular_textures")]
424-
specular_tint_channel: material.specular_tint_channel.clone(),
425-
#[cfg(feature = "pbr_specular_textures")]
426-
specular_tint_texture: material.specular_tint_texture.clone(),
427-
clearcoat: material.clearcoat,
428-
clearcoat_perceptual_roughness: material.clearcoat_perceptual_roughness,
429-
#[cfg(feature = "pbr_multi_layer_material_textures")]
430-
clearcoat_roughness_channel: material.clearcoat_roughness_channel.clone(),
431-
#[cfg(feature = "pbr_multi_layer_material_textures")]
432-
clearcoat_roughness_texture: material.clearcoat_roughness_texture.clone(),
433-
#[cfg(feature = "pbr_multi_layer_material_textures")]
434-
clearcoat_normal_channel: material.clearcoat_normal_channel.clone(),
435-
#[cfg(feature = "pbr_multi_layer_material_textures")]
436-
clearcoat_normal_texture: material.clearcoat_normal_texture.clone(),
437-
anisotropy_strength: material.anisotropy_strength,
438-
anisotropy_rotation: material.anisotropy_rotation,
439-
#[cfg(feature = "pbr_anisotropy_texture")]
440-
anisotropy_channel: material.anisotropy_channel.clone(),
441-
#[cfg(feature = "pbr_anisotropy_texture")]
442-
anisotropy_texture: material.anisotropy_texture.clone(),
443-
double_sided: material.double_sided,
444-
cull_mode: material.cull_mode,
445-
unlit: material.unlit,
446-
alpha_mode: material.alpha_mode,
447-
uv_transform: material.uv_transform,
448-
..Default::default()
449-
}
450-
}
451-
452-
#[derive(Default, Clone)]
453-
struct GltfExtensionHandlerPbr;
454-
455-
impl GltfExtensionHandler for GltfExtensionHandlerPbr {
456-
fn dyn_clone(&self) -> Box<dyn GltfExtensionHandler> {
457-
Box::new((*self).clone())
458-
}
459-
fn on_root(&mut self, load_context: &mut LoadContext<'_>, _gltf: &gltf::Gltf) {
460-
// create the `StandardMaterial` for the glTF `DefaultMaterial` so
461-
// it can be accessed when meshes don't have materials.
462-
let std_label = format!("{}#std", GltfAssetLabel::DefaultMaterial);
463-
464-
load_context.add_labeled_asset(
465-
std_label,
466-
standard_material_from_gltf_material(&GltfMaterial::default()),
467-
);
468-
}
469-
470-
fn on_material(
471-
&mut self,
472-
load_context: &mut LoadContext<'_>,
473-
_gltf_material: &gltf::Material,
474-
_material: Handle<GltfMaterial>,
475-
material_asset: &GltfMaterial,
476-
material_label: &str,
477-
) {
478-
let std_label = format!("{}#std", material_label);
479-
480-
load_context.add_labeled_asset(
481-
std_label,
482-
standard_material_from_gltf_material(material_asset),
483-
);
484-
}
485-
486-
fn on_spawn_mesh_and_material(
487-
&mut self,
488-
load_context: &mut LoadContext<'_>,
489-
_primitive: &gltf::Primitive,
490-
_mesh: &gltf::Mesh,
491-
_material: &gltf::Material,
492-
entity: &mut EntityWorldMut,
493-
material_label: &str,
494-
) {
495-
let std_label = format!("{}#std", material_label);
496-
let handle = load_context.get_label_handle::<StandardMaterial>(std_label);
497-
498-
entity.insert(MeshMaterial3d(handle));
499-
}
500-
}
501-
502368
impl SyncComponent<PbrPlugin> for DirectionalLight {
503369
type Out = Self;
504370
}

0 commit comments

Comments
 (0)