Skip to content

Commit e679715

Browse files
committed
-Dev: Procedural PBR sky done. Problems with sun direction...
1 parent 9873058 commit e679715

File tree

16 files changed

+235
-114
lines changed

16 files changed

+235
-114
lines changed

examples/resources/scenes/sponza.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@
383383

384384
<intensity value="10.0" />
385385
<color r="1.0" g="0.9" b="0.8" />
386-
<direction x="2.0" y="30.0" z="5.0" />
386+
<direction x="0.086" y="-0.97" z="0.21" />
387387

388388
<Shadow type="rt">
389-
<samples value="2" />
390-
<area value="0.1" />
389+
<samples value="1" />
390+
<area value="0.0" />
391391
</Shadow>
392392
</Light>
393393

include/engine/core/passes/sky_pass.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ VULKAN_ENGINE_NAMESPACE_BEGIN
1414

1515
namespace Core {
1616
/*
17-
Procedural Physically Based Sky generation pass. Based on "A Scalable and Production Ready Sky and Atmosphere Rendering Technique"
18-
by Sébastien Hillaire (2020).
17+
Procedural Physically Based Sky generation pass. Based on "A Scalable and Production Ready Sky and Atmosphere Rendering
18+
Technique" by Sébastien Hillaire (2020).
1919
2020
Transmittance LUT is based on "Precomputed Atmospheric Scattering" by Eric Bruneton and Fabrice Neyret (2008).
2121
@@ -26,6 +26,29 @@ class SkyPass : public GraphicPass
2626
protected:
2727
Graphics::DescriptorSet m_imageDescriptor;
2828

29+
/*
30+
* Every aerosol type expects 5 parameters:
31+
* - Scattering cross section
32+
* - Absorption cross section
33+
* - Base density (km^-3)
34+
* - Background density (km^-3)
35+
* - Height scaling parameter
36+
* These parameters can be sent as uniforms.
37+
*
38+
* This model for aerosols and their corresponding parameters come from
39+
* "A Physically-Based Spatio-Temporal Sky Model"
40+
* by Guimera et al. (2018).
41+
*/
42+
struct AerosolParams {
43+
Vec4 aerosolAbsorptionCrossSection = Vec4(0.0);
44+
Vec4 aerosolScatteringCrossSection = Vec4(0.0);
45+
float aerosolBaseDensity = 0.0f;
46+
float aerosolBackgroundDensity = 0.0f;
47+
float aerosolHeightScale = 0.0f;
48+
};
49+
50+
AerosolParams get_aerosol_params(AerosolType type);
51+
2952
public:
3053
SkyPass(Graphics::Device* ctx, Extent2D extent)
3154
: BasePass(ctx, extent, 3, 1, false, "SKY GENERATION") {
@@ -40,6 +63,8 @@ class SkyPass : public GraphicPass
4063

4164
virtual void setup_shader_passes();
4265

66+
virtual void update_framebuffer();
67+
4368
virtual void render(Graphics::Frame& currentFrame, Scene* const scene, uint32_t presentImageIndex = 0);
4469
};
4570

include/engine/core/scene/light.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ class PointLight : public Light
200200
class DirectionalLight : public Light
201201
{
202202
Vec3 m_direction;
203+
// Only works if using procedural sky as enviroment
204+
bool m_useAsSun = false;
203205

204206
static int m_instanceCount;
205207

@@ -212,13 +214,22 @@ class DirectionalLight : public Light
212214
, m_direction(direction) {
213215
DirectionalLight::m_instanceCount++;
214216
}
215-
217+
216218
inline Vec3 get_direction() const {
217219
return m_direction;
218220
}
219221
inline void set_direction(Vec3 d) {
220222
m_direction = d;
223+
m_direction = math::normalize(m_direction);
224+
}
225+
inline bool use_as_sun() const {
226+
return m_useAsSun;
221227
}
228+
inline void use_as_sun(bool op) {
229+
m_useAsSun = op;
230+
}
231+
232+
static Vec3 get_sun_direction(float elevationDeg, float rotationDeg);
222233

223234
virtual Graphics::LightUniforms get_uniforms(Mat4 cameraView) const;
224235
};

resources/shaders/VXGI/voxelization.glsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void main() {
192192
//Diffuse Component ________________________
193193
vec3 lighting = vec3(0.0);
194194
lighting = evalDiffuseLighting(
195-
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(scene.lights[i].worldPosition.xyz), //wi //wo
195+
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(-scene.lights[i].worldPosition.xyz), //wi //wo
196196
scene.lights[i].color * computeAttenuation(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity
197197
);
198198

@@ -207,10 +207,12 @@ void main() {
207207
TLAS,
208208
samplerMap,
209209
_pos,
210-
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : scene.lights[i].shadowData.xyz,
210+
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : -scene.lights[i].shadowData.xyz,
211211
1,
212-
0.0,
212+
0.0,
213+
scene.lights[i].type != DIRECTIONAL_LIGHT ? length(scene.lights[i].worldPosition.xyz - _pos) : 30.0,
213214
0);
215+
214216
}
215217

216218
color += lighting;

resources/shaders/deferred/composition.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void main()
183183
//Direct Component ________________________
184184
vec3 lighting = vec3(0.0);
185185
lighting = evalCookTorranceBRDF(
186-
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].position - g_pos) : normalize(scene.lights[i].position.xyz), //wi
186+
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].position - g_pos) : normalize(-scene.lights[i].position.xyz), //wi
187187
normalize(-g_pos), //wo
188188
scene.lights[i].color * computeAttenuation(scene.lights[i].position, g_pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity, //radiance
189189
brdf
@@ -200,9 +200,10 @@ void main()
200200
TLAS,
201201
samplerMap,
202202
modelPos,
203-
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : scene.lights[i].shadowData.xyz,
203+
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : -scene.lights[i].shadowData.xyz,
204204
int(scene.lights[i].shadowData.w),
205205
scene.lights[i].area,
206+
scene.lights[i].type != DIRECTIONAL_LIGHT ? length(scene.lights[i].worldPosition.xyz - modelPos) : 30.0,
206207
0);
207208
}
208209
direct += lighting;

