Blender V5.3
node_shader_mapping.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
12#include "UI_resources.hh"
13
14namespace blender {
15
17
19{
20 b.add_input<decl::Vector>("Vector"_ustr)
21 .default_value({0.0f, 0.0f, 0.0f})
22 .min(-FLT_MAX)
23 .max(FLT_MAX)
24 .description("The vector to be transformed");
25 b.add_input<decl::Vector>("Location"_ustr)
26 .default_value({0.0f, 0.0f, 0.0f})
27 .min(-FLT_MAX)
28 .max(FLT_MAX)
29 .subtype(PROP_TRANSLATION)
30 .description("The amount of translation along each axis");
31 b.add_input<decl::Vector>("Rotation"_ustr)
32 .default_value({0.0f, 0.0f, 0.0f})
33 .min(-FLT_MAX)
34 .max(FLT_MAX)
35 .subtype(PROP_EULER)
36 .description("The amount of rotation along each axis, XYZ order");
37 b.add_input<decl::Vector>("Scale"_ustr)
38 .default_value({1.0f, 1.0f, 1.0f})
39 .min(-FLT_MAX)
40 .max(FLT_MAX)
41 .subtype(PROP_XYZ)
42 .description("The amount of scaling along each axis");
43 b.add_output<decl::Vector>("Vector"_ustr);
44}
45
46static void node_shader_buts_mapping(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
47{
48 layout.prop(ptr, "vector_type", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
49}
50
51static const char *gpu_shader_get_name(int mode)
52{
53 switch (mode) {
55 return "mapping_point";
57 return "mapping_texture";
59 return "mapping_vector";
61 return "mapping_normal";
62 }
63 return nullptr;
64}
65
67 bNode *node,
68 bNodeExecData * /*execdata*/,
71{
72 if (gpu_shader_get_name(node->custom1)) {
73 return GPU_stack_link(mat, node, gpu_shader_get_name(node->custom1), in, out);
74 }
75
76 return 0;
77}
78
85
87#ifdef WITH_MATERIALX
88{
89 NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
90 NodeItem scale = get_input_value("Scale", NodeItem::Type::Vector3);
91 NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
92 val(float(180.0f / M_PI));
93
94 int type = node_->custom1;
95 switch (type) {
97 NodeItem location = get_input_value("Location", NodeItem::Type::Vector3);
98 return (vector * scale).rotate(rotation) + location;
99 }
101 NodeItem location = get_input_value("Location", NodeItem::Type::Vector3);
102 return (vector - location).rotate(rotation, true) / scale;
103 }
105 return (vector * scale).rotate(rotation * val(MaterialX::Vector3(1.0f, 1.0f, -1.0f)));
106 }
108 return (vector / scale).rotate(rotation).normalize();
109 }
110 default:
112 }
113 return empty();
114}
115#endif
117
118} // namespace nodes::node_shader_mapping_cc
119
121{
122 namespace file_ns = nodes::node_shader_mapping_cc;
123
124 static bke::bNodeType ntype;
125
126 sh_node_type_base(&ntype, "ShaderNodeMapping"_ustr, SH_NODE_MAPPING);
127 ntype.ui_name = "Mapping";
128 ntype.ui_description = "Transform the input vector by applying translation, rotation, and scale";
129 ntype.enum_name_legacy = "MAPPING";
131 ntype.declare = file_ns::node_declare;
132 ntype.draw_buttons = file_ns::node_shader_buts_mapping;
133 ntype.gpu_fn = file_ns::gpu_shader_mapping;
134 ntype.updatefunc = file_ns::node_shader_update_mapping;
135 ntype.materialx_fn = file_ns::node_shader_materialx;
136
138}
139
140} // namespace blender
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_MAPPING
#define M_PI
#define BLI_assert_unreachable()
#define ELEM(...)
#define FLT_MAX
float3x3 scale(float3x3 mat, float2 scale)
@ NODE_MAPPING_TYPE_VECTOR
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
static const char * gpu_shader_get_name(int mode)
static void node_shader_update_mapping(bNodeTree *ntree, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static int gpu_shader_mapping(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_mapping(ui::Layout &layout, bContext *, PointerRNA *ptr)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ NODE_MAPPING_TYPE_VECTOR
@ NODE_MAPPING_TYPE_POINT
@ NODE_MAPPING_TYPE_NORMAL
@ NODE_MAPPING_TYPE_TEXTURE
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void register_node_type_sh_mapping()
@ PROP_TRANSLATION
Definition RNA_types.hh:318
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#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
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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)