Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 'viewPosition' is saved in GBuffer and written as ViewMatrix * ModelMatrix * VertexPosition
- * 'linearViewDepth' is saved in GBuffer and written as LinearizeDepth(gl_FragCoord.z, nearclip, farclip)
- * 'uniform_shadowmap' is a texturesampler for the shadow depth map
- * 'uniform_snearclip' and 'uniform_sfarclip' are the near and far clip planes for the shadowmaps projection
- * 'uniform_invViewMatrix' is the inverse of ViewMatrix
- * 'uniform_lightViewProjMatrix' is the viewprojection matrix for the spotlight "camera"
- */
- float LinearizeDepth(float depth, float near, float far)
- {
- float z = depth * 2.0 - 1.0;
- return (2.0 * near * far) / (far + near - z * (far - near));
- }
- void checkShadow(vec3 viewPosition, float linearViewDepth)
- {
- vec4 worldPosition = uniform_invViewMatrix * vec4(viewPosition, 1);
- vec4 lightPosition = uniform_lightViewProjMatrix * worldPosition;
- lightPosition.xyz /= lightPosition.w;
- vec2 shadowUV = lightPosition.xy * vec2(0.5) + vec2(0.5);
- float linearShadowDepth = LinearizeDepth(texture(uniform_shadowmap, shadowUV).r, uniform_snearclip, uniform_sfarclip);
- if (linearShadowDepth < linearViewDepth)
- discard;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement