2023-01-29 04:25:14 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2023-01-24 05:22:40 +01:00
|
|
|
#include "cloudshader.h"
|
2023-01-24 06:44:20 +01:00
|
|
|
#include "common/noise/cloudnoise.h"
|
2023-01-24 05:22:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
Color CloudShader::shade(const Scene &scene, const Ray &ray) const
|
|
|
|
{
|
|
|
|
Vector3d hitPoint = ray.origin + ray.direction * ray.length; // Potentially add epsilon
|
|
|
|
|
|
|
|
// Collect getNoise through the cloud
|
|
|
|
float cloudLength = 0.0f; // Length of cloud in ray direction
|
|
|
|
|
|
|
|
// Get background color behind cloud and information about the clouds length
|
|
|
|
Ray cloudRay = ray;
|
|
|
|
cloudRay.origin = ray.origin + (ray.length + REFR_EPS) * ray.direction;
|
|
|
|
cloudRay.length = INFINITY;
|
|
|
|
cloudRay.primitive = nullptr;
|
|
|
|
|
|
|
|
// Get out of cloud primitive first
|
|
|
|
if (ray.primitive->intersect(cloudRay))
|
|
|
|
{
|
|
|
|
// Get length
|
|
|
|
cloudLength = cloudRay.length;
|
|
|
|
|
|
|
|
// Prepare ray for background color
|
|
|
|
cloudRay.setRemainingBounces(cloudRay.getRemainingBounces() + 1);
|
|
|
|
cloudRay.origin = cloudRay.origin + (cloudRay.length + REFR_EPS) * cloudRay.direction;
|
|
|
|
cloudRay.length = INFINITY;
|
|
|
|
cloudRay.primitive = nullptr;
|
|
|
|
}
|
|
|
|
Color background = scene.traceRay(cloudRay);
|
|
|
|
|
|
|
|
if (cloudLength == 0.0f) return background; // No cloud or at edge
|
|
|
|
|
2023-01-24 11:18:03 +01:00
|
|
|
// Calculate step length
|
2023-01-29 04:25:14 +01:00
|
|
|
float stepLength = settings.densitySteps;
|
|
|
|
int noiseSamples = std::floor(cloudLength / stepLength);
|
2023-01-24 11:18:03 +01:00
|
|
|
|
|
|
|
// Step through cloud
|
2023-01-29 04:25:14 +01:00
|
|
|
float transmittance = 1.0f;
|
2023-01-26 05:04:30 +01:00
|
|
|
Color cloudColor = Color(0, 0, 0);
|
2023-01-24 11:18:03 +01:00
|
|
|
for (int i = 0; i < noiseSamples; ++i)
|
2023-01-24 05:22:40 +01:00
|
|
|
{
|
2023-01-24 11:18:03 +01:00
|
|
|
// Get sample point
|
2023-01-29 04:25:14 +01:00
|
|
|
Vector3d lengthDirection = (float) i * stepLength * ray.direction;
|
2023-01-26 05:04:30 +01:00
|
|
|
Vector3d samplePoint = hitPoint + lengthDirection;
|
2023-01-24 05:22:40 +01:00
|
|
|
|
2023-01-24 11:18:03 +01:00
|
|
|
// Get data at point
|
2023-01-29 04:25:14 +01:00
|
|
|
float sampleDensity = getCloudDensity(samplePoint, ray.primitive) * stepLength;
|
2023-01-24 22:55:48 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
if (sampleDensity <= 0)
|
2023-01-26 05:04:30 +01:00
|
|
|
{
|
2023-01-29 04:25:14 +01:00
|
|
|
continue;
|
2023-01-24 22:55:48 +01:00
|
|
|
}
|
2023-01-24 11:18:03 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
cloudColor += lightMarch(scene, samplePoint, lengthDirection, ray.primitive) * sampleDensity * transmittance;
|
|
|
|
transmittance *= calcBeer(sampleDensity * settings.lightAbsorptionThroughCloud);
|
2023-01-27 05:27:42 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
if (transmittance < TRANSMITTANCE_BREAK) break;
|
|
|
|
}
|
2023-01-27 05:22:54 +01:00
|
|
|
|
2023-01-26 05:04:30 +01:00
|
|
|
return background * transmittance + cloudColor;
|
2023-01-24 05:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudShader::isTransparent() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloudShader::CloudShader(const CloudSettings &settings) : settings(settings),
|
2023-01-29 15:48:17 +01:00
|
|
|
cloudNoise(CloudNoise(settings.noiseSize, settings.seed))
|
2023-01-24 05:22:40 +01:00
|
|
|
{
|
2023-01-24 06:26:24 +01:00
|
|
|
cloudNoise.invert = true;
|
2023-01-24 05:22:40 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
float CloudShader::getCloudDensity(Vector3d point, Primitive const *primitive) const
|
2023-01-24 05:22:40 +01:00
|
|
|
{
|
2023-01-29 04:25:14 +01:00
|
|
|
//! Requires the unscaled point
|
|
|
|
float edgeDensity = getEdgeDensity(point, primitive);
|
2023-01-24 05:22:40 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
point /= settings.scale;
|
2023-01-24 06:26:24 +01:00
|
|
|
float density = cloudNoise.getNoise(point);
|
2023-01-24 05:22:40 +01:00
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
// Threshold
|
|
|
|
// TODO: Smooth out!
|
2023-01-26 05:04:30 +01:00
|
|
|
density = std::max(0.0f, density + settings.densityOffset) * settings.densityIntensity;
|
2023-01-24 05:22:40 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
return density * edgeDensity;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CloudShader::getEdgeDensity(const Vector3d &point, const Primitive *primitive) const
|
|
|
|
{
|
|
|
|
if (primitive == nullptr)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3d size = Vector3d(0, 0, 0);
|
|
|
|
size.x = primitive->maximumBounds(0) - primitive->minimumBounds(0);
|
|
|
|
size.y = primitive->maximumBounds(1) - primitive->minimumBounds(1);
|
|
|
|
size.z = primitive->maximumBounds(2) - primitive->minimumBounds(2);
|
|
|
|
|
|
|
|
Vector3d center = Vector3d(0, 0, 0);
|
|
|
|
center.x = primitive->maximumBounds(0) + primitive->minimumBounds(0);
|
|
|
|
center.y = primitive->maximumBounds(1) + primitive->minimumBounds(1);
|
|
|
|
center.z = primitive->maximumBounds(2) + primitive->minimumBounds(2);
|
|
|
|
center /= 2;
|
|
|
|
|
|
|
|
Vector3d distance = point - center;
|
|
|
|
distance.x = std::abs(distance.x);
|
|
|
|
distance.y = std::abs(distance.y);
|
|
|
|
distance.z = std::abs(distance.z);
|
|
|
|
|
|
|
|
Vector3d distanceFromEdge = size / 2 - distance;
|
|
|
|
distanceFromEdge.x = std::max(0.0f, distanceFromEdge.x);
|
|
|
|
distanceFromEdge.y = std::max(0.0f, distanceFromEdge.y);
|
|
|
|
distanceFromEdge.z = std::max(0.0f, distanceFromEdge.z);
|
|
|
|
|
|
|
|
float fallOff = std::min(distanceFromEdge.x, std::min(distanceFromEdge.y, distanceFromEdge.z)) /
|
|
|
|
settings.edgeFadeOffDistance;
|
|
|
|
|
|
|
|
return std::min(1.0f, fallOff);
|
2023-01-24 05:22:40 +01:00
|
|
|
}
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-27 05:22:54 +01:00
|
|
|
Color CloudShader::lightMarch(const Scene &scene, Vector3d currentInCloudPosition, Vector3d lengthDistance,
|
|
|
|
const Primitive *cloudObject) const
|
2023-01-24 19:38:12 +01:00
|
|
|
{
|
2023-01-27 05:22:54 +01:00
|
|
|
Color cloudColor = Color(0, 0, 0);
|
2023-01-24 19:38:12 +01:00
|
|
|
|
|
|
|
// For alle lights
|
|
|
|
for (const auto &light: scene.lights())
|
|
|
|
{
|
2023-01-26 05:04:30 +01:00
|
|
|
Ray ray = Ray(currentInCloudPosition - lengthDistance, normalized(lengthDistance));
|
|
|
|
ray.length = length(lengthDistance);
|
2023-01-27 05:22:54 +01:00
|
|
|
ray.primitive = cloudObject;
|
2023-01-26 05:04:30 +01:00
|
|
|
auto illumination = light->illuminate(scene, ray);
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
// Handle ambient lights
|
2023-01-26 05:04:30 +01:00
|
|
|
if (illumination.distance == 0.0f)
|
|
|
|
{
|
2023-01-24 22:55:48 +01:00
|
|
|
cloudColor += illumination.color;
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
// Light ray from object to light
|
2023-01-24 22:55:48 +01:00
|
|
|
Ray lightRay;
|
2023-01-26 05:04:30 +01:00
|
|
|
lightRay.origin = currentInCloudPosition;
|
2023-01-29 04:25:14 +01:00
|
|
|
lightRay.direction = -illumination.direction;
|
2023-01-27 05:22:54 +01:00
|
|
|
lightRay.primitive = cloudObject;
|
2023-01-24 22:55:48 +01:00
|
|
|
lightRay.length = 0; // Starting in cloud itself
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-26 05:04:30 +01:00
|
|
|
float density = this->rayDensity(lightRay, illumination.distance);
|
|
|
|
|
|
|
|
// Proper light calculation
|
2023-01-29 04:25:14 +01:00
|
|
|
float transmittance = calcBeer(density * settings.lightAbsorptionTowardsLight);
|
2023-01-26 05:04:30 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
transmittance *= phase(normalized(lengthDistance), lightRay.direction);
|
2023-01-27 05:36:27 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
cloudColor += transmittance * illumination.color;
|
2023-01-24 22:55:48 +01:00
|
|
|
}
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
float darknessFactor = settings.darknessThreshold +
|
|
|
|
(1.0f - settings.darknessThreshold);
|
|
|
|
return cloudColor * darknessFactor;
|
2023-01-27 05:22:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
Color CloudShader::transparency(const Scene &scene, const Ray &ray, float maxLength) const
|
2023-01-26 05:04:30 +01:00
|
|
|
{
|
|
|
|
float density = rayDensity(ray, maxLength);
|
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
float transmittance = calcBeer(density * settings.shadowLightAbsorption);
|
2023-01-26 05:04:30 +01:00
|
|
|
transmittance = 1 - (1 - transmittance) * settings.shadowIntensity;
|
|
|
|
|
|
|
|
return Color(1, 1, 1) * transmittance;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CloudShader::rayDensity(const Ray &ray, float maxLength) const
|
2023-01-24 22:55:48 +01:00
|
|
|
{
|
2023-01-29 04:25:14 +01:00
|
|
|
Vector3d startPoint = ray.origin + ray.direction * (ray.length + REFR_EPS);
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
// Determine length of cloud
|
2023-01-29 04:25:14 +01:00
|
|
|
Ray cloudRay;
|
2023-01-24 22:55:48 +01:00
|
|
|
cloudRay.origin = startPoint;
|
|
|
|
cloudRay.length = INFINITY;
|
2023-01-29 04:25:14 +01:00
|
|
|
cloudRay.direction = ray.direction;
|
2023-01-24 22:55:48 +01:00
|
|
|
cloudRay.primitive = nullptr;
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
// Get out of cloud primitive first
|
2023-01-26 05:04:30 +01:00
|
|
|
if (ray.primitive != nullptr && !ray.primitive->intersect(cloudRay) || cloudRay.length == INFINITY ||
|
|
|
|
cloudRay.length <= 0)
|
2023-01-24 22:55:48 +01:00
|
|
|
{
|
2023-01-29 04:25:14 +01:00
|
|
|
// Assume ray started in cloud
|
|
|
|
cloudRay.length = ray.length;
|
|
|
|
cloudRay.primitive = ray.primitive;
|
2023-01-24 22:55:48 +01:00
|
|
|
}
|
2023-01-26 20:30:59 +01:00
|
|
|
float cloudLength = std::min(cloudRay.length, maxLength - ray.length);
|
2023-01-24 22:55:48 +01:00
|
|
|
|
|
|
|
// Calculate step length
|
2023-01-29 04:25:14 +01:00
|
|
|
float stepLength = settings.densitySteps;
|
|
|
|
int noiseSamples = std::floor(cloudLength / stepLength);
|
2023-01-24 22:55:48 +01:00
|
|
|
|
|
|
|
// Step through cloud
|
2023-01-26 20:30:59 +01:00
|
|
|
float density = 0.0f;
|
2023-01-24 22:55:48 +01:00
|
|
|
for (int i = 0; i < noiseSamples; ++i)
|
|
|
|
{
|
|
|
|
// Get sample point
|
2023-01-29 04:25:14 +01:00
|
|
|
Vector3d samplePoint = startPoint + (float) i * stepLength * ray.direction;
|
2023-01-24 19:38:12 +01:00
|
|
|
|
2023-01-24 22:55:48 +01:00
|
|
|
// Get data at point
|
2023-01-29 04:25:14 +01:00
|
|
|
density += getCloudDensity(samplePoint, ray.primitive) * stepLength;
|
2023-01-24 19:38:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 05:04:30 +01:00
|
|
|
// If there is length left, check if it is in the cloud recursively
|
|
|
|
if (cloudLength < maxLength - ray.length)
|
|
|
|
{
|
|
|
|
Vector3d endPoint = startPoint + (cloudLength + REFR_EPS) * ray.direction;
|
|
|
|
Ray recursiveRay = cloudRay;
|
|
|
|
recursiveRay.origin = endPoint;
|
|
|
|
recursiveRay.length = 0;
|
|
|
|
recursiveRay.primitive = nullptr;
|
|
|
|
|
|
|
|
if (ray.primitive != nullptr && ray.primitive->intersect(recursiveRay) && recursiveRay.length > 0)
|
|
|
|
{
|
|
|
|
density += rayDensity(recursiveRay, maxLength - (cloudLength + REFR_EPS));
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 22:55:48 +01:00
|
|
|
|
2023-01-26 05:04:30 +01:00
|
|
|
return density;
|
|
|
|
}
|
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
float CloudShader::phase(Vector3d visualRay, Vector3d illuminationRay) const
|
2023-01-26 05:04:30 +01:00
|
|
|
{
|
|
|
|
// The angle between the visual and illumination rays
|
|
|
|
float cosTheta = dotProduct(visualRay, illuminationRay);
|
|
|
|
|
|
|
|
// The Dual-Lob Henyey-Greenstein function
|
|
|
|
float blend = .5;
|
2023-01-29 04:25:14 +01:00
|
|
|
float phaseBlend = calcDualHenyeyGreenstein(cosTheta, settings.phaseA) * (1 - blend) +
|
|
|
|
calcDualHenyeyGreenstein(cosTheta, -settings.phaseB) * blend;
|
2023-01-26 05:04:30 +01:00
|
|
|
|
|
|
|
// Clamp the result to the range [0, 1]
|
2023-01-29 04:25:14 +01:00
|
|
|
phaseBlend = std::max(std::min(phaseBlend, 1.0f), 0.0f);
|
2023-01-26 05:04:30 +01:00
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
return settings.phaseOffset + phaseBlend * settings.phaseIntensity;
|
2023-01-26 05:04:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 04:25:14 +01:00
|
|
|
float CloudShader::calcBeer(float d)
|
|
|
|
{
|
|
|
|
float beer = std::exp(-d);
|
|
|
|
return beer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float CloudShader::calcDualHenyeyGreenstein(float cosTheta, float g)
|
2023-01-26 05:04:30 +01:00
|
|
|
{
|
|
|
|
float g2 = g * g;
|
2023-01-29 04:25:14 +01:00
|
|
|
return (1 - g2) / (4 * 3.1415f * std::pow(1 + g2 - 2 * g * (cosTheta), 1.5f));
|
2023-01-24 19:38:12 +01:00
|
|
|
}
|