Blender  V2.93
node_shader_tex_sky.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #include "../node_shader_util.h"
21 #include "sky_model.h"
22 
23 /* **************** OUTPUT ******************** */
24 
26  {SOCK_VECTOR, N_("Vector"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
27  {-1, ""},
28 };
29 
31  {SOCK_RGBA, N_("Color"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_NO_INTERNAL_LINK},
32  {-1, ""},
33 };
34 
36 {
37  NodeTexSky *tex = MEM_callocN(sizeof(NodeTexSky), "NodeTexSky");
39  BKE_texture_colormapping_default(&tex->base.color_mapping);
40  tex->sun_direction[0] = 0.0f;
41  tex->sun_direction[1] = 0.0f;
42  tex->sun_direction[2] = 1.0f;
43  tex->turbidity = 2.2f;
44  tex->ground_albedo = 0.3f;
45  tex->sun_disc = true;
46  tex->sun_size = DEG2RADF(0.545f);
47  tex->sun_intensity = 1.0f;
48  tex->sun_elevation = DEG2RADF(15.0f);
49  tex->sun_rotation = 0.0f;
50  tex->altitude = 0.0f;
51  tex->air_density = 1.0f;
52  tex->dust_density = 1.0f;
53  tex->ozone_density = 1.0f;
54  tex->sky_model = SHD_SKY_NISHITA;
55  node->storage = tex;
56 }
57 
58 typedef struct SkyModelPreetham {
59  float config_Y[5], config_x[5], config_y[5]; /* named after xyY color space */
60  float radiance[3];
62 
63 typedef struct XYZ_to_RGB /* transposed imbuf_xyz_to_rgb, passed as 3x vec3 */
64 {
65  float r[3], g[3], b[3];
67 
68 static float sky_perez_function(const float *lam, float theta, float gamma)
69 {
70  float ctheta = cosf(theta);
71  float cgamma = cosf(gamma);
72 
73  return (1.0 + lam[0] * expf(lam[1] / ctheta)) *
74  (1.0 + lam[2] * expf(lam[3] * gamma) + lam[4] * cgamma * cgamma);
75 }
76 
77 static void sky_precompute_old(SkyModelPreetham *sunsky, const float sun_angles[], float turbidity)
78 {
79  float theta = sun_angles[0];
80  float theta2 = theta * theta;
81  float theta3 = theta2 * theta;
82  float T = turbidity;
83  float T2 = T * T;
84  float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI - 2.0f * theta);
85 
86  sunsky->radiance[0] = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
87  sunsky->radiance[0] *= 0.06f;
88 
89  sunsky->radiance[1] = (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
90  (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) *
91  T +
92  (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
93 
94  sunsky->radiance[2] = (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
95  (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) *
96  T +
97  (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
98 
99  sunsky->config_Y[0] = (0.1787f * T - 1.4630f);
100  sunsky->config_Y[1] = (-0.3554f * T + 0.4275f);
101  sunsky->config_Y[2] = (-0.0227f * T + 5.3251f);
102  sunsky->config_Y[3] = (0.1206f * T - 2.5771f);
103  sunsky->config_Y[4] = (-0.0670f * T + 0.3703f);
104 
105  sunsky->config_x[0] = (-0.0193f * T - 0.2592f);
106  sunsky->config_x[1] = (-0.0665f * T + 0.0008f);
107  sunsky->config_x[2] = (-0.0004f * T + 0.2125f);
108  sunsky->config_x[3] = (-0.0641f * T - 0.8989f);
109  sunsky->config_x[4] = (-0.0033f * T + 0.0452f);
110 
111  sunsky->config_y[0] = (-0.0167f * T - 0.2608f);
112  sunsky->config_y[1] = (-0.0950f * T + 0.0092f);
113  sunsky->config_y[2] = (-0.0079f * T + 0.2102f);
114  sunsky->config_y[3] = (-0.0441f * T - 1.6537f);
115  sunsky->config_y[4] = (-0.0109f * T + 0.0529f);
116 
117  sunsky->radiance[0] /= sky_perez_function(sunsky->config_Y, 0, theta);
118  sunsky->radiance[1] /= sky_perez_function(sunsky->config_x, 0, theta);
119  sunsky->radiance[2] /= sky_perez_function(sunsky->config_y, 0, theta);
120 }
121 
123 {
125  data->r[0] = xyz_to_rgb[0];
126  data->r[1] = xyz_to_rgb[3];
127  data->r[2] = xyz_to_rgb[6];
128  data->g[0] = xyz_to_rgb[1];
129  data->g[1] = xyz_to_rgb[4];
130  data->g[2] = xyz_to_rgb[7];
131  data->b[0] = xyz_to_rgb[2];
132  data->b[1] = xyz_to_rgb[5];
133  data->b[2] = xyz_to_rgb[8];
134 }
135 
137  bNode *node,
138  bNodeExecData *UNUSED(execdata),
139  GPUNodeStack *in,
140  GPUNodeStack *out)
141 {
142  node_shader_gpu_default_tex_coord(mat, node, &in[0].link);
143  node_shader_gpu_tex_mapping(mat, node, in, out);
144  NodeTexSky *tex = (NodeTexSky *)node->storage;
145  float sun_angles[2]; /* [0]=theta=zenith angle [1]=phi=azimuth */
146  sun_angles[0] = acosf(tex->sun_direction[2]);
147  sun_angles[1] = atan2f(tex->sun_direction[0], tex->sun_direction[1]);
148 
149  if (tex->sky_model == 0) {
150  /* Preetham */
151  SkyModelPreetham sunsky;
152  sky_precompute_old(&sunsky, sun_angles, tex->turbidity);
155  return GPU_stack_link(mat,
156  node,
157  "node_tex_sky_preetham",
158  in,
159  out,
160  /* Pass config_Y/x/y as 3x(vec4+float) */
161  GPU_uniform(&sunsky.config_Y[0]),
162  GPU_uniform(&sunsky.config_Y[4]),
163  GPU_uniform(&sunsky.config_x[0]),
164  GPU_uniform(&sunsky.config_x[4]),
165  GPU_uniform(&sunsky.config_y[0]),
166  GPU_uniform(&sunsky.config_y[4]),
167  GPU_uniform(sun_angles),
168  GPU_uniform(sunsky.radiance),
172  }
173  if (tex->sky_model == 1) {
174  /* Hosek / Wilkie */
175  sun_angles[0] = fmin(M_PI_2, sun_angles[0]); /* clamp to horizon */
177  tex->turbidity, tex->ground_albedo, fmax(0.0, M_PI_2 - sun_angles[0]));
178  /* Pass sky_state->configs[3][9] as 3*(vec4+vec4)+vec3 */
179  float config_x07[8], config_y07[8], config_z07[8], config_xyz8[3];
180  for (int i = 0; i < 8; ++i) {
181  config_x07[i] = (float)sky_state->configs[0][i];
182  config_y07[i] = (float)sky_state->configs[1][i];
183  config_z07[i] = (float)sky_state->configs[2][i];
184  }
185  for (int i = 0; i < 3; ++i) {
186  config_xyz8[i] = (float)sky_state->configs[i][8];
187  }
188  float radiance[3];
189  for (int i = 0; i < 3; i++) {
190  radiance[i] = sky_state->radiances[i] * (2 * M_PI / 683);
191  }
195  return GPU_stack_link(mat,
196  node,
197  "node_tex_sky_hosekwilkie",
198  in,
199  out,
200  GPU_uniform(&config_x07[0]),
201  GPU_uniform(&config_x07[4]),
202  GPU_uniform(&config_y07[0]),
203  GPU_uniform(&config_y07[4]),
204  GPU_uniform(&config_z07[0]),
205  GPU_uniform(&config_z07[4]),
206  GPU_uniform(config_xyz8),
207  GPU_uniform(sun_angles),
208  GPU_uniform(radiance),
212  }
213 
214  return GPU_stack_link(mat, node, "node_tex_sky_nishita", in, out);
215 }
216 
218 {
219  bNodeSocket *sockVector = nodeFindSocket(node, SOCK_IN, "Vector");
220 
221  NodeTexSky *tex = (NodeTexSky *)node->storage;
222  nodeSetSocketAvailability(sockVector, !(tex->sky_model == 2 && tex->sun_disc == 1));
223 }
224 
225 /* node type definition */
227 {
228  static bNodeType ntype;
229 
230  sh_node_type_base(&ntype, SH_NODE_TEX_SKY, "Sky Texture", NODE_CLASS_TEXTURE, 0);
236  /* remove Vector input for Nishita */
238 
239  nodeRegisterType(&ntype);
240 }
typedef float(TangentPoint)[2]
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void nodeSetSocketAvailability(struct bNodeSocket *sock, bool is_available)
Definition: node.cc:3726
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4623
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:346
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_MIDDLE
Definition: BKE_node.h:372
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition: texture.c:252
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition: texture.c:354
#define M_PI_2
Definition: BLI_math_base.h:41
#define M_PI
Definition: BLI_math_base.h:38
#define DEG2RADF(_deg)
#define UNUSED(x)
#define N_(msgid)
#define SHD_SKY_NISHITA
@ SOCK_IN
@ SOCK_HIDE_VALUE
@ SOCK_NO_INTERNAL_LINK
@ SOCK_VECTOR
@ SOCK_RGBA
#define TEXMAP_TYPE_POINT
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
const float * IMB_colormangement_get_xyz_to_rgb(void)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image SH_NODE_TEX_SKY
@ PROP_NONE
Definition: RNA_types.h:113
OperationNode * node
bNodeTree * ntree
CCL_NAMESPACE_BEGIN ccl_device float3 xyz_to_rgb(KernelGlobals *kg, float3 xyz)
Definition: kernel_color.h:24
#define cosf(x)
#define expf(x)
#define tanf(x)
#define atan2f(x, y)
#define acosf(x)
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
#define T
static float sky_perez_function(const float *lam, float theta, float gamma)
static void node_shader_update_sky(bNodeTree *UNUSED(ntree), bNode *node)
static bNodeSocketTemplate sh_node_tex_sky_out[]
static void get_XYZ_to_RGB_for_gpu(XYZ_to_RGB *data)
struct XYZ_to_RGB XYZ_to_RGB
static void node_shader_init_tex_sky(bNodeTree *UNUSED(ntree), bNode *node)
static void sky_precompute_old(SkyModelPreetham *sunsky, const float sun_angles[], float turbidity)
static int node_shader_gpu_tex_sky(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static bNodeSocketTemplate sh_node_tex_sky_in[]
struct SkyModelPreetham SkyModelPreetham
void register_node_type_sh_tex_sky(void)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
void SKY_arhosekskymodelstate_free(SKY_ArHosekSkyModelState *state)
Definition: sky_model.cpp:290
SKY_ArHosekSkyModelState * SKY_arhosek_xyz_skymodelstate_alloc_init(const double turbidity, const double albedo, const double elevation)
Definition: sky_model.cpp:328
SKY_ArHosekSkyModelConfiguration configs[11]
Definition: sky_model.h:339
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
#define T2
Definition: util_md5.cpp:36