resources/shaders/env/sky_generation.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ layout(location = 0) in vec2 v_uv;
2020

2121
layout(push_constant) uniform Settings {
2222
SkySettings sky;
23+
AerosolParams aerosolParams;
2324
} settings;
2425
layout(set = 0, binding = 0) uniform sampler2D transmitanceLUT;
2526

@@ -56,6 +57,7 @@ vec4 compute_inscattering(vec3 ray_origin, vec3 ray_dir, float t_d, out vec4 tra
5657
altitude,
5758
settings.sky.month,
5859
settings.sky.aerosolTurbidity,
60+
settings.aerosolParams,
5961
aerosol_absorption, aerosol_scattering,
6062
molecular_absorption, molecular_scattering,
6163
extinction);

resources/shaders/env/sky_tt_compute.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ layout(location = 0) in vec2 v_uv;
2828

2929
layout(push_constant) uniform Settings {
3030
SkySettings sky;
31+
AerosolParams aerosolParams;
3132
} settings;
3233

3334
layout(location = 0) out vec4 outColor;
@@ -61,6 +62,7 @@ void main()
6162
altitude,
6263
settings.sky.month,
6364
settings.sky.aerosolTurbidity,
65+
settings.aerosolParams,
6466
aerosol_absorption, aerosol_scattering,
6567
molecular_absorption, molecular_scattering,
6668
extinction);

resources/shaders/forward/physically_based.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ void main() {
214214
v_modelPos,
215215
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].shadowData.xyz - v_modelPos : scene.lights[i].shadowData.xyz,
216216
int(scene.lights[i].shadowData.w),
217-
scene.lights[i].area,
217+
scene.lights[i].area,
218+
scene.lights[i].type != DIRECTIONAL_LIGHT ? lenght(scene.lights[i].shadowData.xyz - v_modelPos) : 30.0,
218219
0);
219220
}
220221

resources/shaders/include/raytracing.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define EPSILON 0.001
1212
#endif
1313

