Files
PuzGame/src/game/shaders/frag.sc
2025-02-26 01:20:54 +01:00

74 lines
2.3 KiB
Scala

$input v_color0
$input v_normal
$input v_uv0
$input v_wpos
#include "common.sh"
SAMPLER2D(s_texColor, 0);
SAMPLER3D(s_ditherSampler, 1);
uniform vec4 u_time;
uniform vec4 u_testColor;
float calcBrightness(vec3 lightPos, vec3 vertPos, vec3 normal)
{
vec3 lightDir = normalize(lightPos - vertPos);
float diffuse = max(0.0, dot(lightDir, normal));
return diffuse * clamp(1.0 - distance(lightPos, vertPos) * 0.01, 0.0, 1.0);
}
float calcBrightnessDirectional(vec3 sunDir, vec3 normal)
{
return max(0.0, dot(sunDir, normal));
}
vec3 desaturate(vec3 color)
{
return vec3_splat(dot(color, vec3(0.33, 0.34, 0.33)));
}
void main()
{
float testRadius = 30.0;
float testSpeed = 1.0;
vec3 testOffset = vec3(0.0, 0.0, 50.0);
float3 lightPos = vec3(sin(u_time.x * testSpeed) * testRadius, 5.0, cos(u_time.x * testSpeed) * testRadius);
// float brightness = calcBrightness(lightPos, v_wpos, v_normal);
float brightness = calcBrightnessDirectional(vec3(0.5, 0.3, 1.0), v_normal);
brightness = 1.0;
// brightness = sin(u_time.x) * 0.5 + 0.5;
// Magic dot frequency calculation
float baseScale = 8.0;
float2 dx = ddx(v_uv0 * baseScale);
float2 dy = ddy(v_uv0 * baseScale);
float2x2 mat = float2x2(dx, dy);
float4 vectorized = float4(dx, dy);
float qq = dot(vectorized, vectorized);
float rr = determinant(mat);
float discriminantSqr = max(0.0, qq*qq - 4.0*rr*rr);
float discriminant = sqrt(discriminantSqr);
float2 freq = sqrt(float2(qq + discriminant, qq - discriminant) / 2.0);
// Figuring out how many dots we want
float spacing = freq.y * exp2(2.0);
spacing = 1.0 / spacing;
spacing *= brightness; // TODO: check reference to see how to calculate this!
float spacingLog = max(log2(spacing), 0.0);
int patternScaleLevel = floor(spacingLog);
float patternFractional = spacingLog - patternScaleLevel;
vec2 uv = v_uv0 * exp2(patternScaleLevel);
float dotsTotal = 4;
float subLayer = lerp(0.25 * dotsTotal, dotsTotal, patternFractional);
subLayer = (subLayer - 0.5) / dotsTotal;
vec4 circleSample = texture3D(s_ditherSampler, vec3(uv, subLayer));
float circleVal = circleSample.x < 0.75 ? 0.1 : 0.9;
// Coloring
vec3 texColor = u_testColor.x > 0.1 ? u_testColor.xyz : texture2D(s_texColor, v_uv0).xyz;
vec3 finalColor = texColor * vec3(circleVal, circleVal, circleVal);
gl_FragColor = vec4(finalColor, 1.0);
}