From bb4e0aa66907fd41787f7807bf1f17b63d9e6d5e Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 20 Jun 2026 09:20:38 -0400 Subject: [PATCH] Add a coefficients trait for converting the affine coefficient notation `flam3` uses to how `glam` represents it --- enkou-shaders/src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/enkou-shaders/src/lib.rs b/enkou-shaders/src/lib.rs index d2b343a..cfadb05 100644 --- a/enkou-shaders/src/lib.rs +++ b/enkou-shaders/src/lib.rs @@ -2,11 +2,73 @@ use bytemuck::{Pod, Zeroable}; use core::f32::consts::PI; -use glam::{Vec3, Vec4, vec2, vec3}; +use glam::{Affine2, Vec3, Vec4, vec2, vec3}; #[cfg(target_arch = "spirv")] use spirv_std::num_traits::Float; use spirv_std::spirv; +/// Utility trait for [`Affine2`] to convert between `flam3` notation and [`glam`]. +pub trait Coefficients2 { + /// Convert affine transformation coefficients to the [`Affine2`] representation. + /// Parameters use the following form: + /// + /// ```text + /// (a * x + b * y + c, d * x + e * y + f) + /// ``` + /// + /// ``` + /// # use glam::{Affine2, vec2}; + /// # use crate::enkou_shaders::Coefficients2; + /// let coefs = Affine2::from_coefficients(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); + /// let (x, y) = (7.0, 8.0); + /// assert_eq!( + /// coefs.transform_point2(vec2(x, y)), + /// vec2( + /// coefs.a() * x + coefs.b() * y + coefs.c(), + /// coefs.d() * x + coefs.e() * y + coefs.f() + /// ) + /// ); + /// ``` + fn from_coefficients(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Affine2; + + fn a(&self) -> f32; + fn b(&self) -> f32; + fn c(&self) -> f32; + fn d(&self) -> f32; + fn e(&self) -> f32; + fn f(&self) -> f32; +} + +impl Coefficients2 for Affine2 { + fn from_coefficients(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Affine2 { + Affine2::from_cols_array(&[a, d, b, e, c, f]) + } + + fn a(&self) -> f32 { + self.matrix2.x_axis.x + } + + fn b(&self) -> f32 { + self.matrix2.y_axis.x + } + + fn c(&self) -> f32 { + self.translation.x + } + + fn d(&self) -> f32 { + self.matrix2.x_axis.y + } + + fn e(&self) -> f32 { + self.matrix2.y_axis.y + } + + fn f(&self) -> f32 { + self.translation.y + } +} + #[derive(Copy, Clone, Pod, Zeroable)] #[repr(C)] pub struct ShaderConstants {