bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Ui Anti-aliasing radius setting

Open ickshonpe opened this issue 1 year ago • 1 comments

What problem does this solve or what need does it fill?

The UI AA radius should be be configurable I think to allow for fine-tuning

What solution would you like?

My naive idea was to give the On variant of UiAntiAlias a radius: f32 field, then extract the radius, bitcast the float to an int, put it into a ShaderDefVal and then bitcast it back again to a float in the shader. This might be a terrible thing to do though.

ickshonpe avatar Nov 14 '24 12:11 ickshonpe

i made a rough draft here, where i just changed the rust side (and nothing on the shader side, and no documentation) but i am a little skeptical of these changes, because UiAntiAlias will lose Eq, and users can pass in invalid values like 0, NaN or negative values

my heart told me to keep the radius private like this

pub struct UiAntiAlias {
    radius_bits: u32,
}

impl UiAntiAlias {
    pub const OFF: Self = Self {
        radius_bits: 0f32.to_bits(),
    };

    pub fn new(radius: f32) -> Self {
        if radius <= 0. || !radius.is_finite() {
            return Self::OFF;
        }

        Self {
            radius_bits: radius.to_bits(),
        }
    }

    pub fn radius(self) -> f32 {
        f32::from_bits(self.radius_bits)
    }

    pub fn radius_bits(self) -> u32 {
        self.radius_bits
    }
}

so that UiAntiAlias can keep Eq, and invalid values get guarded against, but it might be a bit extreme However, UiPipelineKey and UiGradientPipelineKey need to store the radius, and they need to be Eq, so passing in -0 or NaN will be a subtle issue otherwise

Wuketuke avatar Jun 03 '25 19:06 Wuketuke