aboutsummaryrefslogtreecommitdiff
path: root/KaplaDemo/externalIP/resources/shaders/dof_fs.cpp
blob: 98ca0566f8b31f112687ce58f5bdaa5779e06c9d (plain) (blame)
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
	uniform sampler2D colorTex;
	uniform sampler2D depthTex;
	uniform float sx;
	uniform float sy;

	void main (void)
	{
		const float depthEnd = 0.993;
		const float depthSize = 0.015;

		vec3 colorP = texture2D(colorTex, gl_TexCoord[0]).rgb;
		float depth = texture2D(depthTex, gl_TexCoord[0].st).r;

		if ((depth - depthEnd) < depthSize)
		{
			const int depthKernelSize = 5;
			vec3 colorSum = vec3(0.0);
			float cnt = 0.0;
			for (int x = -depthKernelSize; x <= depthKernelSize; x++)
				for (int y = -depthKernelSize; y <= depthKernelSize; y++)
				{
					float s = gl_TexCoord[0].s + x * sy;
					float t = gl_TexCoord[0].t + y * sy;
					float scalex = ((depthKernelSize+1) - abs(float(x))) / depthKernelSize;
					float scaley = ((depthKernelSize+1) - abs(float(y))) / depthKernelSize;
					float scale = scalex * scaley;
					vec3 color = texture2D(colorTex, vec2(s,t)).rgb;
					colorSum += scale * color;
					cnt += scale;
				}

			colorSum /= cnt;
			float depthScale = pow(max(0.0f,min(1.0, ( abs(depth-depthEnd)) / depthSize)),1.5);
			colorP = depthScale * colorSum + (1.0 - depthScale) * colorP;
		}

		gl_FragColor = vec4(colorP, 1.0);
	}