Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ssao_alchemy.glsl « shaders « clay « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94e2d6f3c7bb3cf9e8bacfd4f080b5c67d25ac9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#define ssao_distance		matcaps_param[mat_id].ssao_params_var.x
#define ssao_factor_cavity	matcaps_param[mat_id].ssao_params_var.y
#define ssao_factor_edge	matcaps_param[mat_id].ssao_params_var.z
#define ssao_attenuation	matcaps_param[mat_id].ssao_params_var.w

/*  from The Alchemy screen-space ambient obscurance algorithm
 * http://graphics.cs.williams.edu/papers/AlchemyHPG11/VV11AlchemyAO.pdf */

void ssao_factors(
        in float depth, in vec3 normal, in vec3 position, in vec2 screenco,
        out float cavities, out float edges)
{
	cavities = edges = 0.0;
	/* early out if there is no need for SSAO */
	if (ssao_factor_cavity == 0.0 && ssao_factor_edge == 0.0)
		return;

	/* take the normalized ray direction here */
	vec3 noise = texture(ssao_jitter, screenco.xy * jitter_tilling).rgb;

	/* find the offset in screen space by multiplying a point
	 * in camera space at the depth of the point by the projection matrix. */
	vec2 offset;
	float homcoord = WinMatrix[2][3] * position.z + WinMatrix[3][3];
	offset.x = WinMatrix[0][0] * ssao_distance / homcoord;
	offset.y = WinMatrix[1][1] * ssao_distance / homcoord;
	/* convert from -1.0...1.0 range to 0.0..1.0 for easy use with texture coordinates */
	offset *= 0.5;

	int num_samples = int(ssao_samples_num);

	/* Note. Putting noise usage here to put some ALU after texture fetch. */
	vec2 rotX = noise.rg;
	vec2 rotY = vec2(-rotX.y, rotX.x);

	for (int x = 0; x < num_samples && x < 500; x++) {
		/* ssao_samples[x].xy is sample direction (normalized).
		 * ssao_samples[x].z is sample distance from disk center. */

		/* Rotate with random direction to get jittered result. */
		vec2 dir_jittered = vec2(dot(ssao_samples[x].xy, rotX), dot(ssao_samples[x].xy, rotY));
		dir_jittered.xy *= ssao_samples[x].z + noise.b;

		vec2 uvcoords = screenco.xy + dir_jittered * offset;

		if (uvcoords.x > 1.0 || uvcoords.x < 0.0 || uvcoords.y > 1.0 || uvcoords.y < 0.0)
			continue;

		float depth_new = texture(depthtex, uvcoords).r;

		/* Handle Background case */
		bool is_background = (depth_new == 1.0);

		/* This trick provide good edge effect even if no neighboor is found. */
		vec3 pos_new = get_view_space_from_depth(uvcoords, (is_background) ? depth : depth_new);

		if (is_background)
			pos_new.z -= ssao_distance;

		vec3 dir = pos_new - position;
		float len = length(dir);
		float f_cavities = dot(dir, normal);
		float f_edge = -f_cavities;
		float f_bias = 0.05 * len + 0.0001;

		float attenuation = 1.0 / (len * (1.0 + len * len * ssao_attenuation));

		/* use minor bias here to avoid self shadowing */
		if (f_cavities > -f_bias)
			cavities += f_cavities * attenuation;

		if (f_edge > f_bias)
			edges += f_edge * attenuation;
	}

	cavities /= ssao_samples_num;
	edges /= ssao_samples_num;

	/* don't let cavity wash out the surface appearance */
	cavities = clamp(cavities * ssao_factor_cavity, 0.0, 1.0);
	edges = edges * ssao_factor_edge;
}