Blender V5.3
node_shader_tex_wave.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#include "node_util.hh"
11
12#include "BKE_texture.h"
13
14#include "BLI_math_vector_c.hh"
15#include "BLI_noise.hh"
16
17#include "NOD_multi_function.hh"
18
19#include "RNA_access.hh"
20
22#include "UI_resources.hh"
23
24namespace blender {
25
27
29{
30 b.is_function_node();
31
32 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
33 const NodeDefaultInputType default_input_type =
36 b.add_input<decl::Vector>("Vector"_ustr).default_input_type(default_input_type);
37
38 b.add_input<decl::Float>("Scale"_ustr)
39 .min(-1000.0f)
40 .max(1000.0f)
41 .default_value(5.0f)
42 .description("Overall texture scale");
43 b.add_input<decl::Float>("Distortion"_ustr)
44 .min(-1000.0f)
45 .max(1000.0f)
46 .default_value(0.0f)
47 .description("Amount of distortion of the wave");
48 b.add_input<decl::Float>("Detail"_ustr)
49 .min(0.0f)
50 .max(15.0f)
51 .default_value(2.0f)
52 .description("Amount of distortion noise detail");
53 b.add_input<decl::Float>("Detail Scale"_ustr)
54 .min(-1000.0f)
55 .max(1000.0f)
56 .default_value(1.0f)
57 .description("Scale of distortion noise");
58 b.add_input<decl::Float>("Detail Roughness"_ustr)
59 .min(0.0f)
60 .max(1.0f)
61 .default_value(0.5f)
62 .subtype(PROP_FACTOR)
63 .description("Blend between a smoother noise pattern, and rougher with sharper peaks");
64 b.add_input<decl::Float>("Phase Offset"_ustr)
65 .min(-1000.0f)
66 .max(1000.0f)
67 .default_value(0.0f)
68 .description(
69 "Position of the wave along the Bands Direction.\n"
70 "This can be used as an input for more control over the distortion");
71 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
72 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr).no_muted_links();
73}
74
75static void node_shader_buts_tex_wave(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
76{
77 layout.prop(ptr, "wave_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
78 int type = RNA_enum_get(ptr, "wave_type");
79 if (type == SHD_WAVE_BANDS) {
80 layout.prop(ptr, "bands_direction", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
81 }
82 else { /* SHD_WAVE_RINGS */
83 layout.prop(ptr, "rings_direction", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
84 }
85
86 layout.prop(ptr, "wave_profile", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
87}
88
100
102 bNode *node,
103 bNodeExecData * /*execdata*/,
106{
109
110 NodeTexWave *tex = static_cast<NodeTexWave *>(node->storage);
111 float wave_type = tex->wave_type;
112 float bands_direction = tex->bands_direction;
113 float rings_direction = tex->rings_direction;
114 float wave_profile = tex->wave_profile;
115
116 return GPU_stack_link(mat,
117 node,
118 "node_tex_wave",
119 in,
120 out,
121 GPU_constant(&wave_type),
122 GPU_constant(&bands_direction),
123 GPU_constant(&rings_direction),
124 GPU_constant(&wave_profile));
125}
126
127class WaveFunction : public mf::MultiFunction {
128 private:
129 int wave_type_;
130 int bands_direction_;
131 int rings_direction_;
132 int wave_profile_;
133
134 public:
135 WaveFunction(int wave_type, int bands_direction, int rings_direction, int wave_profile)
136 : wave_type_(wave_type),
137 bands_direction_(bands_direction),
138 rings_direction_(rings_direction),
139 wave_profile_(wave_profile)
140 {
141 static const mf::Signature signature = []() {
142 mf::Signature signature;
143 mf::SignatureBuilder builder{"MagicFunction", signature};
144 builder.single_input<float3>("Vector");
145 builder.single_input<float>("Scale");
146 builder.single_input<float>("Distortion");
147 builder.single_input<float>("Detail");
148 builder.single_input<float>("Detail Scale");
149 builder.single_input<float>("Detail Roughness");
150 builder.single_input<float>("Phase Offset");
151 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
152 builder.single_output<float>("Fac");
153 return signature;
154 }();
155 this->set_signature(&signature);
156 }
157
158 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
159 {
160 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
161 const VArray<float> &scale = params.readonly_single_input<float>(1, "Scale");
162 const VArray<float> &distortion = params.readonly_single_input<float>(2, "Distortion");
163 const VArray<float> &detail = params.readonly_single_input<float>(3, "Detail");
164 const VArray<float> &dscale = params.readonly_single_input<float>(4, "Detail Scale");
165 const VArray<float> &droughness = params.readonly_single_input<float>(5, "Detail Roughness");
166 const VArray<float> &phase = params.readonly_single_input<float>(6, "Phase Offset");
167
169 params.uninitialized_single_output_if_required<ColorGeometry4f>(7, "Color");
170 MutableSpan<float> r_fac = params.uninitialized_single_output<float>(8, "Fac");
171
172 mask.foreach_index([&](const int64_t i) {
173 float3 p = vector[i] * scale[i];
174 /* Prevent precision issues on unit coordinates. */
175 p = (p + 0.000001f) * 0.999999f;
176
177 float n = 0.0f;
178 float val = 0.0f;
179
180 switch (wave_type_) {
181 case SHD_WAVE_BANDS:
182 switch (bands_direction_) {
184 n = p.x * 20.0f;
185 break;
187 n = p.y * 20.0f;
188 break;
190 n = p.z * 20.0f;
191 break;
193 n = (p.x + p.y + p.z) * 10.0f;
194 break;
195 }
196 break;
197 case SHD_WAVE_RINGS:
198 float3 rp = p;
199 switch (rings_direction_) {
201 rp *= float3(0.0f, 1.0f, 1.0f);
202 break;
204 rp *= float3(1.0f, 0.0f, 1.0f);
205 break;
207 rp *= float3(1.0f, 1.0f, 0.0f);
208 break;
210 /* Ignore. */
211 break;
212 }
213 n = len_v3(rp) * 20.0f;
214 break;
215 }
216
217 n += phase[i];
218
219 if (distortion[i] != 0.0f) {
220 n += distortion[i] *
221 (noise::perlin_fbm<float3>(p * dscale[i], detail[i], droughness[i], 2.0f, true) *
222 2.0f -
223 1.0f);
224 }
225
226 switch (wave_profile_) {
228 val = 0.5f + 0.5f * sinf(n - M_PI_2);
229 break;
231 n /= M_PI * 2.0f;
232 val = n - floorf(n);
233 break;
235 n /= M_PI * 2.0f;
236 val = fabsf(n - floorf(n + 0.5f)) * 2.0f;
237 break;
238 }
239
240 r_fac[i] = val;
241 });
242 if (!r_color.is_empty()) {
243 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
244 r_color[i] = ColorGeometry4f(r_fac[i], r_fac[i], r_fac[i], 1.0f);
245 });
246 }
247 }
248
249 void hash_unique(UniqueHashBytes &hash) const override
250 {
251 static constexpr int8_t id = 0;
252 hash.add(&id);
253 hash.add(wave_type_);
254 hash.add(bands_direction_);
255 hash.add(rings_direction_);
256 hash.add(wave_profile_);
257 }
258};
259
261{
262 const bNode &node = builder.node();
263 NodeTexWave *tex = static_cast<NodeTexWave *>(node.storage);
266}
267
269#ifdef WITH_MATERIALX
270{
271 NodeTexWave *tex = (NodeTexWave *)node_->storage;
272
273 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
274 NodeItem distortion = get_input_value("Distortion", NodeItem::Type::Float);
275 NodeItem detail = get_input_default("Detail", NodeItem::Type::Float);
276 NodeItem detail_scale = get_input_value("Detail Scale", NodeItem::Type::Float);
277 NodeItem detail_rough = get_input_value("Detail Roughness", NodeItem::Type::Float);
278 NodeItem phase_offset = get_input_value("Phase Offset", NodeItem::Type::Float);
279 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector3);
280 if (!vector) {
281 vector = texcoord_node(NodeItem::Type::Vector3);
282 }
283
284 /* Adjustment to get result as Cycles. */
285 distortion = distortion * val(10.0f);
286 detail_scale = detail_scale * val(10.0f);
287
288 NodeItem pos = vector * scale;
289 NodeItem fractal = create_node("fractal3d",
290 NodeItem::Type::Float,
291 {{"position", pos},
292 {"octaves", val(int(detail.value->asA<float>()))},
293 {"lacunarity", val(2.0f)}});
294 NodeItem value = val(0.0f);
295 switch (tex->wave_type) {
296 case SHD_WAVE_BANDS:
297 switch (tex->bands_direction) {
299 value = pos[0] * val(20.0f);
300 break;
302 value = pos[1] * val(20.0f);
303 break;
305 value = pos[2] * val(20.0f);
306 break;
308 value = (pos[0] + pos[1] + pos[2]) * val(10.0f);
309 break;
310 default:
312 }
313 break;
314 case SHD_WAVE_RINGS:
315 NodeItem rpos = pos;
316 switch (tex->rings_direction) {
318 rpos = pos * val(MaterialX::Vector3(0.0f, 1.0f, 1.0f));
319 break;
321 rpos = pos * val(MaterialX::Vector3(1.0f, 0.0f, 1.0f));
322 break;
324 rpos = pos * val(MaterialX::Vector3(1.0f, 1.0f, 0.0f));
325 break;
327 /* Ignore. */
328 break;
329 default:
331 }
332 value = rpos.length() * val(20.0f);
333 break;
334 }
335 value = value + phase_offset + distortion * detail_scale * fractal;
336
337 NodeItem res = empty();
338 switch (tex->wave_profile) {
340 res = val(0.5f) + val(0.5f) * (value - val(float(M_PI_2))).sin();
341 break;
343 value = value / val(float(M_PI * 2.0f));
344 res = value - value.floor();
345 break;
347 value = value / val(float(M_PI * 2.0f));
348 res = (value - (value + val(0.5f)).floor()).abs() * val(2.0f);
349 break;
350 default:
352 }
353 return res;
354}
355#endif
357
358} // namespace nodes::node_shader_tex_wave_cc
359
361{
362 namespace file_ns = nodes::node_shader_tex_wave_cc;
363
364 static bke::bNodeType ntype;
365
366 common_node_type_base(&ntype, "ShaderNodeTexWave"_ustr, SH_NODE_TEX_WAVE);
367 ntype.ui_name = "Wave Texture";
368 ntype.ui_description = "Generate procedural bands or rings with noise";
369 ntype.enum_name_legacy = "TEX_WAVE";
371 ntype.declare = file_ns::sh_node_tex_wave_declare;
372 ntype.draw_buttons = file_ns::node_shader_buts_tex_wave;
374 ntype.initfunc = file_ns::node_shader_init_tex_wave;
377 ntype.gpu_fn = file_ns::node_shader_gpu_tex_wave;
378 ntype.build_multi_function = file_ns::sh_node_wave_tex_build_multi_function;
379 ntype.materialx_fn = file_ns::node_shader_materialx;
381
383}
384
385} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_WAVE
#define M_PI_2
#define M_PI
#define BLI_assert_unreachable()
void set_signature(const Signature *signature)
void hash_unique(UniqueHashBytes &hash) const override
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
WaveFunction(int wave_type, int bands_direction, int rings_direction, int wave_profile)
#define sin
float3x3 scale(float3x3 mat, float2 scale)
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 sh_node_wave_tex_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_tex_wave(bNodeTree *, bNode *node)
static void node_shader_buts_tex_wave(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_tex_wave_declare(NodeDeclarationBuilder &b)
static int node_shader_gpu_tex_wave(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
template float perlin_fbm< float3 >(float3 p, const float detail, const float roughness, const float lacunarity, const bool normalize)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ SHD_WAVE_BANDS_DIRECTION_Z
@ SHD_WAVE_BANDS_DIRECTION_DIAGONAL
@ SHD_WAVE_BANDS_DIRECTION_X
@ SHD_WAVE_BANDS_DIRECTION_Y
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
void register_node_type_sh_tex_wave()
@ SHD_WAVE_PROFILE_SAW
@ SHD_WAVE_PROFILE_TRI
@ SHD_WAVE_PROFILE_SIN
GPUNodeLink * GPU_constant(const float *num)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
@ SHD_WAVE_RINGS_DIRECTION_SPHERICAL
@ SHD_WAVE_RINGS_DIRECTION_X
@ SHD_WAVE_RINGS_DIRECTION_Y
@ SHD_WAVE_RINGS_DIRECTION_Z
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
@ PROP_FACTOR
Definition RNA_types.hh:308
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)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define hash
Definition noise_c.cc:156
#define floorf
#define fabsf
#define sinf
#define min(a, b)
Definition sort.cc:38
static constexpr int _160
Definition BKE_node.hh:246
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)