Moved shaders to new directory

This commit is contained in:
Sascha Willems 2023-05-09 21:03:02 +02:00
parent 0b3f8340e3
commit 99b226237a
1244 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,168 @@
#version 450
layout (binding = 1) uniform sampler2D samplerposition;
layout (binding = 2) uniform sampler2D samplerNormal;
layout (binding = 3) uniform sampler2D samplerAlbedo;
layout (binding = 5) uniform sampler2DArray samplerShadowMap;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
#define LIGHT_COUNT 3
#define SHADOW_FACTOR 0.25
#define AMBIENT_LIGHT 0.1
#define USE_PCF
struct Light
{
vec4 position;
vec4 target;
vec4 color;
mat4 viewMatrix;
};
layout (binding = 4) uniform UBO
{
vec4 viewPos;
Light lights[LIGHT_COUNT];
int useShadows;
int debugDisplayTarget;
} ubo;
float textureProj(vec4 P, float layer, vec2 offset)
{
float shadow = 1.0;
vec4 shadowCoord = P / P.w;
shadowCoord.st = shadowCoord.st * 0.5 + 0.5;
if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
{
float dist = texture(samplerShadowMap, vec3(shadowCoord.st + offset, layer)).r;
if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
{
shadow = SHADOW_FACTOR;
}
}
return shadow;
}
float filterPCF(vec4 sc, float layer)
{
ivec2 texDim = textureSize(samplerShadowMap, 0).xy;
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, layer, vec2(dx*x, dy*y));
count++;
}
}
return shadowFactor / count;
}
vec3 shadow(vec3 fragcolor, vec3 fragpos) {
for(int i = 0; i < LIGHT_COUNT; ++i)
{
vec4 shadowClip = ubo.lights[i].viewMatrix * vec4(fragpos, 1.0);
float shadowFactor;
#ifdef USE_PCF
shadowFactor= filterPCF(shadowClip, i);
#else
shadowFactor = textureProj(shadowClip, i, vec2(0.0));
#endif
fragcolor *= shadowFactor;
}
return fragcolor;
}
void main()
{
// Get G-Buffer values
vec3 fragPos = texture(samplerposition, inUV).rgb;
vec3 normal = texture(samplerNormal, inUV).rgb;
vec4 albedo = texture(samplerAlbedo, inUV);
// Debug display
if (ubo.debugDisplayTarget > 0) {
switch (ubo.debugDisplayTarget) {
case 1:
outFragColor.rgb = shadow(vec3(1.0), fragPos).rgb;
break;
case 2:
outFragColor.rgb = fragPos;
break;
case 3:
outFragColor.rgb = normal;
break;
case 4:
outFragColor.rgb = albedo.rgb;
break;
case 5:
outFragColor.rgb = albedo.aaa;
break;
}
outFragColor.a = 1.0;
return;
}
// Ambient part
vec3 fragcolor = albedo.rgb * AMBIENT_LIGHT;
vec3 N = normalize(normal);
for(int i = 0; i < LIGHT_COUNT; ++i)
{
// Vector to light
vec3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float dist = length(L);
L = normalize(L);
// Viewer to fragment
vec3 V = ubo.viewPos.xyz - fragPos;
V = normalize(V);
float lightCosInnerAngle = cos(radians(15.0));
float lightCosOuterAngle = cos(radians(25.0));
float lightRange = 100.0;
// Direction vector from source to target
vec3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
// Dual cone spot light with smooth transition between inner and outer angle
float cosDir = dot(L, dir);
float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
// Diffuse lighting
float NdotL = max(0.0, dot(N, L));
vec3 diff = vec3(NdotL);
// Specular lighting
vec3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
vec3 spec = vec3(pow(NdotR, 16.0) * albedo.a * 2.5);
fragcolor += vec3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
}
// Shadow calculations in a separate pass
if (ubo.useShadows > 0)
{
fragcolor = shadow(fragcolor, fragPos);
}
outFragColor = vec4(fragcolor, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
#version 450
layout (location = 0) out vec2 outUV;
void main()
{
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
}

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,29 @@
#version 450
layout (binding = 1) uniform sampler2D samplerColor;
layout (binding = 2) uniform sampler2D samplerNormalMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inWorldPos;
layout (location = 4) in vec3 inTangent;
layout (location = 0) out vec4 outPosition;
layout (location = 1) out vec4 outNormal;
layout (location = 2) out vec4 outAlbedo;
void main()
{
outPosition = vec4(inWorldPos, 1.0);
// Calculate normal in tangent space
vec3 N = normalize(inNormal);
vec3 T = normalize(inTangent);
vec3 B = cross(N, T);
mat3 TBN = mat3(T, B, N);
vec3 tnorm = TBN * normalize(texture(samplerNormalMap, inUV).xyz * 2.0 - vec3(1.0));
outNormal = vec4(tnorm, 1.0);
outAlbedo = texture(samplerColor, inUV);
}

Binary file not shown.

View file

@ -0,0 +1,41 @@
#version 450
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 (location = 4) in vec4 inTangent;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec4 instancePos[3];
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outColor;
layout (location = 3) out vec3 outWorldPos;
layout (location = 4) out vec3 outTangent;
void main()
{
vec4 tmpPos = vec4(inPos.xyz, 1.0) + ubo.instancePos[gl_InstanceIndex];
gl_Position = ubo.projection * ubo.view * ubo.model * tmpPos;
outUV = inUV;
// Vertex position in world space
outWorldPos = vec3(ubo.model * tmpPos);
// Normal in world space
mat3 mNormal = transpose(inverse(mat3(ubo.model)));
outNormal = mNormal * normalize(inNormal);
outTangent = mNormal * normalize(inTangent.xyz);
// Currently just vertex color
outColor = inColor;
}

Binary file not shown.

View file

@ -0,0 +1,27 @@
#version 450
#define LIGHT_COUNT 3
layout (triangles, invocations = LIGHT_COUNT) in;
layout (triangle_strip, max_vertices = 3) out;
layout (binding = 0) uniform UBO
{
mat4 mvp[LIGHT_COUNT];
vec4 instancePos[3];
} ubo;
layout (location = 0) in int inInstanceIndex[];
void main()
{
vec4 instancedPos = ubo.instancePos[inInstanceIndex[0]];
for (int i = 0; i < gl_in.length(); i++)
{
gl_Layer = gl_InvocationID;
vec4 tmpPos = gl_in[i].gl_Position + instancedPos;
gl_Position = ubo.mvp[gl_InvocationID] * tmpPos;
EmitVertex();
}
EndPrimitive();
}

Binary file not shown.

View file

@ -0,0 +1,11 @@
#version 450
layout (location = 0) in vec4 inPos;
layout (location = 0) out int outInstanceIndex;
void main()
{
outInstanceIndex = gl_InstanceIndex;
gl_Position = inPos;
}

Binary file not shown.