17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
907 stars 63 forks source link

Custom blend modes #296

Closed davideGiovannini closed 2 years ago

davideGiovannini commented 2 years ago

I am using a texture to store lighting information (like in this article) in each RGBA channel and in order to write/erase light I need two custom blendmodes.

            store_blend_mode: BlendMode::Custom {
                equation: BlendEquation::Add,
                src_rgb: BlendFactor::One,
                src_alpha: BlendFactor::One,
                dst_rgb: BlendFactor::One,
                dst_alpha: BlendFactor::One
            },

            erase_blend_mode: BlendMode::Custom {
                equation: BlendEquation::ReverseSubtract,
                src_rgb: BlendFactor::One,
                src_alpha: BlendFactor::One,
                dst_rgb: BlendFactor::One,
                dst_alpha: BlendFactor::One
            }
17cupsofcoffee commented 2 years ago

This seems like a good thing to add :+1: The only thing I'm not sure about is the naming of BlendModeCustomParam (feels a bit overly wordy) - I'll have a look at what other frameworks call that.

I think in 0.7 it might make sense to just have one 'BlendMode' struct with these fields, and then make stuff like BlendMode::Add a constant/function that returns a preset? That way there's not two ways of doing the same thing.

It's also worth noting that this is technically a breaking change, due to #276 - that said, I think it's fairly unlikely to cause issues in practice. I do have quite a few breaking changes/deprecations building up in my backlog though, so maybe I should just do a 0.7 soon anyway? I'll give that some thought, don't think it'll block this either way 🙂

davideGiovannini commented 2 years ago

BlendModeCustomParam (feels a bit overly wordy) 100% agree, it was the first thing I could think of and forgot to change.

Maybe BlendModeMultiplier or BlendModeFactor ?

17cupsofcoffee commented 2 years ago

BlendFactor is what other graphics APIs (and the OpenGL spec) seem to call it (I think we can leave mode out of the name). Similarly, BlendEquation or BlendOperation seems to be the common name for what we currently have as BlendModeEquation.

17cupsofcoffee commented 2 years ago

I got completely nerd sniped by this PR and ended up implementing the new blending API I had in mind for 0.7 😅

It's on the 0.7 branch now, if you want to use it/cherry pick onto your own fork. Let me know if you have any feedback or if there's any bits that don't work for your use case :)

davideGiovannini commented 2 years ago

I got completely nerd sniped by this PR and ended up implementing the new blending API I had in mind for 0.7 sweat_smile

It's on the 0.7 branch now, if you want to use it/cherry pick onto your own fork. Let me know if you have any feedback or if there's any bits that don't work for your use case :)

Just tried it out, everything seems to be working :+1:

Thanks :smile:

17cupsofcoffee commented 2 years ago

No problem - thank you for finally forcing me to learn how blending works 😆