Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_sky_texture.osl « shaders « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9fa9f938b6a5ca17e7981294c5dbbbaa1f473571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright 2011-2013 Blender Foundation
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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
 * limitations under the License
 */

#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);

	if (cospsi > 1.0)
		return 0.0;
	if (cospsi < -1.0)
		return M_PI;

	return acos(cospsi);
}

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)
{
	float ctheta = cos(theta);
	float cgamma = cos(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)
{
	/* convert vector to spherical coordinates */
	vector spherical = sky_spherical_coordinates(dir);
	float theta = spherical[0];
	float phi = spherical[1];

	/* angle between sun direction and dir */
	float gamma = sky_angle_between(theta, phi, sunsky.theta, sunsky.phi);

	/* 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);

	/* 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)
{
	vector spherical = sky_spherical_coordinates(dir);
	float theta = spherical[0];
	float phi = spherical[1];

	sunsky.theta = theta;
	sunsky.phi = phi;
	sunsky.dir = dir;

	float theta2 = theta * theta;
	float theta3 = theta2 * theta;
	float T = turbidity;
	float T2 = T * T;

	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;

	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);
}

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)
{
	vector p = Vector;

	if (use_mapping)
		p = transform(mapping, p);

	KernelSunSky sunsky;

	precompute_sunsky(sun_direction, turbidity, sunsky);
	Color = sky_xyz_radiance(sunsky, p);
}