Files
gren/assets/shaders/mesh.frag
2026-01-09 13:50:52 +01:00

35 lines
811 B
GLSL

#version 450 core
layout(location = 0) out vec4 oColor;
layout(std140, binding = 0) uniform PerFrame {
mat4 uView;
mat4 uProj;
vec4 uLightDir;
};
layout(std140, binding = 2) uniform Material {
vec4 uAlbedo; // rgb used, a padding
};
in VS_OUT {
vec3 wPos;
vec3 wNrm;
vec2 uv;
flat int hasNrm;
flat int hasUV;
} v;
vec3 fallbackNormal(vec3 wPos) {
vec3 dx = dFdx(wPos), dy = dFdy(wPos);
vec3 c = cross(dx, dy);
float l2 = dot(c, c);
return (l2 < 1e-12) ? vec3(0, 0, 1) : normalize(c);
}
void main() {
vec3 N = (v.hasNrm != 0) ? normalize(v.wNrm) : fallbackNormal(v.wPos);
vec3 L = normalize(-uLightDir.xyz);
float ndl = max(dot(N, L), 0.0);
vec3 ambient = vec3(0.08);
vec3 colorLinear = ambient + uAlbedo.rgb * ndl;
oColor = vec4(pow(colorLinear, vec3(1.0/2.2)), 1.0);
}