Improved caustic shaders

This commit is contained in:
Joshua Barretto 2022-09-22 21:12:20 +01:00
parent 1452dee886
commit b48b3eea02
2 changed files with 36 additions and 3 deletions

View File

@ -119,4 +119,36 @@ float norm2tri(float n) {
n = flip ? 1.0 - n : n;
return n;
}
// Caustics, ported and modified from https://www.shadertoy.com/view/3tlfR7, originally David Hoskins.
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License: https://creativecommons.org/licenses/by-nc-sa/3.0/legalcode.
// Modifying these three functions mean that you agree to release your changes under the above license, *not* under GPL 3 as with the rest of the project.
float hashvec2(vec2 p) {return fract(sin(p.x * 1e2 + p.y) * 1e5 + sin(p.y * 1e3) * 1e3 + sin(p.x * 735. + p.y * 11.1) * 1.5e2); }
float n12(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f *= f * (3.-2.*f);
return mix(
mix(hashvec2(i+vec2(0.,0.)),hashvec2(i+vec2(1.,0.)),f.x),
mix(hashvec2(i+vec2(0.,1.)),hashvec2(i+vec2(1.,1.)),f.x),
f.y
);
}
float caustics(vec2 p, float t) {
vec3 k = vec3(p,t);
float l;
mat3 m = mat3(-2.,-1.,2.,3.,-2.,1.,1.,2.,2.);
float n = n12(p);
k = k*m*.5;
l = length(.5 - fract(k+n));
k = k*m*.4;
l = min(l, length(.5-fract(k+n)));
k = k*m*.3;
l = min(l, length(.5-fract(k+n)));
return pow(l,4.)*5.5;
}
#endif

View File

@ -399,9 +399,10 @@ void main() {
#if (FLUID_MODE == FLUID_MODE_SHINY)
if (faces_fluid) {
vec3 wpos = f_pos + vec3(focus_off.xy, 0);
vec3 spos = (wpos + (fluid_alt - wpos.z) * vec3(sun_dir.xy, 0)) * 0.05;
reflected_light += max(1.0 - pow(abs(noise_3d(vec3(spos.xy, tick.x * 0.1 + dot(sin(wpos.xy * 0.8), vec2(1)) * 0.05)) - 0.5) * 10, 0.001), 0)
* 1000
vec3 spos = (wpos + (fluid_alt - wpos.z) * vec3(sun_dir.xy, 0)) * 0.25;
reflected_light += caustics(spos.xy * 1.0, tick.x * 0.5)
* 2
* (1.0 + abs(fluid_alt - wpos.z) * 0.2)
* cam_attenuation
* max(dot(f_norm, -sun_dir.xyz), 0)
* sun_diffuse