Blender V5.3
node_texture_bricks.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 "BKE_material.hh"
10#include "BLI_math_vector_c.hh"
11#include "DNA_material_types.h"
12#include "node_texture_util.hh"
13
14#include <cmath>
15
16namespace blender {
17
19 {SOCK_RGBA, N_("Bricks 1"), 0.596f, 0.282f, 0.0f, 1.0f},
20 {SOCK_RGBA, N_("Bricks 2"), 0.632f, 0.504f, 0.05f, 1.0f},
21 {SOCK_RGBA, N_("Mortar"), 0.0f, 0.0f, 0.0f, 1.0f},
22 {SOCK_FLOAT, N_("Thickness"), 0.02f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_UNSIGNED},
23 {SOCK_FLOAT, N_("Bias"), 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, PROP_NONE},
24 {SOCK_FLOAT, N_("Brick Width"), 0.5f, 0.0f, 0.0f, 0.0f, 0.001f, 99.0f, PROP_UNSIGNED},
25 {SOCK_FLOAT, N_("Row Height"), 0.25f, 0.0f, 0.0f, 0.0f, 0.001f, 99.0f, PROP_UNSIGNED},
26 {-1, ""},
27};
29 {SOCK_RGBA, N_("Color")},
30 {-1, ""},
31};
32
33static void init(bNodeTree * /*ntree*/, bNode *node)
34{
35 node->custom3 = 0.5; /* offset */
36 node->custom4 = 1.0; /* squash */
37}
38
39static float noise(int n) /* fast integer noise */
40{
41 int nn;
42 n = (n >> 13) ^ n;
43 nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
44 return 0.5f * (float(nn) / 1073741824.0f);
45}
46
47static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
48{
49 const float *co = p->co;
50
51 float x = co[0];
52 float y = co[1];
53
54 int bricknum, rownum;
55 float offset = 0;
56 float ins_x, ins_y;
57 float tint;
58
59 float bricks1[4];
60 float bricks2[4];
61 float mortar[4];
62
63 float mortar_thickness = tex_input_value(in[3], p, thread);
64 float bias = tex_input_value(in[4], p, thread);
65 float brick_width = tex_input_value(in[5], p, thread);
66 float row_height = tex_input_value(in[6], p, thread);
67
68 tex_input_rgba(bricks1, in[0], p, thread);
69 tex_input_rgba(bricks2, in[1], p, thread);
70 tex_input_rgba(mortar, in[2], p, thread);
71
72 rownum = int(floor(y / row_height));
73
74 if (node->custom1 && node->custom2) {
75 brick_width *= (rownum % node->custom2) ? 1.0f : node->custom4; /* squash */
76 offset = (rownum % node->custom1) ? 0 : (brick_width * node->custom3); /* offset */
77 }
78
79 bricknum = int(floor((x + offset) / brick_width));
80
81 ins_x = (x + offset) - brick_width * bricknum;
82 ins_y = y - row_height * rownum;
83
84 tint = noise((rownum << 16) + (bricknum & 0xFFFF)) + bias;
85 CLAMP(tint, 0.0f, 1.0f);
86
87 if (ins_x < mortar_thickness || ins_y < mortar_thickness ||
88 ins_x > (brick_width - mortar_thickness) || ins_y > (row_height - mortar_thickness))
89 {
90 copy_v4_v4(out, mortar);
91 }
92 else {
93 copy_v4_v4(out, bricks1);
94 ramp_blend(MA_RAMP_BLEND, out, tint, bricks2);
95 }
96}
97
98static void exec(void *data,
99 int /*thread*/,
100 bNode *node,
101 bNodeExecData *execdata,
102 bNodeStack **in,
103 bNodeStack **out)
104{
105 tex_output(node, execdata, in, out[0], &colorfn, static_cast<TexCallData *>(data));
106}
107
109{
110 static bke::bNodeType ntype;
111
112 tex_node_type_base(&ntype, "TextureNodeBricks"_ustr, TEX_NODE_BRICKS);
113 ntype.ui_name = "Bricks";
114 ntype.enum_name_legacy = "BRICKS";
118 ntype.initfunc = init;
119 ntype.exec_fn = exec;
120 ntype.flag |= NODE_PREVIEW;
121
123}
124
125} // namespace blender
General operations, lookup, etc. for materials.
#define NODE_CLASS_PATTERN
Definition BKE_node.hh:487
#define TEX_NODE_BRICKS
#define CLAMP(a, b, c)
#define floor
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_socket_templates(bNodeType *ntype, bNodeSocketTemplate *inputs, bNodeSocketTemplate *outputs)
Definition node.cc:6134
BMesh Iterators.
void ramp_blend(int type, float r_col[4], float fac, const float col[4])
static void exec(void *data, int, bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
static void colorfn(float *out, TexParams *p, bNode *, bNodeStack **in, short thread)
void register_node_type_tex_bricks()
BMesh const char void * data
void tex_output(bNode *node, bNodeExecData *, bNodeStack **in, bNodeStack *out, TexFn texfn, TexCallData *cdata)
static bke::bNodeSocketTemplate inputs[]
void tex_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
float tex_input_value(bNodeStack *in, TexParams *params, short thread)
static bke::bNodeSocketTemplate outputs[]
MINLINE void copy_v4_v4(float r[4], const float a[4])
@ PROP_UNSIGNED
Definition RNA_types.hh:306
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int _160
Definition BKE_node.hh:246
Compact definition of a node socket.
Definition BKE_node.hh:102
Defines a node type.
Definition BKE_node.hh:265
NodeExecFunction exec_fn
Definition BKE_node.hh:371
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
#define N_(msgid)