johanhelsing / bevy_smud

Sdf 2d shape renderer for Bevy
Other
143 stars 10 forks source link

Pass custom instance data to sdf shader function (to allow single-shader parametrized shapes) #3

Open johanhelsing opened 2 years ago

johanhelsing commented 2 years ago

For some shapes, for instance rectangles, circles, rounded rectangels, it makes a lot of sense to be able to send per instance data into the sdf function so you could change side length/radius without compiling a new shader.

I'm not really sure what the best way to implement this would be... One simple way could be to just have a couple of floats of "user data" that will just always be passed around. It may come with a performance cost, though.

Perhaps some way to toggle it with shader defs would be good, and then have it be an optional component SdfParameters(Vec4) so we don't pay the price for it when not used.

wainwrightmark commented 11 months ago

Hey. Just letting you know, that I've had a go at doing this with const generics and it's working great for me but my implementation probably isn't ergonomic enough for general use.

Basically I added a const PARAMS: usize everywhere and you set that to highest number of params you need for any of your sdf/fill combinations. I also added a ShaderParamUsage argument which controls which params are sent to the sdf and which to the fill. It does what I need it to do but is a bit of a pain to use and it probably isn't optimal performance wise so I'm definitely not suggesting this be merged or that this approach is even the right one, just wanted to share in case this was useful to anyone.

My code is here but

Just some notes:

image