#Assigning new material to a GLTF mesh

5 messages · Page 1 of 1 (latest)

young current
#

I am trying to assign a new custom material to an GLTF grass mesh yet I couldn't even achieve assigning a StandartMaterial. Whenever I insert or remove an MeshMaterial3d component the becomes invisible.

How I spawn GLTF

#[derive(Component)]
struct WillGetCustomMaterial;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {  commands.spawn((SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("grass/MeshGrass.gltf"))),
        WillGetCustomMaterial,
    ));
}

My observer;

fn change_material(
    scene_ready: On<SceneInstanceReady>,
    mut commands: Commands,
    will_get_material_q: Query<&WillGetCustomMaterial>,
    children_q: Query<&Children>,
    mesh_entities_q: Query<(&MeshMaterial3d<StandardMaterial>, &GltfMaterialName)>,
    mut standard_materials: ResMut<Assets<StandardMaterial>>,
) {
    if will_get_material_q.get(scene_ready.entity).is_err() {
        return;
    }

    for child_entity in children_q.iter_descendants(scene_ready.entity) {
        dbg!(child_entity);
        if let Ok((id, material_name)) = mesh_entities_q.get(child_entity) {
            match material_name.0.as_str() {
                "InstancedGrassMat" => {
                    // Reaches here
                    info!("Found mesh child {:?}, changing material...", child_entity);

                    // Uncommenting any of these two make gltf mesh disappear
                    // commands
                    //     .entity(child_entity)
                    //     .remove::<MeshMaterial3d<StandardMaterial>>();

                    // commands.entity(child_entity).insert(MeshMaterial3d(
                    //     standard_materials.add(Color::srgb(0.4, 0.4, 0.1)),
                    // ));
                }
                name => {
                    info!("not replacing: {name}");
                }
            }
        }
    }
    commands
        .entity(scene_ready.entity)
        .remove::<WillGetCustomMaterial>();
}
glass mango
#

can you post the gltf?

young current
#

You are right on the problem. Sorry for taking your time

#

I tried the example helmet model and it worked. Problem was my grass model had its normals reversed and its material had culling to none