+ | Volumetric ligthing

This commit is contained in:
TheRedShip
2025-01-13 23:45:13 +01:00
parent 819c07f90d
commit 1d3627000d
5 changed files with 113 additions and 16 deletions

View File

@ -139,21 +139,114 @@ hitInfo traceRay(Ray ray)
return (hit);
}
float sampleHG(float g, inout uint rng_state)
{
if (abs(g) < 0.001)
return 2.0 * randomValue(rng_state) - 1.0;
float sqr_term = (1.0 - g * g) / (1.0 + g - 2.0 * g * randomValue(rng_state));
return (1.0 + g * g - sqr_term * sqr_term) / (2.0 * g);
}
vec3 sampleDirection(vec3 forward, float cos_theta, inout uint rng_state)
{
float phi = 2.0 * M_PI * randomValue(rng_state);
float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta));
vec3 dir;
dir.x = sin_theta * cos(phi);
dir.y = sin_theta * sin(phi);
dir.z = cos_theta;
vec3 up = abs(forward.z) < 0.999 ? vec3(0, 0, 1) : vec3(1, 0, 0);
vec3 right = normalize(cross(up, forward));
up = cross(forward, right);
return normalize(
dir.x * right +
dir.y * up +
dir.z * forward
);
}
vec3 sampleLights(vec3 position)
{
vec3 light = vec3(0.0);
for (int i = 0; i < u_objectsNum; i++)
{
GPUObject obj = objects[i];
GPUMaterial mat = materials[obj.mat_index];
if (obj.type == 0 && mat.emission > 0.0)
{
vec3 light_dir = normalize(obj.position - position);
float light_dist = length(obj.position - position);
Ray shadow_ray = Ray(position + light_dir * 0.01, light_dir);
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index == i)
light += mat.emission * mat.color / (light_dist);
}
}
return (light);
}
struct VolumeProperties {
vec3 sigma_a; // absorption coefficient
vec3 sigma_s; // scattering coefficient
vec3 sigma_t; // extinction coefficient
float g; // phase function parameter
};
vec3 pathtrace(Ray ray, inout uint rng_state)
{
vec3 color = vec3(1.0);
vec3 light = vec3(0.0);
vec3 transmittance = vec3(1.0);
VolumeProperties volume;
volume.sigma_a = vec3(0.0001);
volume.sigma_s = vec3(0.0800);
volume.sigma_t = volume.sigma_a + volume.sigma_s;
volume.g = 1.;
for (int i = 0; i < camera.bounce; i++)
{
hitInfo hit = traceRay(ray);
float t_scatter = -log(randomValue(rng_state)) / volume.sigma_t.x;
float t_surface = hit.t;
if (t_scatter < t_surface && volume.sigma_t.x > 0.0)
{
vec3 scatter_pos = ray.origin + ray.direction * t_scatter;
transmittance *= exp(-volume.sigma_t * t_scatter);
color *= volume.sigma_s / volume.sigma_t;
light += transmittance * color * sampleLights(scatter_pos);
float cos_theta = sampleHG(volume.g, rng_state);
vec3 new_dir = sampleDirection(ray.direction, cos_theta, rng_state);
ray.origin = scatter_pos;
ray.direction = new_dir;
continue;
}
if (hit.obj_index == -1)
{
light += GetEnvironmentLight(ray);
light += transmittance * GetEnvironmentLight(ray);
// light += vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f); //ambient color
break;
}
transmittance *= exp(-volume.sigma_t * t_surface);
GPUObject obj = objects[hit.obj_index];
GPUMaterial mat = materials[obj.mat_index];
@ -166,6 +259,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
color *= mat.color;
light += mat.emission * mat.color;
// light += sampleLights(hit.position);
if (mat.emission > 0.0)
break;