shader changes & screen quads

This commit is contained in:
Asuro
2025-03-28 00:11:56 +01:00
parent 26ba03b7fe
commit f56ffdaa13
20 changed files with 193 additions and 43 deletions

View File

@@ -3,7 +3,7 @@ $input v_normal
$input v_uv0
$input v_wpos
#include "common.sh"
#include "../common.sh"
SAMPLER2D(s_texColor, 0);
SAMPLER3D(s_ditherSampler, 1);
@@ -107,4 +107,5 @@ void main()
float3 ditheredColor = dither(brightness, v_uv0);
float3 finalColor = mix(u_baseColor, texColor, ditheredColor);
gl_FragColor = vec4(finalColor, 1.0);
}

View File

@@ -1,7 +1,7 @@
$input a_position, a_normal, a_color0, a_texcoord0
$output v_color0, v_normal, v_uv0, v_wpos
#include "common.sh"
#include "../common.sh"
void main()
{

View File

@@ -0,0 +1,112 @@
$input v_color0
$input v_normal
$input v_uv0
$input v_wpos
#include "../common.sh"
SAMPLER2D(s_texColor, 0);
SAMPLER3D(s_ditherSampler, 1);
SAMPLER2D(s_rampSampler, 2);
uniform vec4 u_time;
uniform vec4 u_testColor;
uniform vec4 u_texInfo;
uniform vec4 u_baseColor;
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)));
}
float dither(float brightness, vec2 inputUv)
{
float globalScale = 8;
// constants
float xRes = u_texInfo.z;
float dotsPerSide = xRes / 16;
float dotsTotal = dotsPerSide * dotsPerSide;
float invXRes = 1 / xRes;
float zRes = dotsTotal;
float invZRes = 1 / zRes;
float2 rampLookupUv = vec2((0.5 * invXRes + (1 - invXRes) * brightness), 0.5);
float brightnessCurve = texture2D(s_rampSampler, rampLookupUv).r;
// Magic dot frequency calculation
float2 dx = ddx(inputUv);
float2 dy = ddy(inputUv);
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 scaleExp = exp2(globalScale);
float spacing = freq.y * scaleExp;
spacing *= dotsPerSide * 0.125;
float brightnessSpacingMultiplier = pow(brightnessCurve * 2 + 0.001, -(1 - 0.5));
// float brightnessSpacingMultiplier = 1;
spacing *= brightnessSpacingMultiplier;
float spacingLog = log2(spacing);
int patternScaleLevel = floor(spacingLog);
float patternFractional = spacingLog - patternScaleLevel;
vec2 uv = inputUv / exp2(patternScaleLevel);
// patternFractional *= patternFractional;
// patternFractional = smoothstep(0, 1, patternFractional);
float subLayer = lerp(0.25 * dotsTotal, dotsTotal, 1 - patternFractional);
subLayer = (subLayer - 0.5) * invZRes;
float pattern = texture3D(s_ditherSampler, vec3(uv, subLayer)).r;
float contrast = 2 * scaleExp * brightnessSpacingMultiplier * 0.1;
contrast *= pow(freq.y / freq.x, 1.0);
float baseVal = lerp(0.5, brightness, saturate(1.05 / (1 + contrast)));
float threshold = 1 - brightnessCurve + 0.6;
return saturate((pattern - threshold) * contrast + baseVal);
}
void main()
{
// setup
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);
vec3 texColor = u_testColor.x > 0.1 ? u_testColor.xyz : texture2D(s_texColor, v_uv0).xyz;
// lighting
// float brightness = calcBrightness(lightPos, v_wpos, v_normal);
float brightness = lerp(0.5, 0.9, calcBrightnessDirectional(vec3(0.5, 0.3, 1.0), v_normal));
// float brightness = 0.5;
// brightness = lerp(0.2, 1.0, sin(u_time.x) * 0.5 + 0.5);
float r = dither(brightness * texColor.r, v_uv0);
float g = dither(brightness * texColor.g, v_uv0);
float b = dither(brightness * texColor.b, v_uv0);
// float3 finalColor = vec3(r, g, b);
float3 ditheredColor = dither(brightness, v_uv0);
float3 finalColor = mix(u_baseColor, texColor, ditheredColor);
gl_FragColor = vec4(finalColor, 1.0);
gl_FragColor = vec4(texColor, 1.0);
}

View File

@@ -0,0 +1,9 @@
vec4 v_color0 : COLOR0 = vec4(1.0, 1.0, 0.0, 1.0);
vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0);
vec2 v_uv0 : TEXCOORD0 = vec2(0.0, 0.0);
vec3 v_wpos : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
vec3 a_position : POSITION;
vec3 a_normal : NORMAL;
vec4 a_color0 : COLOR0;
vec2 a_texcoord0 : TEXCOORD0;

View File

@@ -0,0 +1,13 @@
$input a_position, a_normal, a_color0, a_texcoord0
$output v_color0, v_normal, v_uv0, v_wpos
#include "../common.sh"
void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
v_color0 = a_color0;
v_uv0 = a_texcoord0;
v_wpos = mul(u_model[0], vec4(a_position, 1.0)).xyz;
v_normal = normalize(mul(u_model[0], a_normal));
}