bevy/examples/3d
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
..
3d_scene.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
3d_shapes.rs Add binned 2d/3d Wireframe render phase (#18587) 2025-04-09 21:34:53 +00:00
3d_viewport_to_world.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
animated_material.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
anisotropy.rs Upgrade to Rust 1.88 (#19825) 2025-06-26 19:38:19 +00:00
anti_aliasing.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
atmosphere.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
atmospheric_fog.rs Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
auto_exposure.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
blend_modes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
bloom_3d.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
camera_sub_view.rs simplify example, replace get_single to Single Query (#16187) 2024-11-01 18:25:42 +00:00
clearcoat.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
clustered_decals.rs Disable clustered decals on Metal. (#17554) 2025-01-27 05:39:07 +00:00
color_grading.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
decal.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
deferred_rendering.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
depth_of_field.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
edit_material_on_gltf.rs Event Split: Event, EntityEvent, and BufferedEvent (#19647) 2025-06-15 16:46:34 +00:00
fog_volumes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
fog.rs Explanation for the 'classic' fog example (#19196) 2025-06-23 20:55:38 +00:00
generate_custom_mesh.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
irradiance_volumes.rs Make IrradianceVolume require LightProbe (and document this). (#19621) 2025-06-13 17:09:31 +00:00
lighting.rs Upgrade to Rust 1.88 (#19825) 2025-06-26 19:38:19 +00:00
lightmaps.rs Add GltfMeshName component and Deref implementations (#19331) 2025-05-23 20:56:48 +00:00
lines.rs Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
load_gltf_extras.rs Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
load_gltf.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
manual_material.rs Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
mesh_ray_cast.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
meshlet.rs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
mixed_lighting.rs Event Split: Event, EntityEvent, and BufferedEvent (#19647) 2025-06-15 16:46:34 +00:00
motion_blur.rs Remove WebGL padding from MotionBlur (#18727) 2025-04-06 20:00:59 +00:00
occlusion_culling.rs Upgrade to Rust 1.88 (#19825) 2025-06-26 19:38:19 +00:00
order_independent_transparency.rs Relationships (non-fragmenting, one-to-many) (#17398) 2025-01-18 22:20:30 +00:00
orthographic.rs Improve API for scaling orthographic cameras (#15969) 2024-10-17 17:50:06 +00:00
parallax_mapping.rs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
parenting.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
pbr.rs Fix pbr example text rotation (#19571) 2025-06-10 16:57:57 +00:00
pcss.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
post_processing.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
query_gltf_primitives.rs Cosmetic tweaks to query_gltf_primitives (#16102) 2024-10-27 19:06:19 +00:00
reflection_probes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
render_to_texture.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
rotate_environment_map.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
scrolling_fog.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
shadow_biases.rs Fix shadow_biases example (#18303) 2025-03-14 19:50:49 +00:00
shadow_caster_receiver.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
skybox.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
solari.rs bevy_solari: RIS for Direct Lighting (#19620) 2025-06-23 00:47:10 +00:00
specular_tint.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
spherical_area_lights.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
split_screen.rs separate border colors (#18682) 2025-05-26 16:57:13 +00:00
spotlight.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
ssao.rs Upgrade to Rust 1.88 (#19825) 2025-06-26 19:38:19 +00:00
ssr.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
texture.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
tonemapping.rs Rename JustifyText to Justify (#19522) 2025-06-09 19:59:48 +00:00
transmission.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
transparency_3d.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
two_passes.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
update_gltf_scene.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
vertex_colors.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
visibility_range.rs Switch ChildOf back to tuple struct (#18672) 2025-04-02 00:10:10 +00:00
volumetric_fog.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
wireframe.rs Add binned 2d/3d Wireframe render phase (#18587) 2025-04-09 21:34:53 +00:00