Skip to content

Commit 4caa57d

Browse files
committed
Merge branch 'depth-of-field-static'
2 parents 41abe89 + f4b0281 commit 4caa57d

75 files changed

Lines changed: 1301 additions & 251 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Nu/Nu.Gaia/Assets/Default/FilterDepthOfField.glsl

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ uniform mat4 viewInverse;
1919
uniform mat4 projectionInverse;
2020
uniform float nearDistance;
2121
uniform float farDistance;
22+
uniform int focalType;
23+
uniform float focalDistance;
2224
uniform vec2 focalPoint;
2325
uniform sampler2D depthTexture;
2426
uniform sampler2D blurredTexture;
@@ -28,6 +30,15 @@ in vec2 texCoordsOut;
2830

2931
layout(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+
3142
vec4 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

4051
void 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
}

Nu/Nu.Gaia/Assets/Default/PhysicallyBasedDeferredLightMapping.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void main()
1818
const float PI = 3.141592654;
1919
const float FLOAT_MAX = 3.402823466e+38;
2020
const int LIGHT_MAPS_MAX = 26;
21-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
2221

2322
uniform vec3 eyeCenter;
2423
uniform mat4 viewInverse;
@@ -29,6 +28,7 @@ uniform vec3 lightMapOrigins[LIGHT_MAPS_MAX];
2928
uniform vec3 lightMapMins[LIGHT_MAPS_MAX];
3029
uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
3130
uniform int lightMapsCount;
31+
uniform float lightMapSingletonBlendMargin;
3232

3333
in vec2 texCoordsOut;
3434

