Blender V5.3
node_shader_bump.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
16/* **************** BUMP ******************** */
17
19
21{
22#define SOCK_STRENGTH_ID 0
23 b.add_input<decl::Float>("Strength"_ustr)
24 .default_value(1.0f)
25 .min(0.0f)
26 .max(1.0f)
28 .description(
29 "Strength of the bump mapping effect, interpolating between "
30 "no bump mapping and full bump mapping")
31 .translation_context(BLT_I18NCONTEXT_AMOUNT);
32#define SOCK_DISTANCE_ID 1
33 b.add_input<decl::Float>("Distance"_ustr)
34 .default_value(0.001f)
35 .min(0.0f)
36 .max(1000.0f)
38 "Multiplier for the height value to control the overall distance for bump mapping");
39#define SOCK_FILTER_WIDTH_ID 2
40 b.add_input<decl::Float>("Filter Width"_ustr)
41 .default_value(0.1f)
42 .min(0.001)
43 .max(10.0f)
45 .description(
46 "Filter width in pixels, used to compute the bump mapping direction. For most textures "
47 "the default value of 0.1 enables subpixel filtering for stable results. For stepwise "
48 "textures a larger filter width can be used to get a bevel like effect on the edges");
49#define SOCK_HEIGHT_ID 3
50 b.add_input<decl::Float>("Height"_ustr)
51 .default_value(1.0f)
52 .min(-1000.0f)
53 .max(1000.0f)
54 .hide_value()
55 .description("Height above surface. Connect the height map texture to this input");
56#define SOCK_NORMAL_ID 4
57 b.add_input<decl::Vector>("Normal"_ustr).min(-1.0f).max(1.0f).hide_value();
58 b.add_output<decl::Vector>("Normal"_ustr);
59}
60
61static void node_shader_buts_bump(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
62{
63 layout.prop(ptr, "invert", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
64}
65
67 bNode *node,
68 bNodeExecData * /*execdata*/,
71{
72 /* If there is no Height input, the node becomes a no-op. */
73 if (!in[SOCK_HEIGHT_ID].link) {
74 if (!in[SOCK_NORMAL_ID].link) {
75 return GPU_link(mat, "world_normals_get", GPU_shading_data(), &out[0].link);
76 }
77 /* Actually running the bump code would normalize, but Cycles handles it as total no-op. */
78 return GPU_link(mat, "vector_copy", in[SOCK_NORMAL_ID].link, &out[0].link);
79 }
80
81 if (!in[SOCK_NORMAL_ID].link) {
82 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[SOCK_NORMAL_ID].link);
83 }
84
85 const float filter_width = std::get<float>(in[SOCK_FILTER_WIDTH_ID].value);
86 const char *height_function = GPU_material_split_sub_function(
87 mat, GPU_FLOAT, &in[SOCK_HEIGHT_ID].link);
88
89 /* TODO (Miguel Pozo):
90 * Currently, this doesn't compute the actual differentials, just the height at dX and dY
91 * offsets. The actual differentials are computed inside the GLSL node_bump function by
92 * subtracting the height input. This avoids redundant computations when the height input is
93 * also needed by regular nodes as part in the main function (See #103903 for context).
94 * A better option would be to add a "value" input socket (in this case the height) to the
95 * differentiate node, but currently this kind of intermediate nodes are pruned in the
96 * code generation process (see #104265), so we need to fix that first. */
97 GPUNodeLink *dheight = GPU_differentiate_float_function(height_function, filter_width);
98
99 float invert = (node->custom1) ? -1.0 : 1.0;
100
101 return GPU_stack_link(mat,
102 node,
103 "node_bump",
104 in,
105 out,
106 dheight,
110}
111
113#ifdef WITH_MATERIALX
114{
115 NodeItem height = get_input_link("Height", NodeItem::Type::Float);
116 NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
117
118 if (!height) {
119 if (!normal) {
120 return create_node(
121 "normal", NodeItem::Type::Vector3, {{"space", val(std::string("world"))}});
122 }
123 return normal;
124 }
125
126 NodeItem strength = get_input_value("Strength", NodeItem::Type::Float);
127 NodeItem distance = get_input_value("Distance", NodeItem::Type::Float);
128 NodeItem height_normal = create_node(
129 "heighttonormal", NodeItem::Type::Vector3, {{"in", height}, {"scale", strength}});
130
131 return create_node("normalmap",
132 NodeItem::Type::Vector3,
133 {{"in", height_normal},
134 {"scale", node_->custom1 ? distance * val(-1.0f) : distance},
135 {"normal", normal}});
136}
137#endif
139
140} // namespace nodes::node_shader_bump_cc
141
142/* node type definition */
144{
145 namespace file_ns = nodes::node_shader_bump_cc;
146
147 static bke::bNodeType ntype;
148
149 sh_node_type_base(&ntype, "ShaderNodeBump"_ustr, SH_NODE_BUMP);
150 ntype.ui_name = "Bump";
151 ntype.ui_description =
152 "Generate a perturbed normal from a height texture for bump mapping. Typically used for "
153 "faking highly detailed surfaces";
154 ntype.enum_name_legacy = "BUMP";
156 ntype.declare = file_ns::node_declare;
157 ntype.draw_buttons = file_ns::node_shader_buts_bump;
158 ntype.gpu_fn = file_ns::gpu_shader_bump;
159 ntype.materialx_fn = file_ns::node_shader_materialx;
160
162}
163
164} // namespace blender
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_BUMP
#define BLT_I18NCONTEXT_AMOUNT
float distance(float, float) RET
float2x2 invert(float2x2 mat)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static int gpu_shader_bump(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
static void node_shader_buts_bump(ui::Layout &layout, bContext *, PointerRNA *ptr)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void register_node_type_sh_bump()
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_differentiate_float_function(const char *function_name, const float filter_width)
GPUNodeLink * GPU_shading_data()
char * GPU_material_split_sub_function(GPUMaterial *material, GPUType return_type, GPUNodeLink **link)
GPUNodeLink * GPU_kernel_globals()
@ PROP_FACTOR
Definition RNA_types.hh:308
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define SOCK_NORMAL_ID
#define SOCK_HEIGHT_ID
#define SOCK_FILTER_WIDTH_ID
#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
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)