Blender V5.3
node_texture_util.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/*
10 * HOW TEXTURE NODES WORK
11 *
12 * In contrast to Shader nodes, which place a color into the output
13 * stack when executed, Texture nodes place a TexDelegate* there. To
14 * obtain a color value from this, a node further up the chain reads
15 * the TexDelegate* from its input stack, and uses tex_call_delegate to
16 * retrieve the color from the delegate.
17 *
18 * comments: (ton)
19 *
20 * This system needs recode, a node system should rely on the stack, and
21 * callbacks for nodes only should evaluate their own node, not recursively go
22 * over other previous ones.
23 */
24
25#include "BLI_listbase.hh"
26
27#include "BKE_node_runtime.hh"
28
29#include "NOD_texture.h"
30
31#include "node_texture_util.hh"
32#include "node_util.hh"
33#include <optional>
34
35namespace blender {
36
38 const bNodeTree *ntree,
39 const char **r_disabled_hint)
40{
41 if (!STREQ(ntree->idname, "TextureNodeTree")) {
42 *r_disabled_hint = RPT_("Not a texture node tree");
43 return false;
44 }
45 return true;
46}
47
49 UString idname,
50 const std::optional<int16_t> legacy_type)
51{
52 bke::node_type_base(*ntype, idname, legacy_type);
53
56}
57
58static void tex_call_delegate(TexDelegate *dg, float *out, TexParams *params, short thread)
59{
60 if (dg->node->runtime->need_exec) {
61 dg->fn(out, params, dg->node, dg->in, thread);
62 }
63}
64
65static void tex_input(float *out, int num, bNodeStack *in, TexParams *params, short thread)
66{
67 TexDelegate *dg = static_cast<TexDelegate *>(in->data);
68 if (dg) {
69 tex_call_delegate(dg, in->vec, params, thread);
70
71 if (in->hasoutput && in->sockettype == SOCK_FLOAT) {
72 in->vec[1] = in->vec[2] = in->vec[0];
73 }
74 }
75 memcpy(out, in->vec, num * sizeof(float));
76}
77
78void tex_input_vec(float *out, bNodeStack *in, TexParams *params, short thread)
79{
80 tex_input(out, 3, in, params, thread);
81}
82
83void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
84{
85 tex_input(out, 4, in, params, thread);
86
87 if (in->hasoutput && in->sockettype == SOCK_FLOAT) {
88 out[1] = out[2] = out[0];
89 out[3] = 1;
90 }
91
92 if (in->hasoutput && in->sockettype == SOCK_VECTOR) {
93 out[0] = out[0] * 0.5f + 0.5f;
94 out[1] = out[1] * 0.5f + 0.5f;
95 out[2] = out[2] * 0.5f + 0.5f;
96 out[3] = 1;
97 }
98}
99
101{
102 float out[4];
103 tex_input_vec(out, in, params, thread);
104 return out[0];
105}
106
108{
109 out->co = in->co;
110 out->previewco = in->co;
111 out->cfra = in->cfra;
112 out->mtex = in->mtex;
113}
114
116 bNodeExecData * /*execdata*/,
117 bNodeStack **in,
119 TexFn texfn,
120 TexCallData *cdata)
121{
122 TexDelegate *dg;
123
124 if (node->is_muted()) {
125 /* do not add a delegate if the node is muted */
126 return;
127 }
128
129 if (!out->data) {
130 /* Freed in tex_end_exec (node.cc) */
131 dg = MEM_new_zeroed<TexDelegate>("tex delegate");
132 out->data = dg;
133 }
134 else {
135 dg = static_cast<TexDelegate *>(out->data);
136 }
137
138 dg->cdata = cdata;
139 dg->fn = texfn;
140 dg->node = node;
141 memcpy(dg->in, in, MAX_SOCKET * sizeof(bNodeStack *));
142 dg->type = out->sockettype;
143}
144
146{
147 for (bNode &node : ntree->nodes) {
148
149 if (node.type_legacy == TEX_NODE_TEXTURE && node.id) {
150 /* custom2 stops the node from rendering */
151 if (node.custom1) {
152 node.custom2 = 1;
153 node.custom1 = 0;
154 }
155 else {
156 Tex *tex = id_cast<Tex *>(node.id);
157
158 node.custom2 = 0;
159
160 node.custom1 = 1;
161 if (tex->use_nodes && tex->nodetree) {
163 }
164 node.custom1 = 0;
165 }
166 }
167 }
168}
169
170} // namespace blender
#define MAX_SOCKET
Definition BKE_node.hh:30
#define TEX_NODE_TEXTURE
#define RPT_(msgid)
#define STREQ(a, b)
void node_type_base(bNodeType &ntype, UString idname, std::optional< int16_t > legacy_type=std::nullopt)
Definition node.cc:5866
BMesh Iterators.
void tex_input_vec(float *out, bNodeStack *in, TexParams *params, short thread)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
void(*)(float *out, TexParams *params, bNode *node, bNodeStack **in, short thread) TexFn
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void tex_output(bNode *node, bNodeExecData *, bNodeStack **in, bNodeStack *out, TexFn texfn, TexCallData *cdata)
void params_from_cdata(TexParams *out, TexCallData *in)
void tex_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool node_insert_link_default(bke::NodeInsertLinkParams &)
Definition node_util.cc:223
ATTR_WARN_UNUSED_RESULT const size_t num
bool tex_node_poll_default(const bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
void ntreeTexCheckCyclics(struct bNodeTree *ntree)
float tex_input_value(bNodeStack *in, TexParams *params, short thread)
static void tex_call_delegate(TexDelegate *dg, float *out, TexParams *params, short thread)
static void tex_input(float *out, int num, bNodeStack *in, TexParams *params, short thread)
bNodeStack * in[MAX_SOCKET]
struct bNodeTree * nodetree
ListBaseT< bNode > nodes
bke::bNodeRuntime * runtime
Defines a node type.
Definition BKE_node.hh:265
bool(*) poll(const bNodeType *ntype, const bNodeTree *nodetree, const char **r_disabled_hint)
Definition BKE_node.hh:352
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364