0.2.0-dev Download ZIP
156 lines ·4.9 KB Raw
| 1 | shader_type sky; |
| 2 | |
| 3 | uniform samplerCube sky_cube : filter_linear_mipmap; |
| 4 | uniform vec3 tint = vec3(1.0); |
| 5 | uniform bool define_add_stars = false; |
| 6 | uniform bool define_add_moon = false; |
| 7 | uniform bool define_test_clouds_2 = false; |
| 8 | uniform bool define_moreclouds = false; |
| 9 | |
| 10 | #include "level.gdshaderinc" |
| 11 | |
| 12 | const float SPEC = 2.0; |
| 13 | |
| 14 | vec3 sky_spec(vec3 normal, float lod) { |
| 15 | return has_level_sky ? og_spec(normal, lod) : textureLod(sky_cube, normal, lod + SPEC).rgb; |
| 16 | } |
| 17 | |
| 18 | vec2 og_simplex_hash(vec2 p) { |
| 19 | p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3))); |
| 20 | return -1.0 + 2.0 * fract(sin(p) * 43758.5453123); |
| 21 | } |
| 22 | |
| 23 | float og_simplex(vec2 p) { |
| 24 | const float K1 = 0.366025404; |
| 25 | const float K2 = 0.211324865; |
| 26 | vec2 i = floor(p + (p.x + p.y) * K1); |
| 27 | vec2 a = p - i + (i.x + i.y) * K2; |
| 28 | vec2 o = a.x > a.y ? vec2(1.0, 0.0) : vec2(0.0, 1.0); |
| 29 | vec2 b = a - o + K2; |
| 30 | vec2 c = a - 1.0 + 2.0 * K2; |
| 31 | vec3 h = max(0.5 - vec3(dot(a, a), dot(b, b), dot(c, c)), 0.0); |
| 32 | vec3 n = h * h * h * h * vec3(dot(a, og_simplex_hash(i)), dot(b, og_simplex_hash(i + o)), dot(c, og_simplex_hash(i + 1.0))); |
| 33 | return dot(n, vec3(70.0)); |
| 34 | } |
| 35 | |
| 36 | float og_clouds_fbm(vec2 n) { |
| 37 | mat2 m = mat2(vec2(1.6, 1.2), vec2(-1.2, 1.6)); |
| 38 | float total = 0.0; |
| 39 | float amplitude = 0.1; |
| 40 | for (int i = 0; i < 7; i++) { |
| 41 | total += og_simplex(n) * amplitude; |
| 42 | n = m * n; |
| 43 | amplitude *= 0.4; |
| 44 | } |
| 45 | return total; |
| 46 | } |
| 47 | |
| 48 | vec3 og_clouds(vec3 colour, vec3 normal, float time) { |
| 49 | mat2 m = mat2(vec2(1.6, 1.2), vec2(-1.2, 1.6)); |
| 50 | const float cloudscale = 1.1; |
| 51 | const float speed = 0.01; |
| 52 | vec2 old_uv = (normal / (normal.y + 0.2)).xz; |
| 53 | float q = og_clouds_fbm(old_uv * cloudscale * 0.5); |
| 54 | float t = time * speed; |
| 55 | float r = 0.0; |
| 56 | vec2 uv = old_uv * cloudscale - (q - t); |
| 57 | float weight = 0.8; |
| 58 | for (int i = 0; i < 8; i++) { |
| 59 | r += abs(weight * og_noise(uv)); |
| 60 | uv = m * uv + t; |
| 61 | weight *= 0.7; |
| 62 | } |
| 63 | float f = 0.0; |
| 64 | uv = old_uv * cloudscale - (q - t); |
| 65 | weight = 0.7; |
| 66 | for (int i = 0; i < 8; i++) { |
| 67 | f += weight * og_noise(uv); |
| 68 | uv = m * uv + t; |
| 69 | weight *= 0.6; |
| 70 | } |
| 71 | f *= r + f; |
| 72 | float c = 0.0; |
| 73 | t = time * speed * 2.0; |
| 74 | uv = old_uv * cloudscale * 2.0 - (q - t); |
| 75 | weight = 0.4; |
| 76 | for (int i = 0; i < 7; i++) { |
| 77 | c += weight * og_noise(uv); |
| 78 | uv = m * uv + t; |
| 79 | weight *= 0.6; |
| 80 | } |
| 81 | float c1 = 0.0; |
| 82 | t = time * speed * 3.0; |
| 83 | uv = old_uv * cloudscale * 3.0 - (q - t); |
| 84 | weight = 0.4; |
| 85 | for (int i = 0; i < 7; i++) { |
| 86 | c1 += abs(weight * og_noise(uv)); |
| 87 | uv = m * uv + t; |
| 88 | weight *= 0.6; |
| 89 | } |
| 90 | c += c1; |
| 91 | vec3 sky = mix(vec3(0.4, 0.7, 1.0), vec3(0.2, 0.4, 0.6), normal.y); |
| 92 | vec3 cloud = vec3(1.1, 1.1, 0.9) * clamp(0.5 + 0.3 * c, 0.0, 1.0); |
| 93 | f = (define_moreclouds ? 0.8 : 0.2) + 8.0 * f * r; |
| 94 | vec3 result = mix(sky, clamp(0.5 * sky + cloud, 0.0, 1.0), clamp(f + c, 0.0, 1.0)); |
| 95 | return mix(colour, result * 0.5, max(normal.y, 0.0)); |
| 96 | } |
| 97 | |
| 98 | void sky() { |
| 99 | vec3 normal = normalize(EYEDIR); |
| 100 | vec3 colour = texture(sky_cube, normal).rgb; |
| 101 | if (AT_CUBEMAP_PASS) { |
| 102 | COLOR = has_level_sky ? og_spec(normal, -level_sky_bias) : colour; |
| 103 | } else { |
| 104 | float foggy = define_water_horizon ? 0.0 : clamp((level_fog_amount - 1.0) / 2.0, 0.0, 1.0); |
| 105 | float fogness = mix(-1.0, 1.0, foggy); |
| 106 | if (normal.y < 0.0) { |
| 107 | fogness = mix(fogness, 1.0, -normal.y * level_fog_amount / 5.0); |
| 108 | } |
| 109 | if (!define_misty && !define_misty2 && !define_sky_ark && !define_waterfall_arena) { |
| 110 | float blur = clamp(1.0 - abs(normal.y) + fogness, 0.0, 1.0); |
| 111 | colour = mix(colour, textureLod(sky_cube, normal, mix(pow(blur, 2.0), 1.0, fogness * 0.5 + 0.5) * 5.0 + SPEC).rgb, min(1.0, blur * 4.0)); |
| 112 | } |
| 113 | colour *= tint; |
| 114 | if (define_add_stars) { |
| 115 | float star = max(0.0, og_simplex(normal.xz / (normal.y + 2.0) * 200.0 + vec2(TIME * 0.05, 0.0)) - 0.7) * 100.0; |
| 116 | star *= max(0.0, 1.0 - abs(og_simplex(normal.xz / (normal.y + 2.0) * 20.0 + vec2(TIME, 0.0)))); |
| 117 | if (colour.b < 0.1) { |
| 118 | colour += vec3(star) * 0.01; |
| 119 | } |
| 120 | } |
| 121 | if (define_add_moon) { |
| 122 | float k = dot(normal, level_sun); |
| 123 | if (k > 0.9999) { |
| 124 | colour += vec3(2.0); |
| 125 | } else if (k > 0.999) { |
| 126 | colour += vec3(pow((k - 0.9989) * 1000.0, 40.0)) * 2.0; |
| 127 | } |
| 128 | } |
| 129 | if (define_volcano) { |
| 130 | colour += (sky_spec(normal, 5.0) - colour) * clamp(1.0 - normal.y, 0.0, 1.0); |
| 131 | } |
| 132 | vec3 mult = og_fog_colour_mult(normal, 1000.0, POSITION); |
| 133 | if (define_misty || define_misty2 || define_sky_ark || define_cave || define_rainy || define_wet) { |
| 134 | float haze = og_haze(normal * 1000.0, POSITION, TIME); |
| 135 | colour = mix(colour, sky_spec(normal, 3.0) * mult, haze); |
| 136 | } |
| 137 | if (define_sky_ark && normal.y < -0.02) { |
| 138 | float val = 1.0 - clamp((normal.y + 0.04) / 0.02, 0.0, 1.0); |
| 139 | colour = mix(colour, sky_spec(normal, 0.0) * vec3(1.2, 1.1, 1.0), val); |
| 140 | } |
| 141 | if (define_water_horizon) { |
| 142 | float opac = min(1.0, pow(max(0.0, 1.0 - normal.y), 8.0)); |
| 143 | colour = mix(colour, (define_alt2 ? vec3(1.0) : sky_spec(normal, 3.0)) * mult, opac); |
| 144 | } |
| 145 | if (define_snow_everywhere2) { |
| 146 | colour = mult; |
| 147 | } |
| 148 | if (define_test_clouds_2) { |
| 149 | colour = og_clouds(colour, normal, TIME); |
| 150 | } |
| 151 | if (define_waterfall_arena && !define_cave) { |
| 152 | colour = sky_spec(normal, 3.0) * mult; |
| 153 | } |
| 154 | COLOR = colour; |
| 155 | } |
| 156 | } |