Blender  V3.3
node_shader_tex_noise.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
4 #include "node_shader_util.hh"
5 
6 #include "BLI_noise.hh"
7 
8 #include "UI_interface.h"
9 #include "UI_resources.h"
10 
12 
14 
16 {
17  b.is_function_node();
18  b.add_input<decl::Vector>(N_("Vector")).implicit_field();
19  b.add_input<decl::Float>(N_("W")).min(-1000.0f).max(1000.0f).make_available([](bNode &node) {
20  /* Default to 1 instead of 4, because it is much faster. */
21  node_storage(node).dimensions = 1;
22  });
23  b.add_input<decl::Float>(N_("Scale")).min(-1000.0f).max(1000.0f).default_value(5.0f);
24  b.add_input<decl::Float>(N_("Detail")).min(0.0f).max(15.0f).default_value(2.0f);
25  b.add_input<decl::Float>(N_("Roughness"))
26  .min(0.0f)
27  .max(1.0f)
28  .default_value(0.5f)
29  .subtype(PROP_FACTOR);
30  b.add_input<decl::Float>(N_("Distortion")).min(-1000.0f).max(1000.0f).default_value(0.0f);
31  b.add_output<decl::Float>(N_("Fac")).no_muted_links();
32  b.add_output<decl::Color>(N_("Color")).no_muted_links();
33 }
34 
36 {
37  uiItemR(layout, ptr, "noise_dimensions", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
38 }
39 
41 {
42  NodeTexNoise *tex = MEM_cnew<NodeTexNoise>(__func__);
44  BKE_texture_colormapping_default(&tex->base.color_mapping);
45  tex->dimensions = 3;
46 
47  node->storage = tex;
48 }
49 
50 static const char *gpu_shader_get_name(const int dimensions)
51 {
52  BLI_assert(dimensions >= 1 && dimensions <= 4);
53  return std::array{"node_noise_texture_1d",
54  "node_noise_texture_2d",
55  "node_noise_texture_3d",
56  "node_noise_texture_4d"}[dimensions - 1];
57  return nullptr;
58 }
59 
61  bNode *node,
62  bNodeExecData *UNUSED(execdata),
63  GPUNodeStack *in,
65 {
66  node_shader_gpu_default_tex_coord(mat, node, &in[0].link);
68 
69  const NodeTexNoise &storage = node_storage(*node);
70  const char *name = gpu_shader_get_name(storage.dimensions);
71  return GPU_stack_link(mat, node, name, in, out);
72 }
73 
75 {
76  bNodeSocket *sockVector = nodeFindSocket(node, SOCK_IN, "Vector");
77  bNodeSocket *sockW = nodeFindSocket(node, SOCK_IN, "W");
78 
79  const NodeTexNoise &storage = node_storage(*node);
80  nodeSetSocketAvailability(ntree, sockVector, storage.dimensions != 1);
81  nodeSetSocketAvailability(ntree, sockW, storage.dimensions == 1 || storage.dimensions == 4);
82 }
83 
85  private:
86  int dimensions_;
87 
88  public:
89  NoiseFunction(int dimensions) : dimensions_(dimensions)
90  {
91  BLI_assert(dimensions >= 1 && dimensions <= 4);
92  static std::array<fn::MFSignature, 4> signatures{
97  };
98  this->set_signature(&signatures[dimensions - 1]);
99  }
100 
101  static fn::MFSignature create_signature(int dimensions)
102  {
104 
105  if (ELEM(dimensions, 2, 3, 4)) {
106  signature.single_input<float3>("Vector");
107  }
108  if (ELEM(dimensions, 1, 4)) {
109  signature.single_input<float>("W");
110  }
111 
112  signature.single_input<float>("Scale");
113  signature.single_input<float>("Detail");
114  signature.single_input<float>("Roughness");
115  signature.single_input<float>("Distortion");
116 
117  signature.single_output<float>("Fac");
118  signature.single_output<ColorGeometry4f>("Color");
119 
120  return signature.build();
121  }
122 
124  {
125  int param = ELEM(dimensions_, 2, 3, 4) + ELEM(dimensions_, 1, 4);
126  const VArray<float> &scale = params.readonly_single_input<float>(param++, "Scale");
127  const VArray<float> &detail = params.readonly_single_input<float>(param++, "Detail");
128  const VArray<float> &roughness = params.readonly_single_input<float>(param++, "Roughness");
129  const VArray<float> &distortion = params.readonly_single_input<float>(param++, "Distortion");
130 
131  MutableSpan<float> r_factor = params.uninitialized_single_output_if_required<float>(param++,
132  "Fac");
134  params.uninitialized_single_output_if_required<ColorGeometry4f>(param++, "Color");
135 
136  const bool compute_factor = !r_factor.is_empty();
137  const bool compute_color = !r_color.is_empty();
138 
139  switch (dimensions_) {
140  case 1: {
141  const VArray<float> &w = params.readonly_single_input<float>(0, "W");
142  if (compute_factor) {
143  for (int64_t i : mask) {
144  const float position = w[i] * scale[i];
145  r_factor[i] = noise::perlin_fractal_distorted(
146  position, detail[i], roughness[i], distortion[i]);
147  }
148  }
149  if (compute_color) {
150  for (int64_t i : mask) {
151  const float position = w[i] * scale[i];
153  position, detail[i], roughness[i], distortion[i]);
154  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
155  }
156  }
157  break;
158  }
159  case 2: {
160  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
161  if (compute_factor) {
162  for (int64_t i : mask) {
163  const float2 position = float2(vector[i] * scale[i]);
164  r_factor[i] = noise::perlin_fractal_distorted(
165  position, detail[i], roughness[i], distortion[i]);
166  }
167  }
168  if (compute_color) {
169  for (int64_t i : mask) {
170  const float2 position = float2(vector[i] * scale[i]);
172  position, detail[i], roughness[i], distortion[i]);
173  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
174  }
175  }
176  break;
177  }
178  case 3: {
179  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
180  if (compute_factor) {
181  for (int64_t i : mask) {
182  const float3 position = vector[i] * scale[i];
183  r_factor[i] = noise::perlin_fractal_distorted(
184  position, detail[i], roughness[i], distortion[i]);
185  }
186  }
187  if (compute_color) {
188  for (int64_t i : mask) {
189  const float3 position = vector[i] * scale[i];
191  position, detail[i], roughness[i], distortion[i]);
192  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
193  }
194  }
195  break;
196  }
197  case 4: {
198  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
199  const VArray<float> &w = params.readonly_single_input<float>(1, "W");
200  if (compute_factor) {
201  for (int64_t i : mask) {
202  const float3 position_vector = vector[i] * scale[i];
203  const float position_w = w[i] * scale[i];
204  const float4 position{
205  position_vector[0], position_vector[1], position_vector[2], position_w};
206  r_factor[i] = noise::perlin_fractal_distorted(
207  position, detail[i], roughness[i], distortion[i]);
208  }
209  }
210  if (compute_color) {
211  for (int64_t i : mask) {
212  const float3 position_vector = vector[i] * scale[i];
213  const float position_w = w[i] * scale[i];
214  const float4 position{
215  position_vector[0], position_vector[1], position_vector[2], position_w};
217  position, detail[i], roughness[i], distortion[i]);
218  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
219  }
220  }
221  break;
222  }
223  }
224  }
225 
227  {
228  ExecutionHints hints;
229  hints.allocates_array = false;
230  hints.min_grain_size = 100;
231  return hints;
232  }
233 };
234 
236 {
237  const NodeTexNoise &storage = node_storage(builder.node());
239 }
240 
241 } // namespace blender::nodes::node_shader_tex_noise_cc
242 
244 {
245  namespace file_ns = blender::nodes::node_shader_tex_noise_cc;
246 
247  static bNodeType ntype;
248 
249  sh_fn_node_type_base(&ntype, SH_NODE_TEX_NOISE, "Noise Texture", NODE_CLASS_TEXTURE);
254  &ntype, "NodeTexNoise", node_free_standard_storage, node_copy_standard_storage);
258 
259  nodeRegisterType(&ntype);
260 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4443
#define NODE_STORAGE_FUNCS(StorageT)
Definition: BKE_node.h:1563
void nodeSetSocketAvailability(struct bNodeTree *ntree, struct bNodeSocket *sock, bool is_available)
Definition: node.cc:3664
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4426
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:355
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition: texture.c:238
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition: texture.c:340
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED(x)
#define ELEM(...)
@ SOCK_IN
#define TEXMAP_TYPE_POINT
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture SH_NODE_TEX_NOISE
@ PROP_FACTOR
Definition: RNA_types.h:144
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
void set_signature(const MFSignature *signature)
const MFSignature & signature() const
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static unsigned c
Definition: RandGen.cpp:83
static void node_shader_buts_tex_noise(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static void node_shader_init_tex_noise(bNodeTree *UNUSED(ntree), bNode *node)
static int node_shader_gpu_tex_noise(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_noise_build_multi_function(NodeMultiFunctionBuilder &builder)
static const char * gpu_shader_get_name(const int dimensions)
static void node_shader_update_tex_noise(bNodeTree *ntree, bNode *node)
static void sh_node_tex_noise_declare(NodeDeclarationBuilder &b)
float perlin_fractal_distorted(float position, float octaves, float roughness, float distortion)
Definition: noise.cc:643
float3 perlin_float3_fractal_distorted(float position, float octaves, float roughness, float distortion)
Definition: noise.cc:670
vec_base< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition: BLI_color.hh:346
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
void register_node_type_sh_tex_noise()
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:55
void node_free_standard_storage(bNode *node)
Definition: node_util.c:43
#define min(a, b)
Definition: sort.c:35
__int64 int64_t
Definition: stdint.h:89
Defines a node type.
Definition: BKE_node.h:226
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:244
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480