-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshadertoy_flyby.py
190 lines (162 loc) · 5.02 KB
/
shadertoy_flyby.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from wgpu_shadertoy import Shadertoy
# shadertoy source: https://www.shadertoy.com/view/csjGDD by Kali CC-BY-NC-SA-3.0
# ported to wgsl
shader_code = """
var<private> det : f32 = 0.001;
var<private> br : f32 = 0.0;
var<private> tub : f32 = 0.0;
var<private> hit : f32 = 0.0;
var<private> pos: vec3<f32>;
var<private> sphpos: vec3<f32>;
fn lookat(dir: vec3<f32>, up: vec3<f32>) -> mat3x3<f32> {
let rt = normalize(cross(dir, up));
return mat3x3<f32>(rt, cross(rt, dir), dir);
}
fn path(t: f32) -> vec3<f32> {
return vec3<f32>(sin(t+cos(t)*0.5)*0.5, cos(t*0.5), t);
}
fn rot(a: f32) -> mat2x2<f32> {
let s=sin(a);
let c=cos(a);
return mat2x2<f32>(c, s, -s, c);
}
fn fractal(p_: vec2<f32>) -> vec3<f32> {
var p = fract(p_*0.1);
var m = 1000.0;
for (var i = 0; i < 7; i = i + 1) {
p = ( abs(p) / clamp( abs(p.x*p.y), 0.25, 2.0 ) ) - 1.2;
m = min(m,abs(p.y)+fract(p.x*0.3 + i_time*0.5 + f32(i)*0.25));
}
m=exp(-6.0 * m);
return m*vec3<f32>(abs(p.x),m,abs(p.y));
}
fn coso(pp_: vec3<f32>) -> f32 {
var pp = pp_;
pp*=.7;
pp = vec3<f32>( pp.xy * rot(pp.z*2.0), pp.z);
let ppxz = pp.xz * rot(i_time*2.0);
pp = vec3<f32>(ppxz.x, pp.y, ppxz.y);
pp = vec3<f32>(pp.x, pp.yz * rot(i_time));
var sph=length(pp) - 0.04;
sph-=length(sin(pp*40.))*.05;
sph=max(sph,-length(pp)+.11);
var br2=length(pp) - 0.03;
br2=min(br2,length(pp.xy)+.005);
br2=min(br2,length(pp.xz)+.005);
br2=min(br2,length(pp.yz)+.005);
br2=max(br2,length(pp) - 1.0);
br=min(br2,br);
let d=min(br,sph);
return d;
}
fn de(p_: vec3<f32>) -> f32 {
hit=0.;
br=1000.;
let pp = p_ - sphpos;
var p = p_;
let pxy = p.xy - path(p.z).xy;
p.x = pxy.x;
p.y = pxy.y;
let rxy = p.xy * rot(p.z + i_time* 0.5);
p.x = rxy.x;
p.y = rxy.y;
let s = sin(p.z*0.5 + i_time * 0.5);
p.x *= ( 1.3 - s*s*0.7 );
p.y *= ( 1.3 - s*s*0.7 );
for(var i=0; i<6; i+=1) {
p = abs(p) - 0.4;
}
pos = p;
tub = -length(p.xy) + 0.45 + sin(p.z*10.0) * 0.1 * smoothstep(0.4,0.5,abs(0.5-fract(p.z*0.05))*2.0);
var co = coso(pp);
co=min(co, coso(pp + 0.7) );
co=min(co, coso(pp - 0.7) );
let d = min(tub,co);
if (d==tub) {
hit = step(fract(0.1 * length(sin(p*10.0))), 0.05);
}
return d * 0.3;
}
fn march(fro: vec3<f32>, dir_: vec3<f32>) -> vec3<f32> {
var dir = dir_;
var uv: vec2<f32> = vec2<f32>( atan2( dir.x , dir.y ) + i_time * 0.5, length(dir.xy) + sin(i_time * 0.2));
var col: vec3<f32> = fractal(uv);
var d: f32 = 0.0;
var td: f32 = 0.0;
var g: f32 = 0.0;
var reff: f32 = 0.0;
var ltd: f32 = 0.0;
var li: f32 = 0.0;
var p: vec3<f32> = fro;
for(var i: i32 = 0; i < 200; i += 1) {
p += dir * d;
d = de(p);
if (d < det && reff == 0.0 && hit == 1.0) {
var e: vec2<f32> = vec2<f32>(0.0, 0.1);
var n: vec3<f32> = normalize(vec3<f32>(de(p + e.yxx), de(p + e.xyx), de(p + e.xxy)) - de(p));
p -= dir * d * 2.0;
dir = reflect(dir, n);
reff = 1.0;
td = 0.0;
ltd = td;
continue;
}
if (d < det || td > 5.0) {
break;
}
td += d;
g += 0.1 / (0.1 + br * 13.0);
li += 0.1 / (0.1 + tub * 5.0);
}
g = max(g, li * 0.15);
var f: f32 = 1.0 - td / 3.0;
if (reff == 1.0) {
f = 1.0 - ltd / 3.0;
}
if (d < 0.01) {
col = vec3<f32>(1.0);
var e: vec2<f32> = vec2<f32>(0.0, det);
var n: vec3<f32> = normalize(vec3<f32>( de(p + e.yxx), de(p + e.xyx), de(p + e.xxy)) - de(p));
col = vec3<f32>(n.x) * 0.7;
col += fract(pos.z * 5.0) * vec3<f32>(0.2, 0.1, 0.5);
col += fractal(pos.xz * 2.0);
if (tub > 0.01) {
col = vec3<f32>(0.0);
}
}
col *= f;
let so = fract(sin(i_time)*123.456);
var glo: vec3<f32> = g * 0.1 * vec3<f32>(2.0, 1.0, 2.0) * (0.5 + so * 1.5) * 0.5;
let glo_rb = glo.rb * rot(dir.y * 1.5);
glo = vec3<f32>(glo_rb.x, glo.y, glo_rb.y);
col += glo;
col *= vec3<f32>(0.8, 0.7, 0.7);
col = mix(col, vec3<f32>(1.0), reff * 0.3);
return col;
}
fn mod1( x : f32, y : f32 ) -> f32 {
return x - y * floor( x / y );
}
fn shader_main(frag_coord : vec2<f32>) -> vec4<f32> {
var uv = frag_coord / i_resolution.xy;
uv = uv - 0.5;
uv /= vec2<f32>(i_resolution.y / i_resolution.x, 1.0);
var t = i_time;
var fro = path(t);
if (mod1(t, 10.0) > 5.0) {
fro = path(floor(t / 4.0 + 0.5) * 4.0);
}
sphpos = path(t + 0.5);
fro.x += 0.2;
var fw = normalize(path(t + 0.5) - fro);
var dir = normalize(vec3<f32>(uv, 0.5));
dir = lookat(fw, vec3<f32>(fw.x * 2.0, 1.0, 0.0)) * dir;
dir = vec3<f32>(dir.x+sin(t) * 0.3, dir.y, dir.z+sin(t) * 0.3);
var col = march(fro, dir);
col = mix(vec3<f32>(0.5) * length(col), col, 0.8);
return vec4<f32>(col, 1.0);
}
"""
shader = Shadertoy(shader_code, resolution=(800, 450))
if __name__ == "__main__":
shader.show()