[ad_1]
I used the cascaded shadow maps approach earlier than implementing deferred shading to my renderer, and, general, the shadows regarded OK. But now, after altering the render circulation, it appears to be like horrible. I didn’t change how the shadow map is rendered, and I didn’t change the bias and different constants, so it positively has one thing to do with the g-pass textures.
The approach the renderer works for the time being is the next:
Geometry go:
Assign place to RGB channels of texture 1
Assign regular to RGB channels of texture 2
Assign diffuse to RGB channels of texture 3
Set texture 3 alpha channel to specular worth
Lighting go:
Extract knowledge from each texture (place, regular, shade, specular)
Calculate diffuse, ambient, and specular lighting values for every gentle
Multiply directional gentle diffuse shade by shadow issue
Mix all of it collectively and get the ultimate shade
The end result appears to be like like this:
The end result, I`m in search of, is one thing like this:
Main code components:
g-pass shader:
#model 330 core
structure (location = 0) in vec3 aPos;
structure (location = 1) in vec3 aNormal;
structure (location = 2) in vec2 aTexCoords;
structure (location = 3) in vec3 aTangent;
structure (location = 4) in vec3 aBitangent;
out mat3 TBN;
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoords;
uniform mat4 view;
uniform mat4 mannequin;
uniform mat4 projection;
void most important()
{
vec4 worldPos = mannequin * vec4(aPos, 1.0);
FragPos = worldPos.xyz;
TexCoords = aTexCoords;
mat3 normalMatrix = transpose(inverse(mat3(mannequin)));
Normal = normalMatrix * aNormal;
vec3 T = normalize(normalMatrix * aTangent);
vec3 N = normalize(normalMatrix * aNormal);
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T);
TBN = transpose(mat3(T, B, N));
gl_Position = projection * view * worldPos;
}
#model 330 core
structure (location = 0) out vec3 gPosition;
structure (location = 1) out vec3 gNormal;
structure (location = 2) out vec4 gAlbedoSpec;
in mat3 TBN;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
struct Material
{
bool shouldBeLit;
bool hasNormalTexture;
bool hasDiffuseTexture;
bool hasSpecularTexture;
bool hasTranslucencyTexture;
int tilingFactor;
float opacity;
float specular;
sampler2D mapNormal_1;
sampler2D mapDiffuse_1;
sampler2D mapSpecular_1;
sampler2D mapRoughness_1;
sampler2D mapTranslucency_1;
vec3 colorDiffuse;
};
uniform Material materials;
void most important()
{
vec2 texCoords = TexCoords;
// retailer the fragment place vector within the first gbuffer texture
gPosition = FragPos;
// additionally retailer the per-fragment normals into the gbuffer
if(materials.hasNormalTexture)
{
gNormal = normalize((texture( materials.mapNormal_1, texCoords).rgb * 2.0 - 1.0) * TBN);
}
else
{
gNormal = normalize(Normal);
}
// retailer specular depth in gAlbedoSpec's alpha part
if(materials.hasSpecularTexture)
{
gAlbedoSpec.a = texture(materials.mapSpecular_1, texCoords).r;
}
else
{
gAlbedoSpec.a = materials.specular;
}
// // and the diffuse per-fragment shade
if(materials.hasDiffuseTexture)
{
gAlbedoSpec.rgb = texture(materials.mapDiffuse_1, texCoords).rgb;
}
else
{
gAlbedoSpec.rgb = materials.colorDiffuse;
}
}
Shadow calculation:
vec3 fragPosition = texture(gPosition, TexCoords).rgb;
vec3 normalVector = texture(gNormal, TexCoords).rgb;
vec3 diffuseColor = texture(gAlbedoSpec, TexCoords).rgb;
float specularFactor = texture(gAlbedoSpec, TexCoords).a;
...
diffuseLighting += CalculateDirectionalDiffuseLighting(dLight, normalVector, viewDirection) * (1.0 - shadowFactor);
...
float CalculateDirecionalShadowIssue(vec3 lightDir, vec3 regular, vec3 viewDir, vec3 fragPos)
{
float shadow = 0.0f;
// choose cascade layer
vec4 fragPosViewSpace = view * vec4(fragPos, 1.0);
float depthValue = abs(fragPosViewSpace.z);
int layer = -1;
for (int i = 0; i < cascadeCount; ++i)
{
if (depthValue < cascadePlaneDistances[i])
{
layer = i;
break;
}
}
if (layer == -1)
{
layer = cascadeCount - 1;
}
vec4 fragPosLightSpace = lightSpaceMatrices[layer] * vec4(fragPos, 1.0);
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// rework to [0,1] vary
projCoords = projCoords * 0.5f + 0.5f;
// get depth of present fragment from gentle's perspective
float currentDepth = projCoords.z;
if (currentDepth > 1.0)
{
return 0.0;
}
// PCF
vec2 texelSize = 1.0f / vec2(textureSize(dLight.mapShadow, 0));
const int sampleRadius = 3;
const float sampleRadiusCount = pow(sampleRadius * 2 + 1, 2);
for(int x = -sampleRadius; x <= sampleRadius; ++x)
{
for(int y = -sampleRadius; y <= sampleRadius; ++y)
{
float randomFactor = clamp(combine(0.0f, 1.0f, rand((projCoords.xy + vec2(x, y))* fragPos.xy)), 0.0f, 1.0f);
float pcfDepth = texture(dLight.mapShadow, vec3(projCoords.xy + vec2(x + randomFactor, y + randomFactor) * texelSize, layer)).r;
shadow += currentDepth > pcfDepth ? 1.0f : 0.0f;
}
}
shadow /= sampleRadiusCount;
const float shadowFadeDistance = 10.0f;
float delta = cascadePlaneDistances[layer] - depthValue;
if (layer == cascadeCount - 1 && delta < shadowFadeDistance)
{
shadow *= delta / shadowFadeDistance;
}
return shadow;
}
[ad_2]