Add a color coordinate to the chaos game
Also refactors the gasket example to avoid using shader entry points; the entry point signatures are not stable, and I don't want to keep refactoring the gasket while adding more advanced color/image features.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//! but produce more interesting images once we add variations.
|
||||
use crate::variation::Variation;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use glam::{Affine2, UVec2, Vec2};
|
||||
use glam::{Affine2, FloatExt, UVec2, Vec2};
|
||||
use rand::Rng;
|
||||
|
||||
/// Affine transform for use in the [`chaos_game`](crate::chaos_game).
|
||||
@@ -15,15 +15,22 @@ pub struct Transform {
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: Vec2,
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
/// Create a new transform from an affine transformation matrix
|
||||
pub fn new(coefficients: Affine2, coefficients_post: Affine2, variation_range: UVec2) -> Self {
|
||||
pub fn new(
|
||||
coefficients: Affine2,
|
||||
coefficients_post: Affine2,
|
||||
variation_range: UVec2,
|
||||
color: Vec2,
|
||||
) -> Self {
|
||||
Transform {
|
||||
coefficients,
|
||||
coefficients_post,
|
||||
variation_range,
|
||||
color,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +54,11 @@ impl Transform {
|
||||
|
||||
self.coefficients_post.transform_point2(point)
|
||||
}
|
||||
|
||||
/// Apply this transform to a color
|
||||
pub fn transform_color(&self, color: f32) -> f32 {
|
||||
color.lerp(self.color.x, self.color.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -54,7 +66,7 @@ mod test {
|
||||
use crate::rng::xoshiro256starstar_from_seed;
|
||||
use crate::transform::Transform;
|
||||
use crate::variation::{Variation, VariationKind};
|
||||
use glam::{Affine2, uvec2, vec2};
|
||||
use glam::{Affine2, uvec2, vec2, Vec2};
|
||||
|
||||
#[test]
|
||||
fn transform_scaling() {
|
||||
@@ -63,6 +75,7 @@ mod test {
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
Affine2::IDENTITY,
|
||||
uvec2(0, 1),
|
||||
Vec2::ZERO,
|
||||
);
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed([0; 32]);
|
||||
@@ -78,11 +91,12 @@ mod test {
|
||||
#[test]
|
||||
fn transform_scaling_post() {
|
||||
let scale_coefficients = vec2(2.0, 0.5);
|
||||
let transform_pdj = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1));
|
||||
let transform_pdj = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), Vec2::ZERO);
|
||||
let transform_pdj_post = Transform::new(
|
||||
Affine2::IDENTITY,
|
||||
Affine2::from_scale(scale_coefficients),
|
||||
uvec2(0, 1),
|
||||
Vec2::ZERO,
|
||||
);
|
||||
|
||||
let mut rng = xoshiro256starstar_from_seed([0; 32]);
|
||||
@@ -94,4 +108,23 @@ mod test {
|
||||
|
||||
assert_eq!(point_pdj * scale_coefficients, point_pdj_post);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transform_color() {
|
||||
// Color 0.5, color speed 1.0, so color value will always be 0.5 after transform
|
||||
let color = vec2(0.5, 1.0);
|
||||
let transform = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), color);
|
||||
|
||||
assert_eq!(transform.transform_color(0.0), 0.5);
|
||||
assert_eq!(transform.transform_color(1.0), 0.5);
|
||||
assert_eq!(transform.transform_color(2.0), 0.5);
|
||||
|
||||
// Color 1.0, color speed 0.5, so color value moves to halfway between current and 1.0
|
||||
let color = vec2(1.0, 0.5);
|
||||
let transform = Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), color);
|
||||
|
||||
assert_eq!(transform.transform_color(0.0), 0.5);
|
||||
assert_eq!(transform.transform_color(1.0), 1.0);
|
||||
assert_eq!(transform.transform_color(2.0), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user