Blender V5.3
node_shader_tex_brick.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 <algorithm>
10
11#include "node_shader_util.hh"
12#include "node_util.hh"
13
14#include "BKE_texture.h"
15
16#include "BLI_math_vector_c.hh"
18
19#include "NOD_multi_function.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)
37 .min(-10000.0f)
38 .max(10000.0f)
39 .default_input_type(default_input_type);
40
41 b.add_input<decl::Color>("Color1"_ustr)
42 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
43 .description("Color of the first reference brick");
44 b.add_input<decl::Color>("Color2"_ustr)
45 .default_value({0.2f, 0.2f, 0.2f, 1.0f})
46 .description("Color of the second reference brick");
47 b.add_input<decl::Color>("Mortar"_ustr)
48 .default_value({0.0f, 0.0f, 0.0f, 1.0f})
49 .no_muted_links()
50 .description("Color of the area between bricks");
51 b.add_input<decl::Float>("Scale"_ustr)
52 .min(-1000.0f)
53 .max(1000.0f)
54 .default_value(5.0f)
55 .no_muted_links()
56 .description("Scale of the texture");
57 b.add_input<decl::Float>("Mortar Size"_ustr)
58 .min(0.0f)
59 .max(0.125f)
60 .default_value(0.02f)
61 .no_muted_links()
62 .description(
63 "Size of the filling between the bricks (known as \"mortar\"). "
64 "0 means no mortar");
65 b.add_input<decl::Float>("Mortar Smooth"_ustr)
66 .min(0.0f)
67 .max(1.0f)
68 .default_value(0.1f)
69 .no_muted_links()
70 .description(
71 "Blurs/softens the edge between the mortar and the bricks. "
72 "This can be useful with a texture and displacement textures");
73 b.add_input<decl::Float>("Bias"_ustr)
74 .min(-1.0f)
75 .max(1.0f)
76 .no_muted_links()
77 .description(
78 "The color variation between Color1 and Color2. "
79 "Values of -1 and 1 only use one of the two colors. "
80 "Values in between mix the colors");
81 b.add_input<decl::Float>("Brick Width"_ustr)
82 .min(0.01f)
83 .max(100.0f)
84 .default_value(0.5f)
85 .no_muted_links()
86 .description("Ratio of brick's width relative to the texture scale");
87 b.add_input<decl::Float>("Row Height"_ustr)
88 .min(0.01f)
89 .max(100.0f)
90 .default_value(0.25f)
91 .no_muted_links()
92 .description("Ratio of brick's row height relative to the texture scale");
93 b.add_output<decl::Color>("Color"_ustr);
94 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr);
95}
96
97static void node_shader_buts_tex_brick(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
98{
99 {
100 ui::Layout &col = layout.column(true);
101 col.prop(ptr,
102 "offset",
104 IFACE_("Offset"),
105 ICON_NONE);
106 col.prop(ptr, "offset_frequency", ui::ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Frequency"), ICON_NONE);
107 }
108 {
109 ui::Layout &col = layout.column(true);
110 col.prop(ptr, "squash", ui::ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Squash"), ICON_NONE);
111 col.prop(ptr, "squash_frequency", ui::ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Frequency"), ICON_NONE);
112 }
113}
114
116{
117 NodeTexBrick *tex = MEM_new<NodeTexBrick>(__func__);
120
121 tex->offset = 0.5f;
122 tex->squash = 1.0f;
123 tex->offset_freq = 2;
124 tex->squash_freq = 2;
125
126 node->storage = tex;
127}
128
130 bNode *node,
131 bNodeExecData * /*execdata*/,
134{
137 NodeTexBrick *tex = static_cast<NodeTexBrick *>(node->storage);
138 float offset_freq = tex->offset_freq;
139 float squash_freq = tex->squash_freq;
140 return GPU_stack_link(mat,
141 node,
142 "node_tex_brick",
143 in,
144 out,
145 GPU_uniform(&tex->offset),
146 GPU_constant(&offset_freq),
147 GPU_uniform(&tex->squash),
148 GPU_constant(&squash_freq));
149}
150
151class BrickFunction : public mf::MultiFunction {
152 private:
153 const float offset_;
154 const int offset_freq_;
155 const float squash_;
156 const int squash_freq_;
157
158 public:
159 BrickFunction(const float offset,
160 const int offset_freq,
161 const float squash,
162 const int squash_freq)
163 : offset_(offset), offset_freq_(offset_freq), squash_(squash), squash_freq_(squash_freq)
164 {
165 static const mf::Signature signature = []() {
166 mf::Signature signature;
167 mf::SignatureBuilder builder{"BrickTexture", signature};
168 builder.single_input<float3>("Vector");
169 builder.single_input<ColorGeometry4f>("Color1");
170 builder.single_input<ColorGeometry4f>("Color2");
171 builder.single_input<ColorGeometry4f>("Mortar");
172 builder.single_input<float>("Scale");
173 builder.single_input<float>("Mortar Size");
174 builder.single_input<float>("Mortar Smooth");
175 builder.single_input<float>("Bias");
176 builder.single_input<float>("Brick Width");
177 builder.single_input<float>("Row Height");
178 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
179 builder.single_output<float>("Fac", mf::ParamFlag::SupportsUnusedOutput);
180 return signature;
181 }();
182 this->set_signature(&signature);
183 }
184
185 /* Fast integer noise. */
186 static float brick_noise(uint n)
187 {
188 n = (n + 1013) & 0x7fffffff;
189 n = (n >> 13) ^ n;
190 const uint nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
191 return 0.5f * (float(nn) / 1073741824.0f);
192 }
193
194 static float smoothstepf(const float f)
195 {
196 const float ff = f * f;
197 return (3.0f * ff - 2.0f * ff * f);
198 }
199
201 float mortar_size,
202 float mortar_smooth,
203 float bias,
204 float brick_width,
205 float row_height,
206 float offset_amount,
207 int offset_frequency,
208 float squash_amount,
209 int squash_frequency)
210 {
211 float offset = 0.0f;
212
213 const int rownum = int(floorf(p.y / row_height));
214
215 if (offset_frequency && squash_frequency) {
216 brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount;
217 offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount);
218 }
219
220 const int bricknum = int(floorf((p.x + offset) / brick_width));
221
222 const float x = (p.x + offset) - brick_width * bricknum;
223 const float y = p.y - row_height * rownum;
224
225 const float tint = clamp_f(
226 brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias, 0.0f, 1.0f);
227 float min_dist = std::min({x, y, brick_width - x, row_height - y});
228
229 float mortar;
230 if (min_dist >= mortar_size) {
231 mortar = 0.0f;
232 }
233 else if (mortar_smooth == 0.0f) {
234 mortar = 1.0f;
235 }
236 else {
237 min_dist = 1.0f - min_dist / mortar_size;
238 mortar = (min_dist < mortar_smooth) ? smoothstepf(min_dist / mortar_smooth) : 1.0f;
239 }
240
241 return float2(tint, mortar);
242 }
243
244 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
245 {
246 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
247 const VArray<ColorGeometry4f> &color1_values = params.readonly_single_input<ColorGeometry4f>(
248 1, "Color1");
249 const VArray<ColorGeometry4f> &color2_values = params.readonly_single_input<ColorGeometry4f>(
250 2, "Color2");
251 const VArray<ColorGeometry4f> &mortar_values = params.readonly_single_input<ColorGeometry4f>(
252 3, "Mortar");
253 const VArray<float> &scale = params.readonly_single_input<float>(4, "Scale");
254 const VArray<float> &mortar_size = params.readonly_single_input<float>(5, "Mortar Size");
255 const VArray<float> &mortar_smooth = params.readonly_single_input<float>(6, "Mortar Smooth");
256 const VArray<float> &bias = params.readonly_single_input<float>(7, "Bias");
257 const VArray<float> &brick_width = params.readonly_single_input<float>(8, "Brick Width");
258 const VArray<float> &row_height = params.readonly_single_input<float>(9, "Row Height");
259
261 params.uninitialized_single_output_if_required<ColorGeometry4f>(10, "Color");
262 MutableSpan<float> r_fac = params.uninitialized_single_output_if_required<float>(11, "Fac");
263
264 const bool store_fac = !r_fac.is_empty();
265 const bool store_color = !r_color.is_empty();
266
267 mask.foreach_index([&](const int64_t i) {
268 const float2 f2 = brick(vector[i] * scale[i],
269 mortar_size[i],
270 mortar_smooth[i],
271 bias[i],
272 brick_width[i],
273 row_height[i],
274 offset_,
275 offset_freq_,
276 squash_,
277 squash_freq_);
278
279 float4 color_data, color1, color2, mortar;
280 copy_v4_v4(color_data, color1_values[i]);
281 copy_v4_v4(color1, color1_values[i]);
282 copy_v4_v4(color2, color2_values[i]);
283 copy_v4_v4(mortar, mortar_values[i]);
284 const float tint = f2.x;
285 const float f = f2.y;
286
287 if (f != 1.0f) {
288 const float facm = 1.0f - tint;
289 color_data = color1 * facm + color2 * tint;
290 }
291
292 if (store_color) {
293 color_data = color_data * (1.0f - f) + mortar * f;
294 copy_v4_v4(r_color[i], color_data);
295 }
296 if (store_fac) {
297 r_fac[i] = f;
298 }
299 });
300 }
301
302 void hash_unique(UniqueHashBytes &hash) const override
303 {
304 static constexpr int8_t id = 0;
305 hash.add(&id);
306 hash.add(offset_);
307 hash.add(offset_freq_);
308 hash.add(squash_);
309 hash.add(squash_freq_);
310 }
311};
312
314{
315 const bNode &node = builder.node();
316 NodeTexBrick *tex = static_cast<NodeTexBrick *>(node.storage);
317
319 tex->offset, tex->offset_freq, tex->squash, tex->squash_freq);
320}
321
322} // namespace nodes::node_shader_tex_brick_cc
323
325{
326 namespace file_ns = nodes::node_shader_tex_brick_cc;
327
328 static bke::bNodeType ntype;
329
330 common_node_type_base(&ntype, "ShaderNodeTexBrick"_ustr, SH_NODE_TEX_BRICK);
331 ntype.ui_name = "Brick Texture";
332 ntype.ui_description = "Generate a procedural texture producing bricks";
333 ntype.enum_name_legacy = "TEX_BRICK";
335 ntype.declare = file_ns::sh_node_tex_brick_declare;
336 ntype.draw_buttons = file_ns::node_shader_buts_tex_brick;
338 ntype.initfunc = file_ns::node_shader_init_tex_brick;
341 ntype.gpu_fn = file_ns::node_shader_gpu_tex_brick;
342 ntype.build_multi_function = file_ns::sh_node_brick_build_multi_function;
344
346}
347
348} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_BRICK
#define IFACE_(msgid)
constexpr bool is_empty() const
Definition BLI_span.hh:515
void set_signature(const Signature *signature)
BrickFunction(const float offset, const int offset_freq, const float squash, const int squash_freq)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void hash_unique(UniqueHashBytes &hash) const override
static float2 brick(float3 p, float mortar_size, float mortar_smooth, float bias, float brick_width, float row_height, float offset_amount, int offset_frequency, float squash_amount, int squash_frequency)
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_brick_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_tex_brick(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_tex_brick_declare(NodeDeclarationBuilder &b)
static void node_shader_init_tex_brick(bNodeTree *, bNode *node)
static int node_shader_gpu_tex_brick(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
unsigned int uint
Definition BKE_text.h:55
GPUNodeLink * GPU_uniform(const float *num)
VecBase< float, 4 > float4
GPUNodeLink * GPU_constant(const float *num)
VecBase< float, 2 > float2
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 register_node_type_sh_tex_brick()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
MINLINE void copy_v4_v4(float r[4], const float a[4])
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)
MINLINE float clamp_f(float value, float min, float max)
#define hash
Definition noise_c.cc:156
#define floorf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
static constexpr int _160
Definition BKE_node.hh:246
static constexpr int _180
Definition BKE_node.hh:247
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)
Layout & column(bool align)