Skip to content

Commit fd102a7

Browse files
committed
Light.angularRadius
1 parent 30a4409 commit fd102a7

6 files changed

Lines changed: 28 additions & 20 deletions

File tree

data/__internal/shaders/AreaLight/AreaLight.frag.glsl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ subroutine(srtLightRadiance) vec3 lightRadianceAreaSphere(
9898

9999
vec3 positionToLightSource = lightPosition - pos;
100100
float distanceToLight = length(positionToLightSource);
101-
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.001)), 0.0, 1.0), 4.0) * lightEnergy;
101+
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.00001)), 0.0, 1.0), 4.0) * lightEnergy;
102102

103103
vec3 Lpt = normalize(positionToLightSource);
104104

105105
vec3 centerToRay = dot(positionToLightSource, R) * R - positionToLightSource;
106-
vec3 closestPoint = positionToLightSource + centerToRay * clamp(lightAreaRadius / max(length(centerToRay), 0.001), 0.0, 1.0);
106+
vec3 closestPoint = positionToLightSource + centerToRay * clamp(lightAreaRadius / max(length(centerToRay), 0.00001), 0.0, 1.0);
107107
vec3 L = normalize(closestPoint);
108108

109109
float NL = max(dot(N, Lpt), 0.0);
@@ -122,15 +122,15 @@ subroutine(srtLightRadiance) vec3 lightRadianceAreaSphere(
122122
// fss90 used to "flatten" retroreflection based on roughness
123123
float FL = schlickFresnel(NL);
124124
float FV = schlickFresnel(NE);
125-
float fss90 = LH * LH * max(roughness, 0.001);
125+
float fss90 = LH * LH * max(roughness, 0.00001);
126126
float fss = mix(1.0, fss90, FL) * mix(1.0, fss90, FV);
127127
float ss = 1.25 * (fss * (1.0 / max(NL + NE, 0.1) - 0.5) + 0.5);
128128

129129
vec3 diffuse = invPI * albedo * mix(kD * NL * occlusion, vec3(ss), subsurface) * (1.0 - metallic);
130130

131131
vec3 numerator = NDF * G * F;
132132
float denominator = 4.0 * max(dot(N, E), 0.0) * NL;
133-
vec3 specular = numerator / max(denominator, 0.001);
133+
vec3 specular = numerator / max(denominator, 0.00001);
134134

135135
vec3 lightColorLinear = toLinear(lightColor.rgb);
136136

@@ -168,7 +168,7 @@ subroutine(srtLightRadiance) vec3 lightRadianceAreaTube(
168168

169169
vec3 positionToLightSource = (lightPosition + lightPosition2) * 0.5 - pos;
170170
float distanceToLight = length(positionToLightSource);
171-
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.001)), 0.0, 1.0), 4.0) * lightEnergy;
171+
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.00001)), 0.0, 1.0), 4.0) * lightEnergy;
172172

173173
vec3 Lpt = normalize(positionToLightSource);
174174

@@ -188,15 +188,15 @@ subroutine(srtLightRadiance) vec3 lightRadianceAreaTube(
188188
// fss90 used to "flatten" retroreflection based on roughness
189189
float FL = schlickFresnel(NL);
190190
float FV = schlickFresnel(NE);
191-
float fss90 = LH * LH * max(roughness, 0.001);
191+
float fss90 = LH * LH * max(roughness, 0.00001);
192192
float fss = mix(1.0, fss90, FL) * mix(1.0, fss90, FV);
193193
float ss = 1.25 * (fss * (1.0 / max(NL + NE, 0.1) - 0.5) + 0.5);
194194

195195
vec3 diffuse = invPI * albedo * mix(kD * NL * occlusion, vec3(ss), subsurface) * (1.0 - metallic);
196196

197197
vec3 numerator = NDF * G * F;
198198
float denominator = 4.0 * max(dot(N, E), 0.0) * NL;
199-
vec3 specular = numerator / max(denominator, 0.001);
199+
vec3 specular = numerator / max(denominator, 0.00001);
200200

201201
vec3 incomingLight = toLinear(lightColor.rgb) * attenuation;
202202
vec3 radiance = (diffuse * lightDiffuse + specular * lightSpecular * NL) * incomingLight;
@@ -221,7 +221,7 @@ subroutine(srtLightRadiance) vec3 lightRadianceSpot(
221221
vec3 positionToLightSource = lightPosition - pos;
222222
float distanceToLight = length(positionToLightSource);
223223
vec3 L = normalize(positionToLightSource);
224-
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.001)), 0.0, 1.0), 2.0) * lightEnergy;
224+
float attenuation = pow(clamp(1.0 - (distanceToLight / max(lightRadius, 0.00001)), 0.0, 1.0), 2.0) * lightEnergy;
225225

