Files
vr-poser/assets/shaders/toon.frag
2026-03-15 22:03:30 -04:00

57 lines
1.5 KiB
GLSL

#version 130
varying vec3 v_normalVS;
varying vec3 v_posVS;
varying vec2 v_uv;
varying vec4 v_color;
uniform sampler2D osg_Sampler0;
uniform bool u_hasTexture;
uniform vec3 u_lightDirVS;
uniform vec3 u_lightColor;
uniform vec3 u_ambientColor;
uniform vec3 u_outlineColor;
uniform bool u_outlinePass;
uniform int u_bands;
uniform float u_specularThreshold;
uniform float u_specularIntensity;
uniform float u_rimThreshold;
uniform float u_rimIntensity;
uniform vec3 u_rimColor;
float celStep(float value, int bands) {
return floor(value * float(bands) + 0.5) / float(bands);
}
void main() {
if (u_outlinePass) {
gl_FragColor = vec4(u_outlineColor, 1.0);
return;
}
vec3 N = normalize(v_normalVS);
vec3 L = normalize(u_lightDirVS);
vec3 V = normalize(-v_posVS);
// Cel diffuse
float NdL = dot(N, L) * 0.5 + 0.5;
float celDif = celStep(NdL, u_bands);
// Snap specular
vec3 H = normalize(L + V);
float NdH = max(dot(N, H), 0.0);
float spec = step(u_specularThreshold, NdH) * u_specularIntensity;
// Rim light
float rim = 1.0 - max(dot(N, V), 0.0);
rim = step(u_rimThreshold, rim) * u_rimIntensity;
vec4 base = u_hasTexture ? texture2D(osg_Sampler0, v_uv) : v_color;
vec3 ambient = base.rgb * u_ambientColor;
vec3 diffuse = base.rgb * u_lightColor * celDif;
vec3 specCol = u_lightColor * spec;
vec3 rimCol = u_rimColor * rim;
gl_FragColor = vec4(ambient + diffuse + specCol + rimCol, base.a);
}