Blender V5.3
node_shader_tex_sky.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11#include "sky_hosek.h"
12#include "sky_nishita.h"
13
14#include "BKE_texture.h"
15
16#include "RNA_access.hh"
17
19#include "UI_resources.hh"
20
22
23namespace blender {
24
26
28{
29 b.add_input<decl::Vector>("Vector"_ustr).hide_value();
30 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
31}
32
33static void node_shader_buts_tex_sky(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
34{
35 layout.prop(ptr, "sky_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
36
37 if (RNA_enum_get(ptr, "sky_type") == SHD_SKY_PREETHAM) {
38 layout.prop(ptr, "sun_direction", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
39 layout.prop(ptr, "turbidity", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
40 }
41 else if (RNA_enum_get(ptr, "sky_type") == SHD_SKY_HOSEK) {
42 layout.prop(ptr, "sun_direction", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
43 layout.prop(ptr, "turbidity", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
44 layout.prop(ptr, "ground_albedo", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
45 }
46 else {
47 layout.prop(ptr, "sun_disc", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
48
49 if (RNA_boolean_get(ptr, "sun_disc")) {
50 ui::Layout &col = layout.column(true);
51 col.prop(ptr, "sun_size", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
52 col.prop(ptr, "sun_intensity", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
53 }
54
55 {
56 ui::Layout &col = layout.column(true);
57 col.prop(ptr, "sun_elevation", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
58 col.prop(ptr, "sun_rotation", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
59 }
60 layout.prop(ptr, "altitude", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
61
62 {
63 ui::Layout &col = layout.column(true);
64 col.prop(ptr, "air_density", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
65 col.prop(ptr, "aerosol_density", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
66 col.prop(ptr, "ozone_density", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
67 }
68 }
69}
70
71static void node_shader_init_tex_sky(bNodeTree * /*ntree*/, bNode *node)
72{
73 NodeTexSky *tex = MEM_new<NodeTexSky>("NodeTexSky");
76 tex->sun_direction[0] = 0.0f;
77 tex->sun_direction[1] = 0.0f;
78 tex->sun_direction[2] = 1.0f;
79 tex->turbidity = 2.2f;
80 tex->ground_albedo = 0.3f;
81 tex->sun_disc = true;
82 tex->sun_size = DEG2RADF(0.545f);
83 tex->sun_intensity = 1.0f;
84 tex->sun_elevation = DEG2RADF(15.0f);
85 tex->sun_rotation = 0.0f;
86 tex->altitude = 100.0f;
87 tex->air_density = 1.0f;
88 tex->aerosol_density = 1.0f;
89 tex->ozone_density = 1.0f;
91 node->storage = tex;
92}
93
95 float config_Y[5], config_x[5], config_y[5]; /* named after xyY color space */
96 float radiance[3];
97};
98
99static float sky_perez_function(const float *lam, float theta, float gamma)
100{
101 float ctheta = cosf(theta);
102 float cgamma = cosf(gamma);
103
104 return (1.0 + lam[0] * expf(lam[1] / ctheta)) *
105 (1.0 + lam[2] * expf(lam[3] * gamma) + lam[4] * cgamma * cgamma);
106}
107
108static void sky_precompute_old(SkyModelPreetham *sunsky, const float sun_angles[], float turbidity)
109{
110 float theta = sun_angles[0];
111 float theta2 = theta * theta;
112 float theta3 = theta2 * theta;
113 float T = turbidity;
114 float T2 = T * T;
115 float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI - 2.0f * theta);
116
117 sunsky->radiance[0] = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
118 sunsky->radiance[0] *= 0.06f;
119
120 sunsky->radiance[1] = (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
121 (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) *
122 T +
123 (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
124
125 sunsky->radiance[2] = (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
126 (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) *
127 T +
128 (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
129
130 sunsky->config_Y[0] = (0.1787f * T - 1.4630f);
131 sunsky->config_Y[1] = (-0.3554f * T + 0.4275f);
132 sunsky->config_Y[2] = (-0.0227f * T + 5.3251f);
133 sunsky->config_Y[3] = (0.1206f * T - 2.5771f);
134 sunsky->config_Y[4] = (-0.0670f * T + 0.3703f);
135
136 sunsky->config_x[0] = (-0.0193f * T - 0.2592f);
137 sunsky->config_x[1] = (-0.0665f * T + 0.0008f);
138 sunsky->config_x[2] = (-0.0004f * T + 0.2125f);
139 sunsky->config_x[3] = (-0.0641f * T - 0.8989f);
140 sunsky->config_x[4] = (-0.0033f * T + 0.0452f);
141
142 sunsky->config_y[0] = (-0.0167f * T - 0.2608f);
143 sunsky->config_y[1] = (-0.0950f * T + 0.0092f);
144 sunsky->config_y[2] = (-0.0079f * T + 0.2102f);
145 sunsky->config_y[3] = (-0.0441f * T - 1.6537f);
146 sunsky->config_y[4] = (-0.0109f * T + 0.0529f);
147
148 sunsky->radiance[0] /= sky_perez_function(sunsky->config_Y, 0, theta);
149 sunsky->radiance[1] /= sky_perez_function(sunsky->config_x, 0, theta);
150 sunsky->radiance[2] /= sky_perez_function(sunsky->config_y, 0, theta);
151}
152
153static void sky_simplify_elevation_rotation(float &sun_elevation, float &sun_rotation)
154{
155 /* Patch Sun position so users are able to animate the daylight cycle while keeping the shading
156 * code simple. */
157 float new_sun_elevation = sun_elevation;
158 float new_sun_rotation = sun_rotation;
159
160 /* Wrap `new_sun_elevation` into [-2PI..2PI] range. */
161 new_sun_elevation = fmodf(new_sun_elevation, 2.0f * M_PI);
162 /* Wrap `new_sun_elevation` into [-PI..PI] range. */
163 if (fabsf(new_sun_elevation) >= M_PI) {
164 new_sun_elevation -= copysignf(2.0f, new_sun_elevation) * M_PI;
165 }
166 /* Wrap `new_sun_elevation` into [-PI/2..PI/2] range while keeping the same absolute position.
167 */
168 if (new_sun_elevation >= M_PI / 2.0f || new_sun_elevation <= -M_PI / 2.0f) {
169 new_sun_elevation = copysignf(M_PI, new_sun_elevation) - new_sun_elevation;
170 new_sun_rotation += M_PI;
171 }
172
173 /* Wrap `new_sun_rotation` into [-2PI..2PI] range. */
174 new_sun_rotation = fmodf(new_sun_rotation, 2.0f * M_PI);
175 /* Wrap `new_sun_rotation` into [0..2PI] range. */
176 if (new_sun_rotation < 0.0f) {
177 new_sun_rotation += 2.0f * M_PI;
178 }
179 new_sun_rotation = 2.0f * M_PI - new_sun_rotation;
180
181 sun_elevation = new_sun_elevation;
182 sun_rotation = new_sun_rotation;
183}
184
186 bNode *node,
187 bNodeExecData * /*execdata*/,
190{
193 NodeTexSky *tex = static_cast<NodeTexSky *>(node->storage);
194 float sun_angles[2]; /* [0]=theta=zenith angle [1]=phi=azimuth */
195 sun_angles[0] = acosf(tex->sun_direction[2]);
196 sun_angles[1] = atan2f(tex->sun_direction[0], tex->sun_direction[1]);
197
198 if (tex->sky_model == SHD_SKY_PREETHAM) {
199 /* Preetham */
200 SkyModelPreetham sunsky;
201 sky_precompute_old(&sunsky, sun_angles, tex->turbidity);
204 return GPU_stack_link(mat,
205 node,
206 "node_tex_sky_preetham",
207 in,
208 out,
209 /* Pass config_Y/x/y as 3x(vec4+float) */
210 GPU_uniform(&sunsky.config_Y[0]),
211 GPU_uniform(&sunsky.config_Y[4]),
212 GPU_uniform(&sunsky.config_x[0]),
213 GPU_uniform(&sunsky.config_x[4]),
214 GPU_uniform(&sunsky.config_y[0]),
215 GPU_uniform(&sunsky.config_y[4]),
216 GPU_uniform(sun_angles),
217 GPU_uniform(sunsky.radiance),
221 }
222 if (tex->sky_model == SHD_SKY_HOSEK) {
223 /* Hosek / Wilkie */
224 sun_angles[0] = fmin(M_PI_2, sun_angles[0]); /* clamp to horizon */
226 tex->turbidity, tex->ground_albedo, fmax(0.0, M_PI_2 - sun_angles[0]));
227 /* Pass sky_state->configs[3][9] as 3*(vec4+vec4)+vec3 */
228 float config_x07[8], config_y07[8], config_z07[8], config_xyz8[3];
229 for (int i = 0; i < 8; ++i) {
230 config_x07[i] = float(sky_state->configs[0][i]);
231 config_y07[i] = float(sky_state->configs[1][i]);
232 config_z07[i] = float(sky_state->configs[2][i]);
233 }
234 for (int i = 0; i < 3; ++i) {
235 config_xyz8[i] = float(sky_state->configs[i][8]);
236 }
237 float radiance[3];
238 for (int i = 0; i < 3; i++) {
239 radiance[i] = sky_state->radiances[i] * (2 * M_PI / 683);
240 }
244 return GPU_stack_link(mat,
245 node,
246 "node_tex_sky_hosekwilkie",
247 in,
248 out,
249 GPU_uniform(&config_x07[0]),
250 GPU_uniform(&config_x07[4]),
251 GPU_uniform(&config_y07[0]),
252 GPU_uniform(&config_y07[4]),
253 GPU_uniform(&config_z07[0]),
254 GPU_uniform(&config_z07[4]),
255 GPU_uniform(config_xyz8),
256 GPU_uniform(sun_angles),
257 GPU_uniform(radiance),
261 }
262
263 /* Nishita */
265
266 float sun_rotation = tex->sun_rotation;
267 float sun_elevation = tex->sun_elevation;
268 /* Clamped for numerical precision, as Cycles' get_sun_size() does. The disc's radiance is
269 * divided by its solid angle, which is zero at a size of zero. */
270 const float sun_size = fmaxf(tex->sun_size, 0.0005f);
271 float pixel_bottom[3];
272 float pixel_top[3];
273 sky_simplify_elevation_rotation(sun_elevation, sun_rotation);
276 4,
279 sun_elevation,
280 tex->altitude,
281 tex->air_density,
282 tex->aerosol_density,
283 tex->ozone_density);
285 sun_size,
286 tex->altitude,
287 tex->air_density,
288 tex->aerosol_density,
289 pixel_bottom,
290 pixel_top);
291 }
292 else {
294 4,
297 sun_elevation,
298 tex->altitude,
299 tex->air_density,
300 tex->aerosol_density,
301 tex->ozone_density);
303 sun_size,
304 tex->altitude,
305 tex->air_density,
306 tex->aerosol_density,
307 tex->ozone_density,
308 pixel_bottom,
309 pixel_top);
310 }
311
314
315 /* To fix pole issue we clamp the v coordinate. */
319 float layer;
320 float sky_type = (tex->sky_model == SHD_SKY_SINGLE_SCATTERING) ? 0.0f : 1.0f;
321 /* A negative angular diameter tells the shader the disc is off.
322 * SKY_earth_intersection_angle() returns how far the true horizon dips below the local
323 * horizontal at altitude, so the visible sky reaches that negative elevation. */
324 const float sun_params[4] = {sun_elevation,
325 tex->sun_disc ? sun_size : -1.0f,
326 tex->sun_intensity,
328 GPUNodeLink *sky_texture = GPU_image_sky(
329 mat, GPU_SKY_WIDTH, GPU_SKY_HEIGHT, pixels.data(), &layer, sampler);
330 return GPU_stack_link(mat,
331 node,
332 "node_tex_sky_nishita",
333 in,
334 out,
335 GPU_constant(&sky_type),
336 GPU_uniform(&sun_rotation),
340 GPU_uniform(pixel_bottom),
341 GPU_uniform(pixel_top),
342 GPU_uniform(sun_params),
343 sky_texture,
345}
346
348{
349 bNodeSocket *sockVector = bke::node_find_socket(*node, SOCK_IN, "Vector"_ustr);
350
351 NodeTexSky *tex = static_cast<NodeTexSky *>(node->storage);
353 *ntree,
354 *sockVector,
356 tex->sun_disc == 1));
357}
358
360{
361 const NodeDeclaration &declaration = *params.node_type().static_declaration;
362 if (params.in_out() == SOCK_OUT) {
363 search_link_ops_for_declarations(params, declaration.outputs);
364 return;
365 }
366 if (params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_FLOAT)) {
367 params.add_item(IFACE_("Vector"), [](LinkSearchOpParams &params) {
368 bNode &node = params.add_node("ShaderNodeTexSky"_ustr);
369 NodeTexSky *tex = static_cast<NodeTexSky *>(node.storage);
370 tex->sun_disc = false;
371 params.update_and_connect_available_socket(node, "Vector"_ustr);
372 });
373 }
374}
375
376} // namespace nodes::node_shader_tex_sky_cc
377
378/* node type definition */
380{
381 namespace file_ns = nodes::node_shader_tex_sky_cc;
382
383 static bke::bNodeType ntype;
384
385 sh_node_type_base(&ntype, "ShaderNodeTexSky"_ustr, SH_NODE_TEX_SKY);
386 ntype.ui_name = "Sky Texture";
387 ntype.ui_description = "Generate a procedural sky texture";
388 ntype.enum_name_legacy = "TEX_SKY";
390 ntype.declare = file_ns::node_declare;
391 ntype.draw_buttons = file_ns::node_shader_buts_tex_sky;
393 ntype.initfunc = file_ns::node_shader_init_tex_sky;
396 ntype.gpu_fn = file_ns::node_shader_gpu_tex_sky;
397 ntype.updatefunc = file_ns::node_shader_update_sky;
398 ntype.gather_link_search_ops = file_ns::node_gather_link_searches;
399
401}
402
403} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_SKY
#define DEG2RADF(_deg)
#define M_PI_2
#define M_PI
#define IFACE_(msgid)
#define GPU_SKY_WIDTH
#define GPU_SKY_HEIGHT
#define ELEM(...)
const T * data() const
Definition BLI_array.hh:323
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
Vector< SocketDeclaration * > outputs
#define expf(x)
#define fmodf(x, y)
#define acosf(x)
#define copysignf(x, y)
#define sampler(slot)
#define layer
#define T
#define T2
Definition md5.cpp:21
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void sky_precompute_old(SkyModelPreetham *sunsky, const float sun_angles[], float turbidity)
static void node_shader_init_tex_sky(bNodeTree *, bNode *node)
static void node_shader_buts_tex_sky(ui::Layout &layout, bContext *, PointerRNA *ptr)
static int node_shader_gpu_tex_sky(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sky_simplify_elevation_rotation(float &sun_elevation, float &sun_rotation)
static void node_shader_update_sky(bNodeTree *ntree, bNode *node)
static float sky_perez_function(const float *lam, float theta, float gamma)
static void node_declare(NodeDeclarationBuilder &b)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
BMesh Iterators.
void register_node_type_sh_tex_sky()
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_FILTERING_LINEAR
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GPUNodeLink * GPU_uniform(const float *num)
GPUNodeLink * GPU_constant(const float *num)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void get_XYZ_to_RGB_for_gpu(XYZ_to_RGB *data)
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
GPUNodeLink * GPU_image_sky(GPUMaterial *mat, int width, int height, const float *pixels, float *layer, GPUSamplerState sampler_state)
@ SHD_SKY_SINGLE_SCATTERING
@ SHD_SKY_MULTIPLE_SCATTERING
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define fabsf
#define fmaxf
#define tanf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void SKY_arhosekskymodelstate_free(SKY_ArHosekSkyModelState *state)
SKY_ArHosekSkyModelState * SKY_arhosek_xyz_skymodelstate_alloc_init(const double turbidity, const double albedo, const double elevation)
void SKY_multiple_scattering_precompute_texture(float *pixels, int stride, int width, int height, float sun_elevation, float altitude, float air_density, float aerosol_density, float ozone_density)
void SKY_single_scattering_precompute_sun(float sun_elevation, float angular_diameter, float altitude, float air_density, float aerosol_density, float r_pixel_bottom[3], float r_pixel_top[3])
void SKY_single_scattering_precompute_texture(float *pixels, int stride, int width, int height, float sun_elevation, float altitude, float air_density, float aerosol_density, float ozone_density)
void SKY_multiple_scattering_precompute_sun(float sun_elevation, float angular_diameter, float altitude, float air_density, float aerosol_density, float ozone_density, float r_pixel_bottom[3], float r_pixel_top[3])
float SKY_earth_intersection_angle(float altitude)
SKY_ArHosekSkyModelConfiguration configs[11]
Definition sky_hosek.h:312
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
static constexpr int _160
Definition BKE_node.hh:246
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)