Add an initial implementation of the chaos game

This commit is contained in:
2026-06-20 10:05:04 -04:00
parent bb4e0aa669
commit 1709336062
4 changed files with 67 additions and 4 deletions
+3 -2
View File
@@ -10,6 +10,7 @@ repository.workspace = true
workspace = true
[dependencies]
spirv-std.workspace = true
glam.workspace = true
bytemuck.workspace = true
glam.workspace = true
rand.workspace = true
spirv-std.workspace = true
+45 -1
View File
@@ -1,8 +1,12 @@
//! # Enkou
#![no_std]
#![warn(missing_docs)]
use bytemuck::{Pod, Zeroable};
use core::f32::consts::PI;
use glam::{Affine2, Vec3, Vec4, vec2, vec3};
use glam::{Affine2, Vec3, Vec4, vec2, vec3, Vec2};
use rand::{Rng, RngExt};
use rand::distr::StandardUniform;
#[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float;
use spirv_std::spirv;
@@ -69,6 +73,46 @@ impl Coefficients2 for Affine2 {
}
}
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct Transform {
pub coefficients: Affine2,
}
impl Transform {
pub fn new(coefficients: Affine2) -> Self {
Transform { coefficients }
}
pub fn transform_point(&self, point: Vec2) -> Vec2 {
self.coefficients.transform_point2(point)
}
}
/// Iterate one step in the chaos game; choose the next transform, apply it,
/// and return the resulting point. Also returns the transform index so that
/// path-dependent weights (the "Xaos" table in Apophysis) can be chosen
/// for the next iteration step.
///
/// # Arguments
///
/// * `weights` - Weights are assumed to be normalized; adding all elements together should return the value 1
pub fn step_chaos_game<R: Rng>(rng: &mut R, point: Vec2, weights: &[f32], transforms: &[Transform]) -> (Vec2, u32) {
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
let mut transform_index: u32 = 0;
for weight in weights {
choice_weight -= weight;
if choice_weight <= 0.0 {
break;
}
transform_index += 1;
}
(transforms[transform_index as usize].transform_point(point), transform_index)
}
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct ShaderConstants {