Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sun disc. #2866

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/appleseed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1890,8 +1890,11 @@ source_group ("renderer\\modeling\\input" FILES
)

set (renderer_modeling_light_sources
renderer/modeling/light/ArHosekSkyModelData_Spectral.h
renderer/modeling/light/directionallight.cpp
renderer/modeling/light/directionallight.h
renderer/modeling/light/HosekSun.cpp
renderer/modeling/light/HosekSun.h
renderer/modeling/light/ilightfactory.cpp
renderer/modeling/light/ilightfactory.h
renderer/modeling/light/light.cpp
Expand All @@ -1907,6 +1910,8 @@ set (renderer_modeling_light_sources
renderer/modeling/light/maxspotlight.h
renderer/modeling/light/pointlight.cpp
renderer/modeling/light/pointlight.h
renderer/modeling/light/PreethamSun.cpp
renderer/modeling/light/PreethamSun.h
renderer/modeling/light/spotlight.cpp
renderer/modeling/light/spotlight.h
renderer/modeling/light/sunlight.cpp
Expand Down
6 changes: 5 additions & 1 deletion src/appleseed/foundation/image/colorspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,21 +1468,25 @@ LightingConditions::LightingConditions(
const RegularSpectrum31f& illuminant,
const RegularSpectrum31f cmf[3])
{
constexpr float StepLambda = 10.0f;

// Precompute convolution of color matching functions and illuminant.
for (std::size_t w = 0; w < 31; ++w)
{
m_cmf_reflectance[w][0] = m_cmf_illuminance[w][0] = cmf[0][w];
m_cmf_reflectance[w][1] = m_cmf_illuminance[w][1] = cmf[1][w];
m_cmf_reflectance[w][2] = m_cmf_illuminance[w][2] = cmf[2][w];
m_cmf_illuminance[w][3] = m_cmf_reflectance[w][3] = 0.0f;
m_cmf_reflectance[w] = m_cmf_illuminance[w] *= StepLambda;
m_cmf_reflectance[w] *= illuminant[w];
}

m_cmf_illuminance[31].set(0.0f);
m_cmf_reflectance[31].set(0.0f);

const float reflectance_normalizer = get_cmf_normalizer(m_cmf_reflectance);
const float illuminance_normalizer = get_cmf_normalizer(m_cmf_illuminance);
// Adjust the brightness for coherence with linear_rgb_to_spectrum function.
const float illuminance_normalizer = 1 / 100.0f;

// Normalize color matching functions.
for (std::size_t w = 0; w < 31; ++w)
Expand Down
114 changes: 77 additions & 37 deletions src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "renderer/modeling/input/inputarray.h"
#include "renderer/modeling/input/source.h"
#include "renderer/modeling/input/sourceinputs.h"
#include "renderer/modeling/light/sunlight.h"
#include "renderer/utility/transformsequence.h"

// appleseed.foundation headers.
Expand Down Expand Up @@ -88,9 +89,6 @@ namespace

const char* Model = "hosek_environment_edf";

// The smallest valid turbidity value.
const float BaseTurbidity = 2.0f;

class HosekEnvironmentEDF
: public EnvironmentEDF
{
Expand All @@ -104,11 +102,12 @@ namespace
m_inputs.declare("sun_phi", InputFormat::Float);
m_inputs.declare("turbidity", InputFormat::Float);
m_inputs.declare("turbidity_multiplier", InputFormat::Float, "2.0");
m_inputs.declare("ground_albedo", InputFormat::Float, "0.3");
m_inputs.declare("ground_albedo", InputFormat::SpectralIlluminance, "0.3");
m_inputs.declare("luminance_multiplier", InputFormat::Float, "1.0");
m_inputs.declare("luminance_gamma", InputFormat::Float, "1.0");
m_inputs.declare("saturation_multiplier", InputFormat::Float, "1.0");
m_inputs.declare("horizon_shift", InputFormat::Float, "0.0");
m_inputs.declare("sun_light", InputFormat::Entity, "");
}

void release() override
Expand All @@ -133,6 +132,9 @@ namespace
// Evaluate uniform values.
m_inputs.evaluate_uniforms(&m_uniform_values);

// If there is a bound sun get it.
m_sun = dynamic_cast<SunLight*>(m_inputs.get_entity("sun_light"));

// Compute the sun direction.
m_sun_theta = deg_to_rad(m_uniform_values.m_sun_theta);
m_sun_phi = deg_to_rad(m_uniform_values.m_sun_phi);
Expand All @@ -145,11 +147,10 @@ namespace
{
// Apply turbidity multiplier and bias.
m_uniform_values.m_turbidity *= m_uniform_values.m_turbidity_multiplier;
m_uniform_values.m_turbidity += BaseTurbidity;

compute_coefficients(
m_uniform_values.m_turbidity,
m_uniform_values.m_ground_albedo,
m_uniform_values.m_ground_albedo.illuminance_to_ciexyz(g_std_lighting_conditions),
m_sun_theta,
m_uniform_coeffs,
m_uniform_master_Y);
Expand Down Expand Up @@ -189,6 +190,10 @@ namespace
{
assert(is_normalized(outgoing));

Spectrum sun_value(0.0f);
if (m_sun)
m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value);

Transformd scratch;
const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch);
const Vector3f local_outgoing = transform.vector_to_local(outgoing);
Expand All @@ -197,9 +202,11 @@ namespace
RegularSpectrum31f radiance;
if (shifted_outgoing.y > 0.0f)
compute_sky_radiance(shading_context, shifted_outgoing, radiance);
else radiance.set(0.0f);
else
compute_ground_color(shading_context, shifted_outgoing, radiance);

value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);
value += sun_value;
}

void evaluate(
Expand All @@ -210,6 +217,10 @@ namespace
{
assert(is_normalized(outgoing));

Spectrum sun_value(0.0f);
if (m_sun)
m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value);

Transformd scratch;
const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch);
const Vector3f local_outgoing = transform.vector_to_local(outgoing);
Expand All @@ -218,10 +229,18 @@ namespace
RegularSpectrum31f radiance;
if (shifted_outgoing.y > 0.0f)
compute_sky_radiance(shading_context, shifted_outgoing, radiance);
else radiance.set(0.0f);
else
compute_ground_color(shading_context, shifted_outgoing, radiance);

value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);

