Blender  V2.93
node_shader_volume_principled.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 
22 /* **************** OUTPUT ******************** */
23 
25  {SOCK_RGBA, N_("Color"), 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f},
26  {SOCK_STRING, N_("Color Attribute"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
27  {SOCK_FLOAT, N_("Density"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
28  {SOCK_STRING, N_("Density Attribute"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
29  {SOCK_FLOAT, N_("Anisotropy"), 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, PROP_FACTOR},
30  {SOCK_RGBA, N_("Absorption Color"), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
31  {SOCK_FLOAT, N_("Emission Strength"), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1000.0f},
32  {SOCK_RGBA, N_("Emission Color"), 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
33  {SOCK_FLOAT, N_("Blackbody Intensity"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
34  {SOCK_RGBA, N_("Blackbody Tint"), 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
35  {SOCK_FLOAT, N_("Temperature"), 1000.0f, 0.0f, 0.0f, 0.0f, 0.0f, 6500.0f},
36  {SOCK_STRING, N_("Temperature Attribute"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
37  {-1, ""},
38 };
39 
41  {SOCK_SHADER, N_("Volume")},
42  {-1, ""},
43 };
44 
46 {
47  LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
48  if (STREQ(sock->name, "Density Attribute")) {
49  strcpy(((bNodeSocketValueString *)sock->default_value)->value, "density");
50  }
51  else if (STREQ(sock->name, "Temperature Attribute")) {
52  strcpy(((bNodeSocketValueString *)sock->default_value)->value, "temperature");
53  }
54  }
55 }
56 
58  bNode *node,
59  bNodeExecData *UNUSED(execdata),
60  GPUNodeStack *in,
61  GPUNodeStack *out)
62 {
63  /* Test if blackbody intensity is enabled. */
64  bool use_blackbody = (in[8].link || in[8].vec[0] != 0.0f);
65 
66  /* Get volume attributes. */
67  GPUNodeLink *density = NULL, *color = NULL, *temperature = NULL;
68 
69  LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
70  if (sock->typeinfo->type != SOCK_STRING) {
71  continue;
72  }
73 
74  bNodeSocketValueString *value = sock->default_value;
75  const char *attribute_name = value->value;
76  if (attribute_name[0] == '\0') {
77  continue;
78  }
79 
80  if (STREQ(sock->name, "Density Attribute")) {
81  density = GPU_volume_grid(mat, attribute_name, GPU_VOLUME_DEFAULT_1);
82  }
83  else if (STREQ(sock->name, "Color Attribute")) {
84  color = GPU_volume_grid(mat, attribute_name, GPU_VOLUME_DEFAULT_1);
85  }
86  else if (use_blackbody && STREQ(sock->name, "Temperature Attribute")) {
87  temperature = GPU_volume_grid(mat, attribute_name, GPU_VOLUME_DEFAULT_0);
88  }
89  }
90 
91  /* Default values if attributes not found. */
92  if (!density) {
93  static float one = 1.0f;
94  density = GPU_constant(&one);
95  }
96  if (!color) {
97  static float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
98  color = GPU_constant(white);
99  }
100  if (!temperature) {
101  static float one = 1.0f;
102  temperature = GPU_constant(&one);
103  }
104 
105  /* Create blackbody spectrum. */
106  const int size = CM_TABLE + 1;
107  float *data, layer;
108  if (use_blackbody) {
109  data = MEM_mallocN(sizeof(float) * size * 4, "blackbody texture");
110  blackbody_temperature_to_rgb_table(data, size, 965.0f, 12000.0f);
111  }
112  else {
113  data = MEM_callocN(sizeof(float) * size * 4, "blackbody black");
114  }
115  GPUNodeLink *spectrummap = GPU_color_band(mat, size, data, &layer);
116 
117  return GPU_stack_link(mat,
118  node,
119  "node_volume_principled",
120  in,
121  out,
122  density,
123  color,
124  temperature,
125  spectrummap,
126  GPU_constant(&layer));
127 }
128 
129 /* node type definition */
131 {
132  static bNodeType ntype;
133 
134  sh_node_type_base(&ntype, SH_NODE_VOLUME_PRINCIPLED, "Principled Volume", NODE_CLASS_SHADER, 0);
138  node_type_storage(&ntype, "", NULL, NULL);
140 
141  nodeRegisterType(&ntype);
142 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
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
#define NODE_CLASS_SHADER
Definition: BKE_node.h:358
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max)
Definition: math_color.c:704
#define UNUSED(x)
#define STREQ(a, b)
#define N_(msgid)
#define CM_TABLE
@ SOCK_SHADER
@ SOCK_FLOAT
@ SOCK_STRING
@ SOCK_RGBA
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_volume_grid(GPUMaterial *mat, const char *name, eGPUVolumeDefaultValue default_value)
@ GPU_VOLUME_DEFAULT_0
Definition: GPU_material.h:133
@ GPU_VOLUME_DEFAULT_1
Definition: GPU_material.h:134
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume SH_NODE_VOLUME_PRINCIPLED
@ PROP_FACTOR
Definition: RNA_types.h:131
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
static bNodeSocketTemplate sh_node_volume_principled_out[]
static void node_shader_init_volume_principled(bNodeTree *UNUSED(ntree), bNode *node)
static int node_shader_gpu_volume_principled(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static bNodeSocketTemplate sh_node_volume_principled_in[]
void register_node_type_sh_volume_principled(void)
struct GPUNodeLink * link
Definition: GPU_material.h:119
float vec[4]
Definition: GPU_material.h:118
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221