Added shadow mapping example
This commit is contained in:
parent
9ecbaca69f
commit
e45c90f180
19 changed files with 3577 additions and 0 deletions
7
data/shaders/shadowmapping/generate-spirv.bat
Normal file
7
data/shaders/shadowmapping/generate-spirv.bat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
glslangvalidator -V offscreen.vert -o offscreen.vert.spv
|
||||
glslangvalidator -V offscreen.frag -o offscreen.frag.spv
|
||||
glslangvalidator -V quad.vert -o quad.vert.spv
|
||||
glslangvalidator -V quad.frag -o quad.frag.spv
|
||||
glslangvalidator -V scene.vert -o scene.vert.spv
|
||||
glslangvalidator -V scene.frag -o scene.frag.spv
|
||||
|
||||
13
data/shaders/shadowmapping/offscreen.frag
Normal file
13
data/shaders/shadowmapping/offscreen.frag
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
//layout(location = 0) out float fragmentdepth;
|
||||
|
||||
void main()
|
||||
{
|
||||
// fragmentdepth = gl_FragCoord.z;
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/offscreen.frag.spv
Normal file
BIN
data/shaders/shadowmapping/offscreen.frag.spv
Normal file
Binary file not shown.
16
data/shaders/shadowmapping/offscreen.vert
Normal file
16
data/shaders/shadowmapping/offscreen.vert
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 depthMVP;
|
||||
} ubo;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ubo.depthMVP * vec4(inPos, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/offscreen.vert.spv
Normal file
BIN
data/shaders/shadowmapping/offscreen.vert.spv
Normal file
Binary file not shown.
24
data/shaders/shadowmapping/quad.frag
Normal file
24
data/shaders/shadowmapping/quad.frag
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (binding = 1) uniform sampler2D samplerColor;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
float LinearizeDepth(float depth)
|
||||
{
|
||||
float n = 1.0; // camera z near
|
||||
float f = 128.0; // camera z far
|
||||
float z = depth;
|
||||
return (2.0 * n) / (f + n - z * (f - n));
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float depth = texture(samplerColor, inUV).r;
|
||||
outFragColor = vec4(vec3(1.0-LinearizeDepth(depth)), 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/quad.frag.spv
Normal file
BIN
data/shaders/shadowmapping/quad.frag.spv
Normal file
Binary file not shown.
21
data/shaders/shadowmapping/quad.vert
Normal file
21
data/shaders/shadowmapping/quad.vert
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#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 vec2 inUV;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 model;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec2 outUV;
|
||||
|
||||
void main()
|
||||
{
|
||||
outUV = inUV;
|
||||
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/quad.vert.spv
Normal file
BIN
data/shaders/shadowmapping/quad.vert.spv
Normal file
Binary file not shown.
69
data/shaders/shadowmapping/scene.frag
Normal file
69
data/shaders/shadowmapping/scene.frag
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (binding = 1) uniform sampler2D shadowMap;
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inColor;
|
||||
layout (location = 2) in vec3 inViewVec;
|
||||
layout (location = 3) in vec3 inLightVec;
|
||||
layout (location = 4) in vec4 inShadowCoord;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
#define ambient 0.1
|
||||
|
||||
float textureProj(vec4 P, vec2 off)
|
||||
{
|
||||
float shadow = 1.0;
|
||||
vec4 shadowCoord = P / P.w;
|
||||
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
|
||||
{
|
||||
float dist = texture( shadowMap, shadowCoord.st + off ).r;
|
||||
if ( shadowCoord.w > 0.0 && dist < shadowCoord.z )
|
||||
{
|
||||
shadow = ambient;
|
||||
}
|
||||
}
|
||||
return shadow;
|
||||
}
|
||||
|
||||
float filterPCF(vec4 sc)
|
||||
{
|
||||
ivec2 texDim = textureSize(shadowMap, 0);
|
||||
float scale = 1.5;
|
||||
float dx = scale * 1.0 / float(texDim.x);
|
||||
float dy = scale * 1.0 / float(texDim.y);
|
||||
|
||||
float shadowFactor = 0.0;
|
||||
int count = 0;
|
||||
int range = 1;
|
||||
|
||||
for (int x = -range; x <= range; x++)
|
||||
{
|
||||
for (int y = -range; y <= range; y++)
|
||||
{
|
||||
shadowFactor += textureProj(sc, vec2(dx*x, dy*y));
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
return shadowFactor / count;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float shadow = filterPCF(inShadowCoord / inShadowCoord.w);
|
||||
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = normalize(-reflect(L, N));
|
||||
vec3 diffuse = max(dot(N, L), ambient) * inColor;
|
||||
// vec3 specular = pow(max(dot(R, V), 0.0), 50.0) * vec3(0.75);
|
||||
|
||||
outFragColor = vec4(diffuse * shadow, 1.0);
|
||||
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/scene.frag.spv
Normal file
BIN
data/shaders/shadowmapping/scene.frag.spv
Normal file
Binary file not shown.
47
data/shaders/shadowmapping/scene.vert
Normal file
47
data/shaders/shadowmapping/scene.vert
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#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 vec2 inUV;
|
||||
layout (location = 2) in vec3 inColor;
|
||||
layout (location = 3) in vec3 inNormal;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 view;
|
||||
mat4 model;
|
||||
mat4 lightSpace;
|
||||
vec3 lightPos;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outColor;
|
||||
layout (location = 2) out vec3 outViewVec;
|
||||
layout (location = 3) out vec3 outLightVec;
|
||||
layout (location = 4) out vec4 outShadowCoord;
|
||||
|
||||
|
||||
const mat4 biasMat = mat4(
|
||||
0.5, 0.0, 0.0, 0.0,
|
||||
0.0, 0.5, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.5, 0.5, 0.0, 1.0 );
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = inColor;
|
||||
outNormal = inNormal;
|
||||
|
||||
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
|
||||
vec4 pos = ubo.model * vec4(inPos, 1.0);
|
||||
outNormal = mat3(ubo.model) * inNormal;
|
||||
outLightVec = normalize(ubo.lightPos - inPos);
|
||||
outViewVec = -pos.xyz;
|
||||
|
||||
outShadowCoord = ( biasMat * ubo.lightSpace * ubo.model ) * vec4(inPos, 1.0);
|
||||
}
|
||||
|
||||
BIN
data/shaders/shadowmapping/scene.vert.spv
Normal file
BIN
data/shaders/shadowmapping/scene.vert.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue