Created
September 30, 2015 14:23
-
-
Save skrat/51f1fae69b0b225242dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DEPTH | |
varying vec3 vViewPosition; | |
vertex: | |
attribute vec3 position; | |
uniform mat4 proj; | |
uniform mat4 view; | |
void main(){ | |
vViewPosition = (view * vec4(position, 1.0)).xyz; | |
gl_Position = proj * view * vec4(position, 1.0); | |
} | |
fragment: | |
#extension GL_OES_standard_derivatives : enable | |
#require common/pack | |
uniform float near; | |
uniform float far; | |
void main(){ | |
float z = -vViewPosition.z; | |
float z_n = clamp((z - near) / (far - near), 0.0, 1.0); | |
/* float dx = dFdx(z_n); */ | |
/* float dy = dFdy(z_n); */ | |
gl_FragColor = vec4(pack16f(z_n), pack16f(z_n * z_n)); | |
} | |
// SHADOW | |
uniform sampler2D light_depth; | |
uniform mat4 light_view, light_proj; | |
uniform float light_near, light_far; | |
#require common/smootherstep | |
#require common/pack | |
const float MIN_VARIANCE = 0.02; | |
const float REDUCE_BLEEDING = 0.4; | |
float ChebyshevUpperBound(vec2 uv, float z) { | |
vec4 sample = texture2D(light_depth, uv); | |
vec2 moments = vec2( | |
unpack16f(sample.rg) * (light_far - light_near) + light_near, | |
unpack16f(sample.ba) * (light_far - light_near) + light_near); | |
float p = smoothstep(z-0.4, z, moments.x); | |
float variance = moments.y - moments.x*moments.x; | |
variance = max(variance, MIN_VARIANCE); | |
float d = z - moments.x; | |
float p_max = variance/(variance+d*d); | |
p_max = smoothstep(REDUCE_BLEEDING, 1.0, p_max); | |
return max(p, p_max); | |
} | |
float OcclusionVSM(vec3 position, vec3 normal) { | |
vec4 light_view_position = light_view * vec4(position, 1.0); | |
vec4 light_device = light_proj * light_view_position; | |
vec2 light_clip = light_device.xy/light_device.w; | |
vec2 uv = light_clip*0.5+0.5; | |
float z = -(light_view_position.z + light_near); | |
return ChebyshevUpperBound(uv, z); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment