Blender V4.3
node_fn_hash_value.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_hash.h"
8#include "BLI_noise.hh"
9
10#include "NOD_rna_define.hh"
11#include "NOD_socket.hh"
13
14#include "RNA_enum_types.hh"
15
16#include "node_function_util.hh"
17
18#include "UI_interface.hh"
19#include "UI_resources.hh"
20
22
24{
25 b.is_function_node();
26
27 const bNode *node = b.node_or_null();
28 if (node) {
29 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
30 b.add_input(data_type, "Value");
31 }
32 b.add_input<decl::Int>("Seed", "Seed");
33 b.add_output<decl::Int>("Hash");
34}
35
36static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
37{
38 uiItemR(layout, ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
39}
40
41static void node_init(bNodeTree * /*tree*/, bNode *node)
42{
43 node->custom1 = SOCK_INT;
44}
45
46static const mf::MultiFunction *get_multi_function(const bNode &bnode)
47{
48 const eNodeSocketDatatype socket_type = static_cast<eNodeSocketDatatype>(bnode.custom1);
49
50 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
51
52 static auto fn_hash_float = mf::build::SI2_SO<float, int, int>(
53 "Hash Float",
54 [](float a, int seed) { return noise::hash(noise::hash_float(a), seed); },
55 exec_preset);
56 static auto fn_hash_vector = mf::build::SI2_SO<float3, int, int>(
57 "Hash Vector",
58 [](float3 a, int seed) { return noise::hash(noise::hash_float(a), seed); },
59 exec_preset);
60 static auto fn_hash_color = mf::build::SI2_SO<ColorGeometry4f, int, int>(
61 "Hash Color",
62 [](ColorGeometry4f a, int seed) { return noise::hash(noise::hash_float(float4(a)), seed); },
63 exec_preset);
64 static auto fn_hash_int = mf::build::SI2_SO<int, int, int>(
65 "Hash Integer",
66 [](int a, int seed) { return noise::hash(noise::hash(a), seed); },
67 exec_preset);
68 static auto fn_hash_string = mf::build::SI2_SO<std::string, int, int>(
69 "Hash String",
70 [](std::string a, int seed) { return noise::hash(BLI_hash_string(a.c_str()), seed); },
71 exec_preset);
72 static auto fn_hash_rotation = mf::build::SI2_SO<math::Quaternion, int, int>(
73 "Hash Rotation",
74 [](math::Quaternion a, int seed) { return noise::hash(noise::hash_float(float4(a)), seed); },
75 exec_preset);
76 static auto fn_hash_matrix = mf::build::SI2_SO<float4x4, int, int>(
77 "Hash Matrix",
78 [](float4x4 a, int seed) { return noise::hash(noise::hash_float(a), seed); },
79 exec_preset);
80
81 switch (socket_type) {
82 case SOCK_MATRIX:
83 return &fn_hash_matrix;
84 case SOCK_ROTATION:
85 return &fn_hash_rotation;
86 case SOCK_STRING:
87 return &fn_hash_string;
88 case SOCK_FLOAT:
89 return &fn_hash_float;
90 case SOCK_VECTOR:
91 return &fn_hash_vector;
92 case SOCK_RGBA:
93 return &fn_hash_color;
94 case SOCK_INT:
95 return &fn_hash_int;
96 default:
98 return nullptr;
99 }
100}
101
103 public:
107 {
108 bNode &node = params.add_node("FunctionNodeHashValue");
109 node.custom1 = socket_type;
110 params.update_and_connect_available_socket(node, socket_name);
111 }
112};
113
115{
116 const mf::MultiFunction *fn = get_multi_function(builder.node());
117 builder.set_matching_fn(fn);
118}
119
121{
122 eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
123 if (!ELEM(socket_type,
126 SOCK_INT,
131 SOCK_RGBA))
132 {
133 return;
134 }
135
136 if (params.in_out() == SOCK_IN) {
137 if (socket_type == SOCK_BOOLEAN) {
138 socket_type = SOCK_INT;
139 }
140 params.add_item(IFACE_("Value"), SocketSearchOp{"Value", socket_type});
141 params.add_item(IFACE_("Seed"), SocketSearchOp{"Seed", SOCK_INT});
142 }
143 else {
144 if (!ELEM(socket_type, SOCK_STRING)) {
145 const int weight = ELEM(params.other_socket().type, SOCK_INT) ? 0 : -1;
146 params.add_item(IFACE_("Hash"), SocketSearchOp{"Hash", SOCK_INT}, weight);
147 }
148 }
149}
150
151static void node_rna(StructRNA *srna)
152{
154 srna,
155 "data_type",
156 "Data Type",
157 "",
160 SOCK_INT,
161 [](bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
162 *r_free = true;
164 [](const EnumPropertyItem &item) -> bool {
165 return ELEM(item.value,
167 SOCK_INT,
172 SOCK_RGBA);
173 });
174 });
175}
176
177static void node_register()
178{
179 static blender::bke::bNodeType ntype;
180 fn_node_type_base(&ntype, FN_NODE_HASH_VALUE, "Hash Value", NODE_CLASS_CONVERTER);
181 ntype.declare = node_declare;
182 ntype.initfunc = node_init;
187
188 node_rna(ntype.rna_ext.srna);
189}
191
192} // namespace blender::nodes::node_fn_hash_value_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash.h:71
#define ELEM(...)
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_VECTOR
@ SOCK_BOOLEAN
@ SOCK_MATRIX
@ SOCK_FLOAT
@ SOCK_ROTATION
@ SOCK_STRING
@ SOCK_RGBA
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
static unsigned long seed
Definition btSoftBody.h:39
void set_matching_fn(const mf::MultiFunction *fn)
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_rna(StructRNA *srna)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
uint32_t hash_float(float kx)
Definition noise.cc:134
uint32_t hash(uint32_t kx)
Definition noise.cc:72
VecBase< float, 4 > float4
void fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
StructRNA * srna
Definition RNA_types.hh:780
int16_t custom1
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126