Blender  V3.3
node_texture_output.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2006 Blender Foundation. All rights reserved. */
3 
8 #include "BLI_string.h"
9 
10 #include "NOD_texture.h"
11 #include "node_texture_util.h"
12 
13 /* **************** COMPOSITE ******************** */
15  {SOCK_RGBA, N_("Color"), 0.0f, 0.0f, 0.0f, 1.0f},
16  {-1, ""},
17 };
18 
19 /* applies to render pipeline */
20 static void exec(void *data,
21  int UNUSED(thread),
22  bNode *node,
23  bNodeExecData *UNUSED(execdata),
24  bNodeStack **in,
26 {
27  TexCallData *cdata = (TexCallData *)data;
28  TexResult *target = cdata->target;
29 
30  if (cdata->do_preview) {
32  params_from_cdata(&params, cdata);
33 
34  tex_input_rgba(target->trgba, in[0], &params, cdata->thread);
35  }
36  else {
37  /* 0 means don't care, so just use first */
38  if (cdata->which_output == node->custom1 || (cdata->which_output == 0 && node->custom1 == 1)) {
40  params_from_cdata(&params, cdata);
41 
42  tex_input_rgba(target->trgba, in[0], &params, cdata->thread);
43 
44  target->tin = (target->trgba[0] + target->trgba[1] + target->trgba[2]) / 3.0f;
45  target->talpha = true;
46  }
47  }
48 }
49 
50 static void unique_name(bNode *node)
51 {
52  TexNodeOutput *tno = (TexNodeOutput *)node->storage;
53  char new_name[sizeof(tno->name)];
54  int new_len = 0;
55  int suffix;
56  bNode *i;
57  const char *name = tno->name;
58 
59  new_name[0] = '\0';
60  i = node;
61  while (i->prev) {
62  i = i->prev;
63  }
64  for (; i; i = i->next) {
65  if (i == node || i->type != TEX_NODE_OUTPUT ||
66  !STREQ(name, ((TexNodeOutput *)(i->storage))->name)) {
67  continue;
68  }
69 
70  if (new_name[0] == '\0') {
71  int len = strlen(name);
72  if (len >= 4 && sscanf(name + len - 4, ".%03d", &suffix) == 1) {
73  new_len = len;
74  }
75  else {
76  suffix = 0;
77  new_len = len + 4;
78  if (new_len > (sizeof(tno->name) - 1)) {
79  new_len = (sizeof(tno->name) - 1);
80  }
81  }
82 
83  BLI_strncpy(new_name, name, sizeof(tno->name));
84  name = new_name;
85  }
86  sprintf(new_name + new_len - 4, ".%03d", ++suffix);
87  }
88 
89  if (new_name[0] != '\0') {
90  BLI_strncpy(tno->name, new_name, sizeof(tno->name));
91  }
92 }
93 
94 static void assign_index(struct bNode *node)
95 {
96  bNode *tnode;
97  int index = 1;
98 
99  tnode = node;
100  while (tnode->prev) {
101  tnode = tnode->prev;
102  }
103 
104 check_index:
105  for (; tnode; tnode = tnode->next) {
106  if (tnode->type == TEX_NODE_OUTPUT && tnode != node) {
107  if (tnode->custom1 == index) {
108  index++;
109  goto check_index;
110  }
111  }
112  }
113 
114  node->custom1 = index;
115 }
116 
118 {
119  TexNodeOutput *tno = MEM_callocN(sizeof(TexNodeOutput), "TEX_output");
120  node->storage = tno;
121 
122  strcpy(tno->name, "Default");
123  unique_name(node);
125 }
126 
127 static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
128 {
129  node_copy_standard_storage(dest_ntree, dest_node, src_node);
130  unique_name(dest_node);
131  assign_index(dest_node);
132 }
133 
135 {
136  static bNodeType ntype;
137 
141  node_type_init(&ntype, init);
142  node_type_storage(&ntype, "TexNodeOutput", node_free_standard_storage, copy);
143  node_type_exec(&ntype, NULL, NULL, exec);
144 
145  ntype.flag |= NODE_PREVIEW;
146  ntype.no_muting = true;
147 
148  nodeRegisterType(&ntype);
149 }
#define NODE_CLASS_OUTPUT
Definition: BKE_node.h:346
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4358
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4408
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:4426
#define TEX_NODE_OUTPUT
Definition: BKE_node.h:1347
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4455
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
@ NODE_SIZE_MIDDLE
Definition: BKE_node.h:366
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED(x)
#define STREQ(a, b)
#define NODE_PREVIEW
@ SOCK_RGBA
Definition: thread.h:34
OperationNode * node
int len
Definition: draw_manager.c:108
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static void assign_index(struct bNode *node)
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
void register_node_type_tex_output(void)
static void unique_name(bNode *node)
static bNodeSocketTemplate inputs[]
static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *UNUSED(execdata), bNodeStack **in, bNodeStack **UNUSED(out))
static void init(bNodeTree *UNUSED(ntree), bNode *node)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
void tex_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass)
void params_from_cdata(TexParams *out, TexCallData *in)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:55
void node_free_standard_storage(bNode *node)
Definition: node_util.c:43
TexResult * target
int talpha
Definition: RE_texture.h:89
float tin
Definition: RE_texture.h:86
float trgba[4]
Definition: RE_texture.h:87
Compact definition of a node socket.
Definition: BKE_node.h:84
Defines a node type.
Definition: BKE_node.h:226
short flag
Definition: BKE_node.h:236
bool no_muting
Definition: BKE_node.h:338
short custom1
struct bNode * prev
short type
struct bNode * next
void * storage
#define N_(msgid)