Blender V5.3
node_shader_tex_checker.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
16namespace blender {
17
19
21{
22 b.is_function_node();
23
24 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
25 const NodeDefaultInputType default_input_type =
28 b.add_input<decl::Vector>("Vector"_ustr)
29 .min(-10000.0f)
30 .max(10000.0f)
31 .default_input_type(default_input_type);
32
33 b.add_input<decl::Color>("Color1"_ustr)
34 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
35 .description("Color of the first checker");
36 b.add_input<decl::Color>("Color2"_ustr)
37 .default_value({0.2f, 0.2f, 0.2f, 1.0f})
38 .description("Color of the second checker");
39 b.add_input<decl::Float>("Scale"_ustr)
40 .min(-10000.0f)
41 .max(10000.0f)
42 .default_value(5.0f)
43 .no_muted_links()
44 .description(
45 "Overall texture scale.\n"
46 "The scale is a factor of the bounding box of the face divided by the Scale value");
47 b.add_output<decl::Color>("Color"_ustr);
48 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr);
49}
50
52{
53 NodeTexChecker *tex = MEM_new<NodeTexChecker>(__func__);
54 BKE_texture_mapping_default(&tex->base.tex_mapping, TEXMAP_TYPE_POINT);
55 BKE_texture_colormapping_default(&tex->base.color_mapping);
56
57 node->storage = tex;
58}
59
61 bNode *node,
62 bNodeExecData * /*execdata*/,
65{
68
69 return GPU_stack_link(mat, node, "node_tex_checker", in, out);
70}
71
72class NodeTexChecker : public mf::MultiFunction {
73 public:
75 {
76 static const mf::Signature signature = []() {
77 mf::Signature signature;
78 mf::SignatureBuilder builder{"Checker", signature};
79 builder.single_input<float3>("Vector");
80 builder.single_input<ColorGeometry4f>("Color1");
81 builder.single_input<ColorGeometry4f>("Color2");
82 builder.single_input<float>("Scale");
83 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
84 builder.single_output<float>("Fac");
85 return signature;
86 }();
88 }
89
90 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
91 {
92 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
93 const VArray<ColorGeometry4f> &color1 = params.readonly_single_input<ColorGeometry4f>(
94 1, "Color1");
95 const VArray<ColorGeometry4f> &color2 = params.readonly_single_input<ColorGeometry4f>(
96 2, "Color2");
97 const VArray<float> &scale = params.readonly_single_input<float>(3, "Scale");
99 params.uninitialized_single_output_if_required<ColorGeometry4f>(4, "Color");
100 MutableSpan<float> r_fac = params.uninitialized_single_output<float>(5, "Fac");
101
102 mask.foreach_index([&](const int64_t i) {
103 /* Avoid precision issues on unit coordinates. */
104 const float3 p = (vector[i] * scale[i] + 0.000001f) * 0.999999f;
105
106 const int xi = abs(int(floorf(p.x)));
107 const int yi = abs(int(floorf(p.y)));
108 const int zi = abs(int(floorf(p.z)));
109
110 r_fac[i] = ((xi % 2 == yi % 2) == (zi % 2)) ? 1.0f : 0.0f;
111 });
112
113 if (!r_color.is_empty()) {
114 mask.foreach_index_optimized<int64_t>(
115 [&](const int64_t i) { r_color[i] = (r_fac[i] == 1.0f) ? color1[i] : color2[i]; });
116 }
117 }
118};
119
125
127#ifdef WITH_MATERIALX
128{
129 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
130 if (!vector) {
131 vector = texcoord_node();
132 }
133 NodeItem value1 = val(1.0f);
134 NodeItem value2 = val(0.0f);
135 if (STREQ(socket_out_->identifier, "Color")) {
136 value1 = get_input_value("Color1", NodeItem::Type::Color3);
137 value2 = get_input_value("Color2", NodeItem::Type::Color3);
138 }
139 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
140
141 vector = (vector * scale) % val(2.0f);
142 return (vector[0].floor() + vector[1].floor())
143 .if_else(NodeItem::CompareOp::Eq, val(1.0f), value1, value2);
144}
145#endif
147
148} // namespace nodes::node_shader_tex_checker_cc
149
151{
152 namespace file_ns = nodes::node_shader_tex_checker_cc;
153
154 static bke::bNodeType ntype;
155
156 common_node_type_base(&ntype, "ShaderNodeTexChecker"_ustr, SH_NODE_TEX_CHECKER);
157 ntype.ui_name = "Checker Texture";
158 ntype.ui_description = "Generate a checkerboard texture";
159 ntype.enum_name_legacy = "TEX_CHECKER";
161 ntype.declare = file_ns::sh_node_tex_checker_declare;
162 ntype.initfunc = file_ns::node_shader_init_tex_checker;
165 ntype.gpu_fn = file_ns::node_shader_gpu_tex_checker;
166 ntype.build_multi_function = file_ns::sh_node_tex_checker_build_multi_function;
167 ntype.materialx_fn = file_ns::node_shader_materialx;
168
170}
171
172} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_CHECKER
#define STREQ(a, b)
void set_signature(const Signature *signature)
void set_matching_fn(const mf::MultiFunction *fn)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
#define abs
#define floor
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_tex_checker_declare(NodeDeclarationBuilder &b)
static void sh_node_tex_checker_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_tex_checker(bNodeTree *, bNode *node)
static int node_shader_gpu_tex_checker(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_checker()
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
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define floorf
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
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