Skip to content
0.2.0-dev Download ZIP
377 lines ·13.3 KB Raw
1uniform float level_fog_amount = 1.0;
2uniform float level_haze = 0.0008;
3uniform vec3 level_sun = vec3(0.0, 1.0, 0.0);
4uniform vec3 level_sun_colour = vec3(1.0);
5uniform bool define_alt = false;
6uniform bool define_alt2 = false;
7uniform bool define_beach = false;
8uniform bool define_cave = false;
9uniform bool define_cave_arena = false;
10uniform bool define_damp_fog = false;
11uniform bool define_misty = false;
12uniform bool define_misty2 = false;
13uniform bool define_more_reflect = false;
14uniform bool define_no_sky_highlight = false;
15uniform bool define_rainy = false;
16uniform bool define_sky_ark = false;
17uniform bool define_snow_everywhere = false;
18uniform bool define_snow_everywhere2 = false;
19uniform bool define_snow_everywhere3 = false;
20uniform bool define_swamp = false;
21uniform bool define_swamp2 = false;
22uniform bool define_volcano = false;
23uniform bool define_less_glow = false;
24uniform bool define_water_horizon = false;
25uniform bool define_waterfall_arena = false;
26uniform bool define_wet = false;
27
28float og_hash(vec2 p) {
29 return -1.0 + 2.0 * fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
30}
31
32float og_noise(vec2 p) {
33 vec2 i = floor(p);
34 vec2 f = fract(p);
35 vec2 u = f * f * (3.0 - 2.0 * f);
36 return mix(mix(og_hash(i), og_hash(i + vec2(1.0, 0.0)), u.x), mix(og_hash(i + vec2(0.0, 1.0)), og_hash(i + vec2(1.0, 1.0)), u.x), u.y);
37}
38
39float og_fractal(vec2 uv) {
40 mat2 m = mat2(vec2(1.6, 1.2), vec2(-1.2, 1.6));
41 float f = 0.5 * og_noise(uv); uv = m * uv;
42 f += 0.25 * og_noise(uv); uv = m * uv;
43 f += 0.125 * og_noise(uv); uv = m * uv;
44 f += 0.0625 * og_noise(uv); uv = m * uv;
45 f += 0.03125 * og_noise(uv); uv = m * uv;
46 f += 0.016625 * og_noise(uv);
47 return f;
48}
49
50float og_noise_3d(vec3 x) {
51 vec3 p = floor(x);
52 vec3 f = fract(x);
53 f = f * f * (3.0 - 2.0 * f);
54 vec2 uv = (p.xy + vec2(37.0, 17.0) * p.z) + f.xy;
55 return mix(og_noise(uv), og_noise(uv + vec2(37.0, 17.0)), f.z);
56}
57
58float og_beach_level() {
59 if (define_swamp) return 17.1;
60 if (define_swamp2) return define_more_reflect ? -2.2 : 83.0;
61 if (define_water_horizon) return define_alt ? 22.0 : -4.0;
62 if (define_cave_arena) return 101.31;
63 if (define_snow_everywhere) return -3.3;
64 if (define_sky_ark) return 75.4;
65 return -1000.0;
66}
67
68bool og_height_fog() {
69 return define_misty || define_misty2 || define_damp_fog || define_sky_ark || define_waterfall_arena;
70}
71
72float og_cloud_layer(float angle, float dir_y, float t) {
73 return (0.5 + og_fractal(vec2(angle * 1.5 - t * 0.2, dir_y * 1.2 - t * 0.3)) * 0.5)
74 + (0.5 + og_fractal(vec2(angle * 2.5 + t * 0.2, dir_y * 2.2 + t * 0.3)) * 0.5);
75}
76
77float og_rain(float fog, float angle, float dir_y, float first, float second, float fade, float time) {
78 float opac = 1.0 - ((1.0 - fog) * (1.0 + og_fractal(vec2(angle * 100.0, dir_y * 5.0 + time * 16.0)) * 0.2 * first * fade));
79 return 1.0 - ((1.0 - opac) * (1.0 + og_fractal(vec2(angle * 2.0, dir_y + time * 2.0)) * 0.8 * second * fade));
80}
81
82float og_haze(vec3 relative, vec3 cam, float time) {
83 float fog = 1.0 - 1.0 / pow(length(relative) * level_haze + 1.0, 2.0);
84 vec3 dir = normalize(relative);
85 if (og_height_fog()) {
86 float height = define_misty ? 95.0 : define_misty2 ? 18.0 : define_waterfall_arena ? 90.5 : define_damp_fog ? -44.0
87 : min(75.0, 80.0 + (cam + relative).z * -0.05);
88 float angle = atan(dir.z, -dir.x);
89 float fade = 1.0 - dir.y * dir.y;
90 float t = time * 0.4;
91 float val = og_cloud_layer(angle, dir.y, t);
92 float seam = abs(angle - 3.14) * 5.0;
93 if (seam < 1.0) {
94 val = mix(og_cloud_layer(angle - 6.28, dir.y, t), val, seam);
95 }
96 val *= 0.5;
97 val = mix(0.5, val, fade);
98 height += val - 0.5;
99 vec3 world = relative + cam;
100 float base = define_misty ? 0.0 : fog * pow(min(1.0, 1.0 - dir.y), 2.0) * val * 1.5;
101 if (cam.y > height && world.y > height) {
102 return base;
103 }
104 vec3 entry = cam;
105 vec3 clipped = relative;
106 if (world.y > height) {
107 clipped = relative * (1.0 - (world.y - height) / relative.y);
108 } else if (cam.y > height) {
109 clipped = relative * ((height - world.y) / -relative.y);
110 entry = world - clipped;
111 }
112 float dist = length(clipped);
113 float orig = 1.0 - 1.0 / pow(dist * level_haze + 1.0, 2.0);
114 dist *= 0.5;
115 vec3 p = entry * 0.5;
116 p.x += t;
117 p.y += val * 8.0;
118 float n = og_noise_3d(p + dir) * 0.1 * clamp(dist, 0.0, 1.0)
119 + og_noise_3d(p + dir * 2.0) * 0.1 * clamp(dist - 1.0, 0.0, 1.0)
120 + og_noise_3d(p + dir * 3.0) * 0.1 * clamp((dist - 2.0) * 0.5, 0.0, 1.0)
121 + og_noise_3d(p + dir * 4.0) * 0.1 * clamp((dist - 4.0) * 0.25, 0.0, 1.0);
122 if (define_sky_ark && cam.y > height) {
123 n *= 1.0 / ((cam.y - height) * 0.2 + 1.0);
124 }
125 fog = orig + n * (define_misty || define_damp_fog || define_waterfall_arena ? 0.3 : 1.0) + base;
126 fog = clamp(fog, 0.0, 1.0);
127 }
128 if (define_rainy) {
129 float dist = length(relative) * 0.1;
130 float second = pow(min(1.0, dist * 0.025), 0.9);
131 float first = pow(max(min(1.0, dist * 0.05) - second * 0.5, 0.0), 0.9);
132 float angle = atan(dir.z, -dir.x);
133 float fade = 1.0 - dir.y * dir.y;
134 float opac = og_rain(fog, angle, dir.y, first, second, fade, time);
135 float seam = abs(angle - 3.14) * 5.0;
136 if (seam < 1.0) {
137 opac = mix(og_rain(fog, angle - 6.28, dir.y, first, second, fade, time), opac, seam);
138 }
139 fog = clamp(opac, 0.0, 1.0);
140 }
141 return fog;
142}
143
144vec3 og_fog_colour_mult(vec3 dir, float dist, vec3 cam) {
145 float facing = dot(dir, level_sun);
146 if (define_rainy || define_wet) {
147 return define_no_sky_highlight ? vec3(1.0) : vec3(1.0) + level_sun_colour * pow((facing + 1.0) * 0.5, 4.0);
148 }
149 if (define_volcano) {
150 return mix(vec3(1.0), vec3(0.2, 1.6, 3.6), max(0.0, dir.y));
151 }
152 if (define_swamp || (define_waterfall_arena && !define_cave)) {
153 float angle = 180.0 - acos(clamp(facing, -1.0, 1.0)) * 180.0 / 3.1417;
154 vec3 rainbow = vec3(0.0);
155 if (angle > 39.0 && angle < 53.0 && dir.y > 0.0) {
156 rainbow = max(vec3(0.0), 1.0 - abs(angle - vec3(42.0, 41.5, 40.89)));
157 rainbow += max(vec3(0.0), 1.0 - abs(angle - vec3(50.8, 51.5, 52.2)) * 0.8) * 0.25;
158 rainbow *= min(1.0, dist * 0.02);
159 if (define_swamp) {
160 rainbow *= 1.0 / (cam.y - og_beach_level() + 1.0);
161 }
162 rainbow *= max(0.0, dir.y) * 1.5;
163 }
164 if (define_waterfall_arena) {
165 rainbow *= 0.5;
166 }
167 vec3 colour = vec3((facing + 3.0) * 0.5) + rainbow;
168 return define_waterfall_arena && !define_cave ? colour * 0.15 : colour;
169 }
170 if (define_water_horizon) {
171 return vec3(facing + 2.0) * 0.5;
172 }
173 if (define_snow_everywhere2) {
174 float gradient = pow(0.5 + facing * 0.5, 3.0);
175 vec3 colour = mix(vec3(0.1, 0.2, 0.5), vec3(2.0, 1.5, 0.7) * 1.3, gradient) * 0.25;
176 return mix(vec3(0.1, 0.1, 0.4), colour, clamp(1.0 - dir.y, 0.0, 1.0));
177 }
178 return vec3(1.0);
179}
180
181float og_fog_lod(float dist) {
182 if (define_volcano) return 5.0;
183 if (define_misty || define_misty2 || define_sky_ark || define_waterfall_arena || define_rainy || define_wet || (define_water_horizon && !define_alt2)) return 3.0;
184 return min(5.0, 5.0 / (dist * 0.01 + 1.0) + level_haze * 50.0);
185}
186
187uniform samplerCube level_sky : filter_linear_mipmap;
188uniform bool has_level_sky = false;
189uniform float level_sky_bias = 2.0;
190
191vec3 og_spec(vec3 dir, float lod) {
192 return textureLod(level_sky, dir, lod + level_sky_bias).rgb;
193}
194
195const vec3 OG_BROWN = vec3(122.0, 89.0, 30.0) / 255.0;
196
197bool og_snow() {
198 return define_snow_everywhere || define_snow_everywhere2 || define_snow_everywhere3;
199}
200
201void og_surface(inout vec3 albedo, inout float roughness, inout float spec, inout vec3 normal, vec3 world, float dist, float time,
202 bool is_terrain, bool is_plant) {
203 float y = world.y;
204 if (og_snow()) {
205 float blend;
206 if (define_snow_everywhere) {
207 float amount = pow(clamp((y + 3.0) / 83.0, 0.0, 1.0), 0.2);
208 float cover = normal.y - mix(1.0, 0.2, amount);
209 blend = cover > 0.0 ? min(1.0, cover * 20.0) : 0.0;
210 if (y < -3.0) {
211 float wet = min(1.0, (-3.0 - y) * 0.7);
212 spec = mix(spec, 0.1, wet);
213 albedo = mix(albedo, vec3(0.0), wet * 0.9);
214 roughness = mix(roughness, 0.8, wet);
215 }
216 } else {
217 float cover = define_snow_everywhere2 ? normal.y - 0.2 : normal.y + max(0.0, abs(normal.x) * 0.65 + 0.25) - 0.95;
218 blend = clamp(cover * 20.0, 0.0, 1.0) * roughness;
219 }
220 albedo = mix(albedo, vec3(0.55), blend);
221 roughness = mix(roughness, 0.6, blend);
222 spec = mix(spec, 0.0, blend);
223 normal = normalize(mix(normal, vec3(1.0), 0.3 * blend));
224 }
225 if (is_plant) {
226 float kb = og_beach_level();
227 if ((define_swamp || define_swamp2) && y < kb) {
228 float o = clamp((kb - y) * 5.0, 0.0, 1.0);
229 roughness *= mix(1.0, 0.2, o);
230 albedo *= mix(1.0, 0.3, o);
231 }
232 return;
233 }
234 if (define_rainy || define_wet) {
235 roughness *= is_terrain ? mix(0.3, 1.0, min(1.0, dist * 0.1)) : 0.3;
236 if (define_rainy) {
237 normal.y *= 1.0 + og_noise(world.xz * 33.0 + vec2(10.0 * time, 0.0)) * 0.25;
238 normal.y *= 1.0 + og_noise(world.xz * 17.0 + vec2(-10.0 * time, 4.0 * time)) * 0.2;
239 normal = normalize(normal);
240 }
241 }
242 if (define_beach) {
243 float kb = 16.7 + sin(time * 0.5 - 2.0) * 0.05;
244 float o = clamp((kb - y) * 5.0, 0.0, 1.0);
245 roughness *= 1.0 - o;
246 albedo *= mix(1.0, 0.5, o);
247 }
248 if (define_swamp || define_swamp2 || define_cave_arena) {
249 float kb = og_beach_level();
250 float o = y < kb ? clamp((kb - y) * (define_cave_arena ? 10.0 : 5.0), 0.0, 1.0) : 0.0;
251 albedo *= mix(1.0, 0.3, o);
252 if (!is_terrain) {
253 o = max(o, 0.7);
254 }
255 roughness *= 1.0 - o;
256 if (define_cave_arena) {
257 spec += 0.2 / (1.0 + dist * 0.2);
258 }
259 }
260 if (define_damp_fog || define_waterfall_arena) {
261 float o = is_terrain ? 0.3 : 0.6;
262 roughness *= mix(1.0, 0.2, o);
263 albedo *= mix(1.0, 0.3, o);
264 }
265 if (define_water_horizon) {
266 float kb = og_beach_level();
267 if (y < kb) {
268 float o = clamp(kb - y, 0.0, 1.0);
269 roughness *= mix(1.0, 0.2, o);
270 albedo *= mix(1.0, 0.3, o);
271 }
272 if (y < kb + 6.0) {
273 float o = clamp((kb + 6.0 - y) * 0.2 - pow(max(0.0, normal.y), 2.0), 0.0, 1.0);
274 albedo *= mix(vec3(1.0), OG_BROWN, 0.8 * o);
275 }
276 }
277 if (define_sky_ark) {
278 float o = clamp((75.4 - y) * 0.5, 0.0, 1.0);
279 roughness *= mix(1.0, 0.2, o);
280 albedo *= mix(1.0, 0.3, o);
281 float brown = (1.0 - (normal.y * 0.9 + normal.z * 0.4)) * (0.5 + (og_noise((world.xz + vec2(y * 0.05, 0.0)) * 5.0) + 1.0) * 0.5)
282 * max(0.0, (170.0 - y) * 0.02) * 0.7;
283 albedo *= mix(vec3(1.0), OG_BROWN, min(1.0, brown) * 0.8);
284 }
285 if (define_volcano) {
286 roughness = max(0.0, 1.0 - albedo.r * 2.0);
287 }
288}
289
290vec2 og_fractal_footprint(vec2 uv, float width) {
291 mat2 m = mat2(vec2(1.6, 1.2), vec2(-1.2, 1.6));
292 float amplitude[6] = float[](0.5, 0.25, 0.125, 0.0625, 0.03125, 0.016625);
293 vec2 f = vec2(0.0);
294 for (int i = 0; i < 6; i++) {
295 float kept = 1.0 - smoothstep(0.4, 1.0, width);
296 f.x += amplitude[i] * kept * og_noise(uv);
297 f.y += pow(amplitude[i] * (1.0 - kept), 2.0);
298 uv = m * uv;
299 width *= 2.0;
300 }
301 return f;
302}
303
304float og_flame_integral(float x) {
305 float a = min(abs(x), 0.16);
306 float b = 0.7 - min(abs(x), 0.7);
307 return sign(x) * (a - a * a / 0.32 + (0.343 - b * b * b) / 3.0);
308}
309
310float og_caustics(vec3 world, vec3 normal, float time) {
311 if (!(define_water_horizon || define_snow_everywhere || define_more_reflect || define_beach || define_sky_ark || define_waterfall_arena)) {
312 return 0.0;
313 }
314 float kb = define_beach ? 16.7 + sin(time * 0.5 - 2.0) * 0.05 : define_waterfall_arena ? 95.0 : og_beach_level();
315 float y = world.y;
316 float m;
317 if (define_waterfall_arena) {
318 m = (kb + 2.0 - y) / 3.0 * 0.3 + 0.3;
319 m = min(1.0, m * m) * clamp(1.0 - normal.y, 0.0, 1.0) * 0.5;
320 } else {
321 m = min(1.0, pow(max(kb + 2.0 - y, 0.0) / 3.0, 2.0));
322 if (define_water_horizon) {
323 m *= max(0.0, 0.2 - normal.y) * 2.0;
324 } else if (define_beach) {
325 m *= max(0.0, 0.2 - normal.y) * 3.0;
326 } else if (define_snow_everywhere) {
327 m *= clamp(1.0 - normal.y, 0.0, 1.0);
328 } else if (define_sky_ark) {
329 m *= (1.0 - normal.y) * 0.5 * (distance(world, vec3(-97.96, 68.4, 101.8)) < 26.0 ? 1.0 : 0.0);
330 }
331 }
332 m *= step(y, kb + 2.0);
333 float footprint = 0.2 * max(length(dFdx(world)), length(dFdy(world)));
334 vec3 t = world * 0.2;
335 float s = 0.6 * time;
336 vec2 fire = og_fractal_footprint(t.xz * 11.0 + s, footprint * 11.0) + og_fractal_footprint(t.xy * 7.0 - s, footprint * 7.0)
337 + og_fractal_footprint(t.yz * 5.0 - s, footprint * 5.0);
338 float h = sqrt(0.25 * (pow(dFdx(fire.x), 2.0) + pow(dFdy(fire.x), 2.0)) + 0.34 * fire.y);
339 float flame;
340 if (h < 1e-4) {
341 float a = abs(fire.x);
342 flame = max(0.0, 1.0 - a / 0.16) + pow(max(0.0, 0.7 - a), 2.0);
343 } else {
344 flame = (og_flame_integral(fire.x + h) - og_flame_integral(fire.x - h)) / (2.0 * h);
345 }
346 return flame * m;
347}
348
349vec3 og_volcano_glow(vec3 world, vec3 normal) {
350 if (!define_volcano) {
351 return vec3(0.0);
352 }
353 float height = max(world.y + 22.0, 1e-3);
354 float light = max(0.0, dot(normalize(vec3(-33.0, -32.0, 2.5) - world), normal));
355 light = mix(light, 1.0, min(1.0, 1.0 / pow(height, 0.7)));
356 return light * vec3(1.0, 0.1, 0.0) / max(1.0, pow(height, define_less_glow ? 1.8 : 1.0)) * 10.0;
357}
358
359vec4 og_fog(vec3 world, vec3 cam, float time, bool is_water) {
360 vec3 relative = world - cam;
361 float dist = length(relative);
362 vec3 dir = relative / max(dist, 1e-5);
363 float haze = og_haze(relative, cam, time);
364 if (is_water && define_water_horizon) {
365 haze = min(1.0, max(haze, define_alt ? length(world.xz - vec2(94.0, 144.0)) / 1000.0
366 : length(world.xz) / (define_alt2 ? 600.0 : 200.0)));
367 }
368 if (define_waterfall_arena && !define_cave) {
369 haze = max(haze, min(1.0, dist * 0.01));
370 }
371 vec3 colour = og_spec(dir, og_fog_lod(dist));
372 if (define_water_horizon && define_alt2) {
373 colour = mix(colour, vec3(1.0), clamp(length(world.xz) / 200.0 - 2.0, 0.0, 1.0));
374 }
375 colour *= og_fog_colour_mult(dir, dist, cam);
376 return vec4(colour, haze);
377}