Blender V4.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
5#include "node_shader_util.hh"
6#include "node_util.hh"
7
8#include "BKE_texture.h"
9
10#include "NOD_multi_function.hh"
11
13
15{
16 b.is_function_node();
17 b.add_input<decl::Vector>("Vector").min(-10000.0f).max(10000.0f).implicit_field(
19 b.add_input<decl::Color>("Color1")
20 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
21 .description("Color of the first checker");
22 b.add_input<decl::Color>("Color2")
23 .default_value({0.2f, 0.2f, 0.2f, 1.0f})
24 .description("Color of the second checker");
25 b.add_input<decl::Float>("Scale")
26 .min(-10000.0f)
27 .max(10000.0f)
28 .default_value(5.0f)
29 .no_muted_links()
30 .description(
31 "Overall texture scale.\n"
32 "The scale is a factor of the bounding box of the face divided by the Scale value");
33 b.add_output<decl::Color>("Color");
34 b.add_output<decl::Float>("Fac");
35}
36
37static void node_shader_init_tex_checker(bNodeTree * /*ntree*/, bNode *node)
38{
39 NodeTexChecker *tex = MEM_cnew<NodeTexChecker>(__func__);
41 BKE_texture_colormapping_default(&tex->base.color_mapping);
42
43 node->storage = tex;
44}
45
47 bNode *node,
48 bNodeExecData * /*execdata*/,
49 GPUNodeStack *in,
50 GPUNodeStack *out)
51{
52 node_shader_gpu_default_tex_coord(mat, node, &in[0].link);
53 node_shader_gpu_tex_mapping(mat, node, in, out);
54
55 return GPU_stack_link(mat, node, "node_tex_checker", in, out);
56}
57
59 public:
61 {
62 static const mf::Signature signature = []() {
64 mf::SignatureBuilder builder{"Checker", signature};
65 builder.single_input<float3>("Vector");
66 builder.single_input<ColorGeometry4f>("Color1");
67 builder.single_input<ColorGeometry4f>("Color2");
68 builder.single_input<float>("Scale");
69 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
70 builder.single_output<float>("Fac");
71 return signature;
72 }();
73 this->set_signature(&signature);
74 }
75
76 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
77 {
78 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
79 const VArray<ColorGeometry4f> &color1 = params.readonly_single_input<ColorGeometry4f>(
80 1, "Color1");
81 const VArray<ColorGeometry4f> &color2 = params.readonly_single_input<ColorGeometry4f>(
82 2, "Color2");
83 const VArray<float> &scale = params.readonly_single_input<float>(3, "Scale");
85 params.uninitialized_single_output_if_required<ColorGeometry4f>(4, "Color");
86 MutableSpan<float> r_fac = params.uninitialized_single_output<float>(5, "Fac");
87
88 mask.foreach_index([&](const int64_t i) {
89 /* Avoid precision issues on unit coordinates. */
90 const float3 p = (vector[i] * scale[i] + 0.000001f) * 0.999999f;
91
92 const int xi = abs(int(floorf(p.x)));
93 const int yi = abs(int(floorf(p.y)));
94 const int zi = abs(int(floorf(p.z)));
95
96 r_fac[i] = ((xi % 2 == yi % 2) == (zi % 2)) ? 1.0f : 0.0f;
97 });
98
99 if (!r_color.is_empty()) {
100 mask.foreach_index(
101 [&](const int64_t i) { r_color[i] = (r_fac[i] == 1.0f) ? color1[i] : color2[i]; });
102 }
103 }
104};
105
107{
108 static NodeTexChecker fn;
109 builder.set_matching_fn(fn);
110}
111
113#ifdef WITH_MATERIALX
114{
115 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
116 if (!vector) {
117 vector = texcoord_node();
118 }
119 NodeItem value1 = val(1.0f);
120 NodeItem value2 = val(0.0f);
121 if (STREQ(socket_out_->name, "Color")) {
122 value1 = get_input_value("Color1", NodeItem::Type::Color4);
123 value2 = get_input_value("Color2", NodeItem::Type::Color4);
124 }
125 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
126
127 vector = (vector * scale) % val(2.0f);
128 return (vector[0].floor() + vector[1].floor())
129 .if_else(NodeItem::CompareOp::Eq, val(1.0f), value1, value2);
130}
131#endif
133
134} // namespace blender::nodes::node_shader_tex_checker_cc
135
137{
139
140 static blender::bke::bNodeType ntype;
141
143 ntype.declare = file_ns::sh_node_tex_checker_declare;
144 ntype.initfunc = file_ns::node_shader_init_tex_checker;
146 &ntype, "NodeTexChecker", node_free_standard_storage, node_copy_standard_storage);
147 ntype.gpu_fn = file_ns::node_shader_gpu_tex_checker;
148 ntype.build_multi_function = file_ns::sh_node_tex_checker_build_multi_function;
149 ntype.materialx_fn = file_ns::node_shader_materialx;
150
152}
#define SH_NODE_TEX_CHECKER
Definition BKE_node.hh:947
#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
#define STREQ(a, b)
@ TEXMAP_TYPE_POINT
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
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
local_group_size(16, 16) .push_constant(Type b
#define floorf(x)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 floor(const float2 a)
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_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)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
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 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
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:320
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
NodeDeclareFunction declare
Definition BKE_node.hh:347
ccl_device_inline int abs(int x)
Definition util/math.h:120