Blender V5.3
node_fn_random_value.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5// #include "BLI_hash_c.hh"
6#include "BLI_noise.hh"
7
9#include "node_shader_util.hh"
10
12
14#include "UI_resources.hh"
15
17
19
21{
22 b.is_function_node();
23 const bNode *node = b.node_or_null();
24
25 if (node != nullptr) {
26 const NodeRandomValue &storage = node_storage(*node);
27 const eCustomDataType data_type = eCustomDataType(storage.data_type);
28 switch (data_type) {
29 case CD_PROP_FLOAT3:
30 b.add_input<decl::Vector>("Min"_ustr);
31 b.add_input<decl::Vector>("Max"_ustr).default_value({1.0f, 1.0f, 1.0f});
32 break;
33 case CD_PROP_FLOAT:
34 b.add_input<decl::Float>("Min"_ustr);
35 b.add_input<decl::Float>("Max"_ustr).default_value(1.0f);
36 break;
37 case CD_PROP_INT32:
38 b.add_input<decl::Int>("Min"_ustr);
39 b.add_input<decl::Int>("Max"_ustr).default_value(100);
40 break;
41 case CD_PROP_BOOL:
42 b.add_input<decl::Float>("Probability"_ustr)
43 .min(0.0f)
44 .max(1.0f)
45 .default_value(0.5f)
46 .subtype(PROP_FACTOR);
47 break;
48 default:
50 break;
51 }
52 }
53
54 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
55 b.add_input<decl::Int>("ID"_ustr)
56 .structure_type(StructureType::Dynamic)
57 .default_input_type(is_compositor ? NODE_DEFAULT_INPUT_VALUE :
59 b.add_input<decl::Int>("Seed"_ustr);
60
61 if (node != nullptr) {
62 const NodeRandomValue &storage = node_storage(*node);
63 const eCustomDataType data_type = eCustomDataType(storage.data_type);
64 b.add_output(data_type, "Value"_ustr);
65 }
66}
67
68static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
69{
70 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
71}
72
74{
75 NodeRandomValue *data = MEM_new<NodeRandomValue>(__func__);
76 data->data_type = CD_PROP_FLOAT;
77 node->storage = data;
78}
79
80static std::optional<eCustomDataType> node_type_from_other_socket(const bNodeSocket &socket)
81{
82 switch (socket.type) {
83 case SOCK_FLOAT:
84 return CD_PROP_FLOAT;
85 case SOCK_BOOLEAN:
86 return CD_PROP_BOOL;
87 case SOCK_INT:
88 return CD_PROP_INT32;
89 case SOCK_VECTOR:
90 case SOCK_RGBA:
91 case SOCK_ROTATION:
92 return CD_PROP_FLOAT3;
93 default:
94 return {};
95 }
96}
97
99{
100 const std::optional<eCustomDataType> type = node_type_from_other_socket(params.other_socket());
101 if (!type) {
102 return;
103 }
104 if (params.in_out() == SOCK_IN) {
106 params.add_item(IFACE_("Min"), [type](LinkSearchOpParams &params) {
107 bNode &node = params.add_node("FunctionNodeRandomValue"_ustr);
108 node_storage(node).data_type = *type;
109 params.update_and_connect_available_socket(node, "Min"_ustr);
110 });
111 params.add_item(IFACE_("Max"), [type](LinkSearchOpParams &params) {
112 bNode &node = params.add_node("FunctionNodeRandomValue"_ustr);
113 node_storage(node).data_type = *type;
114 params.update_and_connect_available_socket(node, "Max"_ustr);
115 });
116 }
117 if (*type == CD_PROP_FLOAT) {
118 params.add_item(IFACE_("Probability"), [](LinkSearchOpParams &params) {
119 bNode &node = params.add_node("FunctionNodeRandomValue"_ustr);
120 node_storage(node).data_type = CD_PROP_BOOL;
121 params.update_and_connect_available_socket(node, "Probability"_ustr);
122 });
123 }
124 }
125 else {
126 params.add_item(IFACE_("Value"), [type](LinkSearchOpParams &params) {
127 bNode &node = params.add_node("FunctionNodeRandomValue"_ustr);
128 node_storage(node).data_type = *type;
129 params.update_and_connect_available_socket(node, "Value"_ustr);
130 });
131 }
132}
133
135{
136 const NodeRandomValue &storage = node_storage(builder.node());
137 const eCustomDataType data_type = eCustomDataType(storage.data_type);
138
139 switch (data_type) {
140 case CD_PROP_FLOAT3: {
141 static auto fn = mf::build::SI4_SO<float3, float3, int, int, float3>(
142 "Random Vector",
143 [](float3 min_value, float3 max_value, int id, int seed) -> float3 {
144 const float x = noise::hash_to_float(seed, id, 0);
145 const float y = noise::hash_to_float(seed, id, 1);
146 const float z = noise::hash_to_float(seed, id, 2);
147 return float3(x, y, z) * (max_value - min_value) + min_value;
148 },
149 mf::build::exec_presets::SomeSpanOrSingle<2>());
150 builder.set_matching_fn(fn);
151 break;
152 }
153 case CD_PROP_FLOAT: {
154 static auto fn = mf::build::SI4_SO<float, float, int, int, float>(
155 "Random Float",
156 [](float min_value, float max_value, int id, int seed) -> float {
157 const float value = noise::hash_to_float(seed, id);
158 return value * (max_value - min_value) + min_value;
159 },
160 mf::build::exec_presets::SomeSpanOrSingle<2>());
161 builder.set_matching_fn(fn);
162 break;
163 }
164 case CD_PROP_INT32: {
165 static auto fn = mf::build::SI4_SO<int, int, int, int, int>(
166 "Random Int",
167 [](int min_value, int max_value, int id, int seed) -> int {
168 if (min_value > max_value) {
169 std::swap(min_value, max_value);
170 }
171 const uint32_t hash = noise::hash(id, seed);
172
173 /* Calculate range using unsigned types to fit the entire 32-bit space. */
174 const uint32_t range = uint32_t(max_value) - uint32_t(min_value) + 1;
175
176 /* Range wraps around to 0 when min_value is INT_MIN and max_value is INT_MAX.
177 * so the modulo is unnecessary and would cause a division by zero. */
178 const uint32_t modulo_result = (range == 0) ? hash : (hash % range);
179
180 return int(uint32_t(min_value) + modulo_result);
181 },
182 mf::build::exec_presets::SomeSpanOrSingle<2>());
183 builder.set_matching_fn(fn);
184 break;
185 }
186 case CD_PROP_BOOL: {
187 static auto fn = mf::build::SI3_SO<float, int, int, bool>(
188 "Random Bool",
189 [](float probability, int id, int seed) -> bool {
190 return noise::hash_to_float(id, seed) <= probability;
191 },
192 mf::build::exec_presets::SomeSpanOrSingle<1>());
193 builder.set_matching_fn(fn);
194 break;
195 }
196 default: {
198 break;
199 }
200 }
201}
202
203static const char *gpu_shader_get_name(const eCustomDataType data_type)
204{
205 switch (data_type) {
206 case CD_PROP_FLOAT3:
207 return "random_value_vector";
208 case CD_PROP_FLOAT:
209 return "random_value_float";
210 case CD_PROP_INT32:
211 return "random_value_int";
212 case CD_PROP_BOOL:
213 return "random_value_bool";
214 default:
215 break;
216 }
217
219 return nullptr;
220}
221
223 bNode *node,
224 bNodeExecData * /*execdata*/,
227{
228 const NodeRandomValue &storage = node_storage(*node);
229 const char *name = gpu_shader_get_name(eCustomDataType(storage.data_type));
230
231 if (name == nullptr) {
232 return 0;
233 }
234
235 return GPU_stack_link(mat, node, name, in, out);
236}
237
238static void node_register()
239{
240 static bke::bNodeType ntype;
241
242 fn_cmp_node_type_base(&ntype, "FunctionNodeRandomValue"_ustr, FN_NODE_RANDOM_VALUE);
243 ntype.ui_name = "Random Value";
244 ntype.ui_description = "Output a randomized value";
245 ntype.enum_name_legacy = "RANDOM_VALUE";
249 ntype.declare = node_declare;
254 ntype, "NodeRandomValue", node_free_standard_storage, node_copy_standard_storage);
256}
258
259} // namespace blender::nodes::node_fn_random_value_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define FN_NODE_RANDOM_VALUE
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const char * gpu_shader_get_name(const eCustomDataType data_type)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void fn_node_random_value_init(bNodeTree *, bNode *node)
static std::optional< eCustomDataType > node_type_from_other_socket(const bNodeSocket &socket)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
uint32_t hash(uint32_t kx)
Definition noise.cc:89
float hash_to_float(uint32_t kx)
Definition noise.cc:232
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
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
VecBase< float, 3 > float3
@ PROP_FACTOR
Definition RNA_types.hh:308
#define hash
Definition noise_c.cc:156
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)