@@ -19,6 +19,8 @@ uniform mat4 viewInverse;
1919uniform mat4 projectionInverse;
2020uniform float nearDistance;
2121uniform float farDistance;
22+ uniform int focalType;
23+ uniform float focalDistance;
2224uniform vec2 focalPoint;
2325uniform sampler2D depthTexture;
2426uniform sampler2D blurredTexture;
@@ -28,6 +30,15 @@ in vec2 texCoordsOut;
2830
2931layout (location = 0 ) out vec4 frag;
3032
33+ float depthToDistance(float depth)
34+ {
35+ float ndc = depth * 2.0 - 1.0 ;
36+ vec4 clip = vec4 (0.0 , 0.0 , ndc, 1.0 );
37+ vec4 view = projectionInverse * clip;
38+ view /= view.w;
39+ return - view.z;
40+ }
41+
3142vec4 depthToPosition(float depth, vec2 texCoords)
3243{
3344 float z = depth * 2.0 - 1.0 ;
@@ -39,20 +50,57 @@ vec4 depthToPosition(float depth, vec2 texCoords)
3950
4051void main()
4152{
53+ // retrieve unblurred color
4254 vec4 unblurredColor = texture(unblurredTexture, texCoordsOut);
43- float depth = texture(depthTexture, texCoordsOut).r;
44- if (depth != 0.0 )
55+
56+ // sample depth values that may be invalid when 0.0
57+ vec2 texelSize = vec2 (1.0 ) / textureSize(depthTexture, 0 );
58+ float depths[] =
59+ float [](
60+ texture(depthTexture, texCoordsOut + texelSize * vec2 (- 1.0 , - 1.0 )).r,
61+ texture(depthTexture, texCoordsOut + texelSize * vec2 (1.0 , - 1.0 )).r,
62+ texture(depthTexture, texCoordsOut + texelSize * vec2 (- 1.0 , 1.0 )).r,
63+ texture(depthTexture, texCoordsOut + texelSize * vec2 (1.0 , 1.0 )).r);
64+
65+ // compute average of valid depth values
66+ int depthCount = 0 ;
67+ float depth = 0.0 ;
68+ for (int i = 0 ; i < 4 ; ++ i)
69+ {
70+ float depthCurrent = depths[i];
71+ if (depthCurrent != 0.0 )
72+ {
73+ depth += depthCurrent;
74+ ++ depthCount;
75+ }
76+ }
77+ depth /= float (depthCount);
78+
79+ // compute frag
80+ if (depthCount > 0 )
4581 {
4682 vec4 blurredColor = texture(blurredTexture, texCoordsOut);
47- float focalDepth = texture(depthTexture, focalPoint + vec2 (0.5 )).r;
48- if (focalDepth != 0.0 )
83+ if (focalType == 0 )
4984 {
50- vec2 focalTexCoords = focalPoint + vec2 (0.5 );
51- float distance = length (depthToPosition(depth, texCoordsOut).xyz - depthToPosition(focalDepth, focalTexCoords).xyz);
52- float blur = smoothstep (nearDistance, farDistance, distance );
85+ float distance = depthToDistance(depth);
86+ float blur =
87+ distance - focalDistance >= 0.0 ?
88+ smoothstep (focalDistance, farDistance, distance ) :
89+ 1.0 - smoothstep (nearDistance, focalDistance, distance );
5390 frag = mix (unblurredColor, blurredColor, blur);
5491 }
55- else frag = blurredColor;
92+ else
93+ {
94+ float focalDistance = texture(depthTexture, focalPoint + vec2 (0.5 )).r;
95+ if (focalDistance != 0.0 )
96+ {
97+ vec2 focalTexCoords = focalPoint + vec2 (0.5 );
98+ float distance = length (depthToPosition(depth, texCoordsOut).xyz - depthToPosition(focalDistance, focalTexCoords).xyz);
99+ float blur = smoothstep (nearDistance, farDistance, distance );
100+ frag = mix (unblurredColor, blurredColor, blur);
101+ }
102+ else frag = blurredColor;
103+ }
56104 }
57105 else frag = unblurredColor;
58106}
0 commit comments