bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Allow UiMaterial to provide a custom draw function

Open Davier opened this issue 1 year ago • 3 comments
trafficstars

Objective

The UiMaterial abstraction can currently be used to have custom shaders and one custom bind group. By allowing the material to select its own draw function, it could freely define all ~~bind groups and~~ vertex buffers, while reusing all the parts of the pipeline that it wants. (Edit: adding more bind groups will require more than this PR)

Solution

Add a method to the UiMaterial trait that returns the draw function's ID, with a default implementation.

Make the members of UiMaterialMeta public so that they can be reused.


Changelog

Allow UiMaterials to provide a custom draw function

Davier avatar Nov 30 '23 13:11 Davier

Should we make similar changes to other material code?

I honestly don't know, which is why I didn't do it. I would need to try it to see how it interacts with meshes, which parts of the pipelines need to be made public, and if it is usable in the end. I think they deserve their own PRs.

Also, while this PR is sufficient to add vertex buffers, I was too optimistic in the description. It seems that more will be needed to allow for additional bind groups. I'm not sure if this should block this PR or not, since it's already useful on its own.

Davier avatar Nov 30 '23 17:11 Davier

I tried to gather my thoughts.

Vertex buffers

The Material and Material2d pipelines use the Mesh and Mesh2d pipelines, which make it possible to configure the mesh vertex buffer. The UiMaterial pipeline hard-codes its vertex buffer. Being able to add arbitrary vertex buffers makes sense for all of them, but it's much more critical for UiMaterials.

Bind groups

The 3d materials have ExtendedMaterial that can add bindings to the material bind group. It cannot add extra bind groups. I believe its main reason to exist is to make it easier to extend the PBR material (StandardMaterial). I don't think that we should add ExtendedMaterial2d and ExtendedUiMaterial for now, since ColorMaterial is so simple, and there are no implementations of UiMaterial (except the example). Like for vertex buffers, being able to add arbitrary bind groups could be useful for all three traits. What this PR does is definitely a pre-requisite, but it falls short of enabling it. I'm searching for an ergonomic way to do it, but I'd prefer it to be its own PR since it will be less straightforward.

Other thoughts

Draw functions were always meant to be a building block, it's unfortunate that the materials currently hard-code them.

It would be great in the future to de-duplicate the three material traits (and I wouldn't mind doing the groundwork myself if there is consensus that we want it now).

TL;DR

I'm fine with the current PR :smile:

Davier avatar Dec 01 '23 19:12 Davier

@Davier Sorry, never saw that PR when it was made. Wouldn't it be possible to have a SpecializedUiPipeline? It would be more in-line with the other rendering pipelines inside bevy and I think it would give you what you need? It would definitely be way more code though and the current approach is kinda nice since it makes it really flexible.

IceSentry avatar May 13 '24 19:05 IceSentry