Cycles / Sky Texture:

* Added a new sky model by Hosek and Wilkie: "An Analytic Model for Full Spectral Sky-Dome Radiance" http://cgg.mff.cuni.cz/projects/SkylightModelling/ 

Example render:
http://archive.dingto.org/2013/blender/code/new_sky_model.png
Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/Textures#Sky_Texture

Details:
* User can choose between the older Preetham and the new Hosek / Wilkie model via a dropdown. For older files, backwards compatibility is preserved. When we add a new Sky texture, it defaults to the new model though. 
* For the new model, you can specify the ground albedo (see documentation for details). 
* Turbidity now has a UI soft range between 1 and 10, higher values (up to 30) are still possible, but can result in weird colors or black. 
* Removed the limitation of 1 sky texture per SVM stack. (Patch by Lukas Tönne, thanks!)

Thanks to Brecht for code review and some help! 

This is part of my GSoC 2013 project, SVN merge of r59214, r59220, r59251 and r59601.
This commit is contained in:
Thomas Dinges 2013-08-28 14:11:28 +00:00
commit d539bd4672
18 changed files with 5056 additions and 129 deletions

@ -632,8 +632,10 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
else if (b_node.is_a(&RNA_ShaderNodeTexSky)) {
BL::ShaderNodeTexSky b_sky_node(b_node);
SkyTextureNode *sky = new SkyTextureNode();
sky->type = SkyTextureNode::type_enum[(int)b_sky_node.sky_type()];
sky->sun_direction = get_float3(b_sky_node.sun_direction());
sky->turbidity = b_sky_node.turbidity();
sky->ground_albedo = b_sky_node.ground_albedo();
get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
node = sky;
}

