Blender V5.3
gpu_shader_material_tex_sky.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11float sky_angle_between(float thetav, float phiv, float theta, float phi)
12{
13 float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta);
14
15 if (cospsi > 1.0f) {
16 return 0.0f;
17 }
18 if (cospsi < -1.0f) {
19 return M_PI;
20 }
21
22 return acos(cospsi);
23}
24
25/* Angle between two unit vectors. Stable at the small angles the Sun disc needs, where
26 * acos(dot()) loses most of its precision. Matches precise_angle() in the Cycles kernel. */
28{
29 return 2.0f * atan(length(a - b), length(a + b));
30}
31
33{
34 return float3(M_PI_2 - atan(dir.z, length(dir.xy)), atan(dir.x, dir.y), 0.0f);
35}
36
37/* Preetham */
38/* lam03+lam4: 5 floats passed as vec4+float */
39float sky_perez_function(float4 lam03, float lam4, float theta, float gamma)
40{
41 float ctheta = cos(theta);
42 float cgamma = cos(gamma);
43
44 return (1.0f + lam03[0] * exp(lam03[1] / ctheta)) *
45 (1.0f + lam03[2] * exp(lam03[3] * gamma) + lam4 * cgamma * cgamma);
46}
47
48float3 xyY_to_xyz(float x, float y, float Y)
49{
50 float X, Z;
51
52 if (y != 0.0f) {
53 X = (x / y) * Y;
54 }
55 else {
56 X = 0.0f;
57 }
58
59 if (y != 0.0f && Y != 0.0f) {
60 Z = ((1.0f - x - y) / y) * Y;
61 }
62 else {
63 Z = 0.0f;
64 }
65
66 return float3(X, Y, Z);
67}
68
69[[node]]
71 float4 config_Y03,
72 float config_Y4,
73 float4 config_x03,
74 float config_x4,
75 float4 config_y03,
76 float config_y4,
77 float2 sun_angles,
78 float3 radiance,
79 float3 xyz_to_r,
80 float3 xyz_to_g,
81 float3 xyz_to_b,
82 float4 &color)
83{
84 /* convert vector to spherical coordinates */
85 float3 spherical = sky_spherical_coordinates(co);
86 float theta = spherical[0];
87 float phi = spherical[1];
88
89 float suntheta = sun_angles[0];
90 float sunphi = sun_angles[1];
91
92 /* angle between sun direction and dir */
93 float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
94
95 /* clamp theta to horizon */
96 theta = min(theta, M_PI_2 - 0.001f);
97
98 /* compute xyY color space values */
99 float Y = radiance[0] * sky_perez_function(config_Y03, config_Y4, theta, gamma);
100 float x = radiance[1] * sky_perez_function(config_x03, config_x4, theta, gamma);
101 float y = radiance[2] * sky_perez_function(config_y03, config_y4, theta, gamma);
102
103 /* convert to RGB */
104 float3 xyz = xyY_to_xyz(x, y, Y);
105 color = float4(dot(xyz_to_r, xyz), dot(xyz_to_g, xyz), dot(xyz_to_b, xyz), 1);
106}
107
108/* Hosek / Wilkie */
110 float4 config03, float4 config47, float config8, float theta, float gamma)
111{
112 float ctheta = cos(theta);
113 float cgamma = cos(gamma);
114
115 float expM = exp(config47[0] * gamma);
116 float rayM = cgamma * cgamma;
117 float mieM = (1.0f + rayM) / pow((1.0f + config8 * config8 - 2.0f * config8 * cgamma), 1.5f);
118 float zenith = sqrt(ctheta);
119
120 return (1.0f + config03[0] * exp(config03[1] / (ctheta + 0.01f))) *
121 (config03[2] + config03[3] * expM + config47[1] * rayM + config47[2] * mieM +
122 config47[3] * zenith);
123}
124
125[[node]]
127 float4 config_x03,
128 float4 config_x47,
129 float4 config_y03,
130 float4 config_y47,
131 float4 config_z03,
132 float4 config_z47,
133 float3 config_xyz8,
134 float2 sun_angles,
135 float3 radiance,
136 float3 xyz_to_r,
137 float3 xyz_to_g,
138 float3 xyz_to_b,
139 float4 &color)
140{
141 /* convert vector to spherical coordinates */
142 float3 spherical = sky_spherical_coordinates(co);
143 float theta = spherical[0];
144 float phi = spherical[1];
145
146 float suntheta = sun_angles[0];
147 float sunphi = sun_angles[1];
148
149 /* angle between sun direction and dir */
150 float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
151
152 /* clamp theta to horizon */
153 theta = min(theta, M_PI_2 - 0.001f);
154
155 float3 xyz;
156 xyz.x = sky_radiance_hosekwilkie(config_x03, config_x47, config_xyz8[0], theta, gamma) *
157 radiance.x;
158 xyz.y = sky_radiance_hosekwilkie(config_y03, config_y47, config_xyz8[1], theta, gamma) *
159 radiance.y;
160 xyz.z = sky_radiance_hosekwilkie(config_z03, config_z47, config_xyz8[2], theta, gamma) *
161 radiance.z;
162
163 color = float4(dot(xyz_to_r, xyz), dot(xyz_to_g, xyz), dot(xyz_to_b, xyz), 1);
164}
165
166[[node]]
168 float /*sky_type*/,
169 float sun_rotation,
170 float3 xyz_to_r,
171 float3 xyz_to_g,
172 float3 xyz_to_b,
173 float3 sun_pixel_bottom,
174 float3 sun_pixel_top,
175 float4 sun_params,
176 sampler2DArray ima,
177 float layer,
178 float4 &color)
179{
180 float3 spherical = sky_spherical_coordinates(co);
181 float3 xyz;
182 float dir_elevation = M_PI_2 - spherical.x;
183 float x = (spherical.y + M_PI + sun_rotation) / (2.0f * M_PI);
184 float fade = 1.0f;
185 float y;
186
187 /* Undo the non-linear transformation from the sky LUT. */
188 float dir_elevation_abs = (dir_elevation < 0.0f) ? -dir_elevation : dir_elevation;
189 y = sqrt(dir_elevation_abs / M_PI_2) * sign(dir_elevation) * 0.5f + 0.5f;
190
191 /* Look up color in the precomputed map and convert to RGB. */
192 float3x3 xyz_to_rgb = transpose(float3x3(xyz_to_r, xyz_to_g, xyz_to_b));
193 xyz = fade * texture(ima, float3(x, y, layer)).rgb;
194 float3 rgb = xyz_to_rgb * xyz;
195
196 /* The Sun disc covers about half a degree, too small to survive baking into the map, so it's
197 * drawn here instead. */
198 float sun_elevation = sun_params.x;
199 float angular_diameter = sun_params.y;
200 float sun_intensity = sun_params.z;
201 float earth_intersection_angle = sun_params.w;
202 if (angular_diameter > 0.0f && dir_elevation > earth_intersection_angle) {
203 float cos_elevation = cos(sun_elevation);
204 float3 sun_dir = float3(
205 -cos_elevation * sin(sun_rotation), cos_elevation * cos(sun_rotation), sin(sun_elevation));
206 float sun_angle = sky_precise_angle(normalize(co), sun_dir);
207 float half_angular = angular_diameter * 0.5f;
208 if (sun_angle < half_angular) {
209 /* Limb darkening, coefficient is 0.6. */
210 float angle_fraction = sun_angle / half_angular;
211 float limb_darkening = 1.0f - 0.6f * (1.0f - sin_from_cos(angle_fraction));
212 float sun_y = ((dir_elevation - sun_elevation) / angular_diameter) + 0.5f;
213 float3 disc = mix(sun_pixel_bottom, sun_pixel_top, sun_y) * sun_intensity * limb_darkening;
214 rgb += xyz_to_rgb * disc;
215 }
216 }
217
218 color = float4(rgb, 1.0f);
219}
#define M_PI_2
#define M_PI
#define Z
#define Y
#define layer
#define atan
#define sin
#define pow
#define exp
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T sign(T) RET
#define sqrt
MatBase< R, C > transpose(MatBase< C, R >) RET
float length(VecOp< float, D >) RET
#define acos
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2, false, true > sampler2DArray
float fade(float t)
float sky_perez_function(float4 lam03, float lam4, float theta, float gamma)
void node_tex_sky_preetham(float3 co, float4 config_Y03, float config_Y4, float4 config_x03, float config_x4, float4 config_y03, float config_y4, float2 sun_angles, float3 radiance, float3 xyz_to_r, float3 xyz_to_g, float3 xyz_to_b, float4 &color)
void node_tex_sky_hosekwilkie(float3 co, float4 config_x03, float4 config_x47, float4 config_y03, float4 config_y47, float4 config_z03, float4 config_z47, float3 config_xyz8, float2 sun_angles, float3 radiance, float3 xyz_to_r, float3 xyz_to_g, float3 xyz_to_b, float4 &color)
float sky_precise_angle(float3 a, float3 b)
float3 xyY_to_xyz(float x, float y, float Y)
float3 sky_spherical_coordinates(float3 dir)
void node_tex_sky_nishita(float3 co, float, float sun_rotation, float3 xyz_to_r, float3 xyz_to_g, float3 xyz_to_b, float3 sun_pixel_bottom, float3 sun_pixel_top, float4 sun_params, sampler2DArray ima, float layer, float4 &color)
float sky_radiance_hosekwilkie(float4 config03, float4 config47, float config8, float theta, float gamma)
float sky_angle_between(float thetav, float phiv, float theta, float phi)
float sin_from_cos(float c)
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define X(TYPE)
#define mix
#define min(a, b)
Definition sort.cc:38
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225