Blender V5.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
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_texture.h"
13
14#include "NOD_multi_function.hh"
15
17#include "UI_resources.hh"
18
19namespace blender {
20
22
24{
25 b.is_function_node();
26
27 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
28 const NodeDefaultInputType default_input_type =
31 b.add_input<decl::Vector>("Vector"_ustr).default_input_type(default_input_type);
32
33 b.add_input<decl::Float>("Scale"_ustr)
34 .min(-1000.0f)
35 .max(1000.0f)
36 .default_value(5.0f)
37 .description("Scale of the texture");
38 b.add_input<decl::Float>("Distortion"_ustr)
39 .min(-1000.0f)
40 .max(1000.0f)
41 .default_value(1.0f)
42 .description("Amount of distortion");
43 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
44 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr).no_muted_links();
45}
46
47static void node_shader_buts_tex_magic(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
48{
49 layout.prop(ptr, "turbulence_depth", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
50}
51
53{
54 NodeTexMagic *tex = MEM_new<NodeTexMagic>(__func__);
57 tex->depth = 2;
58
59 node->storage = tex;
60}
61
63 bNode *node,
64 bNodeExecData * /*execdata*/,
67{
68 NodeTexMagic *tex = static_cast<NodeTexMagic *>(node->storage);
69 float depth = tex->depth;
70
73
74 return GPU_stack_link(mat, node, "node_tex_magic", in, out, GPU_constant(&depth));
75}
76
77class MagicFunction : public mf::MultiFunction {
78 private:
79 int depth_;
80
81 public:
82 MagicFunction(int depth) : depth_(depth)
83 {
84 static const mf::Signature signature = []() {
85 mf::Signature signature;
86 mf::SignatureBuilder builder{"MagicFunction", signature};
87 builder.single_input<float3>("Vector");
88 builder.single_input<float>("Scale");
89 builder.single_input<float>("Distortion");
90 builder.single_output<ColorGeometry4f>("Color");
91 builder.single_output<float>("Fac", mf::ParamFlag::SupportsUnusedOutput);
92 return signature;
93 }();
95 }
96
97 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
98 {
99 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
100 const VArray<float> &scale = params.readonly_single_input<float>(1, "Scale");
101 const VArray<float> &distortion = params.readonly_single_input<float>(2, "Distortion");
102
103 MutableSpan<ColorGeometry4f> r_color = params.uninitialized_single_output<ColorGeometry4f>(
104 3, "Color");
105 MutableSpan<float> r_fac = params.uninitialized_single_output_if_required<float>(4, "Fac");
106
107 const bool compute_factor = !r_fac.is_empty();
108
109 mask.foreach_index([&](const int64_t i) {
110 const float3 co = vector[i] * scale[i];
111 const float distort = distortion[i];
112 float x = sinf((co[0] + co[1] + co[2]) * 5.0f);
113 float y = cosf((-co[0] + co[1] - co[2]) * 5.0f);
114 float z = -cosf((-co[0] - co[1] + co[2]) * 5.0f);
115
116 if (depth_ > 0) {
117 x *= distort;
118 y *= distort;
119 z *= distort;
120 y = -cosf(x - y + z);
121 y *= distort;
122
123 if (depth_ > 1) {
124 x = cosf(x - y - z);
125 x *= distort;
126
127 if (depth_ > 2) {
128 z = sinf(-x - y - z);
129 z *= distort;
130
131 if (depth_ > 3) {
132 x = -cosf(-x + y - z);
133 x *= distort;
134
135 if (depth_ > 4) {
136 y = -sinf(-x + y + z);
137 y *= distort;
138
139 if (depth_ > 5) {
140 y = -cosf(-x + y + z);
141 y *= distort;
142
143 if (depth_ > 6) {
144 x = cosf(x + y + z);
145 x *= distort;
146
147 if (depth_ > 7) {
148 z = sinf(x + y - z);
149 z *= distort;
150
151 if (depth_ > 8) {
152 x = -cosf(-x - y + z);
153 x *= distort;
154
155 if (depth_ > 9) {
156 y = -sinf(x - y + z);
157 y *= distort;
158 }
159 }
160 }
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168
169 if (distort != 0.0f) {
170 const float d = distort * 2.0f;
171 x /= d;
172 y /= d;
173 z /= d;
174 }
175
176 r_color[i] = ColorGeometry4f(0.5f - x, 0.5f - y, 0.5f - z, 1.0f);
177 });
178 if (compute_factor) {
179 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
180 r_fac[i] = (r_color[i].r + r_color[i].g + r_color[i].b) * (1.0f / 3.0f);
181 });
182 }
183 }
184
185 void hash_unique(UniqueHashBytes &hash) const override
186 {
187 static constexpr int8_t id = 0;
188 hash.add(&id);
189 hash.add(depth_);
190 }
191};
192
194{
195 const bNode &node = builder.node();
196 NodeTexMagic *tex = static_cast<NodeTexMagic *>(node.storage);
198}
199
200} // namespace nodes::node_shader_tex_magic_cc
201
203{
204 namespace file_ns = nodes::node_shader_tex_magic_cc;
205
206 static bke::bNodeType ntype;
207
208 common_node_type_base(&ntype, "ShaderNodeTexMagic"_ustr, SH_NODE_TEX_MAGIC);
209 ntype.ui_name = "Magic Texture";
210 ntype.ui_description = "Generate a psychedelic color texture";
211 ntype.enum_name_legacy = "TEX_MAGIC";
213 ntype.declare = file_ns::sh_node_tex_magic_declare;
214 ntype.draw_buttons = file_ns::node_shader_buts_tex_magic;
215 ntype.initfunc = file_ns::node_shader_init_tex_magic;
218 ntype.gpu_fn = file_ns::node_shader_gpu_tex_magic;
219 ntype.build_multi_function = file_ns::sh_node_magic_tex_build_multi_function;
220
222}
223
224} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_MAGIC
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
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
void hash_unique(UniqueHashBytes &hash) const override
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 node_shader_buts_tex_magic(ui::Layout &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)
BMesh Iterators.
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_magic()
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)
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
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 hash
Definition noise_c.cc:156
#define sinf
#define cosf
#define min(a, b)
Definition sort.cc:38
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
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)