Blender V5.3
node_shader_volume_principled.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
12
13namespace blender {
14
16
18{
19 const bNodeTree *ntree = b.tree_or_null();
20 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
21
22 b.add_input<decl::Color>("Color"_ustr).default_value({0.5f, 0.5f, 0.5f, 1.0f});
23#define SOCK_COLOR_ID 0
24 b.add_input<decl::String>("Color Attribute"_ustr);
25#define SOCK_COLOR_ATTR_ID 1
26 b.add_input<decl::Float>("Density"_ustr).default_value(1.0f).min(0.0f).max(1000.0f);
27#define SOCK_DENSITY_ID 2
28 b.add_input<decl::String>("Density Attribute"_ustr).default_value("density");
29#define SOCK_DENSITY_ATTR_ID 3
30 b.add_input<decl::Float>("Anisotropy"_ustr)
31 .default_value(0.0f)
32 .min(-1.0f)
33 .max(1.0f)
35#define SOCK_ANISOTROPY_ID 4
36 b.add_input<decl::Color>("Absorption Color"_ustr).default_value({0.0f, 0.0f, 0.0f, 1.0f});
37#define SOCK_ABSORPTION_COLOR_ID 5
38 b.add_input<decl::Float>("Emission Strength"_ustr).default_value(0.0f).min(0.0f).max(1000.0f);
39#define SOCK_EMISSION_ID 6
40 b.add_input<decl::Color>("Emission Color"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
41#define SOCK_EMISSION_COLOR_ID 7
42 b.add_input<decl::Float>("Blackbody Intensity"_ustr)
43 .default_value(0.0f)
44 .min(0.0f)
45 .max(1.0f)
47#define SOCK_BLACKBODY_INTENSITY_ID 8
48 b.add_input<decl::Color>("Blackbody Tint"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
49#define SOCK_BLACKBODY_TINT_ID 8
50 b.add_input<decl::Float>("Temperature"_ustr)
51 .default_value(1000.0f)
52 .min(0.0f)
53 .max(6500.0f)
55 b.add_input<decl::String>("Temperature Attribute"_ustr).default_value("temperature");
56 b.add_input<decl::Float>("Weight"_ustr).available(is_gpu_internal);
57 b.add_output<decl::Shader>("Volume"_ustr).translation_context(BLT_I18NCONTEXT_ID_ID);
58}
59
61 const char *attribute_name,
62 GPUNodeLink **attribute_link)
63{
64 if (STREQ(attribute_name, "color")) {
65 GPU_link(mat, "node_attribute_color", GPU_kernel_globals(), *attribute_link, attribute_link);
66 }
67 else if (STREQ(attribute_name, "temperature")) {
69 mat, "node_attribute_temperature", GPU_kernel_globals(), *attribute_link, attribute_link);
70 }
71}
72
74 bNode *node,
75 bNodeExecData * /*execdata*/,
78{
79 /* Test if blackbody intensity is enabled. */
80 bool use_blackbody = in[SOCK_BLACKBODY_INTENSITY_ID].socket_not_zero();
81
82 if (in[SOCK_DENSITY_ID].socket_not_zero() && in[SOCK_COLOR_ID].socket_not_black()) {
83 /* Consider there is absorption phenomenon when there is scattering since
84 * `extinction = scattering + absorption`. */
86 }
87 if (in[SOCK_DENSITY_ID].socket_not_zero() && in[SOCK_ABSORPTION_COLOR_ID].socket_not_white()) {
89 }
90
91 /* Get volume attributes. */
92 GPUNodeLink *density = nullptr, *color = nullptr, *temperature = nullptr;
93
94 for (bNodeSocket &sock : node->inputs) {
95 if (sock.typeinfo->type != SOCK_STRING) {
96 continue;
97 }
98
100 const char *attribute_name = value->value;
101 if (attribute_name[0] == '\0') {
102 continue;
103 }
104
105 if (STREQ(sock.name, "Density Attribute")) {
106 density = GPU_attribute_with_default(mat, CD_AUTO_FROM_NAME, attribute_name, GPU_DEFAULT_1);
107 attribute_post_process(mat, attribute_name, &density);
108 }
109 else if (STREQ(sock.name, "Color Attribute")) {
111 attribute_post_process(mat, attribute_name, &color);
112 }
113 else if (use_blackbody && STREQ(sock.name, "Temperature Attribute")) {
114 temperature = GPU_attribute(mat, CD_AUTO_FROM_NAME, attribute_name);
115 attribute_post_process(mat, attribute_name, &temperature);
116 }
117 }
118
119 /* Default values if attributes not found. */
120 static const float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
121 if (!density) {
122 density = GPU_constant(white);
123 }
124 if (!color) {
125 color = GPU_constant(white);
126 }
127 if (!temperature) {
128 temperature = GPU_constant(white);
129 }
130
131 /* Create blackbody spectrum. */
132 const int size = CM_TABLE + 1;
133 float *data, layer;
134 if (use_blackbody) {
135 data = MEM_new_array_uninitialized<float>(size * 4, "blackbody texture");
137 }
138 else {
139 data = MEM_new_array_zeroed<float>(size * 4, "blackbody black");
140 }
141 GPUNodeLink *spectrummap = GPU_color_band(mat, size, data, &layer);
142
143 return GPU_stack_link(mat,
144 node,
145 "node_volume_principled",
146 in,
147 out,
148 density,
149 color,
150 temperature,
151 spectrummap,
155}
156
157#undef SOCK_COLOR_ID
158#undef SOCK_COLOR_ATTR_ID
159#undef SOCK_DENSITY_ID
160#undef SOCK_DENSITY_ATTR_ID
161#undef SOCK_ANISOTROPY_ID
162#undef SOCK_ABSORPTION_COLOR_ID
163#undef SOCK_EMISSION_ID
164#undef SOCK_EMISSION_COLOR_ID
165#undef SOCK_BLACKBODY_INTENSITY_ID
166#undef SOCK_BLACKBODY_TINT_ID
167
168} // namespace nodes::node_shader_volume_principled_cc
169
170/* node type definition */
172{
173 namespace file_ns = nodes::node_shader_volume_principled_cc;
174
175 static bke::bNodeType ntype;
176
177 sh_node_type_base(&ntype, "ShaderNodeVolumePrincipled"_ustr, SH_NODE_VOLUME_PRINCIPLED);
178 ntype.ui_name = "Principled Volume";
179 ntype.ui_description = "Combine all volume shading components into a single easy to use node";
180 ntype.enum_name_legacy = "PRINCIPLED_VOLUME";
182 ntype.declare = file_ns::node_declare;
185 ntype.gpu_fn = file_ns::node_shader_gpu_volume_principled;
186
188}
189
190} // namespace blender
#define NODE_CLASS_SHADER
Definition BKE_node.hh:491
#define SH_NODE_VOLUME_PRINCIPLED
#define BLT_I18NCONTEXT_ID_ID
#define CM_TABLE
#define STREQ(a, b)
#define layer
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static int node_shader_gpu_volume_principled(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void attribute_post_process(GPUMaterial *mat, const char *attribute_name, GPUNodeLink **attribute_link)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
GPUNodeLink * GPU_attribute_with_default(GPUMaterial *mat, eCustomDataType type, const char *name, GPUDefaultValue default_value)
void register_node_type_sh_volume_principled()
@ GPU_MATFLAG_VOLUME_ABSORPTION
@ GPU_MATFLAG_VOLUME_SCATTER
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
BMesh const char void * data
void search_link_ops_for_shader_bsdf_node(nodes::GatherLinkSearchOpParams &params)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
GPUNodeLink * GPU_shading_data()
void IMB_colormanagement_blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max)
@ NTREE_IS_GPU_SHADER_INTERNAL
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_COLOR_TEMPERATURE
Definition RNA_types.hh:347
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define SOCK_DENSITY_ID
#define SOCK_COLOR_ID
#define SOCK_ABSORPTION_COLOR_ID
#define SOCK_BLACKBODY_INTENSITY_ID
bke::bNodeSocketType * typeinfo
eNodeTree_Flag flag
static constexpr int _240
Definition BKE_node.hh:250
eNodeSocketDatatype type
Definition BKE_node.hh:198
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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