Blender V5.3
node_shader_radial_tiling.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024-2025 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 "NOD_multi_function.hh"
13
14#include "RNA_access.hh"
15
17#include "UI_resources.hh"
18
19#include "BLI_radial_tiling.hh"
20
21namespace blender {
22
24
26
28{
29 b.is_function_node();
30
31 b.add_output<decl::Vector>("Segment Coordinates"_ustr)
32 .no_muted_links()
33 .description("Segment coordinates for texture mapping within each angular segment");
34 b.add_output<decl::Float>("Segment ID"_ustr)
35 .no_muted_links()
36 .description(
37 "Unique ID for every angular segment starting at 0 and increasing counterclockwise by "
38 "1");
39 b.add_output<decl::Float>("Segment Width"_ustr)
40 .no_muted_links()
41 .description(
42 "Relative width of each angular segment. "
43 "May be used to scale textures to fit into each segment");
44 b.add_output<decl::Float>("Segment Rotation"_ustr)
45 .no_muted_links()
46 .description(
47 "Counterclockwise rotation of each segment coordinates system. May be used to align the "
48 "rotation of the textures of each segment");
49
50 b.add_input<decl::Vector>("Vector"_ustr)
51 .dimensions(2)
52 .default_value(float3{0.0f, 0.0f, 0.0f})
53 .description("Input texture coordinates");
54 b.add_input<decl::Float>("Sides"_ustr)
55 .min(2.0f)
56 .max(1000.0f)
57 .default_value(5.0f)
58 .description(
59 "Number of angular segments for tiling. A non-integer value results in an irregular "
60 "segment");
61 b.add_input<decl::Float>("Roundness"_ustr)
62 .min(0.0f)
63 .max(1.0f)
64 .default_value(0.0f)
65 .subtype(PROP_FACTOR)
66 .description("Roundness of the segment coordinates systems");
67}
68
70{
71 layout.prop(ptr, "normalize", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
72}
73
75{
76 NodeRadialTiling *storage = MEM_new<NodeRadialTiling>(__func__);
77 storage->normalize = false;
78
79 node->storage = storage;
80}
81
82static const char *gpu_shader_get_name()
83{
84 return "node_radial_tiling";
85}
86
88 bNode *node,
89 bNodeExecData * /*execdata*/,
92{
93 const NodeRadialTiling &storage = node_storage(*node);
94 float normalize_r_gon_parameter = storage.normalize;
95 float calculate_r_gon_parameter_field = out[0].hasoutput;
96 float calculate_segment_id = out[1].hasoutput;
97 float calculate_max_unit_parameter = out[2].hasoutput;
98 float calculate_x_axis_A_angle_bisector = out[3].hasoutput;
99
100 const char *name = gpu_shader_get_name();
101
102 return GPU_stack_link(mat,
103 node,
104 name,
105 in,
106 out,
107 GPU_constant(&normalize_r_gon_parameter),
108 GPU_constant(&calculate_r_gon_parameter_field),
109 GPU_constant(&calculate_segment_id),
110 GPU_constant(&calculate_max_unit_parameter),
111 GPU_constant(&calculate_x_axis_A_angle_bisector));
112}
113
114class RoundedPolygonFunction : public mf::MultiFunction {
115 private:
116 bool normalize_r_gon_parameter_;
117
118 mf::Signature signature_;
119
120 public:
121 RoundedPolygonFunction(bool normalize_r_gon_parameter)
122 : normalize_r_gon_parameter_(normalize_r_gon_parameter)
123 {
124 signature_ = create_signature();
125 this->set_signature(&signature_);
126 }
127
128 static mf::Signature create_signature()
129 {
130 mf::Signature signature;
131 mf::SignatureBuilder builder{"radial_tiling", signature};
132
133 builder.single_input<float3>("Vector");
134
135 builder.single_input<float>("Sides");
136 builder.single_input<float>("Roundness");
137
138 builder.single_output<float3>("Segment Coordinates", mf::ParamFlag::SupportsUnusedOutput);
139 builder.single_output<float>("Segment ID", mf::ParamFlag::SupportsUnusedOutput);
140 builder.single_output<float>("Segment Width", mf::ParamFlag::SupportsUnusedOutput);
141 builder.single_output<float>("Segment Rotation", mf::ParamFlag::SupportsUnusedOutput);
142
143 return signature;
144 }
145
146 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
147 {
148 int param = 0;
149
150 const VArray<float3> &coord = params.readonly_single_input<float3>(param++, "Vector");
151
152 const VArray<float> &r_gon_sides = params.readonly_single_input<float>(param++, "Sides");
153 const VArray<float> &r_gon_roundness = params.readonly_single_input<float>(param++,
154 "Roundness");
155
156 MutableSpan<float3> r_segment_coordinates =
157 params.uninitialized_single_output_if_required<float3>(param++, "Segment Coordinates");
158 MutableSpan<float> r_segment_id = params.uninitialized_single_output_if_required<float>(
159 param++, "Segment ID");
160 MutableSpan<float> r_max_unit_parameter =
161 params.uninitialized_single_output_if_required<float>(param++, "Segment Width");
162 MutableSpan<float> r_x_axis_A_angle_bisector =
163 params.uninitialized_single_output_if_required<float>(param++, "Segment Rotation");
164
165 const bool calculate_r_gon_parameter_field = !r_segment_coordinates.is_empty();
166 const bool calculate_segment_id = !r_segment_id.is_empty();
167 const bool calculate_max_unit_parameter = !r_max_unit_parameter.is_empty();
168 const bool calculate_x_axis_A_angle_bisector = !r_x_axis_A_angle_bisector.is_empty();
169
170 mask.foreach_index([&](const int64_t i) {
171 if (calculate_r_gon_parameter_field || calculate_max_unit_parameter ||
172 calculate_x_axis_A_angle_bisector)
173 {
174 float4 out_variables = calculate_out_variables(calculate_r_gon_parameter_field,
175 calculate_max_unit_parameter,
176 normalize_r_gon_parameter_,
177 math::max(r_gon_sides[i], 2.0f),
178 math::clamp(r_gon_roundness[i], 0.0f, 1.0f),
179 float2(coord[i].x, coord[i].y));
180
181 if (calculate_r_gon_parameter_field) {
182 r_segment_coordinates[i] = float3(out_variables.y, out_variables.x, 0.0f);
183 }
184 if (calculate_max_unit_parameter) {
185 r_max_unit_parameter[i] = out_variables.z;
186 }
187 if (calculate_x_axis_A_angle_bisector) {
188 r_x_axis_A_angle_bisector[i] = out_variables.w;
189 }
190 }
191
192 if (calculate_segment_id) {
193 r_segment_id[i] = calculate_out_segment_id(math::max(r_gon_sides[i], 2.0f),
194 float2(coord[i].x, coord[i].y));
195 }
196 });
197 }
198
199 void hash_unique(UniqueHashBytes &hash) const override
200 {
201 static constexpr int8_t id = 0;
202 hash.add(&id);
203 hash.add(normalize_r_gon_parameter_);
204 }
205
207 {
208 ExecutionHints hints;
209 hints.allocates_array = false;
210 hints.min_grain_size = 50;
211 return hints;
212 }
213};
214
220
221} // namespace nodes::node_shader_radial_tiling_cc
222
224{
225 namespace file_ns = nodes::node_shader_radial_tiling_cc;
226
227 static bke::bNodeType ntype;
228
229 common_node_type_base(&ntype, "ShaderNodeRadialTiling"_ustr);
230 ntype.ui_name = "Radial Tiling";
231 ntype.ui_description = "Transform Coordinate System for Radial Tiling";
233 ntype.declare = file_ns::sh_node_radial_tiling_declare;
234 ntype.draw_buttons = file_ns::node_shader_buts_radial_tiling;
235 ntype.initfunc = file_ns::node_shader_init_radial_tiling;
237 ntype, "NodeRadialTiling", node_free_standard_storage, node_copy_standard_storage);
238 ntype.gpu_fn = file_ns::node_shader_gpu_radial_tiling;
239 ntype.build_multi_function = file_ns::sh_node_radial_tiling_build_multi_function;
240
242}
243
244} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
btScalar max
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
#define storage(slot, qualifiers)
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
T clamp(const T &a, const T &min, const T &max)
T max(const T &a, const T &b)
static void node_shader_init_radial_tiling(bNodeTree *, bNode *node)
static int node_shader_gpu_radial_tiling(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_radial_tiling_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_radial_tiling(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_radial_tiling_declare(NodeDeclarationBuilder &b)
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
VecBase< float, 4 > float4
GPUNodeLink * GPU_constant(const float *num)
VecBase< float, 2 > float2
float calculate_out_segment_id(float r_gon_sides, float2 coord)
void register_node_type_sh_radial_tiling()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_FACTOR
Definition RNA_types.hh:308
float4 calculate_out_variables(bool calculate_r_gon_parameter_field, bool calculate_max_unit_parameter, bool normalize_r_gon_parameter, float r_gon_sides, float r_gon_roundness, float2 coord)
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
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
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)