bevy/assets
charlotte 🌸 e6ba9a6d18
Type erased materials (#19667)
# Objective

Closes #18075

In order to enable a number of patterns for dynamic materials in the
engine, it's necessary to decouple the renderer from the `Material`
trait.

This opens the possibility for:
- Materials that aren't coupled to `AsBindGroup`.
- 2d using the underlying 3d bindless infrastructure.
- Dynamic materials that can change their layout at runtime.
- Materials that aren't even backed by a Rust struct at all.

## Solution

In short, remove all trait bounds from render world material systems and
resources. This means moving a bunch of stuff onto `MaterialProperties`
and engaging in some hacks to make specialization work. Rather than
storing the bind group data in `MaterialBindGroupAllocator`, right now
we're storing it in a closure on `MaterialProperties`. TBD if this has
bad performance characteristics.

## Benchmarks

- `many_cubes`:
`cargo run --example many_cubes --release --features=bevy/trace_tracy --
--vary-material-data-per-instance`:
![Screenshot 2025-06-26
235426](https://github.com/user-attachments/assets/10a0ee29-9932-4f91-ab43-33518b117ac5)

- @DGriffin91's Caldera
`cargo run --release --features=bevy/trace_tracy -- --random-materials`

![image](https://github.com/user-attachments/assets/ef91ba6a-8e88-4922-a73f-acb0af5b0dbc)


- @DGriffin91's Caldera with 20 unique material types (i.e.
`MaterialPlugin<M>`) and random materials per mesh
`cargo run --release --features=bevy/trace_tracy -- --random-materials`
![Screenshot 2025-06-27
000425](https://github.com/user-attachments/assets/9561388b-881d-46cf-8c3d-b15b3e9aedc7)


### TODO

- We almost certainly lost some parallelization from removing the type
params that could be gained back from smarter iteration.
- Test all the things that could have broken.
- ~Fix meshlets~

## Showcase

See [the
example](https://github.com/bevyengine/bevy/pull/19667/files#diff-9d768cfe1c3aa81eff365d250d3cbe5a63e8df63e81dd85f64c3c3cd993f6d94)
for a custom material implemented without the use of the `Material`
trait and thus `AsBindGroup`.


![image](https://github.com/user-attachments/assets/e3fcca7c-e04e-4a4e-9d89-39d697a9e3b8)

---------

Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Co-authored-by: IceSentry <c.giguere42@gmail.com>
2025-06-27 22:57:24 +00:00
..
android-res/mipmap-mdpi android example polish (#845) 2020-11-11 16:31:16 -08:00
animation_graphs Implement additive blending for animation graphs. (#15631) 2024-10-04 22:13:22 +00:00
branding Initial raytraced lighting progress (bevy_solari) (#19058) 2025-06-12 21:26:10 +00:00
cursors/kenney_crosshairPack Support texture atlases in CustomCursor::Image (#17121) 2025-01-14 22:27:24 +00:00
data Added Support for Extension-less Assets (#10153) 2024-01-31 14:58:08 +00:00
docs Improve Mesh documentation (#9061) 2023-07-31 18:55:42 +00:00
environment_maps Implement percentage-closer soft shadows (PCSS). (#13497) 2024-09-18 18:07:17 +00:00
external Add external assets to .gitignore (#17388) 2025-01-17 01:20:14 +00:00
fonts Add the license for the FiraMono font (#3589) 2022-05-06 19:29:43 +00:00
irradiance_volumes Implement irradiance volumes. (#10268) 2024-02-06 23:23:20 +00:00
lightmaps Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
models Fix validation errors in Fox.glb (#17801) 2025-02-11 22:19:24 +00:00
pixel Add example for pixel-perfect grid snapping in 2D (#8112) 2023-12-26 17:15:50 +00:00
scenes Make entity::index non max (#18704) 2025-05-07 18:20:30 +00:00
shaders Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
sounds Programmed soundtrack example (#12774) 2024-03-29 20:32:30 +00:00
textures New cooldown example (#19234) 2025-05-26 19:44:17 +00:00
volumes Allow fog density texture to be scrolled over time with an offset (#14868) 2024-08-22 19:43:14 +00:00