1+ /*
2+ * shader: ray marching study 201910
3+ * references:
4+ * - [GLSL SandBoxで手軽にレイマーチングで遊ぼう](https://hackerslab.aktsk.jp/2018/12/01/131928)
5+ * - [魔法使いになりたい人のためのシェーダーライブコーディング入門](https://qiita.com/kaneta1992/items/21149c78159bd27e0860)
6+ * - [Phantom Mode](https://www.shadertoy.com/view/MtScWW)
7+ * - [Live Coding Using Phantom Mode](https://www.shadertoy.com/view/wl2GWG)
8+ * - [distance functions](https://iquilezles.org/www/articles/distfunctions/distfunctions.htm)
9+ */
10+
11+ uniform ivec2 resolution;
12+ uniform float time;
13+ uniform float progress;
14+ uniform vec3 soundLevel;
15+
16+ uniform float kick;
17+
18+ const float PI = acos (- 1 );
19+ const float TWO_PI = PI * 2 ;
20+
21+ vec3 hsb2rgb(float h, float s, float v) {
22+ // [[汎用関数]HSV2RGB 関数](https://qiita.com/keim_at_si/items/c2d1afd6443f3040e900)
23+ return ((clamp (abs (fract (h + vec3 (0 , 2 , 1 ) / 3 .) * 6 . - 3 .) - 1 ., 0 ., 1 .) - 1 .) * s + 1 .) * v;
24+ }
25+
26+ float sdTriPrism( vec3 p, vec2 h ) {
27+ vec3 q = abs (p);
28+ return max (q.z- h.y,max (q.x* 0.866025 + p.y* 0.5 ,- p.y)- h.x* 0.5 );
29+ }
30+
31+ vec3 repeat(vec3 p, float interval) {
32+ return mod (p, interval) - 2.0 ;
33+ }
34+
35+ float dist(vec3 p) {
36+ vec3 pos = repeat(p - 2.0 , 4.0 );
37+ return sdTriPrism(pos, vec2 (0.5 , 2.0 * progress));
38+ }
39+
40+ void main() {
41+ vec2 uv = (gl_FragCoord .xy * 2.0 - resolution.xy) / min (resolution.x, resolution.y);
42+
43+ vec3 cameraUp = normalize (vec3 (sin (progress * 5 * TWO_PI), cos (progress * 5 * TWO_PI), 0.0 ));
44+ vec3 cameraDir = vec3 (0.0 , 0.0 , 1.0 * progress);
45+ vec3 cameraSite = normalize (cross (cameraUp, cameraDir));
46+ vec3 rayDir = normalize ((uv.x * cameraSite + uv.y * cameraUp) + cameraDir);
47+
48+ float t = 0.01 ;
49+ float ac = 0.0 ;
50+ for (int i = 0 ; i < 48 ; ++ i) {
51+ float d = dist(rayDir * t);
52+ d = max (abs (d), 0.02 );
53+ ac += exp (- d * 3.0 );
54+ t += d * 0.5 ;
55+ }
56+
57+ gl_FragColor = vec4 (hsb2rgb(soundLevel.z * ac * 0.2 , ac * 0.01 * kick, ac * 0.02 ), 1.0 );
58+ }
0 commit comments