#How can i show / hide a GltfMesh dynamically?

6 messages Β· Page 1 of 1 (latest)

graceful orbit
#

I want to load a glb file (3D character) and display it on the game screen. The basic workflow is as follows, and it works fine.

pub fn born(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut query: Query<(Entity, &WillBeBorn)>,
) {
    for (entity, _) in query.iter_mut() {
        let scene: Handle<Scene> = asset_server.load(format!("{}#Scene0", GLB_FILE_NAME));

        let mut entity_commands = commands.entity(entity);

        entity_commands.insert(PbrBundle { ..default() });

        // Add Scene
        entity_commands.with_children(|parent| {
            parent
                .spawn(SceneBundle {
                        scene: scene.clone(),
                        ..default()
                    })
                    .insert(TargetScene);
            });
        }
    }

However, I would like to dynamically hide a specific GltfMesh (for example, hair) from this displayed character.
How can this be achieved?

brazen silo
#

This may not be the best way, but... I did it the other day, so the Gltf has .named_meshes which you can call .get("name of mesh"), which will get you a Handle<GltfMesh> in which you'll basically find everything you need for a MaterialMeshBundle (i.e a bevy::prelude::Mesh and a StandardMaterial) so you could get those, then spawn those as visible/invisible. you could also use it to despawn the one from the original scene -- I've not seen a lot of great tooling for messing with the internals of a scene dynamically 😦 [sorry about the formatting I'm responding from a phone]

#

The trouble (imo) is the SceneBundle's visibility will propagate to all the children (GltfMesh) that it holds, meaning it's binary for all your meshes by default...

#

maybe @exotic linden knows? the bevy/gltf expert πŸ˜‰

exotic linden
#

Thanks for the kind tag @brazen silo πŸ˜„
@graceful orbit I see multiple ways to achieve this:

  • as @brazen silo said, named meshes are practical for things like this, but as he explained very well, changing one mesh changes all
  • you could use Bevy scene hook and add components / toggle things there
  • Imho I think the best solution would be to work with children entities (not meshes) inside your glb, add Tag components to the different parts you want to manipulate (like a Hair/Clothing/Hand Component) , you then then very easilly query for those Tags + Visibility , and control things that way πŸ™‚
    I use this kind of logic all the time together with my tooling (shameless plug, sorry πŸ˜‰ https://github.com/kaosat-dev/Blender_bevy_components_workflow ) , but you can solve this in various ways
GitHub

Ad-hoc component insertion for bevy scenes. Contribute to nicopap/bevy-scene-hook development by creating an account on GitHub.

GitHub

Bevy Code & Blender addon for a simple workflow to add & edit Bevy components in Blender - GitHub - kaosat-dev/Blender_bevy_components_workflow: Bevy Code & Blender addon fo...

brazen silo
#

^^ this is why @exotic linden is ze gltf boss :p