Blender V5.3
node_shader_tex_white_noise.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#include "node_shader_util.hh"
10
11#include "BLI_noise.hh"
12
13#include "FN_multi_function.hh"
14
15#include "NOD_multi_function.hh"
16
18#include "UI_resources.hh"
19
20namespace blender {
21
23
25{
26 b.is_function_node();
27
28 const int dimensions = b.node_or_null() ? b.node_or_null()->custom1 : 3;
29 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
30 const NodeDefaultInputType default_input_type =
33 b.add_input<decl::Vector>("Vector"_ustr)
34 .min(-10000.0f)
35 .max(10000.0f)
36 .default_input_type(default_input_type)
37 .available(dimensions != 1);
38
39 b.add_input<decl::Float>("W"_ustr)
40 .min(-10000.0f)
41 .max(10000.0f)
42 .available(ELEM(dimensions, 1, 4))
43 .make_available([](bNode &node) {
44 /* Default to 1 instead of 4, because it is faster. */
45 node.custom1 = 1;
46 })
47 .description("Value used as seed in 1D and 4D dimensions");
48
49 b.add_output<decl::Float>("Value"_ustr);
50 b.add_output<decl::Color>("Color"_ustr);
51}
52
54{
55 layout.prop(ptr, "noise_dimensions", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
56}
57
59{
60 node->custom1 = node_tree->type == NTREE_COMPOSIT ? 2 : 3;
61}
62
63static const char *gpu_shader_get_name(const int dimensions)
64{
65 BLI_assert(dimensions >= 1 && dimensions <= 4);
66 return std::array{"node_white_noise_1d",
67 "node_white_noise_2d",
68 "node_white_noise_3d",
69 "node_white_noise_4d"}[dimensions - 1];
70}
71
73 bNode *node,
74 bNodeExecData * /*execdata*/,
77{
78 const char *name = gpu_shader_get_name(node->custom1);
79 return GPU_stack_link(mat, node, name, in, out);
80}
81
82class WhiteNoiseFunction : public mf::MultiFunction {
83 private:
84 int dimensions_;
85
86 public:
87 WhiteNoiseFunction(int dimensions) : dimensions_(dimensions)
88 {
89 BLI_assert(dimensions >= 1 && dimensions <= 4);
90 static std::array<mf::Signature, 4> signatures{
95 };
96 this->set_signature(&signatures[dimensions - 1]);
97 }
98
99 static mf::Signature create_signature(int dimensions)
100 {
101 mf::Signature signature;
102 mf::SignatureBuilder builder{"WhiteNoise", signature};
103
104 if (ELEM(dimensions, 2, 3, 4)) {
105 builder.single_input<float3>("Vector");
106 }
107 if (ELEM(dimensions, 1, 4)) {
108 builder.single_input<float>("W");
109 }
110
111 builder.single_output<float>("Value", mf::ParamFlag::SupportsUnusedOutput);
112 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
113
114 return signature;
115 }
116
117 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
118 {
119 int param = ELEM(dimensions_, 2, 3, 4) + ELEM(dimensions_, 1, 4);
120
121 MutableSpan<float> r_value = params.uninitialized_single_output_if_required<float>(param++,
122 "Value");
124 params.uninitialized_single_output_if_required<ColorGeometry4f>(param++, "Color");
125
126 const bool compute_value = !r_value.is_empty();
127 const bool compute_color = !r_color.is_empty();
128
129 switch (dimensions_) {
130 case 1: {
131 const VArray<float> &w = params.readonly_single_input<float>(0, "W");
132 if (compute_color) {
133 mask.foreach_index([&](const int64_t i) {
135 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
136 });
137 }
138 if (compute_value) {
139 mask.foreach_index(
140 [&](const int64_t i) { r_value[i] = noise::hash_float_to_float(w[i]); });
141 }
142 break;
143 }
144 case 2: {
145 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
146 if (compute_color) {
147 mask.foreach_index([&](const int64_t i) {
148 const float3 c = noise::hash_float_to_float3(float2(vector[i].x, vector[i].y));
149 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
150 });
151 }
152 if (compute_value) {
153 mask.foreach_index([&](const int64_t i) {
154 r_value[i] = noise::hash_float_to_float(float2(vector[i].x, vector[i].y));
155 });
156 }
157 break;
158 }
159 case 3: {
160 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
161 if (compute_color) {
162 mask.foreach_index([&](const int64_t i) {
163 const float3 c = noise::hash_float_to_float3(vector[i]);
164 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
165 });
166 }
167 if (compute_value) {
168 mask.foreach_index(
169 [&](const int64_t i) { r_value[i] = noise::hash_float_to_float(vector[i]); });
170 }
171 break;
172 }
173 case 4: {
174 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
175 const VArray<float> &w = params.readonly_single_input<float>(1, "W");
176 if (compute_color) {
177 mask.foreach_index([&](const int64_t i) {
179 float4(vector[i].x, vector[i].y, vector[i].z, w[i]));
180 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
181 });
182 }
183 if (compute_value) {
184 mask.foreach_index([&](const int64_t i) {
185 r_value[i] = noise::hash_float_to_float(
186 float4(vector[i].x, vector[i].y, vector[i].z, w[i]));
187 });
188 }
189 break;
190 }
191 }
192 }
193
194 void hash_unique(UniqueHashBytes &hash) const override
195 {
196 static constexpr int8_t id = 0;
197 hash.add(&id);
198 hash.add(dimensions_);
199 }
200};
201
203{
204 const bNode &node = builder.node();
206}
207
209#ifdef WITH_MATERIALX
210{
211 /* MaterialX cellnoise node rounds float value of texture coordinate.
212 * Therefore it changes at different integer coordinates.
213 * The simple trick would be to multiply the texture coordinate by a large number. */
214 const float LARGE_NUMBER = 10000.0f;
215
216 NodeItem noise = empty();
217 NodeItem vector = empty();
218 NodeItem w = empty();
219
220 int dimension = node_->custom1;
221 switch (dimension) {
222 case 1:
223 w = get_input_value("W", NodeItem::Type::Vector2);
224 noise = create_node(
225 "cellnoise2d", NodeItem::Type::Float, {{"texcoord", w * val(LARGE_NUMBER)}});
226 break;
227 case 2:
228 vector = get_input_link("Vector", NodeItem::Type::Vector2);
229 if (!vector) {
230 vector = texcoord_node();
231 }
232 noise = create_node(
233 "cellnoise2d", NodeItem::Type::Float, {{"texcoord", vector * val(LARGE_NUMBER)}});
234 break;
235 case 3:
236 vector = get_input_link("Vector", NodeItem::Type::Vector3);
237 if (!vector) {
238 vector = texcoord_node(NodeItem::Type::Vector3);
239 }
240 noise = create_node(
241 "cellnoise3d", NodeItem::Type::Float, {{"position", vector * val(LARGE_NUMBER)}});
242 break;
243 case 4:
244 vector = get_input_link("Vector", NodeItem::Type::Vector3);
245 if (!vector) {
246 vector = texcoord_node(NodeItem::Type::Vector3);
247 }
248 w = get_input_value("W", NodeItem::Type::Float);
249 noise = create_node(
250 "cellnoise3d", NodeItem::Type::Float, {{"position", (vector + w) * val(LARGE_NUMBER)}});
251 break;
252 default:
254 break;
255 }
256
257 if (STREQ(socket_out_->identifier, "Value")) {
258 return noise;
259 }
260
261 /* NOTE: cellnoise node doesn't have colored output, so we create hsvtorgb node and put
262 * noise in first (Hue) channel to generate color. */
263 NodeItem combine = create_node("combine3",
264 NodeItem::Type::Color3,
265 {{"in1", noise}, {"in2", val(1.0f)}, {"in3", val(0.5f)}});
266 return create_node("hsvtorgb", NodeItem::Type::Color3, {{"in", combine}});
267}
268#endif
270
271} // namespace nodes::node_shader_tex_white_noise_cc
272
274{
275 namespace file_ns = nodes::node_shader_tex_white_noise_cc;
276
277 static bke::bNodeType ntype;
278
279 common_node_type_base(&ntype, "ShaderNodeTexWhiteNoise"_ustr, SH_NODE_TEX_WHITE_NOISE);
280 ntype.ui_name = "White Noise Texture";
281 ntype.ui_description = "Calculate a random value or color based on an input seed";
282 ntype.enum_name_legacy = "TEX_WHITE_NOISE";
284 ntype.declare = file_ns::sh_node_tex_white_noise_declare;
285 ntype.draw_buttons = file_ns::node_shader_buts_white_noise;
286 ntype.initfunc = file_ns::node_shader_init_tex_white_noise;
287 ntype.gpu_fn = file_ns::gpu_shader_tex_white_noise;
288 ntype.build_multi_function = file_ns::sh_node_noise_build_multi_function;
289 ntype.materialx_fn = file_ns::node_shader_materialx;
290
292}
293
294} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_WHITE_NOISE
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr bool is_empty() const
Definition BLI_span.hh:515
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
float noise(float p)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static const char * gpu_shader_get_name(const int dimensions)
static void sh_node_tex_white_noise_declare(NodeDeclarationBuilder &b)
static void node_shader_buts_white_noise(ui::Layout &layout, bContext *, PointerRNA *ptr)
static int gpu_shader_tex_white_noise(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_tex_white_noise(bNodeTree *node_tree, bNode *node)
static void sh_node_noise_build_multi_function(NodeMultiFunctionBuilder &builder)
float hash_float_to_float(float k)
Definition noise.cc:254
float3 hash_float_to_float3(float k)
Definition noise.cc:291
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
void register_node_type_sh_tex_white_noise()
VecBase< float, 4 > float4
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define hash
Definition noise_c.cc:156
#define min(a, b)
Definition sort.cc:38
eNodeTree_Type type
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
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
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)