226226
float spotCos = clamp(dot(L, normalize(lightSpotDirection)), 0.0, 1.0);
227227
float spotValue = smoothstep(lightSpotCosCutoff, lightSpotCosInnerCutoff, spotCos);
@@ -243,15 +243,15 @@ subroutine(srtLightRadiance) vec3 lightRadianceSpot(
243243
// fss90 used to "flatten" retroreflection based on roughness
244244
float FL = schlickFresnel(NL);
245245
float FV = schlickFresnel(NE);
246-
float fss90 = LH * LH * max(roughness, 0.001);
246+
float fss90 = LH * LH * max(roughness, 0.00001);
247247
float fss = mix(1.0, fss90, FL) * mix(1.0, fss90, FV);
248248
float ss = 1.25 * (fss * (1.0 / max(NL + NE, 0.1) - 0.5) + 0.5);
249249

250250
vec3 diffuse = invPI * albedo * mix(kD * NL * occlusion, vec3(ss), subsurface) * (1.0 - metallic);
251251

252252
vec3 numerator = NDF * G * F;
253253
float denominator = 4.0 * max(dot(N, E), 0.0) * NL;
254-
vec3 specular = numerator / max(denominator, 0.001);
254+
vec3 specular = numerator / max(denominator, 0.00001);
255255

256256
vec3 incomingLight = toLinear(lightColor.rgb) * attenuation;
257257
vec3 radiance = (diffuse * lightDiffuse + specular * lightSpecular * NL) * incomingLight;
@@ -286,7 +286,7 @@ subroutine(srtShadow) float shadowMapDualParaboloid(in vec3 worldPos)
286286
vec2 uv = dir.xy / (1.0 + abs(dir.z));
287287
uv = uv * 0.5 + 0.5;
288288
float layer = 1.0 - float(dir.z >= 0.0);
289-
float z = distanceToLight / max(0.001, lightRadius) - bias;
289+
float z = distanceToLight / max(0.00001, lightRadius) - bias;
290290
float shadow = texture(shadowTextureArray, vec4(uv, layer, z));
291291
return shadow;
292292
}

