Blender V5.0
node_shader_volume_scatter.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
5#include "node_shader_util.hh"
6
7#include "BLI_string.h"
8
10#include "UI_resources.hh"
11
12#include "BKE_node_runtime.hh"
13
15
17{
18 b.add_input<decl::Color>("Color").default_value({0.8f, 0.8f, 0.8f, 1.0f});
19#define SOCK_COLOR_ID 0
20 b.add_input<decl::Float>("Density").default_value(1.0f).min(0.0f).max(1000.0f);
21#define SOCK_DENSITY_ID 1
22 b.add_input<decl::Float>("Anisotropy")
23 .default_value(0.0f)
24 .min(-1.0f)
25 .max(1.0f)
27 .description(
28 "Directionality of the scattering. Zero is isotropic, negative is backward, "
29 "positive is forward");
30 b.add_input<decl::Float>("IOR")
31 .default_value(1.33f)
32 .min(1.0f)
33 .max(2.0f)
35 .description("Index Of Refraction of the scattering particles");
36 b.add_input<decl::Float>("Backscatter")
37 .default_value(0.1f)
38 .min(0.0f)
39 .max(0.5f)
41 .description("Fraction of light that is scattered backwards");
42 b.add_input<decl::Float>("Alpha").default_value(0.5f).min(0.0f).max(500.0f);
43 b.add_input<decl::Float>("Diameter")
44 .default_value(20.0f)
45 .min(0.0f)
46 .max(50.0f)
47 .description("Diameter of the water droplets, in micrometers");
48 b.add_input<decl::Float>("Weight").available(false);
49 b.add_output<decl::Shader>("Volume").translation_context(BLT_I18NCONTEXT_ID_ID);
50}
51
53{
54 layout->prop(ptr, "phase", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
55}
56
57static void node_shader_init_scatter(bNodeTree * /*ntree*/, bNode *node)
58{
60}
61
62static void node_shader_update_scatter(bNodeTree *ntree, bNode *node)
63{
64 const int phase_function = node->custom1;
65
66 LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
67 if (STR_ELEM(sock->name, "IOR", "Backscatter")) {
69 *ntree, *sock, phase_function == SHD_PHASE_FOURNIER_FORAND);
70 }
71 else if (STR_ELEM(sock->name, "Anisotropy")) {
73 *ntree, *sock, ELEM(phase_function, SHD_PHASE_HENYEY_GREENSTEIN, SHD_PHASE_DRAINE));
74 }
75 else if (STR_ELEM(sock->name, "Alpha")) {
76 bke::node_set_socket_availability(*ntree, *sock, phase_function == SHD_PHASE_DRAINE);
77 }
78 else if (STR_ELEM(sock->name, "Diameter")) {
79 bke::node_set_socket_availability(*ntree, *sock, phase_function == SHD_PHASE_MIE);
80 }
81 }
82}
83
85 bNode *node,
86 bNodeExecData * /*execdata*/,
89{
91 /* Consider there is absorption phenomenon when there is scattering since
92 * `extinction = scattering + absorption`. */
94 }
95 return GPU_stack_link(mat, node, "node_volume_scatter", in, out);
96}
97
98#undef SOCK_COLOR_ID
99#undef SOCK_DENSITY_ID
100
101} // namespace blender::nodes::node_shader_volume_scatter_cc
102
103/* node type definition */
105{
107
108 static blender::bke::bNodeType ntype;
109
110 sh_node_type_base(&ntype, "ShaderNodeVolumeScatter", SH_NODE_VOLUME_SCATTER);
111 ntype.ui_name = "Volume Scatter";
112 ntype.ui_description =
113 "Scatter light as it passes through the volume, often used to add fog to a scene";
114 ntype.enum_name_legacy = "VOLUME_SCATTER";
116 ntype.declare = file_ns::node_declare;
118 ntype.draw_buttons = file_ns::node_shader_buts_scatter;
120 ntype.initfunc = file_ns::node_shader_init_scatter;
121 ntype.gpu_fn = file_ns::node_shader_gpu_volume_scatter;
122 ntype.updatefunc = file_ns::node_shader_update_scatter;
123
125}
#define NODE_CLASS_SHADER
Definition BKE_node.hh:460
#define SH_NODE_VOLUME_SCATTER
#define LISTBASE_FOREACH(type, var, list)
#define STR_ELEM(...)
Definition BLI_string.h:661
#define ELEM(...)
#define BLT_I18NCONTEXT_ID_ID
@ SHD_PHASE_DRAINE
@ SHD_PHASE_HENYEY_GREENSTEIN
@ SHD_PHASE_MIE
@ SHD_PHASE_FOURNIER_FORAND
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ GPU_MATFLAG_VOLUME_SCATTER
@ GPU_MATFLAG_VOLUME_ABSORPTION
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
@ PROP_FACTOR
Definition RNA_types.hh:251
@ UI_ITEM_R_SPLIT_EMPTY_NAME
#define in
#define out
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:4739
void node_type_size_preset(bNodeType &ntype, eNodeSizePreset size)
Definition node.cc:5396
static int node_shader_gpu_volume_scatter(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_scatter(bNodeTree *ntree, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_shader_init_scatter(bNodeTree *, bNode *node)
static void node_shader_buts_scatter(uiLayout *layout, bContext *, PointerRNA *ptr)
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
bool object_shader_nodes_poll(const bContext *C)
bool node_socket_not_black(const GPUNodeStack &socket)
bool node_socket_not_zero(const GPUNodeStack &socket)
#define SOCK_DENSITY_ID
#define SOCK_COLOR_ID
void register_node_type_sh_volume_scatter()
int16_t custom1
ListBase inputs
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
bool(* add_ui_poll)(const bContext *C)
Definition BKE_node.hh:310
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:281
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238