Continued work on basic PBR example
This commit is contained in:
parent
d1c07df7c0
commit
ed6451a956
9 changed files with 209 additions and 251 deletions
|
|
@ -1,9 +1,5 @@
|
|||
#version 450
|
||||
|
||||
layout (binding = 1) uniform samplerCube envmap;
|
||||
layout (binding = 2) uniform samplerCube envmapibldiff;
|
||||
layout (binding = 3) uniform samplerCube envmapiblrefl;
|
||||
|
||||
layout (location = 0) in vec3 inWorldPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
|
@ -16,6 +12,10 @@ layout (binding = 0) uniform UBO
|
|||
vec3 camPos;
|
||||
} ubo;
|
||||
|
||||
layout (binding = 1) uniform UBOShared {
|
||||
vec4 lights[4];
|
||||
} uboParams;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
layout(push_constant) uniform PushConsts {
|
||||
|
|
@ -30,117 +30,98 @@ const float PI = 3.14159265359;
|
|||
|
||||
//#define ROUGHNESS_PATTERN 1
|
||||
|
||||
// Fresnel ------------------------------------------------------------------------
|
||||
|
||||
float fresnelSchlick(float ct, float F0)
|
||||
vec3 materialcolor()
|
||||
{
|
||||
return F0 + (1.0 - F0) * pow(1.0 - ct, 5.0);
|
||||
return vec3(material.r, material.g, material.b);
|
||||
}
|
||||
|
||||
// Normal distribution functions ---------------------------------------------------
|
||||
|
||||
float NDF_blinnPhong(float dotNH, float alphaSqr)
|
||||
{
|
||||
return 1.0 / (PI * alphaSqr) * pow(dotNH, 2.0 / alphaSqr - 2.0);
|
||||
}
|
||||
|
||||
float NDF_beckmann(float dotNH, float alphaSqr)
|
||||
{
|
||||
float dotNH2 = dotNH * dotNH;
|
||||
return 1.0 / (PI * alphaSqr * dotNH2 * dotNH2) * exp((dotNH2 - 1.0) / (alphaSqr * dotNH2));
|
||||
}
|
||||
|
||||
float NDF_GGX(float dotNH, float alphaSqr)
|
||||
{
|
||||
return alphaSqr / (PI * pow(dotNH * dotNH * (alphaSqr - 1.0) + 1.0, 2.0));
|
||||
}
|
||||
|
||||
// Geometry visibility functions ---------------------------------------------------
|
||||
|
||||
float GEOM_SchlickSmith(float dotNL, float dotNV, float alpha)
|
||||
{
|
||||
//float k = alpha * sqrt(2.0 / PI);
|
||||
float k = pow(0.8 + 0.5 * alpha, 2.0) / 2.0;
|
||||
float GL = 1.0 / (dotNL * (1.0 - k) + k);
|
||||
float GV = 1.0 / (dotNV * (1.0 - k) + k);
|
||||
return GL * GV;
|
||||
|
||||
}
|
||||
|
||||
float PBR_Shade(vec3 N, vec3 V, vec3 L, float roughness, float F0)
|
||||
// Normal Distribution function --------------------------------------
|
||||
float D_GGX(float dotNH, float roughness)
|
||||
{
|
||||
float alpha = roughness * roughness;
|
||||
float alphaSqr = alpha * alpha;
|
||||
float alpha2 = alpha * alpha;
|
||||
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
|
||||
return (alpha2)/(PI * denom*denom);
|
||||
}
|
||||
|
||||
// Geometric Shadowing function --------------------------------------
|
||||
float G_SchlickmithGGX(float dotNL, float dotNV, float roughness)
|
||||
{
|
||||
float r = (roughness + 1.0);
|
||||
float k = (r*r) / 8.0;
|
||||
float GL = dotNL / (dotNL * (1.0 - k) + k);
|
||||
float GV = dotNV / (dotNV * (1.0 - k) + k);
|
||||
return GL * GV;
|
||||
}
|
||||
|
||||
// Fresnel function ----------------------------------------------------
|
||||
vec3 F_Schlick(float cosTheta, float metallic)
|
||||
{
|
||||
vec3 F0 = mix(vec3(0.04), materialcolor(), metallic); // * material.specular
|
||||
vec3 F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||
return F;
|
||||
}
|
||||
|
||||
// Specular BRDF composition --------------------------------------------
|
||||
|
||||
vec3 BRDF(vec3 L, vec3 V, vec3 N, float metallic, float roughness)
|
||||
{
|
||||
// Precalculate vectors and dot products
|
||||
vec3 H = normalize (V + L);
|
||||
|
||||
float dotNL = clamp(dot(N, L), 0.0, 1.0);
|
||||
float dotNV = clamp(dot(N, V), 0.0, 1.0);
|
||||
float dotNH = clamp(dot(N, H), 0.0, 1.0);
|
||||
float dotNL = clamp(dot(N, L), 0.0, 1.0);
|
||||
float dotLH = clamp(dot(L, H), 0.0, 1.0);
|
||||
float dotNH = clamp(dot(N, H), 0.0, 1.0);
|
||||
|
||||
// Normal distribution
|
||||
float Di = NDF_GGX(dotNH, alphaSqr);
|
||||
// Fresnel
|
||||
float Fs = fresnelSchlick(dotNV, F0);
|
||||
// Visibility term
|
||||
float Vs = GEOM_SchlickSmith(dotNL, dotNV, alpha);
|
||||
// Light color fixed
|
||||
vec3 lightColor = vec3(1.0);
|
||||
|
||||
return Di * Fs * Vs;
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
if (dotNL > 0.0)
|
||||
{
|
||||
float rroughness = max(0.05, roughness);
|
||||
// D = Normal distribution (Distribution of the microfacets)
|
||||
float D = D_GGX(dotNH, roughness);
|
||||
// G = Geometric shadowing term (Microfacets shadowing)
|
||||
float G = G_SchlickmithGGX(dotNL, dotNV, roughness);
|
||||
// F = Fresnel factor (Reflectance depending on angle of incidence)
|
||||
vec3 F = F_Schlick(dotNV, metallic);
|
||||
|
||||
vec3 spec = D * F * G / (4.0 * dotNL * dotNV);
|
||||
|
||||
color += spec * dotNL * lightColor;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void main()
|
||||
{
|
||||
// Partially based on https://www.shadertoy.com/view/XsfXWX by Alexander Alekseev (https://github.com/tdmaav)
|
||||
|
||||
// One fixed light source
|
||||
vec3 lightPos = vec3(-10.0f, -10.0f, 10.0f);
|
||||
// Take light color from environment map
|
||||
vec3 lightColor = texture(envmapiblrefl, vec3(0.5)).xyz;
|
||||
|
||||
{
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 V = normalize(ubo.camPos - inWorldPos);
|
||||
vec3 L = normalize(lightPos - inWorldPos);
|
||||
vec3 R = reflect(-V, N);
|
||||
|
||||
// Store material values for quick testing/changing inside the shader
|
||||
float roughness = material.roughness;
|
||||
float metallic = material.metallic;
|
||||
|
||||
// Add striped pattern to roughness based on vertex position
|
||||
#ifdef ROUGHNESS_PATTERN
|
||||
roughness = max(roughness, step(fract(inWorldPos.y * 2.02), 0.5));
|
||||
#endif
|
||||
|
||||
// Get IBL components from cube maps
|
||||
vec3 IBLdiffuse = texture(envmapibldiff, inNormal).rgb;
|
||||
vec3 IBLreflection = texture(envmapiblrefl, inNormal).rgb;
|
||||
// Specular contribution
|
||||
vec3 Lo = vec3(0.0);
|
||||
for (int i = 0; i < uboParams.lights.length(); i++) {
|
||||
vec3 L = normalize(uboParams.lights[i].xyz - inWorldPos);
|
||||
Lo += BRDF(L, V, N, material.metallic, roughness);
|
||||
};
|
||||
|
||||
// Fresnel part
|
||||
float fresnel = pow(max(1.0 - abs(dot(N, V)), 0.0), 1.5f + roughness);
|
||||
|
||||
// Reflection part
|
||||
// Combine with ambient
|
||||
vec3 color = materialcolor() * 0.02;
|
||||
color += Lo;
|
||||
|
||||
// Select mip level based on roughness
|
||||
ivec2 dim = textureSize(envmap, 0);
|
||||
float nummips = log2(max(dim.s, dim.y));
|
||||
vec3 reflection = texture(envmap, R).xyz;
|
||||
reflection = textureLod(envmap, R, max(roughness * nummips, textureQueryLod(envmap, R).y)).rgb;
|
||||
reflection = mix(reflection, IBLreflection, (1.0-fresnel) * roughness);
|
||||
reflection = mix(reflection, IBLreflection, roughness);
|
||||
|
||||
// Specular part
|
||||
// F0 based on metallic factor of material
|
||||
vec3 F0 = vec3(0.04);
|
||||
F0 = mix(F0, lightColor, material.metallic);
|
||||
vec3 spec = lightColor * PBR_Shade(N, V, L, roughness, F0.r);
|
||||
reflection -= spec;
|
||||
|
||||
// Diffuse part
|
||||
vec3 matColor = vec3(material.r, material.g, material.b);
|
||||
vec3 diffuse = mix(IBLdiffuse * matColor, reflection, fresnel);
|
||||
// Gamma correct
|
||||
color = pow(color, vec3(0.4545));
|
||||
|
||||
// Final output mixes based on material metalness
|
||||
outColor.rgb = mix(diffuse, reflection, metallic) + spec;
|
||||
outColor = vec4(color, 1.0);
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1,12 +0,0 @@
|
|||
#version 450
|
||||
|
||||
layout (binding = 1) uniform samplerCube samplerEnv;
|
||||
|
||||
layout (location = 0) in vec3 inUVW;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
outFragColor = texture(samplerEnv, inUVW);
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1,27 +0,0 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 model;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outUVW;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
outUVW = inPos;
|
||||
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
}
|
||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue