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