Blender V4.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
9#include "node_shader_util.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
14/* **************** BUMP ******************** */
15
17
19{
20 b.add_input<decl::Float>("Strength")
21 .default_value(1.0f)
22 .min(0.0f)
23 .max(1.0f)
25 .description(
26 "Strength of the bump mapping effect, interpolating between "
27 "no bump mapping and full bump mapping");
28 b.add_input<decl::Float>("Distance")
29 .default_value(1.0f)
30 .min(0.0f)
31 .max(1000.0f)
33 "Multiplier for the height value to control the overall distance for bump mapping");
34 b.add_input<decl::Float>("Height").default_value(1.0f).min(-1000.0f).max(1000.0f).hide_value();
35 b.add_input<decl::Vector>("Normal").min(-1.0f).max(1.0f).hide_value();
36 b.add_output<decl::Vector>("Normal");
37}
38
39static void node_shader_buts_bump(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
40{
41 uiItemR(layout, ptr, "invert", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, ICON_NONE);
42}
43
45 bNode *node,
46 bNodeExecData * /*execdata*/,
47 GPUNodeStack *in,
48 GPUNodeStack *out)
49{
50 /* If there is no Height input, the node becomes a no-op. */
51 if (!in[2].link) {
52 if (!in[3].link) {
53 return GPU_link(mat, "world_normals_get", &out[0].link);
54 }
55 else {
56 /* Actually running the bump code would normalize, but Cycles handles it as total no-op. */
57 return GPU_link(mat, "vector_copy", in[3].link, &out[0].link);
58 }
59 }
60
61 if (!in[3].link) {
62 GPU_link(mat, "world_normals_get", &in[3].link);
63 }
64
65 const char *height_function = GPU_material_split_sub_function(mat, GPU_FLOAT, &in[2].link);
66
67 /* TODO (Miguel Pozo):
68 * Currently, this doesn't compute the actual differentials, just the height at dX and dY
69 * offsets. The actual differentials are computed inside the GLSL node_bump function by
70 * subtracting the height input. This avoids redundant computations when the height input is
71 * also needed by regular nodes as part in the main function (See #103903 for context).
72 * A better option would be to add a "value" input socket (in this case the height) to the
73 * differentiate node, but currently this kind of intermediate nodes are pruned in the
74 * code generation process (see #104265), so we need to fix that first. */
75 GPUNodeLink *dheight = GPU_differentiate_float_function(height_function);
76
77 float invert = (node->custom1) ? -1.0 : 1.0;
78
79 return GPU_stack_link(mat, node, "node_bump", in, out, dheight, GPU_constant(&invert));
80}
81
83#ifdef WITH_MATERIALX
84{
85 NodeItem height = get_input_link("Height", NodeItem::Type::Float);
86 NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
87
88 if (!height) {
89 if (!normal) {
90 return create_node(
91 "normal", NodeItem::Type::Vector3, {{"space", val(std::string("world"))}});
92 }
93 return normal;
94 }
95
96 NodeItem strength = get_input_value("Strength", NodeItem::Type::Float);
97 NodeItem distance = get_input_value("Distance", NodeItem::Type::Float);
98 NodeItem height_normal = create_node(
99 "heighttonormal", NodeItem::Type::Vector3, {{"in", height}, {"scale", strength}});
100
101 return create_node("normalmap",
102 NodeItem::Type::Vector3,
103 {{"in", height_normal},
104 {"scale", node_->custom1 ? distance * val(-1.0f) : distance},
105 {"normal", normal}});
106}
107#endif
109
110} // namespace blender::nodes::node_shader_bump_cc
111
112/* node type definition */
114{
115 namespace file_ns = blender::nodes::node_shader_bump_cc;
116
117 static blender::bke::bNodeType ntype;
118
120 ntype.declare = file_ns::node_declare;
121 ntype.draw_buttons = file_ns::node_shader_buts_bump;
122 ntype.gpu_fn = file_ns::gpu_shader_bump;
123 ntype.materialx_fn = file_ns::node_shader_materialx;
124
126}
#define SH_NODE_BUMP
Definition BKE_node.hh:953
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:407
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
char * GPU_material_split_sub_function(GPUMaterial *material, eGPUType return_type, GPUNodeLink **link)
GPUNodeLink * GPU_constant(const float *num)
@ GPU_FLOAT
GPUNodeLink * GPU_differentiate_float_function(const char *function_name)
bool GPU_link(GPUMaterial *mat, const char *name,...)
@ PROP_FACTOR
Definition RNA_types.hh:154
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
local_group_size(16, 16) .push_constant(Type b
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition invert.h:9
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static int gpu_shader_bump(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_bump(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_bump()
void sh_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition sort.c:32
Defines a node type.
Definition BKE_node.hh:218
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:320
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126