diff --git a/assets/meshes/Cube.mesh b/assets/meshes/Cube.mesh new file mode 100644 index 0000000..627c418 Binary files /dev/null and b/assets/meshes/Cube.mesh differ diff --git a/assets/meshes/Plane.mesh b/assets/meshes/Plane.mesh new file mode 100644 index 0000000..264e9b4 Binary files /dev/null and b/assets/meshes/Plane.mesh differ diff --git a/assets/shaders/deferred.frag b/assets/shaders/deferred.frag index 5f6b527..17f1bee 100644 --- a/assets/shaders/deferred.frag +++ b/assets/shaders/deferred.frag @@ -4,13 +4,18 @@ #include "shaders://_builtins/PBR.glsl" struct Light { - mat4 tranform; + mat4 transform; vec4 color; float intensity; }; const int MAX_LIGHT_COUNT = 128; +#define SHADOW_FACTOR 0.25 +layout(set = 1, binding = 1) uniform LightBuffer { +layout(align = 16) Light lights[MAX_LIGHT_COUNT]; +} LightsBuffer; + layout( push_constant ) uniform constants { uint lightCount; @@ -21,15 +26,96 @@ layout(set = 0, binding = 1) uniform sampler2D normalTexture; layout(set = 0, binding = 2) uniform sampler2D albedoTexture; layout(set = 0, binding = 3) uniform sampler2D specularTexture; -layout(set = 1, binding = 1) uniform LightBuffer { - layout(align = 16) Light lights[MAX_LIGHT_COUNT]; -} LightsBuffer; +layout(set = 0, binding = 4) uniform sampler2D shadowmapTexture; layout (location = 0) in vec2 inUV; layout(location = 1) in vec3 cameraPos; layout(location = 0) out vec4 outColor; +mat4 get_orthographicMatrix(float l, float r, float b, float t, float f, float n) +{ + mat4 M = mat4(0); + + // set OpenGL perspective projection matrix + M[0][0] = 2 / (r - l); + M[0][1] = 0; + M[0][2] = 0; + M[0][3] = 0; + + M[1][0] = 0; + M[1][1] = -2 / (t - b); + M[1][2] = 0; + M[1][3] = 0; + + M[2][0] = 0; + M[2][1] = 0; + M[2][2] = -2 / (f - n); + M[2][3] = 0; + + M[0][3] = -(r + l) / (r - l); + M[1][3] = -(t + b) / (t - b); + M[2][3] = -(f + n) / (f - n); + M[3][3] = 1; + + return M; +} + +float textureProj(vec4 P, float bias, 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(shadowmapTexture, shadowCoord.st + offset).r; + if (shadowCoord.w > 0.0 && dist < shadowCoord.z - bias) + { + shadow = SHADOW_FACTOR; + } + } + return shadow; +} + +float filterPCF(vec4 sc, float bias) +{ + ivec2 texDim = textureSize(shadowmapTexture, 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, bias, vec2(dx*x, dy*y)); + count++; + } + } + + return shadowFactor / count; +} + +vec4 shadow(vec3 fragcolor, float bias, vec3 fragpos) +{ + mat4 projectionMatrix = get_orthographicMatrix(-1, 1, -1, 1, 100, 0.1); + mat4 viewMatrix = inverse( LightsBuffer.lights[0].transform); + vec4 shadowClip = projectionMatrix * viewMatrix * vec4(fragpos, 1.0); + + float shadowFactor; + shadowFactor= filterPCF(shadowClip, bias); + + fragcolor *= shadowFactor; + + return vec4(fragcolor, 1.0); +} + void main() { vec4 gposition = texture(positionTexture, inUV); @@ -37,6 +123,8 @@ void main() vec4 galbedo = texture(albedoTexture, inUV); vec4 gspec = texture(specularTexture, inUV); + vec4 shadowmap = texture(shadowmapTexture, inUV); + vec3 position = gposition.xyz; vec3 normal = gnormal.xyz; vec3 albedo = galbedo.xyz; @@ -53,13 +141,37 @@ void main() vec3 F = fresnelSchlickRoughness(max(dot(normal, V), 0.0), F0, roughnessFactor); vec3 Lo = vec3(0); - for(int i = 0; i < PushConstants.lightCount; ++i) + + { + Light directional_light = LightsBuffer.lights[0]; + + vec3 L = vec3(directional_light.transform[2]); + vec3 H = normalize(V + L); + vec3 radiance = directional_light.color.xyz * directional_light.intensity; + + float NDF = DistributionGGX(normal, H, roughnessFactor); + float G = GeometrySmith(normal, V, L, roughnessFactor); + + vec3 numerator = NDF * G * F; + float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, L), 0.0); + + vec3 specular = numerator / max(denominator, 0.001); + + vec3 kS = F; + vec3 kD = vec3(1.0) - kS; + kD *= 1.0 - metallicFactor; + + float NdotL = max(dot(normal, L), 0.0); + Lo += clamp((kD * albedo / PI + specular) * radiance * NdotL, vec3(0), vec3(1)); + } + + for(int i = 1; i < PushConstants.lightCount; ++i) { Light l = LightsBuffer.lights[i]; - vec3 L = normalize(l.tranform[3].xyz - position); + vec3 L = normalize(l.transform[3].xyz - position); vec3 H = normalize(V + L); - float distance = length(l.tranform[3].xyz - position); + float distance = length(l.transform[3].xyz - position); float attenuation = 1.0 / (distance * distance); vec3 radiance = l.color.xyz * attenuation * l.intensity; @@ -82,11 +194,8 @@ void main() vec3 ambient = vec3(0.2) * albedo; vec3 color = ambient + Lo; - color += spec * specFactor; - // color = color / (color + vec3(1.0)); - - outColor = vec4(color, 1.0); - // outColor = vec4(spec * specFactor, 1.0); + float bias = 0.0000005; + outColor = shadow(color, bias, position); } diff --git a/assets/shaders/deferred.vert b/assets/shaders/deferred.vert index 206a683..1c5a41e 100644 --- a/assets/shaders/deferred.vert +++ b/assets/shaders/deferred.vert @@ -1,10 +1,5 @@ #version 450 -layout( push_constant ) uniform constants -{ - uint lightCount; -} PushConstants; - layout(set = 1, binding = 0) uniform CameraParam { mat4 projection; mat4 view; diff --git a/assets/shaders/shadowmap.vert b/assets/shaders/shadowmap.vert index 294f46c..92f8e9c 100644 --- a/assets/shaders/shadowmap.vert +++ b/assets/shaders/shadowmap.vert @@ -10,7 +10,7 @@ struct Vertex { }; struct Light { - mat4 tranform; + mat4 transform; vec4 color; float intensity; }; @@ -36,19 +36,41 @@ layout(set = 1, binding = 0) uniform LightBuffer { layout(align = 16) Light lights[MAX_LIGHT_COUNT]; } LightsBuffer; -float near = 10; -float far = 0.1; +mat4 get_orthographicMatrix(float l, float r, float b, float t, float f, float n) +{ + mat4 M = mat4(0); + + // set OpenGL perspective projection matrix + M[0][0] = 2 / (r - l); + M[0][1] = 0; + M[0][2] = 0; + M[0][3] = 0; + + M[1][0] = 0; + M[1][1] = -2 / (t - b); + M[1][2] = 0; + M[1][3] = 0; + + M[2][0] = 0; + M[2][1] = 0; + M[2][2] = -2 / (f - n); + M[2][3] = 0; + + M[0][3] = -(r + l) / (r - l); + M[1][3] = -(t + b) / (t - b); + M[2][3] = -(f + n) / (f - n); + M[3][3] = 1; + + return M; +} void main() { uint index = IndexBuffers[ PushConstants.indexBufferIndex ].indices[gl_VertexIndex]; Vertex vertex = VertexBuffers[ PushConstants.vertexBufferIndex ].vertices[ index ]; - mat4 lightView = transpose(inverse( LightsBuffer.lights[0].tranform)); - mat4 lightProjection = mat4(1.0); - lightProjection[3][2] = -(far+near)/(far-near); - lightProjection[2][2] = -2/(far-near); - + mat4 lightView = inverse( LightsBuffer.lights[0].transform); + mat4 lightProjection = get_orthographicMatrix(-1, 1, -1, 1, 100, 0.1); mat4 lightSpaceMatrix = lightProjection * lightView; gl_Position = lightSpaceMatrix * PushConstants.transform * vec4(vertex.position.xyz, 1.0);