@ -717,16 +717,6 @@ typedef struct KernelBackground {
float ao_distance;
} KernelBackground;
typedef struct KernelSunSky {
/* sun direction in spherical and cartesian */
float theta, phi, pad3, pad4;
/* perez function parameters */
float zenith_Y, zenith_x, zenith_y, pad2;
float perez_Y[5], perez_x[5], perez_y[5];
float pad5;
} KernelSunSky;
typedef struct KernelIntegrator {
/* emission */
int use_direct_light;
@ -837,7 +827,6 @@ typedef struct KernelData {
KernelCamera cam;
KernelFilm film;
KernelBackground background;
KernelSunSky sunsky;
KernelIntegrator integrator;
KernelBVH bvh;
KernelCurves curve;

@ -17,16 +17,6 @@
#include "stdosl.h"
#include "node_color.h"
struct KernelSunSky {
/* sun direction in spherical and cartesian */
float theta, phi;
vector dir;
/* perez function parameters */
float zenith_Y, zenith_x, zenith_y;
float perez_Y[5], perez_x[5], perez_y[5];
};
float sky_angle_between(float thetav, float phiv, float theta, float phi)
{
float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta);
@ -44,7 +34,8 @@ vector sky_spherical_coordinates(vector dir)
return vector(acos(dir[2]), atan2(dir[0], dir[1]), 0);
}
float sky_perez_function(float lam[5], float theta, float gamma)
/* Preetham */
float sky_perez_function(float lam[9], float theta, float gamma)
{
float ctheta = cos(theta);
float cgamma = cos(gamma);
@ -52,7 +43,9 @@ float sky_perez_function(float lam[5], float theta, float gamma)
return (1.0 + lam[0] * exp(lam[1] / ctheta)) * (1.0 + lam[2] * exp(lam[3] * gamma) + lam[4] * cgamma * cgamma);
}
color sky_xyz_radiance(KernelSunSky sunsky, vector dir)
color sky_radiance_old(normal dir,
float sunphi, float suntheta, color radiance,
float config_x[9], float config_y[9], float config_z[9])
{
/* convert vector to spherical coordinates */
vector spherical = sky_spherical_coordinates(dir);
@ -60,89 +53,81 @@ color sky_xyz_radiance(KernelSunSky sunsky, vector dir)
float phi = spherical[1];
/* angle between sun direction and dir */
float gamma = sky_angle_between(theta, phi, sunsky.theta, sunsky.phi);
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
/* clamp theta to horizon */
theta = min(theta, M_PI_2 - 0.001);
/* compute xyY color space values */
float x = sunsky.zenith_x * sky_perez_function(sunsky.perez_x, theta, gamma);
float y = sunsky.zenith_y * sky_perez_function(sunsky.perez_y, theta, gamma);
float Y = sunsky.zenith_Y * sky_perez_function(sunsky.perez_Y, theta, gamma);
float x = radiance[1] * sky_perez_function(config_y, theta, gamma);
float y = radiance[2] * sky_perez_function(config_z, theta, gamma);
float Y = radiance[0] * sky_perez_function(config_x, theta, gamma);
/* convert to RGB */
color xyz = xyY_to_xyz(x, y, Y);
return xyz_to_rgb(xyz[0], xyz[1], xyz[2]);
}
void precompute_sunsky(vector dir, float turbidity, output KernelSunSky sunsky)
/* Hosek / Wilkie */
float sky_radiance_internal(float config[9], float theta, float gamma)
{
float ctheta = cos(theta);
float cgamma = cos(gamma);
float expM = exp(config[4] * gamma);
float rayM = cgamma * cgamma;
float mieM = (1.0 + rayM) / pow((1.0 + config[8]*config[8] - 2.0*config[8]*cgamma), 1.5);
float zenith = sqrt(ctheta);
return (1.0 + config[0] * exp(config[1] / (ctheta + 0.01))) *
(config[2] + config[3] * expM + config[5] * rayM + config[6] * mieM + config[7] * zenith);
}
color sky_radiance_new(normal dir,
float sunphi, float suntheta, color radiance,
float config_x[9], float config_y[9], float config_z[9])
{
/* convert vector to spherical coordinates */
vector spherical = sky_spherical_coordinates(dir);
float theta = spherical[0];
float phi = spherical[1];
sunsky.theta = theta;
sunsky.phi = phi;
sunsky.dir = dir;
/* angle between sun direction and dir */
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
float theta2 = theta * theta;
float theta3 = theta2 * theta;
float T = turbidity;
float T2 = T * T;
/* clamp theta to horizon */
theta = min(theta, M_PI_2 - 0.001);
float chi = (4.0 / 9.0 - T / 120.0) * (M_PI - 2.0 * theta);
sunsky.zenith_Y = (4.0453 * T - 4.9710) * tan(chi) - 0.2155 * T + 2.4192;
sunsky.zenith_Y *= 0.06;
/* compute xyz color space values */
float x = sky_radiance_internal(config_x, theta, gamma) * radiance[0];
float y = sky_radiance_internal(config_y, theta, gamma) * radiance[1];
float z = sky_radiance_internal(config_z, theta, gamma) * radiance[2];
sunsky.zenith_x =
( 0.00166 * theta3 - 0.00375 * theta2 + 0.00209 * theta) * T2 +
(-0.02903 * theta3 + 0.06377 * theta2 - 0.03202 * theta + 0.00394) * T +
( 0.11693 * theta3 - 0.21196 * theta2 + 0.06052 * theta + 0.25886);
sunsky.zenith_y =
( 0.00275 * theta3 - 0.00610 * theta2 + 0.00317 * theta) * T2 +
(-0.04214 * theta3 + 0.08970 * theta2 - 0.04153 * theta + 0.00516) * T +
( 0.15346 * theta3 - 0.26756 * theta2 + 0.06670 * theta + 0.26688);
sunsky.perez_Y[0] = ( 0.1787 * T - 1.4630);
sunsky.perez_Y[1] = (-0.3554 * T + 0.4275);
sunsky.perez_Y[2] = (-0.0227 * T + 5.3251);
sunsky.perez_Y[3] = ( 0.1206 * T - 2.5771);
sunsky.perez_Y[4] = (-0.0670 * T + 0.3703);
sunsky.perez_x[0] = (-0.0193 * T - 0.2592);
sunsky.perez_x[1] = (-0.0665 * T + 0.0008);
sunsky.perez_x[2] = (-0.0004 * T + 0.2125);
sunsky.perez_x[3] = (-0.0641 * T - 0.8989);
sunsky.perez_x[4] = (-0.0033 * T + 0.0452);
sunsky.perez_y[0] = (-0.0167 * T - 0.2608);
sunsky.perez_y[1] = (-0.0950 * T + 0.0092);
sunsky.perez_y[2] = (-0.0079 * T + 0.2102);
sunsky.perez_y[3] = (-0.0441 * T - 1.6537);
sunsky.perez_y[4] = (-0.0109 * T + 0.0529);
sunsky.zenith_Y /= sky_perez_function(sunsky.perez_Y, 0, theta);
sunsky.zenith_x /= sky_perez_function(sunsky.perez_x, 0, theta);
sunsky.zenith_y /= sky_perez_function(sunsky.perez_y, 0, theta);
/* convert to RGB and adjust strength */
return xyz_to_rgb(x, y, z) * (M_2PI/683);
}
shader node_sky_texture(
int use_mapping = 0,
matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
vector Vector = P,
vector sun_direction = vector(0, 0, 1),
float turbidity = 2.2,
output color Color = 0.0)
string sky_model = "Hosek / Wilkie",
float theta = 0.0,
float phi = 0.0,
color radiance = color(0.0, 0.0, 0.0),
float config_x[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
float config_y[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
float config_z[9] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
output color Color = color(0.0, 0.0, 0.0))
{
vector p = Vector;
if (use_mapping)
p = transform(mapping, p);
KernelSunSky sunsky;
precompute_sunsky(sun_direction, turbidity, sunsky);
Color = sky_xyz_radiance(sunsky, p);
if (sky_model == "Hosek / Wilkie")
Color = sky_radiance_new(p, phi, theta, radiance, config_x, config_y, config_z);
else
Color = sky_radiance_old(p, phi, theta, radiance, config_x, config_y, config_z);
}

@ -255,7 +255,7 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
#endif
#ifdef __PROCEDURAL_TEXTURES__
case NODE_TEX_SKY:
svm_node_tex_sky(kg, sd, stack, node.y, node.z);
svm_node_tex_sky(kg, sd, stack, node, &offset);
break;
case NODE_TEX_GRADIENT:
svm_node_tex_gradient(sd, stack, node);

@ -16,10 +16,7 @@
CCL_NAMESPACE_BEGIN
/*
* "A Practical Analytic Model for Daylight"
* A. J. Preetham, Peter Shirley, Brian Smits
*/
/* Sky texture */
__device float sky_angle_between(float thetav, float phiv, float theta, float phi)
{
@ -27,6 +24,10 @@ __device float sky_angle_between(float thetav, float phiv, float theta, float ph
return safe_acosf(cospsi);
}
/*
* "A Practical Analytic Model for Daylight"
* A. J. Preetham, Peter Shirley, Brian Smits
*/
__device float sky_perez_function(__constant float *lam, float theta, float gamma)
{
float ctheta = cosf(theta);
@ -35,7 +36,10 @@ __device float sky_perez_function(__constant float *lam, float theta, float gamm
return (1.0f + lam[0]*expf(lam[1]/ctheta)) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cgamma*cgamma);
}
__device float3 sky_radiance(KernelGlobals *kg, float3 dir)
__device float3 sky_radiance_old(KernelGlobals *kg, float3 dir,
float sunphi, float suntheta,
float radiance_x, float radiance_y, float radiance_z,
float *config_x, float *config_y, float *config_z)
{
/* convert vector to spherical coordinates */
float2 spherical = direction_to_spherical(dir);
@ -43,25 +47,135 @@ __device float3 sky_radiance(KernelGlobals *kg, float3 dir)
float phi = spherical.y;
/* angle between sun direction and dir */
float gamma = sky_angle_between(theta, phi, kernel_data.sunsky.theta, kernel_data.sunsky.phi);
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
/* clamp theta to horizon */
theta = min(theta, M_PI_2_F - 0.001f);
/* compute xyY color space values */
float x = kernel_data.sunsky.zenith_x * sky_perez_function(kernel_data.sunsky.perez_x, theta, gamma);
float y = kernel_data.sunsky.zenith_y * sky_perez_function(kernel_data.sunsky.perez_y, theta, gamma);
float Y = kernel_data.sunsky.zenith_Y * sky_perez_function(kernel_data.sunsky.perez_Y, theta, gamma);
float x = radiance_y * sky_perez_function(config_y, theta, gamma);
float y = radiance_z * sky_perez_function(config_z, theta, gamma);
float Y = radiance_x * sky_perez_function(config_x, theta, gamma);
/* convert to RGB */
float3 xyz = xyY_to_xyz(x, y, Y);
return xyz_to_rgb(xyz.x, xyz.y, xyz.z);
}
__device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint dir_offset, uint out_offset)
/*
* "An Analytic Model for Full Spectral Sky-Dome Radiance"
* Lukas Hosek, Alexander Wilkie
*/
__device float sky_radiance_internal(__constant float *configuration, float theta, float gamma)
{
float ctheta = cosf(theta);
float cgamma = cosf(gamma);
float expM = expf(configuration[4] * gamma);
float rayM = cgamma * cgamma;
float mieM = (1.0f + rayM) / powf((1.0f + configuration[8]*configuration[8] - 2.0f*configuration[8]*cgamma), 1.5f);
float zenith = sqrt(ctheta);
return (1.0f + configuration[0] * expf(configuration[1] / (ctheta + 0.01f))) *
(configuration[2] + configuration[3] * expM + configuration[5] * rayM + configuration[6] * mieM + configuration[7] * zenith);
}
__device float3 sky_radiance_new(KernelGlobals *kg, float3 dir,
float sunphi, float suntheta,
float radiance_x, float radiance_y, float radiance_z,
float *config_x, float *config_y, float *config_z)
{
/* convert vector to spherical coordinates */
float2 spherical = direction_to_spherical(dir);
float theta = spherical.x;
float phi = spherical.y;
/* angle between sun direction and dir */
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
/* clamp theta to horizon */
theta = min(theta, M_PI_2_F - 0.001f);
/* compute xyz color space values */
float x = sky_radiance_internal(config_x, theta, gamma) * radiance_x;
float y = sky_radiance_internal(config_y, theta, gamma) * radiance_y;
float z = sky_radiance_internal(config_z, theta, gamma) * radiance_z;
/* convert to RGB and adjust strength */
return xyz_to_rgb(x, y, z) * (M_2PI_F/683);
}
__device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
{
/* Define variables */
float sunphi, suntheta, radiance_x, radiance_y, radiance_z;
float config_x[9], config_y[9], config_z[9];
/* Load data */
uint dir_offset = node.y;
uint out_offset = node.z;
int sky_model = node.w;
float4 data = read_node_float(kg, offset);
sunphi = data.x;
suntheta = data.y;
radiance_x = data.z;
radiance_y = data.w;
data = read_node_float(kg, offset);
radiance_z = data.x;
config_x[0] = data.y;
config_x[1] = data.z;
config_x[2] = data.w;
data = read_node_float(kg, offset);
config_x[3] = data.x;
config_x[4] = data.y;
config_x[5] = data.z;
config_x[6] = data.w;
data = read_node_float(kg, offset);
config_x[7] = data.x;
config_x[8] = data.y;
config_y[0] = data.z;
config_y[1] = data.w;
data = read_node_float(kg, offset);
config_y[2] = data.x;
config_y[3] = data.y;
config_y[4] = data.z;
config_y[5] = data.w;
data = read_node_float(kg, offset);
config_y[6] = data.x;
config_y[7] = data.y;
config_y[8] = data.z;
config_z[0] = data.w;
data = read_node_float(kg, offset);
config_z[1] = data.x;
config_z[2] = data.y;
config_z[3] = data.z;
config_z[4] = data.w;
data = read_node_float(kg, offset);
config_z[5] = data.x;
config_z[6] = data.y;
config_z[7] = data.z;
config_z[8] = data.w;
float3 dir = stack_load_float3(stack, dir_offset);
float3 f = sky_radiance(kg, dir);
float3 f;
/* Compute Sky */
if(sky_model == 0)
f = sky_radiance_old(kg, dir, sunphi, suntheta,
radiance_x, radiance_y, radiance_z,
config_x, config_y, config_z);
else
f = sky_radiance_new(kg, dir, sunphi, suntheta,
radiance_x, radiance_y, radiance_z,
config_x, config_y, config_z);
stack_store_float3(stack, out_offset, f);
}

@ -285,6 +285,11 @@ typedef enum NodeWaveType {
NODE_WAVE_RINGS
} NodeWaveType;
typedef enum NodeSkyType {
NODE_SKY_OLD,
NODE_SKY_NEW
} NodeSkyType;
typedef enum NodeGradientType {
NODE_BLEND_LINEAR,
NODE_BLEND_QUADRATIC,

@ -35,6 +35,7 @@ set(SRC
scene.cpp
session.cpp
shader.cpp
sky_model.cpp
sobol.cpp
svm.cpp
tables.cpp
@ -62,6 +63,8 @@ set(SRC_HEADERS
scene.h
session.h
shader.h
sky_model.h
sky_model_data.h
sobol.h
svm.h
tables.h

@ -18,6 +18,7 @@
#include "nodes.h"
#include "svm.h"
#include "osl.h"
#include "sky_model.h"
#include "util_transform.h"
@ -384,19 +385,35 @@ static float2 sky_spherical_coordinates(float3 dir)
return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
}
typedef struct SunSky {
/* sun direction in spherical and cartesian */
float theta, phi;
/* Parameter */
float radiance_x, radiance_y, radiance_z;
float config_x[9], config_y[9], config_z[9];
} SunSky;
/* Preetham model */
static float sky_perez_function(float lam[6], float theta, float gamma)
{
return (1.0f + lam[0]*expf(lam[1]/cosf(theta))) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cosf(gamma)*cosf(gamma));
}
static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turbidity)
static void sky_texture_precompute_old(SunSky *sunsky, float3 dir, float turbidity)
{
/*
* We re-use the SunSky struct of the new model, to avoid extra variables
* zenith_Y/x/y is now radiance_x/y/z
* perez_Y/x/y is now config_x/y/z
*/
float2 spherical = sky_spherical_coordinates(dir);
float theta = spherical.x;
float phi = spherical.y;
ksunsky->theta = theta;
ksunsky->phi = phi;
sunsky->theta = theta;
sunsky->phi = phi;
float theta2 = theta*theta;
float theta3 = theta2*theta;
@ -404,47 +421,93 @@ static void sky_texture_precompute(KernelSunSky *ksunsky, float3 dir, float turb
float T2 = T * T;
float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
ksunsky->zenith_Y = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
ksunsky->zenith_Y *= 0.06f;
sunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
sunsky->radiance_x *= 0.06f;
ksunsky->zenith_x =
sunsky->radiance_y =
(0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
(-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
(0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
ksunsky->zenith_y =
sunsky->radiance_z =
(0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
(-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
(0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
ksunsky->perez_Y[0] = (0.1787f * T - 1.4630f);
ksunsky->perez_Y[1] = (-0.3554f * T + 0.4275f);
ksunsky->perez_Y[2] = (-0.0227f * T + 5.3251f);
ksunsky->perez_Y[3] = (0.1206f * T - 2.5771f);
ksunsky->perez_Y[4] = (-0.0670f * T + 0.3703f);
sunsky->config_x[0] = (0.1787f * T - 1.4630f);
sunsky->config_x[1] = (-0.3554f * T + 0.4275f);
sunsky->config_x[2] = (-0.0227f * T + 5.3251f);
sunsky->config_x[3] = (0.1206f * T - 2.5771f);
sunsky->config_x[4] = (-0.0670f * T + 0.3703f);
ksunsky->perez_x[0] = (-0.0193f * T - 0.2592f);
ksunsky->perez_x[1] = (-0.0665f * T + 0.0008f);
ksunsky->perez_x[2] = (-0.0004f * T + 0.2125f);
ksunsky->perez_x[3] = (-0.0641f * T - 0.8989f);
ksunsky->perez_x[4] = (-0.0033f * T + 0.0452f);
sunsky->config_y[0] = (-0.0193f * T - 0.2592f);
sunsky->config_y[1] = (-0.0665f * T + 0.0008f);
sunsky->config_y[2] = (-0.0004f * T + 0.2125f);
sunsky->config_y[3] = (-0.0641f * T - 0.8989f);
sunsky->config_y[4] = (-0.0033f * T + 0.0452f);
ksunsky->perez_y[0] = (-0.0167f * T - 0.2608f);
ksunsky->perez_y[1] = (-0.0950f * T + 0.0092f);
ksunsky->perez_y[2] = (-0.0079f * T + 0.2102f);
ksunsky->perez_y[3] = (-0.0441f * T - 1.6537f);
ksunsky->perez_y[4] = (-0.0109f * T + 0.0529f);
sunsky->config_z[0] = (-0.0167f * T - 0.2608f);
sunsky->config_z[1] = (-0.0950f * T + 0.0092f);
sunsky->config_z[2] = (-0.0079f * T + 0.2102f);
sunsky->config_z[3] = (-0.0441f * T - 1.6537f);
sunsky->config_z[4] = (-0.0109f * T + 0.0529f);
ksunsky->zenith_Y /= sky_perez_function(ksunsky->perez_Y, 0, theta);
ksunsky->zenith_x /= sky_perez_function(ksunsky->perez_x, 0, theta);
ksunsky->zenith_y /= sky_perez_function(ksunsky->perez_y, 0, theta);
sunsky->radiance_x /= sky_perez_function(sunsky->config_x, 0, theta);
sunsky->radiance_y /= sky_perez_function(sunsky->config_y, 0, theta);
sunsky->radiance_z /= sky_perez_function(sunsky->config_z, 0, theta);
}
/* Hosek / Wilkie */
static void sky_texture_precompute_new(SunSky *sunsky, float3 dir, float turbidity, float ground_albedo)
{
/* Calculate Sun Direction and save coordinates */
float2 spherical = sky_spherical_coordinates(dir);
float theta = spherical.x;
float phi = spherical.y;
sunsky->theta = theta;
sunsky->phi = phi;
double solarElevation = M_PI_2_F - theta;
/* Initialize Sky Model */
ArHosekSkyModelState *sky_state;
sky_state = arhosek_xyz_skymodelstate_alloc_init(turbidity, ground_albedo, solarElevation);
/* Copy values from sky_state to SunSky */
for (int i = 0; i < 9; ++i) {
sunsky->config_x[i] = sky_state->configs[0][i];
sunsky->config_y[i] = sky_state->configs[1][i];
sunsky->config_z[i] = sky_state->configs[2][i];
}
sunsky->radiance_x = sky_state->radiances[0];
sunsky->radiance_y = sky_state->radiances[1];
sunsky->radiance_z = sky_state->radiances[2];
/* Free sky_state */
arhosekskymodelstate_free(sky_state);
}
static ShaderEnum sky_type_init()
{
ShaderEnum enm;
enm.insert("Preetham", NODE_SKY_OLD);
enm.insert("Hosek / Wilkie", NODE_SKY_NEW);
return enm;
}
ShaderEnum SkyTextureNode::type_enum = sky_type_init();
SkyTextureNode::SkyTextureNode()
: TextureNode("sky_texture")
{
type = ustring("Hosek / Wilkie");
sun_direction = make_float3(0.0f, 0.0f, 1.0f);
turbidity = 2.2f;
ground_albedo = 0.3f;
add_input("Vector", SHADER_SOCKET_VECTOR, ShaderInput::POSITION);
add_output("Color", SHADER_SOCKET_COLOR);
@ -455,15 +518,17 @@ void SkyTextureNode::compile(SVMCompiler& compiler)
ShaderInput *vector_in = input("Vector");
ShaderOutput *color_out = output("Color");
if(compiler.sunsky) {
sky_texture_precompute(compiler.sunsky, sun_direction, turbidity);
compiler.sunsky = NULL;
}
SunSky sunsky;
if(type_enum[type] == NODE_SKY_OLD)
sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
else if(type_enum[type] == NODE_SKY_NEW)
sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
if(vector_in->link)
compiler.stack_assign(vector_in);
int vector_offset = vector_in->stack_offset;
int sky_model = type_enum[type];
if(!tex_mapping.skip()) {
vector_offset = compiler.stack_find_offset(SHADER_SOCKET_VECTOR);
@ -471,7 +536,15 @@ void SkyTextureNode::compile(SVMCompiler& compiler)
}
compiler.stack_assign(color_out);
compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset);
compiler.add_node(NODE_TEX_SKY, vector_offset, color_out->stack_offset, sky_model);
compiler.add_node(__float_as_uint(sunsky.phi), __float_as_uint(sunsky.theta), __float_as_uint(sunsky.radiance_x), __float_as_uint(sunsky.radiance_y));
compiler.add_node(__float_as_uint(sunsky.radiance_z), __float_as_uint(sunsky.config_x[0]), __float_as_uint(sunsky.config_x[1]), __float_as_uint(sunsky.config_x[2]));
compiler.add_node(__float_as_uint(sunsky.config_x[3]), __float_as_uint(sunsky.config_x[4]), __float_as_uint(sunsky.config_x[5]), __float_as_uint(sunsky.config_x[6]));
compiler.add_node(__float_as_uint(sunsky.config_x[7]), __float_as_uint(sunsky.config_x[8]), __float_as_uint(sunsky.config_y[0]), __float_as_uint(sunsky.config_y[1]));
compiler.add_node(__float_as_uint(sunsky.config_y[2]), __float_as_uint(sunsky.config_y[3]), __float_as_uint(sunsky.config_y[4]), __float_as_uint(sunsky.config_y[5]));
compiler.add_node(__float_as_uint(sunsky.config_y[6]), __float_as_uint(sunsky.config_y[7]), __float_as_uint(sunsky.config_y[8]), __float_as_uint(sunsky.config_z[0]));
compiler.add_node(__float_as_uint(sunsky.config_z[1]), __float_as_uint(sunsky.config_z[2]), __float_as_uint(sunsky.config_z[3]), __float_as_uint(sunsky.config_z[4]));
compiler.add_node(__float_as_uint(sunsky.config_z[5]), __float_as_uint(sunsky.config_z[6]), __float_as_uint(sunsky.config_z[7]), __float_as_uint(sunsky.config_z[8]));
if(vector_offset != vector_in->stack_offset)
compiler.stack_clear_offset(vector_in->type, vector_offset);
@ -481,8 +554,19 @@ void SkyTextureNode::compile(OSLCompiler& compiler)
{
tex_mapping.compile(compiler);
compiler.parameter_vector("sun_direction", sun_direction);
compiler.parameter("turbidity", turbidity);
SunSky sunsky;
if(type_enum[type] == NODE_SKY_OLD)
sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
else if(type_enum[type] == NODE_SKY_NEW)
sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
compiler.parameter("sky_model", type);
compiler.parameter("theta", sunsky.theta);
compiler.parameter("phi", sunsky.phi);
compiler.parameter_color("radiance", make_float3(sunsky.radiance_x, sunsky.radiance_y, sunsky.radiance_z));
compiler.parameter_array("config_x", sunsky.config_x, 9);
compiler.parameter_array("config_y", sunsky.config_y, 9);
compiler.parameter_array("config_z", sunsky.config_z, 9);
compiler.add(this, "node_sky_texture");
}

@ -105,6 +105,10 @@ public:
float3 sun_direction;
float turbidity;
float ground_albedo;
ustring type;
static ShaderEnum type_enum;
};
class OutputNode : public ShaderNode {

@ -0,0 +1,397 @@
/*
This source is published under the following 3-clause BSD license.
Copyright (c) 2012 - 2013, Lukas Hosek and Alexander Wilkie
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* None of the names of the contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ============================================================================
This file is part of a sample implementation of the analytical skylight and
solar radiance models presented in the SIGGRAPH 2012 paper
"An Analytic Model for Full Spectral Sky-Dome Radiance"
and the 2013 IEEE CG&A paper
"Adding a Solar Radiance Function to the Hosek Skylight Model"
both by
Lukas Hosek and Alexander Wilkie
Charles University in Prague, Czech Republic
Version: 1.4a, February 22nd, 2013
Version history:
1.4a February 22nd, 2013
Removed unnecessary and counter-intuitive solar radius parameters
from the interface of the colourspace sky dome initialisation functions.
1.4 February 11th, 2013
Fixed a bug which caused the relative brightness of the solar disc
and the sky dome to be off by a factor of about 6. The sun was too
bright: this affected both normal and alien sun scenarios. The
coefficients of the solar radiance function were changed to fix this.
1.3 January 21st, 2013 (not released to the public)
Added support for solar discs that are not exactly the same size as
the terrestrial sun. Also added support for suns with a different
emission spectrum ("Alien World" functionality).
1.2a December 18th, 2012
Fixed a mistake and some inaccuracies in the solar radiance function
explanations found in ArHosekSkyModel.h. The actual source code is
unchanged compared to version 1.2.
1.2 December 17th, 2012
Native RGB data and a solar radiance function that matches the turbidity
conditions were added.
1.1 September 2012
The coefficients of the spectral model are now scaled so that the output
is given in physical units: W / (m^-2 * sr * nm). Also, the output of the
XYZ model is now no longer scaled to the range [0...1]. Instead, it is
the result of a simple conversion from spectral data via the CIE 2 degree
standard observer matching functions. Therefore, after multiplication
with 683 lm / W, the Y channel now corresponds to luminance in lm.
1.0 May 11th, 2012
Initial release.
Please visit http://cgg.mff.cuni.cz/projects/SkylightModelling/ to check if
an updated version of this code has been published!
============================================================================ */
/*
All instructions on how to use this code are in the accompanying header file.
*/
#include "sky_model.h"
#include "sky_model_data.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
CCL_NAMESPACE_BEGIN
// Some macro definitions that occur elsewhere in ART, and that have to be
// replicated to make this a stand-alone module.
#ifndef NIL
#define NIL 0
#endif
#ifndef MATH_PI
#define MATH_PI 3.141592653589793
#endif
#ifndef MATH_DEG_TO_RAD
#define MATH_DEG_TO_RAD ( MATH_PI / 180.0 )
#endif
#ifndef MATH_RAD_TO_DEG
#define MATH_RAD_TO_DEG ( 180.0 / MATH_PI )
#endif
#ifndef DEGREES
#define DEGREES * MATH_DEG_TO_RAD
#endif
#ifndef TERRESTRIAL_SOLAR_RADIUS
#define TERRESTRIAL_SOLAR_RADIUS ( ( 0.51 DEGREES ) / 2.0 )
#endif
#ifndef ALLOC
#define ALLOC(_struct) ((_struct *)malloc(sizeof(_struct)))
#endif
// internal definitions
typedef double *ArHosekSkyModel_Dataset;
typedef double *ArHosekSkyModel_Radiance_Dataset;
// internal functions
void ArHosekSkyModel_CookConfiguration(
ArHosekSkyModel_Dataset dataset,
ArHosekSkyModelConfiguration config,
double turbidity,
double albedo,
double solar_elevation
)
{
double * elev_matrix;
int int_turbidity = (int)turbidity;
double turbidity_rem = turbidity - (double)int_turbidity;
solar_elevation = pow(solar_elevation / (MATH_PI / 2.0), (1.0 / 3.0));
// alb 0 low turb
elev_matrix = dataset + ( 9 * 6 * (int_turbidity-1) );
for( unsigned int i = 0; i < 9; ++i )
{
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] =
(1.0-albedo) * (1.0 - turbidity_rem)
* ( pow(1.0-solar_elevation, 5.0) * elev_matrix[i] +
5.0 * pow(1.0-solar_elevation, 4.0) * solar_elevation * elev_matrix[i+9] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[i+18] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[i+27] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[i+36] +
pow(solar_elevation, 5.0) * elev_matrix[i+45]);
}
// alb 1 low turb
elev_matrix = dataset + (9*6*10 + 9*6*(int_turbidity-1));
for(unsigned int i = 0; i < 9; ++i)
{
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] +=
(albedo) * (1.0 - turbidity_rem)
* ( pow(1.0-solar_elevation, 5.0) * elev_matrix[i] +
5.0 * pow(1.0-solar_elevation, 4.0) * solar_elevation * elev_matrix[i+9] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[i+18] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[i+27] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[i+36] +
pow(solar_elevation, 5.0) * elev_matrix[i+45]);
}
if(int_turbidity == 10)
return;
// alb 0 high turb
elev_matrix = dataset + (9*6*(int_turbidity));
for(unsigned int i = 0; i < 9; ++i)
{
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] +=
(1.0-albedo) * (turbidity_rem)
* ( pow(1.0-solar_elevation, 5.0) * elev_matrix[i] +
5.0 * pow(1.0-solar_elevation, 4.0) * solar_elevation * elev_matrix[i+9] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[i+18] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[i+27] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[i+36] +
pow(solar_elevation, 5.0) * elev_matrix[i+45]);
}
// alb 1 high turb
elev_matrix = dataset + (9*6*10 + 9*6*(int_turbidity));
for(unsigned int i = 0; i < 9; ++i)
{
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] +=
(albedo) * (turbidity_rem)
* ( pow(1.0-solar_elevation, 5.0) * elev_matrix[i] +
5.0 * pow(1.0-solar_elevation, 4.0) * solar_elevation * elev_matrix[i+9] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[i+18] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[i+27] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[i+36] +
pow(solar_elevation, 5.0) * elev_matrix[i+45]);
}
}
double ArHosekSkyModel_CookRadianceConfiguration(
ArHosekSkyModel_Radiance_Dataset dataset,
double turbidity,
double albedo,
double solar_elevation
)
{
double* elev_matrix;
int int_turbidity = (int)turbidity;
double turbidity_rem = turbidity - (double)int_turbidity;
double res;
solar_elevation = pow(solar_elevation / (MATH_PI / 2.0), (1.0 / 3.0));
// alb 0 low turb
elev_matrix = dataset + (6*(int_turbidity-1));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res = (1.0-albedo) * (1.0 - turbidity_rem) *
( pow(1.0-solar_elevation, 5.0) * elev_matrix[0] +
5.0*pow(1.0-solar_elevation, 4.0)*solar_elevation * elev_matrix[1] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[2] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[3] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[4] +
pow(solar_elevation, 5.0) * elev_matrix[5]);
// alb 1 low turb
elev_matrix = dataset + (6*10 + 6*(int_turbidity-1));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (albedo) * (1.0 - turbidity_rem) *
( pow(1.0-solar_elevation, 5.0) * elev_matrix[0] +
5.0*pow(1.0-solar_elevation, 4.0)*solar_elevation * elev_matrix[1] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[2] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[3] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[4] +
pow(solar_elevation, 5.0) * elev_matrix[5]);
if(int_turbidity == 10)
return res;
// alb 0 high turb
elev_matrix = dataset + (6*(int_turbidity));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (1.0-albedo) * (turbidity_rem) *
( pow(1.0-solar_elevation, 5.0) * elev_matrix[0] +
5.0*pow(1.0-solar_elevation, 4.0)*solar_elevation * elev_matrix[1] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[2] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[3] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[4] +
pow(solar_elevation, 5.0) * elev_matrix[5]);
// alb 1 high turb
elev_matrix = dataset + (6*10 + 6*(int_turbidity));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (albedo) * (turbidity_rem) *
( pow(1.0-solar_elevation, 5.0) * elev_matrix[0] +
5.0*pow(1.0-solar_elevation, 4.0)*solar_elevation * elev_matrix[1] +
10.0*pow(1.0-solar_elevation, 3.0)*pow(solar_elevation, 2.0) * elev_matrix[2] +
10.0*pow(1.0-solar_elevation, 2.0)*pow(solar_elevation, 3.0) * elev_matrix[3] +
5.0*(1.0-solar_elevation)*pow(solar_elevation, 4.0) * elev_matrix[4] +
pow(solar_elevation, 5.0) * elev_matrix[5]);
return res;
}
double ArHosekSkyModel_GetRadianceInternal(
ArHosekSkyModelConfiguration configuration,
double theta,
double gamma
)
{
const double expM = exp(configuration[4] * gamma);
const double rayM = cos(gamma)*cos(gamma);
const double mieM = (1.0 + cos(gamma)*cos(gamma)) / pow((1.0 + configuration[8]*configuration[8] - 2.0*configuration[8]*cos(gamma)), 1.5);
const double zenith = sqrt(cos(theta));
return (1.0 + configuration[0] * exp(configuration[1] / (cos(theta) + 0.01))) *
(configuration[2] + configuration[3] * expM + configuration[5] * rayM + configuration[6] * mieM + configuration[7] * zenith);
}
void arhosekskymodelstate_free(
ArHosekSkyModelState * state
)
{
free(state);
}
double arhosekskymodel_radiance(
ArHosekSkyModelState * state,
double theta,
double gamma,
double wavelength
)
{
int low_wl = (wavelength - 320.0 ) / 40.0;
if ( low_wl < 0 || low_wl >= 11 )
return 0.0f;
double interp = fmod((wavelength - 320.0 ) / 40.0, 1.0);
double val_low =
ArHosekSkyModel_GetRadianceInternal(
state->configs[low_wl],
theta,
gamma
)
* state->radiances[low_wl]
* state->emission_correction_factor_sky[low_wl];
if ( interp < 1e-6 )
return val_low;
double result = ( 1.0 - interp ) * val_low;
if ( low_wl+1 < 11 )
{
result +=
interp
* ArHosekSkyModel_GetRadianceInternal(
state->configs[low_wl+1],
theta,
gamma
)
* state->radiances[low_wl+1]
* state->emission_correction_factor_sky[low_wl+1];
}
return result;
}
// xyz and rgb versions
ArHosekSkyModelState * arhosek_xyz_skymodelstate_alloc_init(
const double turbidity,
const double albedo,
const double elevation
)
{
ArHosekSkyModelState * state = ALLOC(ArHosekSkyModelState);
state->solar_radius = TERRESTRIAL_SOLAR_RADIUS;
state->turbidity = turbidity;
state->albedo = albedo;
state->elevation = elevation;
for( unsigned int channel = 0; channel < 3; ++channel )
{
ArHosekSkyModel_CookConfiguration(
datasetsXYZ[channel],
state->configs[channel],
turbidity,
albedo,
elevation
);
state->radiances[channel] =
ArHosekSkyModel_CookRadianceConfiguration(
datasetsXYZRad[channel],
turbidity,
albedo,
elevation
);
}
return state;
}
CCL_NAMESPACE_END

@ -0,0 +1,454 @@
/*
This source is published under the following 3-clause BSD license.
Copyright (c) 2012 - 2013, Lukas Hosek and Alexander Wilkie
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* None of the names of the contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ============================================================================
This file is part of a sample implementation of the analytical skylight and
solar radiance models presented in the SIGGRAPH 2012 paper
"An Analytic Model for Full Spectral Sky-Dome Radiance"
and the 2013 IEEE CG&A paper
"Adding a Solar Radiance Function to the Hosek Skylight Model"
both by
Lukas Hosek and Alexander Wilkie
Charles University in Prague, Czech Republic
Version: 1.4a, February 22nd, 2013
Version history:
1.4a February 22nd, 2013
Removed unnecessary and counter-intuitive solar radius parameters
from the interface of the colourspace sky dome initialisation functions.
1.4 February 11th, 2013
Fixed a bug which caused the relative brightness of the solar disc
and the sky dome to be off by a factor of about 6. The sun was too
bright: this affected both normal and alien sun scenarios. The
coefficients of the solar radiance function were changed to fix this.
1.3 January 21st, 2013 (not released to the public)
Added support for solar discs that are not exactly the same size as
the terrestrial sun. Also added support for suns with a different
emission spectrum ("Alien World" functionality).
1.2a December 18th, 2012
Fixed a mistake and some inaccuracies in the solar radiance function
explanations found in ArHosekSkyModel.h. The actual source code is
unchanged compared to version 1.2.
1.2 December 17th, 2012
Native RGB data and a solar radiance function that matches the turbidity
conditions were added.
1.1 September 2012
The coefficients of the spectral model are now scaled so that the output
is given in physical units: W / (m^-2 * sr * nm). Also, the output of the
XYZ model is now no longer scaled to the range [0...1]. Instead, it is
the result of a simple conversion from spectral data via the CIE 2 degree
standard observer matching functions. Therefore, after multiplication
with 683 lm / W, the Y channel now corresponds to luminance in lm.
1.0 May 11th, 2012
Initial release.
Please visit http://cgg.mff.cuni.cz/projects/SkylightModelling/ to check if
an updated version of this code has been published!
============================================================================ */
/*
This code is taken from ART, a rendering research system written in a
mix of C99 / Objective C. Since ART is not a small system and is intended to
be inter-operable with other libraries, and since C does not have namespaces,
the structures and functions in ART all have to have somewhat wordy
canonical names that begin with Ar.../ar..., like those seen in this example.
Usage information:
==================
Model initialisation
--------------------
A separate ArHosekSkyModelState has to be maintained for each spectral
band you want to use the model for. So in a renderer with 'num_channels'
bands, you would need something like
ArHosekSkyModelState * skymodel_state[num_channels];
You then have to allocate and initialise these states. In the following code
snippet, we assume that 'albedo' is defined as
double albedo[num_channels];
with a ground albedo value between [0,1] for each channel. The solar elevation
is given in radians.
for ( unsigned int i = 0; i < num_channels; i++ )
skymodel_state[i] =
arhosekskymodelstate_alloc_init(
turbidity,
albedo[i],
solarElevation
);
Note that starting with version 1.3, there is also a second initialisation
function which generates skydome states for different solar emission spectra
and solar radii: 'arhosekskymodelstate_alienworld_alloc_init()'.
See the notes about the "Alien World" functionality provided further down for a
discussion of the usefulness and limits of that second initalisation function.
Sky model states that have been initialised with either function behave in a
completely identical fashion during use and cleanup.
Using the model to generate skydome samples
-------------------------------------------
Generating a skydome radiance spectrum "skydome_result" for a given location
on the skydome determined via the angles theta and gamma works as follows:
double skydome_result[num_channels];
for ( unsigned int i = 0; i < num_channels; i++ )
skydome_result[i] =
arhosekskymodel_radiance(
skymodel_state[i],
theta,
gamma,
channel_center[i]
);
The variable "channel_center" is assumed to hold the channel center wavelengths
for each of the num_channels samples of the spectrum we are building.
Cleanup after use
-----------------
After rendering is complete, the content of the sky model states should be
disposed of via
for ( unsigned int i = 0; i < num_channels; i++ )
arhosekskymodelstate_free( skymodel_state[i] );
CIE XYZ Version of the Model
----------------------------
Usage of the CIE XYZ version of the model is exactly the same, except that
num_channels is of course always 3, and that ArHosekTristimSkyModelState and
arhosek_tristim_skymodel_radiance() have to be used instead of their spectral
counterparts.
RGB Version of the Model
------------------------
The RGB version uses sRGB primaries with a linear gamma ramp. The same set of
functions as with the XYZ data is used, except the model is initialized
by calling arhosek_rgb_skymodelstate_alloc_init.
Solar Radiance Function
-----------------------
For each position on the solar disc, this function returns the entire radiance
one sees - direct emission, as well as in-scattered light in the area of the
solar disc. The latter is important for low solar elevations - nice images of
the setting sun would not be possible without this. This is also the reason why
this function, just like the regular sky dome model evaluation function, needs
access to the sky dome data structures, as these provide information on
in-scattered radiance.
CAVEAT #1: in this release, this function is only provided in spectral form!
RGB/XYZ versions to follow at a later date.
CAVEAT #2: (fixed from release 1.3 onwards)
CAVEAT #3: limb darkening renders the brightness of the solar disc
inhomogeneous even for high solar elevations - only taking a single
sample at the centre of the sun will yield an incorrect power
estimate for the solar disc! Always take multiple random samples
across the entire solar disc to estimate its power!
CAVEAT #4: in this version, the limb darkening calculations still use a fairly
computationally expensive 5th order polynomial that was directly
taken from astronomical literature. For the purposes of Computer
Graphics, this is needlessly accurate, though, and will be replaced
by a cheaper approximation in a future release.
"Alien World" functionality
---------------------------
The Hosek sky model can be used to roughly (!) predict the appearance of
outdoor scenes on earth-like planets, i.e. planets of a similar size and
atmospheric make-up. Since the spectral version of our model predicts sky dome
luminance patterns and solar radiance independently for each waveband, and
since the intensity of each waveband is solely dependent on the input radiance
from the star that the world in question is orbiting, it is trivial to re-scale
the wavebands to match a different star radiance.
At least in theory, the spectral version of the model has always been capable
of this sort of thing, and the actual sky dome and solar radiance models were
actually not altered at all in this release. All we did was to add some support
functionality for doing this more easily with the existing data and functions,
and to add some explanations.
Just use 'arhosekskymodelstate_alienworld_alloc_init()' to initialise the sky
model states (you will have to provide values for star temperature and solar
intensity compared to the terrestrial sun), and do everything else as you
did before.
CAVEAT #1: we assume the emission of the star that illuminates the alien world
to be a perfect blackbody emission spectrum. This is never entirely
realistic - real star emission spectra are considerably more complex
than this, mainly due to absorption effects in the outer layers of
stars. However, blackbody spectra are a reasonable first assumption
in a usage scenario like this, where 100% accuracy is simply not
necessary: for rendering purposes, there are likely no visible
differences between a highly accurate solution based on a more
involved simulation, and this approximation.
CAVEAT #2: we always use limb darkening data from our own sun to provide this
"appearance feature", even for suns of strongly different
temperature. Which is presumably not very realistic, but (as with
the unaltered blackbody spectrum from caveat #1) probably not a bad
first guess, either. If you need more accuracy than we provide here,
please make inquiries with a friendly astro-physicst of your choice.
CAVEAT #3: you have to provide a value for the solar intensity of the star
which illuminates the alien world. For this, please bear in mind
that there is very likely a comparatively tight range of absolute
solar irradiance values for which an earth-like planet with an
atmosphere like the one we assume in our model can exist in the
first place!
Too much irradiance, and the atmosphere probably boils off into
space, too little, it freezes. Which means that stars of
considerably different emission colour than our sun will have to be
fairly different in size from it, to still provide a reasonable and
inhabitable amount of irradiance. Red stars will need to be much
larger than our sun, while white or blue stars will have to be
comparatively tiny. The initialisation function handles this and
computes a plausible solar radius for a given emission spectrum. In
terms of absolute radiometric values, you should probably not stray
all too far from a solar intensity value of 1.0.
CAVEAT #4: although we now support different solar radii for the actual solar
disc, the sky dome luminance patterns are *not* parameterised by
this value - i.e. the patterns stay exactly the same for different
solar radii! Which is of course not correct. But in our experience,
solar discs up to several degrees in diameter (! - our own sun is
half a degree across) do not cause the luminance patterns on the sky
to change perceptibly. The reason we know this is that we initially
used unrealistically large suns in our brute force path tracer, in
order to improve convergence speeds (which in the beginning were
abysmal). Later, we managed to do the reference renderings much
faster even with realistically small suns, and found that there was
no real difference in skydome appearance anyway.
Conclusion: changing the solar radius should not be over-done, so
close orbits around red supergiants are a no-no. But for the
purposes of getting a fairly credible first impression of what an
alien world with a reasonably sized sun would look like, what we are
doing here is probably still o.k.
HINT #1: if you want to model the sky of an earth-like planet that orbits
a binary star, just super-impose two of these models with solar
intensity of ~0.5 each, and closely spaced solar positions. Light is
additive, after all. Tattooine, here we come... :-)
P.S. according to Star Wars canon, Tattooine orbits a binary
that is made up of a G and K class star, respectively.
So ~5500K and ~4200K should be good first guesses for their
temperature. Just in case you were wondering, after reading the
previous paragraph.
*/
CCL_NAMESPACE_BEGIN
#ifndef _SKY_MODEL_H_
#define _SKY_MODEL_H_
typedef double ArHosekSkyModelConfiguration[9];
// Spectral version of the model
/* ----------------------------------------------------------------------------
ArHosekSkyModelState struct
---------------------------
This struct holds the pre-computation data for one particular albedo value.
Most fields are self-explanatory, but users should never directly
manipulate any of them anyway. The only consistent way to manipulate such
structs is via the functions 'arhosekskymodelstate_alloc_init' and
'arhosekskymodelstate_free'.
'emission_correction_factor_sky'
'emission_correction_factor_sun'
The original model coefficients were fitted against the emission of
our local sun. If a different solar emission is desired (i.e. if the
model is being used to predict skydome appearance for an earth-like
planet that orbits a different star), these correction factors, which
are determined during the alloc_init step, are applied to each waveband
separately (they default to 1.0 in normal usage). This is the simplest
way to retrofit this sort of capability to the existing model. The
different factors for sky and sun are needed since the solar disc may
be of a different size compared to the terrestrial sun.
---------------------------------------------------------------------------- */
typedef struct ArHosekSkyModelState
{
ArHosekSkyModelConfiguration configs[11];
double radiances[11];
double turbidity;
double solar_radius;
double emission_correction_factor_sky[11];
double emission_correction_factor_sun[11];
double albedo;
double elevation;
}
ArHosekSkyModelState;
/* ----------------------------------------------------------------------------
arhosekskymodelstate_alloc_init() function
------------------------------------------
Initialises an ArHosekSkyModelState struct for a terrestrial setting.
---------------------------------------------------------------------------- */
ArHosekSkyModelState * arhosekskymodelstate_alloc_init(
const double solar_elevation,
const double atmospheric_turbidity,
const double ground_albedo
);
/* ----------------------------------------------------------------------------
arhosekskymodelstate_alienworld_alloc_init() function
-----------------------------------------------------
Initialises an ArHosekSkyModelState struct for an "alien world" setting
with a sun of a surface temperature given in 'kelvin'. The parameter
'solar_intensity' controls the overall brightness of the sky, relative
to the solar irradiance on Earth. A value of 1.0 yields a sky dome that
is, on average over the wavelenghts covered in the model (!), as bright
as the terrestrial sky in radiometric terms.
Which means that the solar radius has to be adjusted, since the
emissivity of a solar surface with a given temperature is more or less
fixed. So hotter suns have to be smaller to be equally bright as the
terrestrial sun, while cooler suns have to be larger. Note that there are
limits to the validity of the luminance patterns of the underlying model:
see the discussion above for more on this. In particular, an alien sun with
a surface temperature of only 2000 Kelvin has to be very large if it is
to be as bright as the terrestrial sun - so large that the luminance
patterns are no longer a really good fit in that case.
If you need information about the solar radius that the model computes
for a given temperature (say, for light source sampling purposes), you
have to query the 'solar_radius' variable of the sky model state returned
*after* running this function.
---------------------------------------------------------------------------- */
ArHosekSkyModelState * arhosekskymodelstate_alienworld_alloc_init(
const double solar_elevation,
const double solar_intensity,
const double solar_surface_temperature_kelvin,
const double atmospheric_turbidity,
const double ground_albedo
);
void arhosekskymodelstate_free(
ArHosekSkyModelState * state
);
double arhosekskymodel_radiance(
ArHosekSkyModelState * state,
double theta,
double gamma,
double wavelength
);
// CIE XYZ and RGB versions
ArHosekSkyModelState * arhosek_xyz_skymodelstate_alloc_init(
const double turbidity,
const double albedo,
const double elevation
);
ArHosekSkyModelState * arhosek_rgb_skymodelstate_alloc_init(
const double turbidity,
const double albedo,
const double elevation
);
double arhosek_tristim_skymodel_radiance(
ArHosekSkyModelState * state,
double theta,
double gamma,
int channel
);
// Delivers the complete function: sky + sun, including limb darkening.
// Please read the above description before using this - there are several
// caveats!
double arhosekskymodel_solar_radiance(
ArHosekSkyModelState * state,
double theta,
double gamma,
double wavelength
);
#endif // _SKY_MODEL_H_
CCL_NAMESPACE_END

File diff suppressed because it is too large Load Diff

@ -62,7 +62,6 @@ void SVMShaderManager::device_update(Device *device, DeviceScene *dscene, Scene
svm_nodes.push_back(make_int4(NODE_SHADER_JUMP, 0, 0, 0));
}
bool sunsky_done = false;
bool use_multi_closure = device->info.advanced_shading;
for(i = 0; i < scene->shaders.size(); i++) {
@ -77,11 +76,8 @@ void SVMShaderManager::device_update(Device *device, DeviceScene *dscene, Scene
SVMCompiler compiler(scene->shader_manager, scene->image_manager,
use_multi_closure);
compiler.sunsky = (sunsky_done)? NULL: &dscene->data.sunsky;
compiler.background = ((int)i == scene->default_background);
compiler.compile(shader, svm_nodes, i);
if(!compiler.sunsky)
sunsky_done = true;
}
dscene->svm_nodes.copy((uint4*)&svm_nodes[0], svm_nodes.size());
@ -111,7 +107,6 @@ SVMCompiler::SVMCompiler(ShaderManager *shader_manager_, ImageManager *image_man
{
shader_manager = shader_manager_;
image_manager = image_manager_;
sunsky = NULL;
max_stack_use = 0;
current_type = SHADER_TYPE_SURFACE;
current_shader = NULL;

@ -29,7 +29,6 @@ CCL_NAMESPACE_BEGIN
class Device;
class DeviceScene;
class ImageManager;
struct KernelSunSky;
class Scene;
class ShaderGraph;
class ShaderInput;
@ -77,7 +76,6 @@ public:
ImageManager *image_manager;
ShaderManager *shader_manager;
KernelSunSky *sunsky;
bool background;
protected:

@ -809,9 +809,13 @@ static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, Poin
}
static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
{
uiItemR(layout, ptr, "sky_type", 0, "", ICON_NONE);
uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
if (RNA_enum_get(ptr, "sky_type") == SHD_SKY_NEW)
uiItemR(layout, ptr, "ground_albedo", 0, NULL, ICON_NONE);
}
static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)

@ -720,8 +720,10 @@ typedef struct NodeTexBase {
typedef struct NodeTexSky {
NodeTexBase base;
int sky_model;
float sun_direction[3];
float turbidity;
float ground_albedo;
} NodeTexSky;
typedef struct NodeTexImage {
@ -937,6 +939,10 @@ typedef struct NodeShaderNormalMap {
#define SHD_WAVE_BANDS 0
#define SHD_WAVE_RINGS 1
/* sky texture */
#define SHD_SKY_OLD 0
#define SHD_SKY_NEW 1
/* image/environment texture */
#define SHD_COLORSPACE_NONE 0
#define SHD_COLORSPACE_COLOR 1

@ -3188,10 +3188,22 @@ static void def_sh_tex(StructRNA *srna)
static void def_sh_tex_sky(StructRNA *srna)
{
static EnumPropertyItem prop_sky_type[] = {
{SHD_SKY_OLD, "PREETHAM", 0, "Preetham", ""},
{SHD_SKY_NEW, "HOSEK_WILKIE", 0, "Hosek / Wilkie", ""},
{0, NULL, 0, NULL, NULL}
};
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeTexSky", "storage");
def_sh_tex(srna);
prop = RNA_def_property(srna, "sky_type", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "sky_model");
RNA_def_property_enum_items(prop, prop_sky_type);
RNA_def_property_ui_text(prop, "Sky Type", "");
RNA_def_property_update(prop, 0, "rna_Node_update");
prop = RNA_def_property(srna, "sun_direction", PROP_FLOAT, PROP_DIRECTION);
RNA_def_property_ui_text(prop, "Sun Direction", "Direction from where the sun is shining");
@ -3199,8 +3211,14 @@ static void def_sh_tex_sky(StructRNA *srna)
prop = RNA_def_property(srna, "turbidity", PROP_FLOAT, PROP_NONE);
RNA_def_property_range(prop, 1.0f, 30.0f);
RNA_def_property_ui_range(prop, 1.0f, 10.0f, 10, 3);
RNA_def_property_ui_text(prop, "Turbidity", "Atmospheric turbidity");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
prop = RNA_def_property(srna, "ground_albedo", PROP_FLOAT, PROP_NONE);
RNA_def_property_range(prop, 0.0f, 1.0f);
RNA_def_property_ui_text(prop, "Ground Albedo", "Ground color that is subtly reflected in the sky");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
}
static void def_sh_tex_environment(StructRNA *srna)

@ -48,6 +48,8 @@ static void node_shader_init_tex_sky(bNodeTree *UNUSED(ntree), bNode *node)
tex->sun_direction[1] = 0.0f;
tex->sun_direction[2] = 1.0f;
tex->turbidity = 2.2f;
tex->ground_albedo = 0.3f;
tex->sky_model = SHD_SKY_NEW;
node->storage = tex;
}
@ -70,6 +72,7 @@ void register_node_type_sh_tex_sky(void)
sh_node_type_base(&ntype, SH_NODE_TEX_SKY, "Sky Texture", NODE_CLASS_TEXTURE, 0);
node_type_compatibility(&ntype, NODE_NEW_SHADING);
node_type_socket_templates(&ntype, sh_node_tex_sky_in, sh_node_tex_sky_out);
node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
node_type_init(&ntype, node_shader_init_tex_sky);
node_type_storage(&ntype, "NodeTexSky", node_free_standard_storage, node_copy_standard_storage);
node_type_gpu(&ntype, node_shader_gpu_tex_sky);