Blender V5.3
node_shader_normal_map.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 "DNA_node_types.h"
10#include "node_shader_util.hh"
11#include "node_util.hh"
12
13#include "BKE_context.hh"
14#include "BKE_node_runtime.hh"
15#include "BKE_scene.hh"
16
18
19#include "ED_node.hh"
20
21#include "RNA_access.hh"
22
24#include "UI_resources.hh"
25
26namespace blender {
27
29
31{
32 b.add_input<decl::Float>("Strength"_ustr)
33 .default_value(1.0f)
34 .min(0.0f)
35 .max(10.0f)
36 .description("Strength of the normal mapping effect")
37 .translation_context(BLT_I18NCONTEXT_AMOUNT);
38 b.add_input<decl::Color>("Color"_ustr)
39 .default_value({0.5f, 0.5f, 1.0f, 1.0f})
40 .description("Color that encodes the normal map in the specified space");
41 b.add_output<decl::Vector>("Normal"_ustr);
42}
43
45{
46 layout.prop(ptr, "space", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
47 layout.prop(ptr, "convention", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
48
49 if (RNA_enum_get(ptr, "space") == SHD_SPACE_TANGENT) {
50 layout.prop(ptr, "base", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
51
53
54 if (object && object->type == OB_MESH) {
55 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
56
57 if (depsgraph) {
58 Object *object_eval = DEG_get_evaluated(depsgraph, object);
59 PointerRNA dataptr = RNA_id_pointer_create(object_eval->data);
60 layout.prop_search(ptr, "uv_map", &dataptr, "uv_layers", "", ICON_GROUP_UVS);
61 return;
62 }
63 }
64
65 layout.prop(ptr, "uv_map", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
66 }
67}
68
70{
71 NodeShaderNormalMap *attr = MEM_new<NodeShaderNormalMap>("NodeShaderNormalMap");
72 node->storage = attr;
73}
74
76 bNode *node,
77 bNodeExecData * /*execdata*/,
80{
81 NodeShaderNormalMap *nm = static_cast<NodeShaderNormalMap *>(node->storage);
82
83 GPUNodeLink *strength;
84 if (in[0].link) {
85 strength = in[0].link;
86 }
87 else {
88 strength = GPU_uniform(in[0]);
89 }
90
91 GPUNodeLink *newnormal;
92 if (in[1].link) {
93 newnormal = in[1].link;
94 }
95 else {
96 newnormal = GPU_uniform(in[1]);
97 }
98
99 const char *color_to_normal_fnc_name = "color_to_normal_new_shading";
101 color_to_normal_fnc_name = "color_to_blender_normal_new_shading";
102 }
103
104 GPU_link(mat, color_to_normal_fnc_name, newnormal, &newnormal);
105
107 GPU_link(mat, "color_invert_green_channel", newnormal, &newnormal);
108 }
109
110 const char *input_fn_name = (nm->base == SHD_NORMAL_MAP_BASE_DISPLACED) ?
111 "input_normal_displaced" :
112 "input_normal_original";
113
114 GPUNodeLink *input_normal;
115 GPU_link(mat, input_fn_name, GPU_shading_data(), &input_normal);
116
117 switch (nm->space) {
120 /* We return directly from the node_normal_map as strength
121 * has already been applied for the tangent case */
122 GPU_link(mat,
123 "node_normal_map",
125 strength,
126 newnormal,
127 input_normal,
130 &out[0].link);
131 return true;
132 case SHD_SPACE_OBJECT:
134 GPU_link(mat,
135 "normal_transform_object_to_world",
136 newnormal,
139 &newnormal);
140 break;
141 case SHD_SPACE_WORLD:
143 /* Nothing to do. */
144 break;
145 }
146
147 /* Final step - mix and apply strength for all other than tangent space. */
148 GPU_link(mat, "node_normal_map_mix", strength, newnormal, GPU_shading_data(), &out[0].link);
149
150 return true;
151}
152
154#ifdef WITH_MATERIALX
155{
156 NodeShaderNormalMap *normal_map_node = static_cast<NodeShaderNormalMap *>(node_->storage);
157 NodeItem color = get_input_value("Color", NodeItem::Type::Vector3);
158 NodeItem strength = get_input_value("Strength", NodeItem::Type::Float);
159
160 if (normal_map_node->convention == SHD_NORMAL_MAP_CONVENTION_DIRECTX) {
161 NodeItem green_mask = val(MaterialX::Vector3(1.0f, -1.0f, 1.0f));
162 color = color * green_mask;
163 }
164
165# if MATERIALX_MAJOR_VERSION <= 1 && MATERIALX_MINOR_VERSION <= 38
166 std::string space;
167 switch (normal_map_node->space) {
169 space = "tangent";
170 break;
171 case SHD_SPACE_OBJECT:
173 space = "object";
174 break;
175 case SHD_SPACE_WORLD:
177 /* World isn't supported, tangent space will be used */
178 space = "tangent";
179 break;
180 default:
182 }
183
184 return create_node("normalmap",
185 NodeItem::Type::Vector3,
186 {{"in", color}, {"scale", strength}, {"space", val(space)}});
187# else
188 if (normal_map_node->space == SHD_SPACE_TANGENT) {
189 return create_node("normalmap", NodeItem::Type::Vector3, {{"in", color}, {"scale", strength}});
190 }
191
192 /* Object space not supported yet. Despite the 1.38 implementation accepting
193 * object space argument, that seems to work either. */
194 NodeItem tangent = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
195 NodeItem bitangent = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
196 NodeItem normal = val(MaterialX::Vector3(0.0f, 0.0f, 1.0f));
197
198 return create_node("normalmap",
199 NodeItem::Type::Vector3,
200 {{"in", color},
201 {"scale", strength},
202 {"tangent", tangent},
203 {"bitangent", bitangent},
204 {"normal", normal}});
205# endif
206}
207#endif
209
210} // namespace nodes::node_shader_normal_map_cc
211
212/* node type definition */
214{
215 namespace file_ns = nodes::node_shader_normal_map_cc;
216
217 static bke::bNodeType ntype;
218
219 sh_node_type_base(&ntype, "ShaderNodeNormalMap"_ustr, SH_NODE_NORMAL_MAP);
220 ntype.ui_name = "Normal Map";
221 ntype.ui_description =
222 "Generate a perturbed normal from an RGB normal map image. Typically used for faking highly "
223 "detailed surfaces";
224 ntype.enum_name_legacy = "NORMAL_MAP";
226 ntype.declare = file_ns::node_declare;
227 ntype.draw_buttons = file_ns::node_shader_buts_normal_map;
229 ntype.initfunc = file_ns::node_shader_init_normal_map;
231 ntype, "NodeShaderNormalMap", node_free_standard_storage, node_copy_standard_storage);
232 ntype.gpu_fn = file_ns::gpu_shader_normal_map;
233 ntype.materialx_fn = file_ns::node_shader_materialx;
234
236}
237
238} // namespace blender
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_NORMAL_MAP
#define BLT_I18NCONTEXT_AMOUNT
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
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
Object * get_space_editor_object(const bContext *C)
static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
static void node_shader_buts_normal_map(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_shader_init_normal_map(bNodeTree *, bNode *node)
BMesh Iterators.
int RNA_enum_get(PointerRNA *ptr, const char *name)
PointerRNA RNA_id_pointer_create(ID *id)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
@ SHD_NORMAL_MAP_CONVENTION_DIRECTX
@ GPU_MATFLAG_OBJECT_INFO
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void register_node_type_sh_normal_map()
GPUNodeLink * GPU_uniform(const float *num)
@ SHD_NORMAL_MAP_BASE_DISPLACED
bool GPU_link(GPUMaterial *mat, const char *name,...)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
@ SHD_SPACE_BLENDER_WORLD
@ SHD_SPACE_BLENDER_OBJECT
GPUNodeLink * GPU_shading_data()
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
static constexpr int _160
Definition BKE_node.hh:246
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
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
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)