Blender V5.3
node_texture_output.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "BLI_string.hh"
12
13#include "node_texture_util.hh"
14#include "node_util.hh"
15
16namespace blender {
17
18/* **************** COMPOSITE ******************** */
20 {SOCK_RGBA, N_("Color"), 0.0f, 0.0f, 0.0f, 1.0f},
21 {-1, ""},
22};
23
24/* applies to render pipeline */
25static void exec(void *data,
26 int /*thread*/,
27 bNode *node,
28 bNodeExecData * /*execdata*/,
29 bNodeStack **in,
30 bNodeStack ** /*out*/)
31{
32 TexCallData *cdata = static_cast<TexCallData *>(data);
33 TexResult *target = cdata->target;
34
35 if (cdata->do_preview) {
36 TexParams params;
37 params_from_cdata(&params, cdata);
38
39 tex_input_rgba(target->trgba, in[0], &params, cdata->thread);
40 }
41 else {
42 /* 0 means don't care, so just use first */
43 if (cdata->which_output == node->custom1 || (cdata->which_output == 0 && node->custom1 == 1)) {
44 TexParams params;
45 params_from_cdata(&params, cdata);
46
47 tex_input_rgba(target->trgba, in[0], &params, cdata->thread);
48
49 target->tin = (target->trgba[0] + target->trgba[1] + target->trgba[2]) / 3.0f;
50 target->talpha = true;
51 }
52 }
53}
54
55static void unique_name(bNode *node)
56{
57 TexNodeOutput *tno = static_cast<TexNodeOutput *>(node->storage);
58 char new_name[sizeof(tno->name)];
59 int new_len = 0;
60 int suffix;
61 bNode *i;
62 const char *name = tno->name;
63
64 new_name[0] = '\0';
65 i = node;
66 while (i->prev) {
67 i = i->prev;
68 }
69 for (; i; i = i->next) {
70 if (i == node || i->type_legacy != TEX_NODE_OUTPUT ||
71 !STREQ(name, ((TexNodeOutput *)(i->storage))->name))
72 {
73 continue;
74 }
75
76 if (new_name[0] == '\0') {
77 int len = strlen(name);
78 if (len >= 4 && sscanf(name + len - 4, ".%03d", &suffix) == 1) {
79 new_len = len;
80 }
81 else {
82 suffix = 0;
83 new_len = len + 4;
84 new_len = std::min<ulong>(new_len, sizeof(tno->name) - 1);
85 }
86
87 STRNCPY(new_name, name);
88 name = new_name;
89 }
90 int name_ofs = new_len - 4;
91 BLI_snprintf(new_name + name_ofs, sizeof(new_name) - name_ofs, ".%03d", ++suffix);
92 }
93
94 if (new_name[0] != '\0') {
95 STRNCPY(tno->name, new_name);
96 }
97}
98
99static void assign_index(bNode *node)
100{
101 bNode *tnode;
102 int index = 1;
103
104 tnode = node;
105 while (tnode->prev) {
106 tnode = tnode->prev;
107 }
108
109check_index:
110 for (; tnode; tnode = tnode->next) {
111 if (tnode->type_legacy == TEX_NODE_OUTPUT && tnode != node) {
112 if (tnode->custom1 == index) {
113 index++;
114 goto check_index;
115 }
116 }
117 }
118
119 node->custom1 = index;
120}
121
122static void init(bNodeTree * /*ntree*/, bNode *node)
123{
124 TexNodeOutput *tno = MEM_new<TexNodeOutput>("TEX_output");
125 node->storage = tno;
126
127 STRNCPY(tno->name, "Default");
130}
131
132static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
133{
134 node_copy_standard_storage(dest_ntree, dest_node, src_node);
135 unique_name(dest_node);
136 assign_index(dest_node);
137}
138
140{
141 static bke::bNodeType ntype;
142
143 tex_node_type_base(&ntype, "TextureNodeOutput"_ustr, TEX_NODE_OUTPUT);
144 ntype.ui_name = "Output";
145 ntype.enum_name_legacy = "OUTPUT";
147 bke::node_type_socket_templates(&ntype, inputs, nullptr);
149 ntype.initfunc = init;
150 bke::node_type_storage(ntype, "TexNodeOutput", node_free_standard_storage, copy);
151 ntype.exec_fn = exec;
152
153 ntype.flag |= NODE_PREVIEW;
154 ntype.no_muting = true;
155
157}
158
159} // namespace blender
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:479
#define TEX_NODE_OUTPUT
#define STREQ(a, b)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_socket_templates(bNodeType *ntype, bNodeSocketTemplate *inputs, bNodeSocketTemplate *outputs)
Definition node.cc:6134
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
BMesh Iterators.
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
void register_node_type_tex_output()
BMesh const char void * data
void params_from_cdata(TexParams *out, TexCallData *in)
static bke::bNodeSocketTemplate inputs[]
void tex_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static void assign_index(bNode *node)
static void unique_name(bNode *node)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
static void exec(void *data, int, bNode *node, bNodeExecData *, bNodeStack **in, bNodeStack **)
struct bNode * prev
struct bNode * next
static constexpr int _160
Definition BKE_node.hh:246
Compact definition of a node socket.
Definition BKE_node.hh:102
Defines a node type.
Definition BKE_node.hh:265
NodeExecFunction exec_fn
Definition BKE_node.hh:371
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
#define N_(msgid)