From 4e892b3288d641beae61a5e0b5398f1ec220020e Mon Sep 17 00:00:00 2001 From: j3oss Date: Sun, 25 Jul 2021 01:07:23 +0200 Subject: [PATCH] added shadow rendering --- assets/meshes/Cube.mesh | Bin 0 -> 2201 bytes assets/meshes/Plane.mesh | Bin 0 -> 477 bytes assets/shaders/deferred.frag | 133 +++++++++++++++++++++++++++++++--- assets/shaders/deferred.vert | 5 -- assets/shaders/shadowmap.vert | 38 ++++++++-- 5 files changed, 151 insertions(+), 25 deletions(-) create mode 100644 assets/meshes/Cube.mesh create mode 100644 assets/meshes/Plane.mesh diff --git a/assets/meshes/Cube.mesh b/assets/meshes/Cube.mesh new file mode 100644 index 0000000000000000000000000000000000000000..627c418df0b78c8986946a183ee5d23ef540c280 GIT binary patch literal 2201 zcmai$&1)1v5XIZi7{3xfqSs;09&{4|<`4sdN6E?SCedBU!35SsL?8~(GUV3~$X5bvx^?s?x z8g(gq#j|`Zk1FfAE|G`OG8PY6m6BB-<58hL=WAD-Pjg9QJl%EVzMc1^XO2A0DQ?S? zPxF!2uE-NdnlYYO)EsfXaOdjJPVg*`@s{u~=tYi;@9zNhxXhcq;#t0yN0s$lmpEJC z8Oucc8J6l}JgW4(Rr84>vhn<$?1`g(huT4y&z{9|c7Uyk34Yt%0)Xyiq1dDN)i@@g-0SG;9ce%txPk!BT7bINBt z&gdbI`W@@xzU*5s7iE2)n!)wl)qWTMb*|ERh+w z8<)+HZJYT1lGj<0Mx3G=>SH|ffzQ?QSTknkZs3$tIooVLUwys{R`E2ad^R8M6Yp!! z_4zvV5+f}0=2EbI(_=02qNZzj_HOCV4)H9Hu>wzZSKIfJ{xhGrgYOQk>uzVhkHCL} y^boP-d*&C2mx$MhXTE2Cig<$9K-~2`^KHZ$;yL26@0o8QULhVK?)jd16Y&p?NcbH9 literal 0 HcmV?d00001 diff --git a/assets/meshes/Plane.mesh b/assets/meshes/Plane.mesh new file mode 100644 index 0000000000000000000000000000000000000000..264e9b42aa551d879a3b5ec9eeac936103f812d5 GIT binary patch literal 477 zcmb#ElGSPtwC+C;ul_*)6!T1n)pmbSk zQ3<*>V -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);