Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johanhelsing opened this issue Feb 22, 2022 · 1 comment · May be fixed by #5
Open
Labels
enhancement New feature or request

Comments

@johanhelsing
Copy link
Owner

johanhelsing commented Feb 22, 2022

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.

@johanhelsing johanhelsing added the enhancement New feature or request label Feb 22, 2022
johanhelsing added a commit that referenced this issue Apr 19, 2022
@johanhelsing johanhelsing linked a pull request Apr 19, 2022 that will close this issue
6 tasks
johanhelsing added a commit that referenced this issue May 7, 2022
johanhelsing added a commit that referenced this issue May 9, 2022
johanhelsing added a commit that referenced this issue Aug 16, 2022
johanhelsing added a commit that referenced this issue Aug 16, 2022
samnm pushed a commit to samnm/bevy_smud that referenced this issue Aug 25, 2022
@wainwrightmark
Copy link

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:

  • I couldn't find a way to pass an array of f32 to the Vertex shader so I passed all the params individually. I'm not sure if this affects performance, but it does make passing an arbitrary subset of the params to each function much easier.
  • I had to write some unsafe code (empty impls of Pod and Zeroable for ShapeVertex since the derive macros don't with with const generics). I think this is fine but I'm not 100% sure.
  • I actually would like a way to pass in u32 params as well as f32. I'm getting away with it by using from_bits on the rust side and bitcast<u32> on the other but I have to be careful of NaNs and can't use the full range of possible values. It might actually be better to go the other way round and pass in everything as a u32.
  • I started passing in the position argument to the fill functions. It let me make some nice gradients

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants