Blender V4.3
node_shader_tex_magic.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
5#include "node_shader_util.hh"
6#include "node_util.hh"
7
8#include "BKE_texture.h"
9
10#include "NOD_multi_function.hh"
11
12#include "UI_interface.hh"
13#include "UI_resources.hh"
14
16
18{
19 b.is_function_node();
20 b.add_input<decl::Vector>("Vector").implicit_field(implicit_field_inputs::position);
21 b.add_input<decl::Float>("Scale").min(-1000.0f).max(1000.0f).default_value(5.0f).description(
22 "Scale of the texture");
23 b.add_input<decl::Float>("Distortion")
24 .min(-1000.0f)
25 .max(1000.0f)
26 .default_value(1.0f)
27 .description("Amount of distortion");
28 b.add_output<decl::Color>("Color").no_muted_links();
29 b.add_output<decl::Float>("Fac").no_muted_links();
30}
31
33{
34 uiItemR(layout, ptr, "turbulence_depth", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, ICON_NONE);
35}
36
37static void node_shader_init_tex_magic(bNodeTree * /*ntree*/, bNode *node)
38{
39 NodeTexMagic *tex = MEM_cnew<NodeTexMagic>(__func__);
41 BKE_texture_colormapping_default(&tex->base.color_mapping);
42 tex->depth = 2;
43
44 node->storage = tex;
45}
46
48 bNode *node,
49 bNodeExecData * /*execdata*/,
50 GPUNodeStack *in,
51 GPUNodeStack *out)
52{
53 NodeTexMagic *tex = (NodeTexMagic *)node->storage;
54 float depth = tex->depth;
55
56 node_shader_gpu_default_tex_coord(mat, node, &in[0].link);
57 node_shader_gpu_tex_mapping(mat, node, in, out);
58
59 return GPU_stack_link(mat, node, "node_tex_magic", in, out, GPU_constant(&depth));
60}
61
63 private:
64 int depth_;
65
66 public:
67 MagicFunction(int depth) : depth_(depth)
68 {
69 static const mf::Signature signature = []() {
71 mf::SignatureBuilder builder{"MagicFunction", signature};
72 builder.single_input<float3>("Vector");
73 builder.single_input<float>("Scale");
74 builder.single_input<float>("Distortion");
75 builder.single_output<ColorGeometry4f>("Color");
76 builder.single_output<float>("Fac", mf::ParamFlag::SupportsUnusedOutput);
77 return signature;
78 }();
79 this->set_signature(&signature);
80 }
81
82 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
83 {
84 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
85 const VArray<float> &scale = params.readonly_single_input<float>(1, "Scale");
86 const VArray<float> &distortion = params.readonly_single_input<float>(2, "Distortion");
87
88 MutableSpan<ColorGeometry4f> r_color = params.uninitialized_single_output<ColorGeometry4f>(
89 3, "Color");
90 MutableSpan<float> r_fac = params.uninitialized_single_output_if_required<float>(4, "Fac");
91
92 const bool compute_factor = !r_fac.is_empty();
93
94 mask.foreach_index([&](const int64_t i) {
95 const float3 co = vector[i] * scale[i];
96 const float distort = distortion[i];
97 float x = sinf((co[0] + co[1] + co[2]) * 5.0f);
98 float y = cosf((-co[0] + co[1] - co[2]) * 5.0f);
99 float z = -cosf((-co[0] - co[1] + co[2]) * 5.0f);
100
101 if (depth_ > 0) {
102 x *= distort;
103 y *= distort;
104 z *= distort;
105 y = -cosf(x - y + z);
106 y *= distort;
107
108 if (depth_ > 1) {
109 x = cosf(x - y - z);
110 x *= distort;
111
112 if (depth_ > 2) {
113 z = sinf(-x - y - z);
114 z *= distort;
115
116 if (depth_ > 3) {
117 x = -cosf(-x + y - z);
118 x *= distort;
119
120 if (depth_ > 4) {
121 y = -sinf(-x + y + z);
122 y *= distort;
123
124 if (depth_ > 5) {
125 y = -cosf(-x + y + z);
126 y *= distort;
127
128 if (depth_ > 6) {
129 x = cosf(x + y + z);
130 x *= distort;
131
132 if (depth_ > 7) {
133 z = sinf(x + y - z);
134 z *= distort;
135
136 if (depth_ > 8) {
137 x = -cosf(-x - y + z);
138 x *= distort;
139
140 if (depth_ > 9) {
141 y = -sinf(x - y + z);
142 y *= distort;
143 }
144 }
145 }
146 }
147 }
148 }
149 }
150 }
151 }
152 }
153
154 if (distort != 0.0f) {
155 const float d = distort * 2.0f;
156 x /= d;
157 y /= d;
158 z /= d;
159 }
160
161 r_color[i] = ColorGeometry4f(0.5f - x, 0.5f - y, 0.5f - z, 1.0f);
162 });
163 if (compute_factor) {
164 mask.foreach_index([&](const int64_t i) {
165 r_fac[i] = (r_color[i].r + r_color[i].g + r_color[i].b) * (1.0f / 3.0f);
166 });
167 }
168 }
169};
170
172{
173 const bNode &node = builder.node();
174 NodeTexMagic *tex = (NodeTexMagic *)node.storage;
176}
177
178} // namespace blender::nodes::node_shader_tex_magic_cc
179
181{
183
184 static blender::bke::bNodeType ntype;
185
187 ntype.declare = file_ns::sh_node_tex_magic_declare;
188 ntype.draw_buttons = file_ns::node_shader_buts_tex_magic;
189 ntype.initfunc = file_ns::node_shader_init_tex_magic;
192 ntype.gpu_fn = file_ns::node_shader_gpu_tex_magic;
193 ntype.build_multi_function = file_ns::sh_node_magic_tex_build_multi_function;
194
196}
#define SH_NODE_TEX_MAGIC
Definition BKE_node.hh:934
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:414
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:247
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:350
@ TEXMAP_TYPE_POINT
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
local_group_size(16, 16) .push_constant(Type b
#define sinf(x)
#define cosf(x)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
void position(const bNode &, void *r_value)
static void node_shader_buts_tex_magic(uiLayout *layout, bContext *, PointerRNA *ptr)
static void sh_node_tex_magic_declare(NodeDeclarationBuilder &b)
static void node_shader_init_tex_magic(bNodeTree *, bNode *node)
static void sh_node_magic_tex_build_multi_function(NodeMultiFunctionBuilder &builder)
static int node_shader_gpu_tex_magic(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:337
void register_node_type_sh_tex_magic()
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)
void sh_fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
#define min(a, b)
Definition sort.c:32
__int64 int64_t
Definition stdint.h:89
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126