14-
float computeRaytracedShadow(accelerationStructureEXT TLAS, sampler2D randomPool, vec3 O, vec3 L, int numSamples, float area, int ditherFactor){
14+
float computeRaytracedShadow(accelerationStructureEXT TLAS, sampler2D randomPool, vec3 O, vec3 L, int numSamples, float area, float tMax, int ditherFactor){
1515
if(numSamples == 0) return 1.0;
1616

1717
//Shadow occlusion contribution
@@ -26,7 +26,7 @@ float computeRaytracedShadow(accelerationStructureEXT TLAS, sampler2D randomPool
2626
vec2 rng = blueNoiseSample(randomPool,i,ditherFactor).rg;
2727
vec2 dsample = diskSample(rng,radius);
2828

29-
float tMax = length(L);
29+
// float tMax = length(L);
3030
vec3 dir = normalize(L + dsample.x * LTangent + dsample.y * LBitangent);
3131

3232
rayQueryEXT rayQuery;

resources/shaders/include/sky.glsl

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ struct SkySettings {
1313
int updateType;
1414
};
1515

16-
#define ANIMATE_SUN 0
16+
struct AerosolParams {
17+
vec4 aerosol_absorption_cross_section;
1718

18-
// 0=Background, 1=Desert Dust, 2=Maritime Clean, 3=Maritime Mineral,
19-
// 4=Polar Antarctic, 5=Polar Artic, 6=Remote Continental, 7=Rural, 8=Urban
20-
#define AEROSOL_TYPE 8
19+
vec4 aerosol_scattering_cross_section;
2120

22-
const float AEROSOL_TURBIDITY = 1.0;
21+
float aerosol_base_density;
22+
float aerosol_background_density;
23+
float aerosol_height_scale;
24+
};
2325

2426
// Ray marching steps. More steps mean better accuracy but worse performance
2527
const int TRANSMITTANCE_STEPS = 32;
@@ -38,7 +40,6 @@ const float CAMERA_ROLL = 0.0;
3840
#define ENABLE_MULTIPLE_SCATTERING 1
3941
#define ENABLE_AEROSOLS 1
4042
#define SHOW_RELATIVE_LUMINANCE 0
41-
#define TONEMAPPING_TECHNIQUE 0 // 0=ACES, 1=simple
4243

4344
//-----------------------------------------------------------------------------
4445
// Constants
@@ -98,75 +99,6 @@ const float ozone_mean_monthly_dobson[] = float[](
9899
315.0 // December
99100
);
100101

101-
/*
102-
* Every aerosol type expects 5 parameters:
103-
* - Scattering cross section
104-
* - Absorption cross section
105-
* - Base density (km^-3)
106-
* - Background density (km^-3)
107-
* - Height scaling parameter
108-
* These parameters can be sent as uniforms.
109-
*
110-
* This model for aerosols and their corresponding parameters come from
111-
* "A Physically-Based Spatio-Temporal Sky Model"
112-
* by Guimera et al. (2018).
113-
*/
114-
#if AEROSOL_TYPE == 0 // Background
115-
const vec4 aerosol_absorption_cross_section = vec4(4.5517e-19, 5.9269e-19, 6.9143e-19, 8.5228e-19);
116-
const vec4 aerosol_scattering_cross_section = vec4(1.8921e-26, 1.6951e-26, 1.7436e-26, 2.1158e-26);
117-
const float aerosol_base_density = 2.584e17;
118-
const float aerosol_background_density = 2e6;
119-
#elif AEROSOL_TYPE == 1 // Desert Dust
120-
const vec4 aerosol_absorption_cross_section = vec4(4.6758e-16, 4.4654e-16, 4.1989e-16, 4.1493e-16);
121-
const vec4 aerosol_scattering_cross_section = vec4(2.9144e-16, 3.1463e-16, 3.3902e-16, 3.4298e-16);
122-
const float aerosol_base_density = 1.8662e18;
123-
const float aerosol_background_density = 2e6;
124-
const float aerosol_height_scale = 2.0;
125-
#elif AEROSOL_TYPE == 2 // Maritime Clean
126-
const vec4 aerosol_absorption_cross_section = vec4(6.3312e-19, 7.5567e-19, 9.2627e-19, 1.0391e-18);
127-
const vec4 aerosol_scattering_cross_section = vec4(4.6539e-26, 2.721e-26, 4.1104e-26, 5.6249e-26);
128-
const float aerosol_base_density = 2.0266e17;
129-
const float aerosol_background_density = 2e6;
130-
const float aerosol_height_scale = 0.9;
131-
#elif AEROSOL_TYPE == 3 // Maritime Mineral
132-
const vec4 aerosol_absorption_cross_section = vec4(6.9365e-19, 7.5951e-19, 8.2423e-19, 8.9101e-19);
133-
const vec4 aerosol_scattering_cross_section = vec4(2.3699e-19, 2.2439e-19, 2.2126e-19, 2.021e-19);
134-
const float aerosol_base_density = 2.0266e17;
135-
const float aerosol_background_density = 2e6;
136-
const float aerosol_height_scale = 2.0;
137-
#elif AEROSOL_TYPE == 4 // Polar Antarctic
138-
const vec4 aerosol_absorption_cross_section = vec4(1.3399e-16, 1.3178e-16, 1.2909e-16, 1.3006e-16);
139-
const vec4 aerosol_scattering_cross_section = vec4(1.5506e-19, 1.809e-19, 2.3069e-19, 2.5804e-19);
140-
const float aerosol_base_density = 2.3864e16;
141-
const float aerosol_background_density = 2e6;
142-
const float aerosol_height_scale = 30.0;
143-
#elif AEROSOL_TYPE == 5 // Polar Arctic
144-
const vec4 aerosol_absorption_cross_section = vec4(1.0364e-16, 1.0609e-16, 1.0193e-16, 1.0092e-16);
145-
const vec4 aerosol_scattering_cross_section = vec4(2.1609e-17, 2.2759e-17, 2.5089e-17, 2.6323e-17);
146-
const float aerosol_base_density = 2.3864e16;
147-
const float aerosol_background_density = 2e6;
148-
const float aerosol_height_scale = 30.0;
149-
#elif AEROSOL_TYPE == 6 // Remote Continental
150-
const vec4 aerosol_absorption_cross_section = vec4(4.5307e-18, 5.0662e-18, 4.4877e-18, 3.7917e-18);
151-
const vec4 aerosol_scattering_cross_section = vec4(1.8764e-18, 1.746e-18, 1.6902e-18, 1.479e-18);
152-
const float aerosol_base_density = 6.103e18;
153-
const float aerosol_background_density = 2e6;
154-
const float aerosol_height_scale = 0.73;
155-
#elif AEROSOL_TYPE == 7 // Rural
156-
const vec4 aerosol_absorption_cross_section = vec4(5.0393e-23, 8.0765e-23, 1.3823e-22, 2.3383e-22);
157-
const vec4 aerosol_scattering_cross_section = vec4(2.6004e-22, 2.4844e-22, 2.8362e-22, 2.7494e-22);
158-
const float aerosol_base_density = 8.544e18;
159-
const float aerosol_background_density = 2e6;
160-
const float aerosol_height_scale = 0.73;
161-
#elif AEROSOL_TYPE == 8 // Urban
162-
const vec4 aerosol_absorption_cross_section = vec4(2.8722e-24, 4.6168e-24, 7.9706e-24, 1.3578e-23);
163-
const vec4 aerosol_scattering_cross_section = vec4(1.5908e-22, 1.7711e-22, 2.0942e-22, 2.4033e-22);
164-
const float aerosol_base_density = 1.3681e20;
165-
const float aerosol_background_density = 2e6;
166-
const float aerosol_height_scale = 0.73;
167-
#endif
168-
const float aerosol_background_divided_by_base_density = aerosol_background_density / aerosol_base_density;
169-
170102
//-----------------------------------------------------------------------------
171103

172104
vec3 get_sun_direction(float sunElevationDeg)
@@ -265,14 +197,15 @@ vec4 get_molecular_absorption_coefficient(float h, int month)
265197
return ozone_absorption_cross_section * ozone_mean_monthly_dobson[month] * density;
266198
}
267199

268-
float get_aerosol_density(float h)
200+
float get_aerosol_density(float h, AerosolParams params)
269201
{
270-
#if AEROSOL_TYPE == 0 // Only for the Background aerosol type, no dependency on height
271-
return aerosol_base_density * (1.0 + aerosol_background_divided_by_base_density);
272-
#else
273-
return aerosol_base_density * (exp(-h / aerosol_height_scale)
202+
const float aerosol_background_divided_by_base_density = params.aerosol_background_density / params.aerosol_base_density;
203+
204+
if(params.aerosol_height_scale != 0.0)
205+
return params.aerosol_base_density * (exp(-h / params.aerosol_height_scale)
274206
+ aerosol_background_divided_by_base_density);
275-
#endif
207+
else
208+
return params.aerosol_base_density * (1.0 + aerosol_background_divided_by_base_density);
276209
}
277210

278211
/*
@@ -282,6 +215,7 @@ float get_aerosol_density(float h)
282215
void get_atmosphere_collision_coefficients(in float h,
283216
in int month,
284217
in float turbidity,
218+
in AerosolParams params,
285219
out vec4 aerosol_absorption,
286220
out vec4 aerosol_scattering,
287221
out vec4 molecular_absorption,
@@ -293,9 +227,9 @@ void get_atmosphere_collision_coefficients(in float h,
293227
aerosol_absorption = vec4(0.0);
294228
aerosol_scattering = vec4(0.0);
295229
#else
296-
float aerosol_density = get_aerosol_density(h);
297-
aerosol_absorption = aerosol_absorption_cross_section * aerosol_density * turbidity;
298-
aerosol_scattering = aerosol_scattering_cross_section * aerosol_density * turbidity;
230+
float aerosol_density = get_aerosol_density(h, params);
231+
aerosol_absorption = params.aerosol_absorption_cross_section * aerosol_density * turbidity;
232+
aerosol_scattering = params.aerosol_scattering_cross_section * aerosol_density * turbidity;
299233
#endif
300234
molecular_absorption = get_molecular_absorption_coefficient(h, month);
301235
molecular_scattering = get_molecular_scattering_coefficient(h);

0 commit comments

Comments
 (0)