-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshadertoy_star.py
103 lines (79 loc) · 2.75 KB
/
shadertoy_star.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# test_example = true
from wgpu_shadertoy import Shadertoy
# shadertoy source: https://www.shadertoy.com/view/XlfGRj by Kali MIT
# ported to wgsl
shader_code = """
// Star Nest by Pablo Roman Andrioli
// License: MIT
const iterations = 17;
const formuparam = 0.53;
const volsteps = 20;
const stepsize = 0.1;
const zoom = 0.800;
const tile = 0.850;
const speed = 0.010;
const brightness = 0.0015;
const darkmatter = 0.300;
const distfading = 0.730;
const saturation = 0.850;
fn mod3( p1 : vec3<f32>, p2 : vec3<f32> ) -> vec3<f32> {
let mx = p1.x - p2.x * floor( p1.x / p2.x );
let my = p1.y - p2.y * floor( p1.y / p2.y );
let mz = p1.z - p2.z * floor( p1.z / p2.z );
return vec3<f32>(mx, my, mz);
}
fn shader_main(frag_coord: vec2<f32>) -> vec4<f32> {
var uv = frag_coord.xy / i_resolution.xy - vec2<f32>(0.5, 0.5);
uv.y *= i_resolution.y / i_resolution.x;
var dir = vec3<f32>(uv * zoom, 1.0);
let time = i_time * speed + 0.25;
//mouse rotation
let a1 = 0.5 + i_mouse.x / i_resolution.x * 2.0;
let a2 = 0.8 + i_mouse.y / i_resolution.y * 2.0;
let rot1 = mat2x2<f32>(cos(a1), sin(a1), -sin(a1), cos(a1));
let rot2 = mat2x2<f32>(cos(a2), sin(a2), -sin(a2), cos(a2));
let dir_xz = dir.xz * rot1;
dir.x = dir_xz.x;
dir.z = dir_xz.y;
let dir_xy = dir.xy * rot2;
dir.x = dir_xy.x;
dir.y = dir_xy.y;
var fro = vec3<f32>(1.0, 0.5, 0.5);
fro += vec3<f32>(time * 2.0, time, -2.0);
let fro_xz = fro.xz * rot1;
fro.x = fro_xz.x;
fro.z = fro_xz.y;
let fro_xy = fro.xy * rot2;
fro.x = fro_xy.x;
fro.y = fro_xy.y;
//volumetric rendering
var s = 0.1;
var fade = 1.0;
var v = vec3<f32>(0.0);
for (var r: i32 = 0; r < volsteps; r = r + 1) {
var p = fro + s * dir * 0.5;
p = abs(vec3<f32>(tile) - mod3(p, vec3<f32>(tile * 2.0))); // tiling fold
var pa = 0.0;
var a = 0.0;
for (var i : i32 = 0; i < iterations; i = i + 1) {
p = abs(p) / dot(p, p) - formuparam; // the magic formula
a += abs(length(p) - pa); // absolute sum of average change
pa = length(p);
}
let dm = max(0.0, darkmatter - a * a * 0.001); //dark matter
a = a * a * a; // add contrast
if (r > 6) {
fade = fade * (1.0 - dm); // dark matter, don't render near
}
v += vec3<f32>(fade);
v += vec3<f32>(s, s * s, s * s * s * s) * a * brightness * fade; // coloring based on distance
fade = fade * distfading; // distance fading
s = s + stepsize;
}
v = mix(vec3<f32>(length(v)), v, saturation); //color adjust
return vec4<f32>(v * 0.01, 1.0);
}
"""
shader = Shadertoy(shader_code, resolution=(800, 450))
if __name__ == "__main__":
shader.show()