Texture tree and terrain scene, refactoring and cleanup

This commit is contained in:
saschawillems 2017-12-21 21:28:31 +01:00
parent 34942b08c5
commit 75b2a72957
13 changed files with 200 additions and 97 deletions

View file

@ -3,15 +3,12 @@
layout (binding = 1) uniform sampler2DArray shadowMap;
layout (location = 0) in vec2 inUV;
layout (location = 1) flat in uint inCascadeIndex;
layout (location = 0) out vec4 outFragColor;
layout(push_constant) uniform PushConsts {
uint cascadeIndex;
} pushConsts;
void main()
{
float depth = texture(shadowMap, vec3(inUV, float(pushConsts.cascadeIndex))).r;
float depth = texture(shadowMap, vec3(inUV, float(inCascadeIndex))).r;
outFragColor = vec4(vec3((depth)), 1.0);
}

View file

@ -1,14 +1,20 @@
#version 450
layout(push_constant) uniform PushConsts {
vec4 position;
uint cascadeIndex;
} pushConsts;
layout (location = 0) out vec2 outUV;
layout (location = 1) out uint outCascadeIndex;
out gl_PerVertex {
vec4 gl_Position;
};
void main()
{
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
outCascadeIndex = pushConsts.cascadeIndex;
gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
}

View file

@ -1,5 +1,13 @@
#version 450
layout (set = 1, binding = 0) uniform sampler2D colorMap;
layout (location = 0) in vec2 inUV;
void main()
{
float alpha = texture(colorMap, inUV).a;
if (alpha < 0.5) {
discard;
}
}

View file

@ -1,11 +1,13 @@
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;
// todo: pass via specialization constant
#define SHADOW_MAP_CASCADE_COUNT 4
layout(push_constant) uniform PushConsts {
vec4 position;
uint cascadeIndex;
} pushConsts;
@ -13,11 +15,15 @@ layout (binding = 0) uniform UBO {
mat4[SHADOW_MAP_CASCADE_COUNT] cascadeViewProjMat;
} ubo;
layout (location = 0) out vec2 outUV;
out gl_PerVertex {
vec4 gl_Position;
};
void main()
{
gl_Position = ubo.cascadeViewProjMat[pushConsts.cascadeIndex] * vec4(inPos, 1.0);
outUV = inUV;
vec3 pos = inPos + pushConsts.position.xyz;
gl_Position = ubo.cascadeViewProjMat[pushConsts.cascadeIndex] * vec4(pos, 1.0);
}

View file

@ -2,20 +2,22 @@
#define SHADOW_MAP_CASCADE_COUNT 4
layout (binding = 1) uniform sampler2DArray shadowMap;
layout (set = 0, binding = 1) uniform sampler2DArray shadowMap;
layout (set = 1, binding = 0) uniform sampler2D colorMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inViewPos;
layout (location = 3) in vec3 inPos;
layout (location = 4) in vec2 inUV;
layout (constant_id = 0) const int enablePCF = 0;
layout (location = 0) out vec4 outFragColor;
#define ambient 0.1
#define ambient 0.3
layout (binding = 2) uniform UBO {
layout (set = 0, binding = 2) uniform UBO {
vec4 cascadeSplits;
mat4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
mat4 inverseViewMat;
@ -69,6 +71,11 @@ float filterPCF(vec4 sc, uint cascadeIndex)
void main()
{
vec4 color = texture(colorMap, inUV);
if (color.a < 0.5) {
discard;
}
// Get cascade index for the current fragment's view position
uint cascadeIndex = 0;
for(uint i = 0; i < SHADOW_MAP_CASCADE_COUNT - 1; ++i) {
@ -91,10 +98,11 @@ void main()
vec3 N = normalize(inNormal);
vec3 L = normalize(-ubo.lightDir);
vec3 H = normalize(L + inViewPos);
float diffuse = max(dot(N, L), 0.0);
float diffuse = max(dot(N, L), ambient);
vec3 lightColor = vec3(1.0);
outFragColor.rgb = max(lightColor * (diffuse * inColor), vec3(0.0));
outFragColor.rgb = max(lightColor * (diffuse * color.rgb), vec3(0.0));
outFragColor.rgb *= shadow;
outFragColor.a = color.a;
// Color cascades (if enabled)
if (ubo.colorCascades == 1) {

View file

@ -15,6 +15,12 @@ layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 outViewPos;
layout (location = 3) out vec3 outPos;
layout (location = 4) out vec2 outUV;
layout(push_constant) uniform PushConsts {
vec4 position;
uint cascadeIndex;
} pushConsts;
out gl_PerVertex {
vec4 gl_Position;
@ -24,8 +30,10 @@ void main()
{
outColor = inColor;
outNormal = inNormal;
outPos = inPos;
outViewPos = (ubo.view * vec4(inPos.xyz, 1.0)).xyz;
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
outUV = inUV;
vec3 pos = inPos + pushConsts.position.xyz;
outPos = pos;
outViewPos = (ubo.view * vec4(pos.xyz, 1.0)).xyz;
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(pos.xyz, 1.0);
}