Reworked inline uniform block example

This commit is contained in:
saschawillems 2018-10-07 10:27:28 +02:00
parent f1a7e66de1
commit 367fce5b46
9 changed files with 349 additions and 302 deletions

View file

@ -1,23 +0,0 @@
#version 450
layout (set = 0, binding = 2) uniform sampler2D samplerColorMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec3 inViewVec;
layout (location = 4) in vec3 inLightVec;
layout (location = 0) out vec4 outFragColor;
void main()
{
vec3 color = texture(samplerColorMap, inUV).rgb * inColor;
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N);
float diffuse = max(dot(N, L), 0.1);
outFragColor = vec4(color * diffuse, 1.0);
}

View file

@ -1,40 +0,0 @@
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec3 inColor;
layout (set = 0, binding = 0) uniform UBOMatrices {
mat4 projection;
mat4 view;
mat4 model;
} uboMatrices;
layout (set = 0, binding = 1) uniform UniformInline {
vec4 color;
} uniformInline;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec2 outUV;
layout (location = 3) out vec3 outViewVec;
layout (location = 4) out vec3 outLightVec;
out gl_PerVertex {
vec4 gl_Position;
};
void main()
{
outColor = inColor * uniformInline.color.rgb;
outUV = inUV;
gl_Position = uboMatrices.projection * uboMatrices.view * uboMatrices.model * vec4(inPos.xyz, 1.0);
vec4 pos = uboMatrices.model * vec4(inPos, 1.0);
outNormal = mat3(transpose(inverse(uboMatrices.model))) * normalize(inNormal);
vec3 lightPos = vec3(0.0f, -25.0f, 25.0f);
vec3 lPos = mat3(uboMatrices.model) * lightPos.xyz;
outLightVec = lPos - pos.xyz;
outViewVec = -pos.xyz;
}

View file

@ -0,0 +1,116 @@
#version 450
layout (location = 0) in vec3 inWorldPos;
layout (location = 1) in vec3 inNormal;
layout (set = 0, binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
// Inline uniform block
layout (set = 1, binding = 0) uniform UniformInline {
float roughness;
float metallic;
float r;
float g;
float b;
float ambient;
} material;
layout (location = 0) out vec4 outColor;
const float PI = 3.14159265359;
vec3 materialcolor()
{
return vec3(material.r, material.g, material.b);
}
// Normal Distribution function --------------------------------------
float D_GGX(float dotNH, float roughness)
{
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
return (alpha2)/(PI * denom*denom);
}
// Geometric Shadowing function --------------------------------------
float G_SchlicksmithGGX(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 dotNV = clamp(dot(N, V), 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);
// Light color fixed
vec3 lightColor = vec3(1.0);
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, rroughness);
// G = Geometric shadowing term (Microfacets shadowing)
float G = G_SchlicksmithGGX(dotNL, dotNV, rroughness);
// 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()
{
vec3 N = normalize(inNormal);
vec3 V = normalize(ubo.camPos - inWorldPos);
float roughness = material.roughness;
// Specular contribution
vec3 lightPos = vec3(0.0f, 0.0f, 10.0f);
vec3 Lo = vec3(0.0);
vec3 L = normalize(lightPos.xyz - inWorldPos);
Lo += BRDF(L, V, N, material.metallic, roughness);
// Combine with ambient
vec3 color = materialcolor() * material.ambient;
color += Lo;
// Gamma correct
color = pow(color, vec3(0.4545));
outColor = vec4(color, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,32 @@
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (set = 0, binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
layout (location = 0) out vec3 outWorldPos;
layout (location = 1) out vec3 outNormal;
layout(push_constant) uniform PushConsts {
vec3 objPos;
} pushConsts;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
vec3 locPos = vec3(ubo.model * vec4(inPos, 1.0));
outWorldPos = locPos + pushConsts.objPos;
outNormal = mat3(ubo.model) * inNormal;
gl_Position = ubo.projection * ubo.view * vec4(outWorldPos, 1.0);
}

Binary file not shown.