Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/eevee/shaders/ambient_occlusion_lib.glsl
| Show First 20 Lines • Show All 185 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| /* Fetch early, hide latency! */ | /* Fetch early, hide latency! */ | ||||
| vec4 horizons = texelFetch(horizonBuffer, ivec2(gl_FragCoord.xy), 0); | vec4 horizons = texelFetch(horizonBuffer, ivec2(gl_FragCoord.xy), 0); | ||||
| vec4 dirs; | vec4 dirs; | ||||
| dirs.xy = get_ao_dir(noise.x * 0.5); | dirs.xy = get_ao_dir(noise.x * 0.5); | ||||
| dirs.zw = get_ao_dir(noise.x * 0.5 + 0.5); | dirs.zw = get_ao_dir(noise.x * 0.5 + 0.5); | ||||
| bent_normal = normal * 1e-8; | bent_normal = vec3(0.0); | ||||
| visibility = 1e-8; | visibility = 0.0; | ||||
| horizons = unpack_horizons(horizons); | horizons = unpack_horizons(horizons); | ||||
| integrate_slice(normal, dirs.xy, horizons.xy, visibility, bent_normal); | integrate_slice(normal, dirs.xy, horizons.xy, visibility, bent_normal); | ||||
| integrate_slice(normal, dirs.zw, horizons.zw, visibility, bent_normal); | integrate_slice(normal, dirs.zw, horizons.zw, visibility, bent_normal); | ||||
| bent_normal = normalize(bent_normal / visibility); | bent_normal = safe_normalize(bent_normal); | ||||
| visibility *= 0.5; /* We integrated 2 slices. */ | visibility *= 0.5; /* We integrated 2 slices. */ | ||||
| } | } | ||||
| void gtao(vec3 normal, vec3 position, vec4 noise, out float visibility, out vec3 bent_normal) | void gtao(vec3 normal, vec3 position, vec4 noise, out float visibility, out vec3 bent_normal) | ||||
| { | { | ||||
| vec2 uvs = get_uvs_from_view(position); | vec2 uvs = get_uvs_from_view(position); | ||||
| vec2 max_dir = get_max_dir(position.z); | vec2 max_dir = get_max_dir(position.z); | ||||
| Show All 24 Lines | float gtao_multibounce(float visibility, vec3 albedo) | ||||
| float a = 2.0404 * lum - 0.3324; | float a = 2.0404 * lum - 0.3324; | ||||
| float b = -4.7951 * lum + 0.6417; | float b = -4.7951 * lum + 0.6417; | ||||
| float c = 2.7552 * lum + 0.6903; | float c = 2.7552 * lum + 0.6903; | ||||
| float x = visibility; | float x = visibility; | ||||
| return max(x, ((x * a + b) * x + c) * x); | return max(x, ((x * a + b) * x + c) * x); | ||||
| } | } | ||||
| float diffuse_occlusion(vec3 N, vec3 vis_cone_dir, float vis_cone_aperture_cos, vec3 albedo) | |||||
| { | |||||
| if ((int(aoSettings) & USE_AO) == 0) { | |||||
| return 1.0; | |||||
| } | |||||
| /* If the shading normal is orthogonal to the geometric normal, it should be half lit. */ | |||||
| float horizon_fac = saturate(dot(N, vis_cone_dir) * 0.5 + 0.5); | |||||
| float ao = vis_cone_aperture_cos * horizon_fac; | |||||
| return gtao_multibounce(ao, albedo); | |||||
| } | |||||
| float specular_occlusion(float NV, float AO, float roughness) | float specular_occlusion(float NV, float AO, float roughness) | ||||
| { | { | ||||
| return saturate(pow(NV + AO, roughness) - 1.0 + AO); | return saturate(pow(NV + AO, roughness) - 1.0 + AO); | ||||
| } | } | ||||
| /* Use the right occlusion */ | /* Use the right occlusion */ | ||||
| float occlusion_compute(vec3 N, vec3 vpos, float user_occlusion, vec4 rand, out vec3 bent_normal) | float occlusion_compute(vec3 N, vec3 vpos, vec4 rand, out vec3 bent_normal) | ||||
| { | { | ||||
| #ifndef USE_REFRACTION | #ifndef USE_REFRACTION | ||||
| if ((int(aoSettings) & USE_AO) != 0) { | if ((int(aoSettings) & USE_AO) != 0) { | ||||
| float visibility; | float visibility; | ||||
| vec3 vnor = mat3(ViewMatrix) * N; | vec3 vnor = mat3(ViewMatrix) * N; | ||||
| # ifdef ENABLE_DEFERED_AO | # ifdef ENABLE_DEFERED_AO | ||||
| gtao_deferred(vnor, rand, gl_FragCoord.z, visibility, bent_normal); | gtao_deferred(vnor, rand, gl_FragCoord.z, visibility, bent_normal); | ||||
| # else | # else | ||||
| gtao(vnor, vpos, rand, visibility, bent_normal); | gtao(vnor, vpos, rand, visibility, bent_normal); | ||||
| # endif | # endif | ||||
| /* Prevent some problems down the road. */ | /* Prevent some problems down the road. */ | ||||
| visibility = max(1e-3, visibility); | visibility = max(1e-3, visibility); | ||||
| if ((int(aoSettings) & USE_BENT_NORMAL) != 0) { | if ((int(aoSettings) & USE_BENT_NORMAL) != 0) { | ||||
| /* The bent normal will show the facet look of the mesh. Try to minimize this. */ | |||||
| float mix_fac = visibility * visibility * visibility; | |||||
| bent_normal = normalize(mix(bent_normal, vnor, mix_fac)); | |||||
| bent_normal = transform_direction(ViewMatrixInverse, bent_normal); | bent_normal = transform_direction(ViewMatrixInverse, bent_normal); | ||||
| } | } | ||||
| else { | else { | ||||
| bent_normal = N; | bent_normal = N; | ||||
| } | } | ||||
| /* Scale by user factor */ | /* Scale by user factor */ | ||||
| visibility = pow(visibility, aoFactor); | visibility = pow(visibility, aoFactor); | ||||
| return min(visibility, user_occlusion); | return visibility; | ||||
| } | } | ||||
| #endif | #endif | ||||
| bent_normal = N; | bent_normal = N; | ||||
| return user_occlusion; | return 1.0; | ||||
| } | } | ||||