0

I have a .glb file and I loaded. Then I want to modify the emissive color.

How is this possible?

fn spawn_main_gltf_scene(mut commands: Commands, asset_server: Res<AssetServer>, mut materials: ResMut<Assets<StandardMaterial>>) {
    for i in 0..26 {
        let path = format!("view_cube/view_cube.glb#Mesh{i}/Primitive0");
        let mesh: Handle<Mesh> = asset_server.load(path);
        let material = asset_server.load(format!("view_cube/view_cube.glb#Material{i}"));

        material.emissive = Color::RED; <-- Problem. How to set the emisive color value?

        commands.spawn((PbrBundle {
                mesh,
                material: material.clone(),
                ..default()
            }, 
            RenderLayers::layer(0), 
            PickableBundle::default()
        ));
    }
}
Mika Vatanen
  • 3,782
  • 1
  • 28
  • 33
George C.
  • 6,574
  • 12
  • 55
  • 80

1 Answers1

1

The load call returns a Handle<StandardMaterial>, if you look at the documentation:

A handle is not the asset itself, but should be seen as a pointer to the asset. Modifying a handle’s id only modifies which asset is being pointed to. To get the actual asset, try using Assets::get or Assets::get_mut.

You'll find you can call Assets::get_mut on it to get mutable access to the underlying asset:

// unfortunately have to wait because `load` will not block until
// the resource is loaded, it would be better if you can change
// the color somewhere else when it's loaded instead
loop {
    if let Some(material) = materials.get_mut(material) {
        material.emissive = Color::RED;
        break;
    }
    std::thread::sleep(std::time::Duration::from_micros(5));
}
cafce25
  • 15,907
  • 4
  • 25
  • 31