diff options
author | pommicket <pommicket@gmail.com> | 2022-12-15 19:35:52 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-15 19:35:52 -0500 |
commit | 35cbbb40298389efcd2fe87a9c6458d49c1c567e (patch) | |
tree | d96318ef6945ab9775d5df5534fcbb7e1175fccb | |
parent | ae29a61c9917da5ad9fbb7a24151bff506669ffb (diff) |
add torus, box frame
-rw-r--r-- | src/main.rs | 20 | ||||
-rw-r--r-- | src/sdf.rs | 30 |
2 files changed, 46 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 41a6d7b..2c42e97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ @TODO: - auto-select level set by sampling a bunch of points - bring time back, w pause/rewind/adjust time speed (start out paused?) -- Params instead of depth +- Params instead of depth for GenRandom + - allow multiple endpoints (cube & sphere & ...) - seed control (maybe save seeds to a file then let user go back&forth through past sdfs) - fullscreen key - mathematical analysis @@ -17,6 +18,7 @@ ----- cool seeds: +commit ae29a61c9917da5ad9fbb7a24151bff506669ffb 18413841503509874975 **17878446840930313726 */ @@ -102,6 +104,22 @@ float smooth_min(float a, float b, float k) { float h = max(k-abs(a-b), 0.0)/k; return min(a, b) - h*h*h*k*(1.0/6.0); } + +// thanks to https://iquilezles.org/articles/distfunctions/ + +float sdf_box_frame( vec3 p, vec3 b, float e ) { + p = abs(p )-b; + vec3 q = abs(p+e)-e; + return min(min( + length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), + length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), + length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); +} + +float sdf_torus(vec3 p, vec2 t) { + vec2 q = vec2(length(p.xy)-t.x,p.z); + return length(q)-t.y; +} ", ); my_sdf.to_glsl_function("sdf", &mut fshader_source); @@ -114,11 +114,11 @@ pub enum R3ToR3 { #[derive(GenRandom, Debug)] pub enum RToR { - #[prob(0)] + #[prob(1)] Identity, - #[prob(2)] + #[prob(0)] Compose(Box<RToR>, Box<RToR>), - #[prob(2)] + #[prob(0)] Subtract(Constant), } @@ -128,6 +128,20 @@ pub enum R3ToR { Sphere(Constant), #[prob(1)] Cube(Constant), + #[prob(1)] + BoxFrame { + #[scale(3.0)] + size: Constant, + #[scale(0.2)] + thickness: Constant + }, + #[prob(1)] + Torus { + #[scale(3.0)] + radius: Constant, + #[scale(0.2)] + thickness: Constant + }, #[prob(8)] Compose(Box<R3ToR3>, Box<R3ToR>, Box<RToR>), #[prob(4)] @@ -388,6 +402,16 @@ impl Function for R3ToR { ); output } + BoxFrame { size, thickness } => { + let output = var.next(); + write_str!(code, "float {output} = sdf_box_frame({input}, vec3({size}), {thickness});\n"); + output + } + Torus { radius, thickness } => { + let output = var.next(); + write_str!(code, "float {output} = sdf_torus({input}, vec2({radius}, {thickness}));\n"); + output + } Mix(a, b, t) => { let a_output = a.to_glsl(input, code, var); let b_output = b.to_glsl(input, code, var); |