#Correct way to modify color of PBR

1 messages · Page 1 of 1 (latest)

lavish holly
#

I'm trying to create a component and system which adds a flashing animation to some bundles. I've the general idea, but I'm not sure the correct way to update the material color of the PBR in the system.

cursor.selection.debug_box_entity = Some(
            commands
                .spawn((PbrBundle {
                    material: materials.add(StandardMaterial {
                        alpha_mode: AlphaMode::Blend,
                        base_color: SELECTED_AREA_BOX_COLOR,
                        emissive: SELECTED_AREA_BOX_COLOR,
                        unlit: false,
                        ..default()
                    }),
                    ..default()
                },))
                .insert(Blinker {
                    timer: Timer::from_seconds(0.3, TimerMode::Once),
                    speed: 2.,
                    number_of_blinks: 2,
                })
...
pub fn blink_system(
    mut commands: Commands,
    mut entities: Query<(Entity, &mut Blinker)>,
    time: Res<Time>,
) {
    for (entity, mut blinker) in &mut entities {
        blinker.timer.tick(time.delta());
        // Need to reference here to the material. 🤔
    }
}

Sorry if I missed this in in the docs.

midnight lotus
#

The entity has a Handle<StandardMaterial> component that is part of the PbrBundle. You need to add this component to your query. Then you can look up the actual StandardMaterial asset by including mut materials: ResMut<Assets<StandardMaterial>> in your system parameters and then using materials.get_mut(handle) to get (if it exists) a mutable reference to the StandardMaterial so you can modify it.

patent perch
#

Note that it will update all entities with the given material. But yeah, that's the way of doing it.