Add a coefficients trait for converting the affine coefficient notation flam3 uses to how glam represents it

This commit is contained in:
2026-06-20 09:20:38 -04:00
parent 5603f19c22
commit bb4e0aa669
+63 -1
View File
@@ -2,11 +2,73 @@
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use core::f32::consts::PI; use core::f32::consts::PI;
use glam::{Vec3, Vec4, vec2, vec3}; use glam::{Affine2, Vec3, Vec4, vec2, vec3};
#[cfg(target_arch = "spirv")] #[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float; use spirv_std::num_traits::Float;
use spirv_std::spirv; 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)] #[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct ShaderConstants { pub struct ShaderConstants {