2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
/* sun direction in spherical and cartesian */
26
/* perez function parameters */
27
float zenith_Y, zenith_x, zenith_y;
28
float perez_Y[5], perez_x[5], perez_y[5];
31
color xyY_to_xyz(float x, float y, float Y)
35
if(y != 0.0) X = (x / y) * Y;
38
if(y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
41
return color(X, Y, Z);
44
color xyz_to_rgb(float x, float y, float z)
46
return color(3.240479 * x + -1.537150 * y + -0.498535 * z,
47
-0.969256 * x + 1.875991 * y + 0.041556 * z,
48
0.055648 * x + -0.204043 * y + 1.057311 * z);
51
float sky_angle_between(float thetav, float phiv, float theta, float phi)
53
float cospsi = sin(thetav)*sin(theta)*cos(phi - phiv) + cos(thetav)*cos(theta);
63
vector sky_spherical_coordinates(vector dir)
65
return vector(acos(dir[2]), atan2(dir[0], dir[1]), 0);
68
float sky_perez_function(float lam[5], float theta, float gamma)
70
float ctheta = cos(theta);
71
float cgamma = cos(gamma);
73
return (1.0 + lam[0]*exp(lam[1] / ctheta)) * (1.0 + lam[2]*exp(lam[3]*gamma) + lam[4]*cgamma*cgamma);
76
color sky_xyz_radiance(KernelSunSky sunsky, vector dir)
78
/* convert vector to spherical coordinates */
79
vector spherical = sky_spherical_coordinates(dir);
80
float theta = spherical[0];
81
float phi = spherical[1];
83
/* angle between sun direction and dir */
84
float gamma = sky_angle_between(theta, phi, sunsky.theta, sunsky.phi);
86
/* clamp theta to horizon */
87
theta = min(theta, M_PI_2 - 0.001);
89
/* compute xyY color space values */
90
float x = sunsky.zenith_x * sky_perez_function(sunsky.perez_x, theta, gamma);
91
float y = sunsky.zenith_y * sky_perez_function(sunsky.perez_y, theta, gamma);
92
float Y = sunsky.zenith_Y * sky_perez_function(sunsky.perez_Y, theta, gamma);
95
color xyz = xyY_to_xyz(x, y, Y);
96
return xyz_to_rgb(xyz[0], xyz[1], xyz[2]);
99
void precompute_sunsky(vector dir, float turbidity, output KernelSunSky sunsky)
101
vector spherical = sky_spherical_coordinates(dir);
102
float theta = spherical[0];
103
float phi = spherical[1];
105
sunsky.theta = theta;
109
float theta2 = theta*theta;
110
float theta3 = theta*theta*theta;
114
float chi = (4.0/ 9.0- T / 120.0) * (M_PI - 2.0* theta);
115
sunsky.zenith_Y = (4.0453*T - 4.9710) * tan(chi) - 0.2155*T + 2.4192;
116
sunsky.zenith_Y *= 0.06;
119
(0.00166* theta3 - 0.00375* theta2 + 0.00209* theta)*T2 +
120
(-0.02903* theta3 + 0.06377* theta2 - 0.03202* theta + 0.00394)*T +
121
(0.11693* theta3 - 0.21196* theta2 + 0.06052* theta + 0.25886);
124
(0.00275* theta3 - 0.00610* theta2 + 0.00317* theta)*T2 +
125
(-0.04214* theta3 + 0.08970* theta2 - 0.04153* theta + 0.00516)*T +
126
(0.15346* theta3 - 0.26756* theta2 + 0.06670* theta + 0.26688);
128
sunsky.perez_Y[0] = (0.1787*T - 1.4630);
129
sunsky.perez_Y[1] = (-0.3554*T + 0.4275);
130
sunsky.perez_Y[2] = (-0.0227*T + 5.3251);
131
sunsky.perez_Y[3] = (0.1206*T - 2.5771);
132
sunsky.perez_Y[4] = (-0.0670*T + 0.3703);
134
sunsky.perez_x[0] = (-0.0193*T - 0.2592);
135
sunsky.perez_x[1] = (-0.0665*T + 0.0008);
136
sunsky.perez_x[2] = (-0.0004*T + 0.2125);
137
sunsky.perez_x[3] = (-0.0641*T - 0.8989);
138
sunsky.perez_x[4] = (-0.0033*T + 0.0452);
140
sunsky.perez_y[0] = (-0.0167*T - 0.2608);
141
sunsky.perez_y[1] = (-0.0950*T + 0.0092);
142
sunsky.perez_y[2] = (-0.0079*T + 0.2102);
143
sunsky.perez_y[3] = (-0.0441*T - 1.6537);
144
sunsky.perez_y[4] = (-0.0109*T + 0.0529);
146
sunsky.zenith_Y /= sky_perez_function(sunsky.perez_Y, 0, theta);
147
sunsky.zenith_x /= sky_perez_function(sunsky.perez_x, 0, theta);
148
sunsky.zenith_y /= sky_perez_function(sunsky.perez_y, 0, theta);
151
shader node_sky_texture(
153
vector sun_direction = vector(0, 0, 1),
154
float turbidity = 2.2,
155
output color Color = color(0.0, 0.0, 0.0))
159
precompute_sunsky(sun_direction, turbidity, sunsky);
160
Color = sky_xyz_radiance(sunsky, Vector);