kaliber/assets/sky.glsl_fragment

70 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2020-04-13 11:24:53 +00:00
#ifdef GL_ES
2020-12-24 23:22:41 +00:00
precision highp float;
2020-04-13 11:24:53 +00:00
#endif
2020-12-24 23:22:41 +00:00
IN(0) vec2 tex_coord_0;
2020-04-13 11:24:53 +00:00
2020-12-24 23:22:41 +00:00
UNIFORM_BEGIN
UNIFORM_V(vec2 scale)
UNIFORM_V(mat4 projection)
UNIFORM_F(vec2 sky_offset)
UNIFORM_F(vec3 nebula_color)
UNIFORM_END
2020-04-13 11:24:53 +00:00
2020-12-24 23:22:41 +00:00
FRAG_COLOR_OUT(frag_color)
2023-05-16 06:51:00 +00:00
const float PHI = 1.61803398874989484820459; // Golden Ratio
2020-12-24 23:22:41 +00:00
float random(vec2 p) {
float sd = sin(dot(p, vec2(54.90898, 18.233)));
2020-04-13 11:24:53 +00:00
return fract(sd * 2671.6182);
}
2023-05-16 06:51:00 +00:00
float random2(in vec2 xy) {
return fract(tan(distance(xy * PHI, xy)) * (xy.x + 0.1));
}
2020-12-24 23:22:41 +00:00
float nebula(in vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
2020-04-13 11:24:53 +00:00
2023-05-16 06:51:00 +00:00
float a = random2(i);
float b = random2(i + vec2(1.0, 0.0));
float c = random2(i + vec2(0.0, 1.0));
float d = random2(i + vec2(1.0, 1.0));
2020-04-13 11:24:53 +00:00
vec2 u = smoothstep(0.0, 1.0, f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
2020-12-24 23:22:41 +00:00
float stars(in vec2 p, float num_cells, float size) {
vec2 n = p * num_cells;
vec2 i = floor(n);
2020-04-13 11:24:53 +00:00
vec2 a = n - i - random(i);
a /= num_cells * size;
float e = dot(a, a);
2020-12-24 23:22:41 +00:00
return smoothstep(0.94, 1.0, (1.0 - e * 35.0));
2020-04-13 11:24:53 +00:00
}
void main() {
2020-12-24 23:22:41 +00:00
vec2 layer1_coord = tex_coord_0 + PARAM(sky_offset);
vec2 layer2_coord = tex_coord_0 + PARAM(sky_offset) * 0.7;
2023-05-16 06:51:00 +00:00
vec3 result = vec3(0.);
2020-04-13 11:24:53 +00:00
float c = nebula(layer2_coord * 3.0) * 0.35 - 0.05;
2020-12-24 23:22:41 +00:00
result += PARAM(nebula_color) * floor(c * 60.0) / 60.0;
2020-04-13 11:24:53 +00:00
c = stars(layer1_coord, 8.0, 0.05);
result += vec3(0.97, 0.74, 0.74) * c;
c = stars(layer2_coord, 16.0, 0.025) * 0.5;
result += vec3(0.9, 0.9, 0.95) * c;
2020-12-24 23:22:41 +00:00
FRAG_COLOR(frag_color) = vec4(result, 1.0);
2020-04-13 11:24:53 +00:00
}