data/__internal/shaders/Forward/Forward.frag.glsl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ subroutine(srtAmbient) vec3 ambientEquirectangularMap(in vec3 wN, in float perce
245245
{
246246
ivec2 envMapSize = textureSize(ambientTexture, 0);
247247
float resolution = float(max(envMapSize.x, envMapSize.y));
248-
//float glossyExponent = 2.0 / pow(perceptualRoughness, 4.0) - 2.0;
249-
//float lod = log2(resolution * sqrt(3.0)) - 0.5 * log2(glossyExponent + 1.0);
250248
float lod = log2(resolution) * perceptualRoughness;
251249
return textureLod(ambientTexture, envMapEquirect(wN), lod).rgb;
252250
}
@@ -256,8 +254,6 @@ subroutine(srtAmbient) vec3 ambientCubemap(in vec3 wN, in float perceptualRoughn
256254
{
257255
ivec2 envMapSize = textureSize(ambientTextureCube, 0);
258256
float resolution = float(max(envMapSize.x, envMapSize.y));
259-
//float glossyExponent = 2.0 / pow(perceptualRoughness, 4.0) - 2.0;
260-
//float lod = log2(resolution * sqrt(3.0)) - 0.5 * log2(glossyExponent + 1.0);
261257
float lod = log2(resolution) * perceptualRoughness;
262258
return textureLod(ambientTextureCube, wN, lod).rgb;
263259
}
@@ -387,7 +383,7 @@ void main()
387383
vec3 F = fresnelRoughness(max(dot(H, E), 0.0), f0, r);
388384

389385
vec3 kD = (1.0 - F) * (1.0 - m);
390-
vec3 specular = (NDF * G * F) / max(4.0 * max(dot(N, E), 0.0) * NL, 0.001);
386+
vec3 specular = (NDF * G * F) / max(4.0 * max(dot(N, E), 0.0) * NL, 0.00001);
391387

392388
vec3 incomingLight = toLinear(sunColor.rgb) * sunEnergy;
393389
vec3 diffuse = albedo * invPI;
@@ -420,7 +416,7 @@ void main()
420416
float invSamples = 1.0 / float(sunScatteringSamples);
421417
float offset = hash((texCoord * 467.759 + time) * eyePosition.z);
422418
accumScatter = 0.0;
423-
for (float i = 0; i < float(sunScatteringSamples); i+=1.0)
419+
for (float i = 0; i < float(sunScatteringSamples); i += 1.0)
424420
{
425421
accumScatter += shadowLookup(shadowTextureArray, 1.0, shadowMatrix2 * vec4(currentPosition, 1.0), vec2(0.0));
426422
currentPosition += rayDirection * (stepSize - offset * sunScatteringMaxRandomStepOffset);

data/__internal/shaders/SunLight/SunLight.frag.glsl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ uniform float fogEnd;
2828
uniform vec3 lightDirection;
2929
uniform vec4 lightColor;
3030
uniform float lightEnergy;
31+
uniform float lightAngularRadius;
3132
uniform bool lightScattering;
3233
uniform float lightScatteringG;
3334
uniform float lightScatteringDensity;
@@ -141,13 +142,16 @@ void main()
141142
float NE = max(dot(N, E), 0.0);
142143
vec3 H = normalize(E + L);
143144
float LH = max(dot(L, H), 0.0);
145+
146+
// Simulate sun disk
147+
roughness = max(roughness, lightAngularRadius);
144148

145149
float NDF = distributionGGX(N, H, roughness);
146150
float G = geometrySmith(N, E, L, roughness);
147151
vec3 F = fresnelRoughness(max(dot(H, E), 0.0), f0, roughness);
148152

149153
vec3 kD = (1.0 - F);
150-
vec3 specular = (NDF * G * F) / max(4.0 * max(dot(N, E), 0.0) * NL, 0.001);
154+
vec3 specular = (NDF * G * F) / max(4.0 * max(dot(N, E), 0.0) * NL, 0.00001);
151155

152156
vec3 incomingLight = toLinear(lightColor.rgb) * lightEnergy;
153157

@@ -156,7 +160,7 @@ void main()
156160
// fss90 used to "flatten" retroreflection based on roughness
157161
float FL = schlickFresnel(NL);
158162
float FV = schlickFresnel(NE);
159-
float fss90 = LH * LH * max(roughness, 0.001);
163+
float fss90 = LH * LH * max(roughness, 0.00001);
160164
float fss = mix(1.0, fss90, FL) * mix(1.0, fss90, FV);
161165
float ss = 1.25 * (fss * (1.0 / max(NL + NE, 0.1) - 0.5) + 0.5);
162166

data/__internal/shaders/include/ggx.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ float distributionGGX(vec3 N, vec3 H, float roughness)
55
float NdotH = max(dot(N, H), 0.0);
66
float NdotH2 = NdotH * NdotH;
77
float num = a2;
8-
float denom = max(NdotH2 * (a2 - 1.0) + 1.0, 0.001);
8+
float denom = max(NdotH2 * (a2 - 1.0) + 1.0, 0.00001);
99
const float Pi = 3.14159265359;
1010
denom = Pi * denom * denom;
1111
return num / denom;

src/dagon/graphics/light.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class Light: Entity
9494

9595
/// The color of the light.
9696
Color4f color;
97+
98+
/// For sun light, specifies effective angular radius of the sun disk.
99+
float angularRadius;
97100

98101
/**
99102
* Attenuation radius of the light volume.
@@ -176,6 +179,7 @@ class Light: Entity
176179
shining = true;
177180
length = 1.0f;
178181
color = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
182+
angularRadius = 0.0f;
179183
volumeRadius = 1.0f;
180184
radius = 0.0f;
181185
energy = 1.0f;

src/dagon/render/deferred/shaders/sunlight.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class SunLightShader: Shader
7070
ShaderParameter!Vector3f lightDirection;
7171
ShaderParameter!Color4f lightColor;
7272
ShaderParameter!float lightEnergy;
73+
ShaderParameter!float lightAngularRadius;
7374
ShaderParameter!int lightScattering;
7475
ShaderParameter!float lightScatteringG;
7576
ShaderParameter!float lightScatteringDensity;
@@ -136,6 +137,7 @@ class SunLightShader: Shader
136137
lightDirection = createParameter!Vector3f("lightDirection");
137138
lightColor = createParameter!Color4f("lightColor");
138139
lightEnergy = createParameter!float("lightEnergy");
140+
lightAngularRadius = createParameter!float("lightAngularRadius");
139141
lightScattering = createParameter!int("lightScattering");
140142
lightScatteringG = createParameter!float("lightScatteringG");
141143
lightScatteringDensity = createParameter!float("lightScatteringDensity");
@@ -207,6 +209,7 @@ class SunLightShader: Shader
207209
lightDirection = (lightDirHg * state.viewMatrix).xyz;
208210
lightColor = light.color;
209211
lightEnergy = light.energy;
212+
lightAngularRadius = light.angularRadius;
210213
lightScattering = light.scatteringEnabled;
211214
lightScatteringG = 1.0f - light.scattering;
212215
lightScatteringDensity = light.mediumDensity;
@@ -222,6 +225,7 @@ class SunLightShader: Shader
222225
lightDirection = (lightDirHg * state.viewMatrix).xyz;
223226
lightColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
224227
lightEnergy = 1.0f;
228+
lightAngularRadius = 0.0f;
225229
lightScattering = 0;
226230
lightScatteringG = 0.0f;
227231
lightScatteringDensity = 0.0f;

0 commit comments

Comments
 (0)