dust3d/application/shaders/model_core.frag

76 lines
2.3 KiB
GLSL
Raw Normal View History

2022-09-20 12:48:22 +00:00
#version 330
2022-09-22 13:07:27 +00:00
uniform samplerCube environmentIrradianceMapId;
uniform samplerCube environmentSpecularMapId;
2022-09-24 13:31:49 +00:00
uniform sampler2D textureId;
uniform int textureEnabled;
uniform sampler2D normalMapId;
uniform int normalMapEnabled;
uniform sampler2D metalnessRoughnessAoMapId;
uniform int metalnessMapEnabled;
uniform int roughnessMapEnabled;
uniform int aoMapEnabled;
uniform vec3 eyePosition;
2022-09-21 12:14:02 +00:00
in vec3 pointPosition;
in vec3 pointNormal;
in vec3 pointColor;
2022-09-24 13:31:49 +00:00
in vec2 pointTexCoord;
2022-09-21 12:14:02 +00:00
in float pointAlpha;
in float pointMetalness;
in float pointRoughness;
2022-09-24 13:31:49 +00:00
in mat3 pointTBN;
2022-09-20 12:48:22 +00:00
out vec4 fragColor;
2022-09-22 13:07:27 +00:00
const float PI = 3.1415926;
vec3 fresnelSchlickRoughness(float NoV, vec3 f0, float roughness)
2022-09-22 13:07:27 +00:00
{
return f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(clamp(1.0 - NoV, 0.0, 1.0), 5.0);
2022-09-22 13:07:27 +00:00
}
2022-09-20 12:48:22 +00:00
void main()
{
2022-09-24 13:31:49 +00:00
vec3 color = pointColor;
float alpha = pointAlpha;
if (1 == textureEnabled) {
vec4 textColor = texture(textureId, pointTexCoord);
color = textColor.rgb;
alpha = textColor.a;
}
vec3 normal = pointNormal;
if (1 == normalMapEnabled) {
normal = texture(normalMapId, pointTexCoord).rgb;
normal = pointTBN * normalize(normal * 2.0 - 1.0);
}
float metalness = pointMetalness;
if (1 == metalnessMapEnabled) {
metalness = texture(metalnessRoughnessAoMapId, pointTexCoord).b;
}
float roughness = pointRoughness;
if (1 == roughnessMapEnabled) {
roughness = texture(metalnessRoughnessAoMapId, pointTexCoord).g;
}
float ambientOcclusion = 1.0;
if (1 == aoMapEnabled) {
ambientOcclusion = texture(metalnessRoughnessAoMapId, pointTexCoord).r;
}
vec3 n = normal;
2022-09-22 13:07:27 +00:00
vec3 v = normalize(eyePosition - pointPosition);
vec3 r = reflect(-v, n);
float NoV = abs(dot(n, v)) + 1e-5;
vec3 irradiance = texture(environmentIrradianceMapId, r).rgb;
2022-09-24 13:31:49 +00:00
vec3 diffuse = irradiance * (1.0 - metalness) * pointColor;
2022-09-22 13:07:27 +00:00
2022-09-24 13:31:49 +00:00
vec3 f0 = mix(vec3(0.04), pointColor, metalness);
vec3 fresnelFactor = fresnelSchlickRoughness(NoV, f0, roughness);
vec3 specular = fresnelFactor * texture(environmentSpecularMapId, r, 0.0).rgb;
2022-09-22 13:07:27 +00:00
2022-09-24 13:31:49 +00:00
color = (diffuse + specular) * ambientOcclusion;
2022-09-22 13:07:27 +00:00
color = color / (color + vec3(1.0));
2022-09-22 13:07:27 +00:00
color = pow(color, vec3(1.0/2.2));
2022-09-24 13:31:49 +00:00
fragColor = vec4(color, alpha);
2022-09-20 12:48:22 +00:00
}