@@ -142,7 +142,7 @@ void main()
142142
vec3 min1 = lightMapMins[lm1];
143143
vec3 size1 = lightMapSizes[lm1];
144144
float distance = distanceToOutside(position.xyz, min1, size1);
145-
ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
145+
ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
146146
}
147147
else if (lm1 != -1 && lm2 != -1)
148148
{

Nu/Nu.Gaia/Assets/Default/PhysicallyBasedForwardAnimated.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ const float GAMMA = 2.2;
8686
const float ATTENUATION_CONSTANT = 1.0f;
8787
const float ENVIRONMENT_FILTER_REFRACTED_SATURATION = 2.0;
8888
const int LIGHT_MAPS_MAX = 2;
89-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
9089
const int LIGHTS_MAX = 9;
9190
const int SHADOW_TEXTURES_MAX = 12;
9291
const int SHADOW_MAPS_MAX = 12;
@@ -163,6 +162,7 @@ uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
163162
uniform vec3 lightMapAmbientColors[LIGHT_MAPS_MAX];
164163
uniform float lightMapAmbientBrightnesses[LIGHT_MAPS_MAX];
165164
uniform int lightMapsCount;
165+
uniform float lightMapSingletonBlendMargin;
166166
uniform vec3 lightOrigins[LIGHTS_MAX];
167167
uniform vec3 lightDirections[LIGHTS_MAX];
168168
uniform vec3 lightColors[LIGHTS_MAX];
@@ -1097,7 +1097,7 @@ void main()
10971097
vec3 min1 = lightMapMins[lm1];
10981098
vec3 size1 = lightMapSizes[lm1];
10991099
float distance = distanceToOutside(position.xyz, min1, size1);
1100-
float ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
1100+
float ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
11011101

11021102
// compute blended ambient values
11031103
vec3 ambientColor1 = lightMapAmbientColors[lm1];

Nu/Nu.Gaia/Assets/Default/PhysicallyBasedForwardStatic.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const float GAMMA = 2.2;
6767
const float ATTENUATION_CONSTANT = 1.0f;
6868
const float ENVIRONMENT_FILTER_REFRACTED_SATURATION = 2.0;
6969
const int LIGHT_MAPS_MAX = 2;
70-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
7170
const int LIGHTS_MAX = 9;
7271
const int SHADOW_TEXTURES_MAX = 12;
7372
const int SHADOW_MAPS_MAX = 12;
@@ -144,6 +143,7 @@ uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
144143
uniform vec3 lightMapAmbientColors[LIGHT_MAPS_MAX];
145144
uniform float lightMapAmbientBrightnesses[LIGHT_MAPS_MAX];
146145
uniform int lightMapsCount;
146+
uniform float lightMapSingletonBlendMargin;
147147
uniform vec3 lightOrigins[LIGHTS_MAX];
148148
uniform vec3 lightDirections[LIGHTS_MAX];
149149
uniform vec3 lightColors[LIGHTS_MAX];
@@ -1078,7 +1078,7 @@ void main()
10781078
vec3 min1 = lightMapMins[lm1];
10791079
vec3 size1 = lightMapSizes[lm1];
10801080
float distance = distanceToOutside(position.xyz, min1, size1);
1081-
float ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
1081+
float ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
10821082

10831083
// compute blended ambient values
10841084
vec3 ambientColor1 = lightMapAmbientColors[lm1];

Nu/Nu.Pipe/Assets/Default/FilterDepthOfField.glsl

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ uniform mat4 viewInverse;
1919
uniform mat4 projectionInverse;
2020
uniform float nearDistance;
2121
uniform float farDistance;
22+
uniform int focalType;
23+
uniform float focalDistance;
2224
uniform vec2 focalPoint;
2325
uniform sampler2D depthTexture;
2426
uniform sampler2D blurredTexture;
@@ -28,6 +30,15 @@ in vec2 texCoordsOut;
2830

2931
layout(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+
3142
vec4 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

4051
void 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
}

Nu/Nu.Pipe/Assets/Default/PhysicallyBasedDeferredLightMapping.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void main()
1818
const float PI = 3.141592654;
1919
const float FLOAT_MAX = 3.402823466e+38;
2020
const int LIGHT_MAPS_MAX = 26;
21-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
2221

2322
uniform vec3 eyeCenter;
2423
uniform mat4 viewInverse;
@@ -29,6 +28,7 @@ uniform vec3 lightMapOrigins[LIGHT_MAPS_MAX];
2928
uniform vec3 lightMapMins[LIGHT_MAPS_MAX];
3029
uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
3130
uniform int lightMapsCount;
31+
uniform float lightMapSingletonBlendMargin;
3232

3333
in vec2 texCoordsOut;
3434

@@ -142,7 +142,7 @@ void main()
142142
vec3 min1 = lightMapMins[lm1];
143143
vec3 size1 = lightMapSizes[lm1];
144144
float distance = distanceToOutside(position.xyz, min1, size1);
145-
ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
145+
ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
146146
}
147147
else if (lm1 != -1 && lm2 != -1)
148148
{

Nu/Nu.Pipe/Assets/Default/PhysicallyBasedForwardAnimated.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ const float GAMMA = 2.2;
8686
const float ATTENUATION_CONSTANT = 1.0f;
8787
const float ENVIRONMENT_FILTER_REFRACTED_SATURATION = 2.0;
8888
const int LIGHT_MAPS_MAX = 2;
89-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
9089
const int LIGHTS_MAX = 9;
9190
const int SHADOW_TEXTURES_MAX = 12;
9291
const int SHADOW_MAPS_MAX = 12;
@@ -163,6 +162,7 @@ uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
163162
uniform vec3 lightMapAmbientColors[LIGHT_MAPS_MAX];
164163
uniform float lightMapAmbientBrightnesses[LIGHT_MAPS_MAX];
165164
uniform int lightMapsCount;
165+
uniform float lightMapSingletonBlendMargin;
166166
uniform vec3 lightOrigins[LIGHTS_MAX];
167167
uniform vec3 lightDirections[LIGHTS_MAX];
168168
uniform vec3 lightColors[LIGHTS_MAX];
@@ -1097,7 +1097,7 @@ void main()
10971097
vec3 min1 = lightMapMins[lm1];
10981098
vec3 size1 = lightMapSizes[lm1];
10991099
float distance = distanceToOutside(position.xyz, min1, size1);
1100-
float ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
1100+
float ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
11011101

11021102
// compute blended ambient values
11031103
vec3 ambientColor1 = lightMapAmbientColors[lm1];

Nu/Nu.Pipe/Assets/Default/PhysicallyBasedForwardStatic.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const float GAMMA = 2.2;
6767
const float ATTENUATION_CONSTANT = 1.0f;
6868
const float ENVIRONMENT_FILTER_REFRACTED_SATURATION = 2.0;
6969
const int LIGHT_MAPS_MAX = 2;
70-
const float LIGHT_MAP_SINGLETON_FADE_MARGIN = 0.1;
7170
const int LIGHTS_MAX = 9;
7271
const int SHADOW_TEXTURES_MAX = 12;
7372
const int SHADOW_MAPS_MAX = 12;
@@ -144,6 +143,7 @@ uniform vec3 lightMapSizes[LIGHT_MAPS_MAX];
144143
uniform vec3 lightMapAmbientColors[LIGHT_MAPS_MAX];
145144
uniform float lightMapAmbientBrightnesses[LIGHT_MAPS_MAX];
146145
uniform int lightMapsCount;
146+
uniform float lightMapSingletonBlendMargin;
147147
uniform vec3 lightOrigins[LIGHTS_MAX];
148148
uniform vec3 lightDirections[LIGHTS_MAX];
149149
uniform vec3 lightColors[LIGHTS_MAX];
@@ -1078,7 +1078,7 @@ void main()
10781078
vec3 min1 = lightMapMins[lm1];
10791079
vec3 size1 = lightMapSizes[lm1];
10801080
float distance = distanceToOutside(position.xyz, min1, size1);
1081-
float ratio = 1.0 - smoothstep(0.0, LIGHT_MAP_SINGLETON_FADE_MARGIN, distance);
1081+
float ratio = 1.0 - smoothstep(0.0, lightMapSingletonBlendMargin, distance);
10821082

10831083
// compute blended ambient values
10841084
vec3 ambientColor1 = lightMapAmbientColors[lm1];

0 commit comments

Comments
 (0)