stick-the-quick/vfx/util.gdshaderinc

199 lines
4.4 KiB
Plaintext
Raw Normal View History

#define TEXTURE_HINTS source_color, filter_linear, repeat_enable
#define GROUND_TEXTURE_HINTS source_color, filter_linear, repeat_disable
#define UNIVERSAL_SOLID_PARAMS \
uniform float wet = 0.0;
#define UNIVERSAL_SOLID_PROCESSING \
METALLIC = mix(METALLIC, 1.0, wet); \
SPECULAR = mix(SPECULAR, 1.0, wet); \
ROUGHNESS = mix(ROUGHNESS, 0.0, wet); \
CLEARCOAT = mix(CLEARCOAT, 1.0, wet); \
CLEARCOAT_ROUGHNESS = mix(CLEARCOAT_ROUGHNESS, 0.0, wet);
float fs2u(float f) {
return (f + 1.0)/2.0;
}
float fu2s(float f) {
return 2.0*f - 1.0;
}
vec2 v2s2u(vec2 v) {
return (v + vec2(1.0, 1.0))/2.0;
}
vec2 v2u2s(vec2 v) {
return 2.0*v - vec2(1.0, 1.0);
}
vec3 v3s2u(vec3 v) {
return (v + vec3(1.0, 1.0, 1.0))/2.0;
}
vec3 v3u2s(vec3 v) {
return 2.0*v - vec3(1.0, 1.0, 1.0);
}
vec4 v4s2u(vec4 v) {
return (v + vec4(1.0, 1.0, 1.0, 1.0))/2.0;
}
vec4 v4u2s(vec4 v) {
return 2.0*v - vec4(1.0, 1.0, 1.0, 1.0);
}
vec3 project(vec3 a, vec3 b) {
float lb = length(b);
return dot(a, b)*b/(lb*lb);
}
vec3 resolute(vec3 a, vec3 b) {
return a - project(a, b);
}
ivec3 sortv3(vec3 v) {
if (v.x <= v.y) {
if (v.x <= v.z) {
if (v.y <= v.z) {
return ivec3(0, 1, 2);
} else {
return ivec3(0, 2, 1);
}
} else {
return ivec3(2, 0, 1);
}
} else if (v.x <= v.z) {
return ivec3(1, 0, 2);
} else if (v.y <= v.z) {
return ivec3(1, 2, 0);
} else {
return ivec3(2, 1, 0);
}
}
vec3 rgb2hsv(vec3 rgb) {
vec3 hsv = vec3(0.0, 0.0, 0.0);
ivec3 sorti = sortv3(rgb);
vec3 sortf = vec3(rgb[sorti.x], rgb[sorti.y], rgb[sorti.z]);
sortf.y = sortf.z - sortf.x;
if (sortf.y >= 0.001) {
switch (sorti.z) {
case 0: {
hsv.x = fract((rgb.g - rgb.b)/(6.0*sortf.y));
} break;
case 1: {
hsv.x = ((rgb.b - rgb.r)/sortf.y + 2.0)/6.0;
} break;
case 2: default: {
hsv.x = ((rgb.r - rgb.g)/sortf.y + 4.0)/6.0;
} break;
}
hsv.y = sortf.y/sortf.z;
}
hsv.z = sortf.z;
return hsv;
}
vec3 hsv2rgb(vec3 hsv) {
float c = hsv.y*hsv.z;
float h6 = mod(6.0*hsv.x, 6.0);
float x = c*(1.0 - abs(mod(h6, 2.0) - 1.0));
float m = hsv.z - c;
switch (int(h6)) {
case 0: return vec3(c + m, x + m, m);
case 1: return vec3(x + m, c + m, m);
case 2: return vec3(m, c + m, x + m);
case 3: return vec3(m, x + m, c + m);
case 4: return vec3(x + m, m, c + m);
case 5: default: return vec3(c + m, m, x + m);
}
}
float prime_noise(float theta) {
return (
sin(2.0*theta + 131.0) +
cos(3.0*theta + 127.0) -
sin(5.0*theta + 113.0) -
cos(7.0*theta + 109.0) +
sin(11.0*theta + 107.0) +
cos(13.0*theta + 103.0) -
sin(17.0*theta + 101.0) -
cos(19.0*theta + 97.0) +
sin(23.0*theta + 89.0) +
cos(29.0*theta + 83.0) -
sin(31.0*theta + 79.0) -
cos(37.0*theta + 73.0) +
sin(41.0*theta + 71.0) +
cos(43.0*theta + 67.0) -
sin(47.0*theta + 61.0) -
cos(53.0*theta + 59.0) +
sin(59.0*theta + 53.0) +
cos(61.0*theta + 47.0) -
sin(67.0*theta + 43.0) -
cos(71.0*theta + 41.0) +
sin(73.0*theta + 37.0) +
cos(79.0*theta + 31.0) -
sin(83.0*theta + 29.0) -
cos(89.0*theta + 23.0) +
sin(97.0*theta + 19.0) +
cos(101.0*theta + 17.0) -
sin(103.0*theta + 13.0) -
cos(107.0*theta + 11.0) +
sin(109.0*theta + 7.0) +
cos(113.0*theta + 5.0) -
sin(127.0*theta + 3.0) -
cos(131.0*theta + 2.0)
)/32.0;
}
float recursive_prime_noise(float seed, int iterations) {
for (int i = 0; i < iterations; i++) {
seed = PI*prime_noise(seed);
}
return seed/PI;
}
float vec2_prime_noise(vec2 v, float seed, int iterations) {
return recursive_prime_noise(
PI*recursive_prime_noise(v.x, iterations) +
PI*recursive_prime_noise(v.y, iterations) +
seed,
iterations
);
}
float vec2_optimal_prime_noise(vec2 v, float seed) {
return vec2_prime_noise(v, seed, 3);
}
float power_of_2_wave_noise(float theta, int depth) {
float sum = 0.0;
float factor = 1.0;
float divisor = 0.0;
for (int i = 0; i < depth; i++) {
factor *= 2.0;
sum += sin(factor*theta)/factor;
divisor += 1.0/factor;
}
return sum/divisor;
}
vec4 alpha_blend(vec4 above, vec4 below) {
return vec4(
above.rgb*above.a + below.rgb*(1.0 - above.a),
above.a + below.a*(1.0 - above.a)
);
}
float linearize_depth(
float depth,
vec2 screen_uv,
mat4 inv_projection_matrix
) {
/* Code taken, with gratitude, from official Godot tutorial:
https://docs.godotengine.org/en/stable/tutorials\
/shaders/advanced_postprocessing.html#depth-texture */
vec3 ndc = vec3(screen_uv * 2.0 - 1.0, depth);
vec4 view = inv_projection_matrix * vec4(ndc, 1.0);
view.xyz /= view.w;
return -view.z;
}