Skip to content

Commit cf3f26f

Browse files
rendaoeralice-i-cecilerparrett
authored
Add GltfMeshName component and Deref implementations (#19331)
Stores mesh names from glTF files in GltfMeshName component rather than Name component, making both GltfMeshName and GltfMaterialName behave like strings via Deref. # Objective Fixed the side effects of #19287 Fixes Examples that modify gltf materials are broken #19322 ## Solution Add GltfMeshName component and Deref implementations Stores mesh names from glTF files in GltfMeshName component rather than Name component, making both GltfMeshName and GltfMaterialName behave like strings via Deref. ## Testing cargo run --example depth_of_field cargo run --example lightmaps cargo run --example mixed_lighting They are consistent with the situation before the error occurred. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Rob Parrett <robparrett@gmail.com>
1 parent d3012df commit cf3f26f

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-5
lines changed

crates/bevy_gltf/src/assets.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Representation of assets present in a glTF file
22
3+
use core::ops::Deref;
4+
35
#[cfg(feature = "bevy_animation")]
46
use bevy_animation::AnimationClip;
57
use bevy_asset::{Asset, Handle};
@@ -297,6 +299,21 @@ pub struct GltfMeshExtras {
297299
pub value: String,
298300
}
299301

302+
/// The mesh name of a glTF primitive.
303+
///
304+
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-mesh).
305+
#[derive(Clone, Debug, Reflect, Default, Component)]
306+
#[reflect(Component, Clone)]
307+
pub struct GltfMeshName(pub String);
308+
309+
impl Deref for GltfMeshName {
310+
type Target = str;
311+
312+
fn deref(&self) -> &Self::Target {
313+
self.0.as_ref()
314+
}
315+
}
316+
300317
/// Additional untyped data that can be present on most glTF types at the material level.
301318
///
302319
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-extras).
@@ -313,3 +330,11 @@ pub struct GltfMaterialExtras {
313330
#[derive(Clone, Debug, Reflect, Default, Component)]
314331
#[reflect(Component, Clone)]
315332
pub struct GltfMaterialName(pub String);
333+
334+
impl Deref for GltfMaterialName {
335+
type Target = str;
336+
337+
fn deref(&self) -> &Self::Target {
338+
self.0.as_ref()
339+
}
340+
}

crates/bevy_gltf/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ impl Plugin for GltfPlugin {
193193
app.register_type::<GltfExtras>()
194194
.register_type::<GltfSceneExtras>()
195195
.register_type::<GltfMeshExtras>()
196+
.register_type::<GltfMeshName>()
196197
.register_type::<GltfMaterialExtras>()
197198
.register_type::<GltfMaterialName>()
198199
.init_asset::<Gltf>()

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use tracing::{error, info_span, warn};
6666

6767
use crate::{
6868
vertex_attributes::convert_attribute, Gltf, GltfAssetLabel, GltfExtras, GltfMaterialExtras,
69-
GltfMaterialName, GltfMeshExtras, GltfNode, GltfSceneExtras, GltfSkin,
69+
GltfMaterialName, GltfMeshExtras, GltfMeshName, GltfNode, GltfSceneExtras, GltfSkin,
7070
};
7171

7272
#[cfg(feature = "bevy_animation")]
@@ -1463,6 +1463,10 @@ fn load_node(
14631463
});
14641464
}
14651465

1466+
if let Some(name) = mesh.name() {
1467+
mesh_entity.insert(GltfMeshName(name.to_string()));
1468+
}
1469+
14661470
if let Some(name) = material.name() {
14671471
mesh_entity.insert(GltfMaterialName(name.to_string()));
14681472
}

examples/3d/depth_of_field.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bevy::{
1515
dof::{self, DepthOfField, DepthOfFieldMode},
1616
tonemapping::Tonemapping,
1717
},
18+
gltf::GltfMeshName,
1819
pbr::Lightmap,
1920
prelude::*,
2021
render::camera::PhysicalCameraParameters,
@@ -186,7 +187,7 @@ fn tweak_scene(
186187
mut materials: ResMut<Assets<StandardMaterial>>,
187188
mut lights: Query<&mut DirectionalLight, Changed<DirectionalLight>>,
188189
mut named_entities: Query<
189-
(Entity, &Name, &MeshMaterial3d<StandardMaterial>),
190+
(Entity, &GltfMeshName, &MeshMaterial3d<StandardMaterial>),
190191
(With<Mesh3d>, Without<Lightmap>),
191192
>,
192193
) {

examples/3d/lightmaps.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use argh::FromArgs;
44
use bevy::{
55
core_pipeline::prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass},
6+
gltf::GltfMeshName,
67
pbr::{DefaultOpaqueRendererMethod, Lightmap},
78
prelude::*,
89
};
@@ -63,7 +64,7 @@ fn add_lightmaps_to_meshes(
6364
asset_server: Res<AssetServer>,
6465
mut materials: ResMut<Assets<StandardMaterial>>,
6566
meshes: Query<
66-
(Entity, &Name, &MeshMaterial3d<StandardMaterial>),
67+
(Entity, &GltfMeshName, &MeshMaterial3d<StandardMaterial>),
6768
(With<Mesh3d>, Without<Lightmap>),
6869
>,
6970
args: Res<Args>,

examples/3d/mixed_lighting.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Demonstrates how to combine baked and dynamic lighting.
22
33
use bevy::{
4+
gltf::GltfMeshName,
45
pbr::Lightmap,
56
picking::{backend::HitData, pointer::PointerInteraction},
67
prelude::*,
@@ -221,7 +222,7 @@ fn update_lightmaps(
221222
mut commands: Commands,
222223
asset_server: Res<AssetServer>,
223224
mut materials: ResMut<Assets<StandardMaterial>>,
224-
meshes: Query<(Entity, &Name, &MeshMaterial3d<StandardMaterial>), With<Mesh3d>>,
225+
meshes: Query<(Entity, &GltfMeshName, &MeshMaterial3d<StandardMaterial>), With<Mesh3d>>,
225226
mut lighting_mode_change_event_reader: EventReader<LightingModeChanged>,
226227
app_status: Res<AppStatus>,
227228
) {
@@ -432,7 +433,7 @@ fn reset_sphere_position(
432433
fn move_sphere(
433434
mouse_button_input: Res<ButtonInput<MouseButton>>,
434435
pointers: Query<&PointerInteraction>,
435-
mut meshes: Query<(&Name, &ChildOf), With<Mesh3d>>,
436+
mut meshes: Query<(&GltfMeshName, &ChildOf), With<Mesh3d>>,
436437
mut transforms: Query<&mut Transform>,
437438
app_status: Res<AppStatus>,
438439
) {

release-content/migration-guides/rename_spawn_gltf_material_name.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ pull_requests: [19287]
77
When loading a Gltf scene in Bevy, each mesh primitive will generate an entity and store a `GltfMaterialName` component and `Name` component.
88

99
The `Name` components were previously stored as mesh name plus primitive index - for example, `MeshName.0` and `MeshName.1`. To make it easier to view these entities in Inspector-style tools, they are now stored as mesh name plus material name - for example, `MeshName.Material1Name` and `MeshName.Material2Name`.
10+
11+
If you were relying on the previous value of the `Name` component on meshes, use the new `GltfMeshName` component instead.

0 commit comments

Comments
 (0)