Files
HydraV3/Media/forward_pbr.frag
2026-07-17 15:35:16 +01:00

186 lines
5.2 KiB
GLSL

//*PIXEL*
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
precision mediump float;
//*UBO*:perFrame:0:SHADER
//*UBOEL*:uView:mat4
//*UBOEL*:uViewerPos:vec3
//*UBOEL*:uViewerDir:vec3
//*UBOEL*:uDirectionalLight:vec3
//*UBOEND*
layout (std140, binding = 0) uniform perFrame
{
mat4 view; // 64 : 64
vec3 viewerPosition; // 16 : 80
vec3 viewerDirection; // 16 : 96
vec3 directionalLight; // 16
} PerFrameUBO;
layout (binding = 2) uniform sampler2D samplerDiffuse; //*TEX*:BASECOLOR:2
layout (binding = 3) uniform sampler2D samplerNormal; //*TEX*:NORMAL:3
layout (binding = 4) uniform sampler2D samplerMetallic; //*TEX*:METALLIC:4
layout (binding = 5) uniform sampler2D samplerRoughness; //*TEX*:ROUGHNESS:5
layout (location = 0) in vec3 inWorldPos;
layout (location = 1) in vec4 inColor;
layout (location = 2) in vec3 inNormal;
layout (location = 3) in vec2 inUV;
layout (location = 4) in vec3 inTangent;
layout (location = 5) in vec3 inBiTangent;
layout (location = 6) in vec3 inTransformedPos;
layout (location = 7) in mat3 inTBN;
layout (location = 0) out vec4 uFragColor;
const float PI = 3.14159265358979323846;
const float M_INV_PI = 0.31830988618379067153776752674503;
float sqr(float x) { return x*x; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Normal distribution functions - D term
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float BeckmannNDF( float t, float m)
{
float M = m*m;
float T = t*t;
return exp((T - 1) / (M*T)) / (M*T*T);
}
float GGXNDF(float nDotH, float roughness)
{
float alpha2 = roughness * roughness;
alpha2 = alpha2 * alpha2;
float denominator = (nDotH * nDotH) * (alpha2 - 1) + 1;
denominator = denominator * denominator;
return alpha2 / ((PI * denominator));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Geometric attenuation functions - G Term
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float G_GGX(float nDotV, float roughness)
{
float alpha = roughness * roughness;
float alpha2 = sqr(alpha);
float numerator = 2* nDotV;
float denominator = nDotV + sqrt(alpha2 + (1 - alpha2) * sqr(nDotV) );
return numerator / denominator;
}
float smithG_GGX(float nDotV, float alphaG)
{
return 2/(1 + sqrt(1 + alphaG*alphaG * (1-nDotV*nDotV)/(nDotV*nDotV)));
}
vec3 Fresnel(vec3 F0, float vDotH)
{
float sphg = pow(2.0, (-5.55473*vDotH - 6.98316) * vDotH);
return F0 + (vec3(1.0, 1.0, 1.0) - F0) * sphg;
}
float FresnelSchlick(float F0, float vDotH)
{
float fresnel = F0 + (1 - F0) * pow(1 - vDotH,5);
return fresnel;
}
vec3 BRDF(vec3 l, vec3 v, vec3 n, float roughness, float F0)
{
vec3 h = normalize(l + v);
float nDotH = dot(n, h);
float nDotV = dot(n, v);
float nDotL = dot(n, l);
float vDotH = dot(v, h);
//roughness = 1.0f - roughness;
float D = GGXNDF(nDotH, roughness);
float G = G_GGX(nDotV, roughness);
float F = FresnelSchlick(F0, vDotH);
float brdfValue = (D * G * F);
return vec3(brdfValue);
}
vec3 GenerateDiffuseColor(vec3 baseColor, float metallic)
{
return baseColor * (1.0 - (metallic / 1.0));
}
vec3 GenerateSpecularColor(float specularLevel, vec3 baseColor, float metallic)
{
return mix(vec3(0.08 * specularLevel), baseColor, metallic);
}
void main()
{
vec3 normal;
vec4 normalTex;
vec4 diffuseTex;
vec4 positionTex;
vec4 lightIntensity;
vec4 outputColor;
vec4 roughnessTex;
vec4 metallicTex;
vec3 viewDir;
vec3 lightDir;
vec3 specularIntensity;
normal = inNormal.xyz; //normalize(((normalTex.x * inTangent.xyz) + (normalTex.y * inBiTangent.xyz) + (normalTex.z * inNormal.xyz)));
if (length(normal) > 0)
{
diffuseTex = texture(samplerDiffuse, inUV);
normalTex = texture(samplerNormal, inUV);
roughnessTex = texture(samplerRoughness, inUV);
metallicTex = texture(samplerMetallic, inUV);
specularIntensity = vec3(1.0f, 1.0f, 1.0f); //specularTexture.Sample(SampleTypePoint, input.tex);
vec3 lightIntensity = vec3(0.7f, 0.7f, 0.7f);
vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);
// normal = normalTex.xyz;
// normal = (normal * 2.0) - 1.0;
// normal = normalize(inTBN * normal);
vec3 lightPos = vec3(1000,1000,1000);
lightDir = normalize(inWorldPos.xyx - lightPos);
viewDir = normalize(PerFrameUBO.viewerPosition - inWorldPos.xyx);
vec3 diffuseColor = GenerateDiffuseColor(diffuseTex.xyz, metallicTex[0]);
vec3 specColor = GenerateSpecularColor(1.0f, diffuseTex.xyz, metallicTex[0]);
vec3 diffuse = clamp(diffuseColor * dot(vec4(lightDir,1), vec4(normal,1)), 0, 1);
vec3 spec = BRDF(lightDir, viewDir, normal, roughnessTex[0], 1.8f);
spec = clamp(spec * (specularIntensity * specColor), 0.0f, 1.0f);
vec3 combinedColor = (spec + diffuse) * lightIntensity * lightColor;
uFragColor =vec4(combinedColor.xyz, 1.0f);
uFragColor =vec4(diffuse, 1.0f);
}
else if (length(normal) == 0)
{
uFragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
uFragColor = vec4(0.5f, 0.5f, 0.5f, 1.0f);
}
}