Skip to content

Dynamic UI Materials#20895

Open
amytimed wants to merge 5 commits into
bevyengine:mainfrom
amytimed:selfish-ui-materials
Open

Dynamic UI Materials#20895
amytimed wants to merge 5 commits into
bevyengine:mainfrom
amytimed:selfish-ui-materials

Conversation

@amytimed
Copy link
Copy Markdown
Contributor

@amytimed amytimed commented Sep 5, 2025

Objective

  • The objective is to allow having a dynamic material for UI, where different instances of it can have a different shader only known at runtime, instead of hardcoding a single shader at compile time
    • In my case, the utility is in my project I am adding background: shader(...) to CSS stylesheets that style Bevy UI. This requires dynamic shaders

Solution

  • UIMaterial's vertex_shader and fragment_shader functions get a &self parameter, and the code now calls those with the self and understands that the shader may vary

Testing

  • Tested in my codebase with 2 instances of the same material, but each with different shader (shown in showcase)
  • Tested ui_material example and it still works fine

Showcase

#[derive(AsBindGroup, Asset, TypePath, Debug, Clone)]
struct CustomUiMaterial {
    shader_handle: Handle<Shader>,
}

impl UiMaterial for CustomUiMaterial {
    fn fragment_shader(&self) -> bevy::shader::ShaderRef {
        self.shader_handle.clone().into() // intuitive dynamic materials !!!
    }
}

Migration

Users would just need to add a &self to their UI materials

@alice-i-cecile alice-i-cecile changed the title Selfish UI Materials Dynamic UI Materials Sep 5, 2025
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 5, 2025
Comment thread crates/bevy_ui_render/src/ui_material_pipeline.rs Outdated
Comment thread crates/bevy_ui_render/src/ui_material_pipeline.rs Outdated
amytimed and others added 2 commits September 5, 2025 20:46
Co-authored-by: Sludge <sludge.phd+gh@gmail.com>
Co-authored-by: Sludge <sludge.phd+gh@gmail.com>
@janhohenheim janhohenheim added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Sep 6, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 6, 2025

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

Copy link
Copy Markdown
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like how this introduces a difference with Material and Material2d. Also, I'm not sure of the potential side effects of adding 'static to the trait bound.

@amytimed
Copy link
Copy Markdown
Contributor Author

amytimed commented Sep 6, 2025

I don't like how this introduces a difference with Material and Material2d

i think the best solution is to give them the same change

but that seems more suited for separate PRs

@tychedelia
Copy link
Copy Markdown
Member

Hi! I'm definitely interested in supporting this use case, and indeed it was one of my original motivations for #14856 which was the work that inspired #19667. In the case of 3d materials, I discovered that adding the &self param resulted in a significant performance regression. While it's likely less severe in ui given the relatively smaller sets of entities (particularly with materials) at play, I'm reluctant to make this change until we can make it across all 3 material types at the same time. The good news is that this work is planned as a high priority for 0.18 and will allow us to be able to support this without any risk of performance problems. :)

@amytimed
Copy link
Copy Markdown
Contributor Author

amytimed commented Sep 6, 2025

okie dokie, that's fine, since bevy is modular I'm able to use this version instead of bevy_ui_render without forking all of bevy which is great 🕊️

@cart cart added this to UI Feb 12, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in UI Feb 12, 2026
@cart cart closed this May 5, 2026
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in UI May 5, 2026
@cart cart reopened this May 5, 2026
@github-project-automation github-project-automation Bot moved this from Done to Needs SME Triage in UI May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage
Status: No status

Development

Successfully merging this pull request may close these issues.

7 participants