probability = shifted_outgoing.y > 0.0f ? shifted_outgoing.y * RcpPi<float>() : 0.0f;
if (sun_value != Spectrum(0.0f))
{
probability *= sun_value.illuminance_to_ciexyz(g_std_lighting_conditions)[1]
/ value.illuminance_to_ciexyz(g_std_lighting_conditions)[1];
value += sun_value;
}
assert(probability >= 0.0f);
}

Expand All @@ -244,15 +263,15 @@ namespace
private:
APPLESEED_DECLARE_INPUT_VALUES(InputValues)
{
float m_sun_theta; // sun zenith angle in degrees, 0=zenith
float m_sun_phi; // degrees
float m_turbidity; // atmosphere turbidity
float m_turbidity_multiplier;
float m_ground_albedo;
float m_luminance_multiplier;
float m_luminance_gamma;
float m_saturation_multiplier;
float m_horizon_shift;
float m_sun_theta; // sun zenith angle in degrees, 0=zenith
float m_sun_phi; // degrees
float m_turbidity; // atmosphere turbidity
float m_turbidity_multiplier;
Spectrum m_ground_albedo;
float m_luminance_multiplier;
float m_luminance_gamma;
float m_saturation_multiplier;
float m_horizon_shift;
};

InputValues m_uniform_values;
Expand All @@ -265,10 +284,12 @@ namespace
float m_uniform_coeffs[3 * 9];
float m_uniform_master_Y[3];

SunLight* m_sun;

// Compute the coefficients of the radiance distribution function and the master luminance value.
static void compute_coefficients(
const float turbidity,
const float albedo,
const Color3f albedo,
const float sun_theta,
float coeffs[3 * 9],
float master_Y[3])
Expand All @@ -278,6 +299,7 @@ namespace
const size_t turbidity_high = std::min(turbidity_low + 1, size_t(9));
const float turbidity_interp = clamped_turbidity - turbidity_low;


