starfield.fs 1.45 KB
Newer Older
1
2
3
4
#version 330

out vec4 frag_color;

Georg Schaefer's avatar
Georg Schaefer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
uniform vec2 resolution;
uniform vec2 inverse_resolution;
uniform vec3 origin;
uniform mat2 rotation;
uniform float zoom;

#define iterations 15
#define volsteps 6
#define sparsity 0.5f
#define stepsize 0.3f
#define frequency_variation 1.3f
#define brightness 0.0018f
#define distfading 0.68f

19
void main() {
Georg Schaefer's avatar
Georg Schaefer committed
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
	vec2 uv = gl_FragCoord.xy * inverse_resolution - 0.5f;
	uv.y *= resolution.y * inverse_resolution.x;

	vec3 direction = vec3(uv * zoom, 1.f);
	direction.xy *= rotation;

	float s = 0.1f;
	float fade = 0.01f;
	frag_color = vec4(0.f);

	for (int r = 0; r < volsteps; ++r) {
		vec3 p = origin + direction * (s * 0.5f);
		p = abs(vec3(frequency_variation) - mod(p, vec3(frequency_variation * 2.f)));

		float prevlen = 0.f;
		float a = 0.f;
		for (int i = 0; i < iterations; ++i) {
			p = abs(p);
			p = p * (1.f / dot(p, p)) + (-sparsity);
			float len = length(p);
			a += abs(len - prevlen);
			prevlen = len;
		}

		a *= a * a;

		frag_color.rgb += (vec3(s, s * s, s * s * s) * a * brightness + 1.f) * fade;
		fade *= distfading;
		s += stepsize;
	}

	frag_color.rgb = min(frag_color.rgb, vec3(1.2f));

	float intensity = min(frag_color.r + frag_color.g + frag_color.b, 0.7f);

	ivec2 sgn = (ivec2(gl_FragCoord.xy) & 1) * 2 - 1;
	vec2 gradient = vec2(dFdx(intensity) * sgn.x, dFdy(intensity) * sgn.y);
	float cutoff = max(max(gradient.x, gradient.y) - 0.1f, 0.f);
	frag_color.rgb *= max(1.f - cutoff * 0.6f, 0.3f);
	frag_color.a = 1.f;
60
}