Skip to content
0.2.0-dev Download ZIP
40 lines ·1.3 KB Raw
1shader_type spatial;
2render_mode unshaded;
3
4uniform sampler2DArray basetexture : filter_nearest, repeat_disable;
5uniform sampler2D palette : filter_nearest, repeat_disable;
6
7int byte_at(vec4 texel) {
8 return int(round(texel.r * 255.0));
9}
10
11float srgb_to_linear(float c) {
12 return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
13}
14
15vec3 palette_colour(int index) {
16 vec3 c = texelFetch(palette, ivec2(index, 0), 0).rgb;
17 return vec3(srgb_to_linear(c.r), srgb_to_linear(c.g), srgb_to_linear(c.b));
18}
19
20vec3 quake_direction(vec3 engine_direction) {
21 vec3 q = normalize(vec3(engine_direction.z, engine_direction.x, engine_direction.y));
22 q.z *= 3.0;
23 return normalize(q);
24}
25
26int sky_index(vec3 direction, float time) {
27 int s = int(floor(time * 8.0 + 378.0 * direction.x));
28 int t = int(floor(time * 8.0 + 378.0 * direction.y));
29 int shift = int(floor(time * 8.0));
30 int right_half = textureSize(basetexture, 0).x / 2;
31 int back_layer = byte_at(texelFetch(basetexture, ivec3(right_half + (s & 127), t & 127, 0), 0));
32 int front_layer = byte_at(texelFetch(basetexture, ivec3((s + shift) & 127, (t + shift) & 127, 0), 0));
33 return front_layer == 0 ? back_layer : front_layer;
34}
35
36void fragment() {
37 vec3 world = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
38 vec3 direction = quake_direction(world - CAMERA_POSITION_WORLD);
39 ALBEDO = palette_colour(sky_index(direction, TIME));
40}