Skip to content

Commit 6916236

Browse files
authored
Merge pull request #3 from LykenSol/MdXSzS
Ported "Galaxy of Universes" (https://www.shadertoy.com/view/MdXSzS).
2 parents fa1b979 + dbdf976 commit 6916236

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

shaders/src/galaxy_of_universes.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Ported to Rust from <https://www.shadertoy.com/view/MdXSzS>
2+
//!
3+
//! Original comment:
4+
//! ```glsl
5+
//! // https://www.shadertoy.com/view/MdXSzS
6+
//! // The Big Bang - just a small explosion somewhere in a massive Galaxy of Universes.
7+
//! // Outside of this there's a massive galaxy of 'Galaxy of Universes'... etc etc. :D
8+
//!
9+
//! // To fake a perspective it takes advantage of the screen being wider than it is tall.
10+
//! ```
11+
12+
use shared::*;
13+
use spirv_std::glam::{Mat2, Vec2, Vec3, Vec3Swizzles, Vec4};
14+
15+
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
16+
// we tie #[no_std] above to the same condition, so it's fine.
17+
#[cfg(target_arch = "spirv")]
18+
use spirv_std::num_traits::Float;
19+
20+
pub struct Inputs {
21+
pub resolution: Vec3,
22+
pub time: f32,
23+
}
24+
25+
impl Inputs {
26+
pub fn main_image(&self, frag_color: &mut Vec4, frag_coord: Vec2) {
27+
let uv: Vec2 = (frag_coord / self.resolution.xy()) - Vec2::splat(0.5);
28+
let t: f32 = self.time * 0.1 + (0.25 + 0.05 * (self.time * 0.1).sin());
29+
let si: f32 = t.sin();
30+
let co: f32 = t.cos();
31+
let ma: Mat2 = Mat2::from_cols_array(&[co, si, -si, co]);
32+
33+
let mut v1: f32 = 0.0;
34+
let mut v2: f32 = 0.0;
35+
let mut v3: f32 = 0.0;
36+
37+
let mut s: f32 = 0.0;
38+
39+
let mut i = 0;
40+
while i < 90 {
41+
let mut p: Vec3 = s * uv.extend(0.0);
42+
p = (ma.transpose() * p.xy()).extend(p.z);
43+
p += Vec3::new(0.22, 0.3, s - 1.5 - (self.time * 0.13).sin() * 0.1);
44+
{
45+
let mut i = 0;
46+
while i < 8 {
47+
p = p.abs() / p.dot(p) - Vec3::splat(0.659);
48+
i += 1;
49+
}
50+
}
51+
v1 += p.dot(p) * 0.0015 * (1.8 + ((uv * 13.0).length() + 0.5 - self.time * 0.2).sin());
52+
v2 += p.dot(p) * 0.0013 * (1.5 + ((uv * 14.5).length() + 1.2 - self.time * 0.3).sin());
53+
v3 += (p.xy() * 10.0).length() * 0.0003;
54+
s += 0.035;
55+
i += 1;
56+
}
57+
58+
let len: f32 = uv.length();
59+
v1 *= smoothstep(0.7, 0.0, len);
60+
v2 *= smoothstep(0.5, 0.0, len);
61+
v3 *= smoothstep(0.9, 0.0, len);
62+
63+
let col: Vec3 = Vec3::new(
64+
v3 * (1.5 + (self.time * 0.2).sin() * 0.4),
65+
(v1 + v3) * 0.3,
66+
v2,
67+
) + Vec3::splat(smoothstep(0.2, 0.0, len) * 0.85)
68+
+ Vec3::splat(smoothstep(0.0, 0.6, v3) * 0.3);
69+
70+
*frag_color = col
71+
.abs()
72+
.powf_vec(Vec3::splat(1.2))
73+
.min(Vec3::splat(1.0))
74+
.extend(1.0);
75+
}
76+
}

shaders/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod a_lot_of_spheres;
1111
pub mod a_question_of_time;
1212
pub mod apollonian;
1313
pub mod clouds;
14+
pub mod galaxy_of_universes;
1415
pub mod heart;
1516
pub mod mandelbrot_smooth;
1617
pub mod phantom_star;
@@ -48,7 +49,7 @@ impl Channel for RgbCube {
4849

4950
pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
5051
const COLS: usize = 4;
51-
const ROWS: usize = 3;
52+
const ROWS: usize = 4;
5253

5354
let resolution = Vec3::new(
5455
constants.width as f32 / COLS as f32,
@@ -105,6 +106,7 @@ pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
105106
mouse,
106107
}
107108
.main_image(&mut color, frag_coord),
109+
12 => galaxy_of_universes::Inputs { resolution, time }.main_image(&mut color, frag_coord),
108110
_ => {}
109111
}
110112
pow(color.truncate(), 2.2).extend(color.w)

0 commit comments

Comments
 (0)