Blender V4.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
5#include "node_shader_util.hh"
6#include "node_util.hh"
7
8#include "BKE_texture.h"
9
10#include "BLI_math_vector.h"
12
13#include "NOD_multi_function.hh"
14
15#include "UI_interface.hh"
16#include "UI_resources.hh"
17
19
21{
22 b.is_function_node();
23 b.add_input<decl::Vector>("Vector").min(-10000.0f).max(10000.0f).implicit_field(
25 b.add_input<decl::Color>("Color1")
26 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
27 .description("Color of the first reference brick");
28 b.add_input<decl::Color>("Color2")
29 .default_value({0.2f, 0.2f, 0.2f, 1.0f})
30 .description("Color of the second reference brick");
31 b.add_input<decl::Color>("Mortar")
32 .default_value({0.0f, 0.0f, 0.0f, 1.0f})
33 .no_muted_links()
34 .description("Color of the area between bricks");
35 b.add_input<decl::Float>("Scale")
36 .min(-1000.0f)
37 .max(1000.0f)
38 .default_value(5.0f)
39 .no_muted_links()
40 .description("Scale of the texture");
41 b.add_input<decl::Float>("Mortar Size")
42 .min(0.0f)
43 .max(0.125f)
44 .default_value(0.02f)
45 .no_muted_links()
46 .description(
47 "Size of the filling between the bricks (known as \"mortar\"). "
48 "0 means no mortar");
49 b.add_input<decl::Float>("Mortar Smooth")
50 .min(0.0f)
51 .max(1.0f)
52 .default_value(0.1f)
53 .no_muted_links()
54 .description(
55 "Blurs/softens the edge between the mortar and the bricks. "
56 "This can be useful with a texture and displacement textures");
57 b.add_input<decl::Float>("Bias").min(-1.0f).max(1.0f).no_muted_links().description(
58 "The color variation between Color1 and Color2. "
59 "Values of -1 and 1 only use one of the two colors. "
60 "Values in between mix the colors");
61 b.add_input<decl::Float>("Brick Width")
62 .min(0.01f)
63 .max(100.0f)
64 .default_value(0.5f)
65 .no_muted_links()
66 .description("Ratio of brick's width relative to the texture scale");
67 b.add_input<decl::Float>("Row Height")
68 .min(0.01f)
69 .max(100.0f)
70 .default_value(0.25f)
71 .no_muted_links()
72 .description("Ratio of brick's row height relative to the texture scale");
73 b.add_output<decl::Color>("Color");
74 b.add_output<decl::Float>("Fac");
75}
76
78{
80
81 col = uiLayoutColumn(layout, true);
83 ptr,
84 "offset",
86 IFACE_("Offset"),
87 ICON_NONE);
88 uiItemR(
89 col, ptr, "offset_frequency", UI_ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Frequency"), ICON_NONE);
90
91 col = uiLayoutColumn(layout, true);
92 uiItemR(col, ptr, "squash", UI_ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Squash"), ICON_NONE);
93 uiItemR(
94 col, ptr, "squash_frequency", UI_ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Frequency"), ICON_NONE);
95}
96
97static void node_shader_init_tex_brick(bNodeTree * /*ntree*/, bNode *node)
98{
99 NodeTexBrick *tex = MEM_cnew<NodeTexBrick>(__func__);
101 BKE_texture_colormapping_default(&tex->base.color_mapping);
102
103 tex->offset = 0.5f;
104 tex->squash = 1.0f;
105 tex->offset_freq = 2;
106 tex->squash_freq = 2;
107
108 node->storage = tex;
109}
110
112 bNode *node,
113 bNodeExecData * /*execdata*/,
114 GPUNodeStack *in,
115 GPUNodeStack *out)
116{
117 node_shader_gpu_default_tex_coord(mat, node, &in[0].link);
118 node_shader_gpu_tex_mapping(mat, node, in, out);
119 NodeTexBrick *tex = (NodeTexBrick *)node->storage;
120 float offset_freq = tex->offset_freq;
121 float squash_freq = tex->squash_freq;
122 return GPU_stack_link(mat,
123 node,
124 "node_tex_brick",
125 in,
126 out,
127 GPU_uniform(&tex->offset),
128 GPU_constant(&offset_freq),
129 GPU_uniform(&tex->squash),
130 GPU_constant(&squash_freq));
131}
132
134 private:
135 const float offset_;
136 const int offset_freq_;
137 const float squash_;
138 const int squash_freq_;
139
140 public:
141 BrickFunction(const float offset,
142 const int offset_freq,
143 const float squash,
144 const int squash_freq)
145 : offset_(offset), offset_freq_(offset_freq), squash_(squash), squash_freq_(squash_freq)
146 {
147 static const mf::Signature signature = []() {
149 mf::SignatureBuilder builder{"BrickTexture", signature};
150 builder.single_input<float3>("Vector");
151 builder.single_input<ColorGeometry4f>("Color1");
152 builder.single_input<ColorGeometry4f>("Color2");
153 builder.single_input<ColorGeometry4f>("Mortar");
154 builder.single_input<float>("Scale");
155 builder.single_input<float>("Mortar Size");
156 builder.single_input<float>("Mortar Smooth");
157 builder.single_input<float>("Bias");
158 builder.single_input<float>("Brick Width");
159 builder.single_input<float>("Row Height");
160 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
161 builder.single_output<float>("Fac", mf::ParamFlag::SupportsUnusedOutput);
162 return signature;
163 }();
164 this->set_signature(&signature);
165 }
166
167 /* Fast integer noise. */
168 static float brick_noise(uint n)
169 {
170 n = (n + 1013) & 0x7fffffff;
171 n = (n >> 13) ^ n;
172 const uint nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
173 return 0.5f * (float(nn) / 1073741824.0f);
174 }
175
176 static float smoothstepf(const float f)
177 {
178 const float ff = f * f;
179 return (3.0f * ff - 2.0f * ff * f);
180 }
181
183 float mortar_size,
184 float mortar_smooth,
185 float bias,
186 float brick_width,
187 float row_height,
188 float offset_amount,
189 int offset_frequency,
190 float squash_amount,
191 int squash_frequency)
192 {
193 float offset = 0.0f;
194
195 const int rownum = int(floorf(p.y / row_height));
196
197 if (offset_frequency && squash_frequency) {
198 brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount;
199 offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount);
200 }
201
202 const int bricknum = int(floorf((p.x + offset) / brick_width));
203
204 const float x = (p.x + offset) - brick_width * bricknum;
205 const float y = p.y - row_height * rownum;
206
207 const float tint = clamp_f(
208 brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias, 0.0f, 1.0f);
209 float min_dist = std::min(std::min(x, y), std::min(brick_width - x, row_height - y));
210
211 float mortar;
212 if (min_dist >= mortar_size) {
213 mortar = 0.0f;
214 }
215 else if (mortar_smooth == 0.0f) {
216 mortar = 1.0f;
217 }
218 else {
219 min_dist = 1.0f - min_dist / mortar_size;
220 mortar = (min_dist < mortar_smooth) ? smoothstepf(min_dist / mortar_smooth) : 1.0f;
221 }
222
223 return float2(tint, mortar);
224 }
225
226 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
227 {
228 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
229 const VArray<ColorGeometry4f> &color1_values = params.readonly_single_input<ColorGeometry4f>(
230 1, "Color1");
231 const VArray<ColorGeometry4f> &color2_values = params.readonly_single_input<ColorGeometry4f>(
232 2, "Color2");
233 const VArray<ColorGeometry4f> &mortar_values = params.readonly_single_input<ColorGeometry4f>(
234 3, "Mortar");
235 const VArray<float> &scale = params.readonly_single_input<float>(4, "Scale");
236 const VArray<float> &mortar_size = params.readonly_single_input<float>(5, "Mortar Size");
237 const VArray<float> &mortar_smooth = params.readonly_single_input<float>(6, "Mortar Smooth");
238 const VArray<float> &bias = params.readonly_single_input<float>(7, "Bias");
239 const VArray<float> &brick_width = params.readonly_single_input<float>(8, "Brick Width");
240 const VArray<float> &row_height = params.readonly_single_input<float>(9, "Row Height");
241
243 params.uninitialized_single_output_if_required<ColorGeometry4f>(10, "Color");
244 MutableSpan<float> r_fac = params.uninitialized_single_output_if_required<float>(11, "Fac");
245
246 const bool store_fac = !r_fac.is_empty();
247 const bool store_color = !r_color.is_empty();
248
249 mask.foreach_index([&](const int64_t i) {
250 const float2 f2 = brick(vector[i] * scale[i],
251 mortar_size[i],
252 mortar_smooth[i],
253 bias[i],
254 brick_width[i],
255 row_height[i],
256 offset_,
257 offset_freq_,
258 squash_,
259 squash_freq_);
260
261 float4 color_data, color1, color2, mortar;
262 copy_v4_v4(color_data, color1_values[i]);
263 copy_v4_v4(color1, color1_values[i]);
264 copy_v4_v4(color2, color2_values[i]);
265 copy_v4_v4(mortar, mortar_values[i]);
266 const float tint = f2.x;
267 const float f = f2.y;
268
269 if (f != 1.0f) {
270 const float facm = 1.0f - tint;
271 color_data = color1 * facm + color2 * tint;
272 }
273
274 if (store_color) {
275 color_data = color_data * (1.0f - f) + mortar * f;
276 copy_v4_v4(r_color[i], color_data);
277 }
278 if (store_fac) {
279 r_fac[i] = f;
280 }
281 });
282 }
283};
284
286{
287 const bNode &node = builder.node();
288 NodeTexBrick *tex = (NodeTexBrick *)node.storage;
289
291 tex->offset, tex->offset_freq, tex->squash, tex->squash_freq);
292}
293
294} // namespace blender::nodes::node_shader_tex_brick_cc
295
297{
299
300 static blender::bke::bNodeType ntype;
301
303 ntype.declare = file_ns::sh_node_tex_brick_declare;
304 ntype.draw_buttons = file_ns::node_shader_buts_tex_brick;
306 ntype.initfunc = file_ns::node_shader_init_tex_brick;
309 ntype.gpu_fn = file_ns::node_shader_gpu_tex_brick;
310 ntype.build_multi_function = file_ns::sh_node_brick_build_multi_function;
311
313}
#define SH_NODE_TEX_BRICK
Definition BKE_node.hh:952
#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
MINLINE float clamp_f(float value, float min, float max)
MINLINE void copy_v4_v4(float r[4], const float a[4])
unsigned int uint
#define IFACE_(msgid)
@ 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)
GPUNodeLink * GPU_uniform(const float *num)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
@ UI_ITEM_R_SLIDER
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
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)
local_group_size(16, 16) .push_constant(Type b
#define floorf(x)
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_size_preset(bNodeType *ntype, eNodeSizePreset size)
Definition node.cc:4614
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 sh_node_brick_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_tex_brick(uiLayout *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)
VecBase< float, 2 > float2
void register_node_type_sh_tex_brick()
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