2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2013-08-28 14:11:28 +00:00
|
|
|
/* Sky texture */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float sky_angle_between(float thetav, float phiv, float theta, float phi)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float cospsi = sinf(thetav)*sinf(theta)*cosf(phi - phiv) + cosf(thetav)*cosf(theta);
|
|
|
|
return safe_acosf(cospsi);
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:11:28 +00:00
|
|
|
/*
|
|
|
|
* "A Practical Analytic Model for Daylight"
|
|
|
|
* A. J. Preetham, Peter Shirley, Brian Smits
|
|
|
|
*/
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float sky_perez_function(float *lam, float theta, float gamma)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float ctheta = cosf(theta);
|
|
|
|
float cgamma = cosf(gamma);
|
|
|
|
|
|
|
|
return (1.0f + lam[0]*expf(lam[1]/ctheta)) * (1.0f + lam[2]*expf(lam[3]*gamma) + lam[4]*cgamma*cgamma);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float3 sky_radiance_old(KernelGlobals *kg, float3 dir,
|
2013-08-28 14:11:28 +00:00
|
|
|
float sunphi, float suntheta,
|
|
|
|
float radiance_x, float radiance_y, float radiance_z,
|
|
|
|
float *config_x, float *config_y, float *config_z)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|
2013-08-28 14:11:28 +00:00
|
|
|
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* clamp theta to horizon */
|
|
|
|
theta = min(theta, M_PI_2_F - 0.001f);
|
|
|
|
|
|
|
|
/* compute xyY color space values */
|
2013-08-28 14:11:28 +00:00
|
|
|
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);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* convert to RGB */
|
|
|
|
float3 xyz = xyY_to_xyz(x, y, Y);
|
|
|
|
return xyz_to_rgb(xyz.x, xyz.y, xyz.z);
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:11:28 +00:00
|
|
|
/*
|
|
|
|
* "An Analytic Model for Full Spectral Sky-Dome Radiance"
|
|
|
|
* Lukas Hosek, Alexander Wilkie
|
|
|
|
*/
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float sky_radiance_internal(float *configuration, float theta, float gamma)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-08-28 14:11:28 +00:00
|
|
|
float ctheta = cosf(theta);
|
|
|
|
float cgamma = cosf(gamma);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-08-28 14:11:28 +00:00
|
|
|
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);
|
2014-05-02 21:22:14 +00:00
|
|
|
float zenith = sqrtf(ctheta);
|
2013-08-28 14:11:28 +00:00
|
|
|
|
|
|
|
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);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float3 sky_radiance_new(KernelGlobals *kg, float3 dir,
|
2013-08-28 14:11:28 +00:00
|
|
|
float sunphi, float suntheta,
|
|
|
|
float radiance_x, float radiance_y, float radiance_z,
|
|
|
|
float *config_x, float *config_y, float *config_z)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|
2013-08-28 14:11:28 +00:00
|
|
|
float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* clamp theta to horizon */
|
|
|
|
theta = min(theta, M_PI_2_F - 0.001f);
|
|
|
|
|
2013-08-17 16:08:03 +00:00
|
|
|
/* compute xyz color space values */
|
2013-08-28 14:11:28 +00:00
|
|
|
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;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-08-28 14:11:28 +00:00
|
|
|
/* convert to RGB and adjust strength */
|
|
|
|
return xyz_to_rgb(x, y, z) * (M_2PI_F/683);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2013-08-28 14:11:28 +00:00
|
|
|
/* 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;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
float3 dir = stack_load_float3(stack, dir_offset);
|
2013-08-28 14:11:28 +00:00
|
|
|
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);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
stack_store_float3(stack, out_offset, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|