Index

zydeco / 5f524c5

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
703 Sep 2023 16:015f524c5OpenGL render abstractionsJosh Stockin11020G

Blob @ zydeco / include / render / shaders / background.fs.glsl

text/plain3020 bytesdownload raw
1R""(
2
3#version 460 core
4
5
6layout(origin_upper_left) in vec4 gl_FragCoord;
7// layout(location = 0) uniform uvec2 screen_size;
8layout(location = 1) uniform uint time;
9
10out vec4 fragColor;
11
12
13// Simplex 3D Noise
14// by Ian McEwan, Ashima Arts
15//
16vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
17vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
18
19float snoise(vec3 v){
20 const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
21 const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
22
23// First corner
24 vec3 i = floor(v + dot(v, C.yyy) );
25 vec3 x0 = v - i + dot(i, C.xxx) ;
26
27// Other corners
28 vec3 g = step(x0.yzx, x0.xyz);
29 vec3 l = 1.0 - g;
30 vec3 i1 = min( g.xyz, l.zxy );
31 vec3 i2 = max( g.xyz, l.zxy );
32
33 // x0 = x0 - 0. + 0.0 * C
34 vec3 x1 = x0 - i1 + 1.0 * C.xxx;
35 vec3 x2 = x0 - i2 + 2.0 * C.xxx;
36 vec3 x3 = x0 - 1. + 3.0 * C.xxx;
37
38// Permutations
39 i = mod(i, 289.0 );
40 vec4 p = permute( permute( permute(
41 i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
42 + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
43 + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
44
45// Gradients
46// ( N*N points uniformly over a square, mapped onto an octahedron.)
47 float n_ = 1.0/7.0; // N=7
48 vec3 ns = n_ * D.wyz - D.xzx;
49
50 vec4 j = p - 49.0 * floor(p * ns.z *ns.z); // mod(p,N*N)
51
52 vec4 x_ = floor(j * ns.z);
53 vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
54
55 vec4 x = x_ *ns.x + ns.yyyy;
56 vec4 y = y_ *ns.x + ns.yyyy;
57 vec4 h = 1.0 - abs(x) - abs(y);
58
59 vec4 b0 = vec4( x.xy, y.xy );
60 vec4 b1 = vec4( x.zw, y.zw );
61
62 vec4 s0 = floor(b0)*2.0 + 1.0;
63 vec4 s1 = floor(b1)*2.0 + 1.0;
64 vec4 sh = -step(h, vec4(0.0));
65
66 vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
67 vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
68
69 vec3 p0 = vec3(a0.xy,h.x);
70 vec3 p1 = vec3(a0.zw,h.y);
71 vec3 p2 = vec3(a1.xy,h.z);
72 vec3 p3 = vec3(a1.zw,h.w);
73
74//Normalise gradients
75 vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
76 p0 *= norm.x;
77 p1 *= norm.y;
78 p2 *= norm.z;
79 p3 *= norm.w;
80
81// Mix final noise value
82 vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
83 m = m * m;
84 return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
85 dot(p2,x2), dot(p3,x3) ) );
86}
87
88void main()
89{
90 float col = snoise(vec3(gl_FragCoord.x/1000., gl_FragCoord.y/1000., time/5000.))*0.5
91 + snoise(vec3(gl_FragCoord.x/500., gl_FragCoord.y/500., time/2500.))*0.25
92 + snoise(vec3(gl_FragCoord.x/250., gl_FragCoord.y/250., time/1000.))*0.125
93 + snoise(vec3(gl_FragCoord.x/125., gl_FragCoord.y/125., time/500.))*0.0625
94 + snoise(vec3(gl_FragCoord.x/62.5, gl_FragCoord.y/62.5, time/200.))*0.0625/2.
95 + snoise(vec3(gl_FragCoord.x/62.5/2., gl_FragCoord.y/62.5/2., time/100.))*0.0625/4.
96 + snoise(vec3(gl_FragCoord.x/62.5/4., gl_FragCoord.y/62.5/4., time/50.))*0.0625/8.;
97 col = clamp(col, 0.18f, 1.0f);
98 if (col - 0.18f < 0.000001f) discard;
99 fragColor = vec4(col, col, col, col);
100}
101
102)""
103