// Compute solar elevation.
const float eta = HalfPi<float>() - sun_theta;

Expand Down Expand Up @@ -326,8 +348,8 @@ namespace

coeffs[w * 9 + p] =
lerp(
lerp(clow0, clow1, albedo),
lerp(chigh0, chigh1, albedo),
lerp(clow0, clow1, albedo[w]),
lerp(chigh0, chigh1, albedo[w]),
turbidity_interp);
}

Expand All @@ -339,8 +361,8 @@ namespace

master_Y[w] =
lerp(
lerp(rlow0, rlow1, albedo),
lerp(rhigh0, rhigh1, albedo),
lerp(rlow0, rlow1, albedo[w]),
lerp(rhigh0, rhigh1, albedo[w]),
turbidity_interp);

master_Y[w] *= 1000.0f; // Kcd.m^-2 to cd.m^-2
Expand Down Expand Up @@ -416,13 +438,12 @@ namespace

// Apply turbidity multiplier and bias.
turbidity *= m_uniform_values.m_turbidity_multiplier;
turbidity += BaseTurbidity;

// Compute the coefficients of the radiance distribution function and the master luminance value.
float coeffs[3 * 9], master_Y[3];
compute_coefficients(
turbidity,
m_uniform_values.m_ground_albedo,
m_uniform_values.m_ground_albedo.illuminance_to_ciexyz(g_std_lighting_conditions),
m_sun_theta,
coeffs,
master_Y);
Expand Down Expand Up @@ -459,18 +480,42 @@ namespace
luminance *= m_uniform_values.m_luminance_multiplier;

// Compute the final sky radiance.
constexpr float StepLambda = 10.0f;
radiance *=
luminance // start with computed luminance
/ sum_value(radiance * XYZCMFCIE19312Deg[1]) // normalize to unit luminance
* (1.0f / 683.0f) // convert lumens to Watts
* RcpPi<float>(); // convert irradiance to radiance
luminance // start with computed luminance
/ sum_value(radiance * XYZCMFCIE19312Deg[1] * StepLambda) // normalize to unit luminance
* (1.0f / 683.0f); // convert lumens to Watts
}

Vector3f shift(Vector3f v) const
{
v.y -= m_uniform_values.m_horizon_shift;
return normalize(v);
}

// Compute the sky radiance along a given direction.
void compute_ground_color(
const ShadingContext& shading_context,
const Vector3f& outgoing,
RegularSpectrum31f& radiance) const
{
Color3f ground_Color(0.0f);
compute_sky_radiance(shading_context,
Vector3f(outgoing.x, 0.0f, outgoing.z), radiance);
Color3f sky_color = ciexyz_to_linear_rgb(
spectral_illuminance_to_ciexyz<float>(g_std_lighting_conditions, radiance));

std::vector<float> knotx = { 0.0f, 1.0f };
std::vector<float> knotd(2);

for (unsigned int i = 0; i < 3; i++)
{
std::vector<float> knoty = { sky_color[i], m_uniform_values.m_ground_albedo[i]};
compute_cardinal_spline_tangents(2, &knotx[0], &knoty[0], 0.0f, &knotd[0]);
ground_Color[i] = cubic_hermite_spline(2, &knotx[0], &knoty[0], &knotd[0], -outgoing.y);
}
linear_rgb_illuminance_to_spectrum(ground_Color, radiance);
}
};
}

Expand Down Expand Up @@ -509,16 +554,11 @@ DictionaryArray HosekEnvironmentEDFFactory::get_input_metadata() const
Dictionary()
.insert("name", "ground_albedo")
.insert("label", "Ground Albedo")
.insert("type", "numeric")
.insert("min",
Dictionary()
.insert("value", "0.0")
.insert("type", "hard"))
.insert("max",
.insert("type", "colormap")
.insert("entity_types",
Dictionary()
.insert("value", "1.0")
.insert("type", "hard"))
.insert("use", "optional")
.insert("color", "Colors"))
.insert("use", "required")
.insert("default", "0.3")
.insert("help", "Ground albedo (reflection coefficient of the ground)"));

Expand Down
Loading