Blender V5.0
node_shader_displacement.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
8
10{
11 b.add_input<decl::Float>("Height").default_value(0.0f).min(0.0f).max(1000.0f).description(
12 "Distance to displace the surface along the normal");
13 b.add_input<decl::Float>("Midlevel")
14 .default_value(0.5f)
15 .min(0.0f)
16 .max(1000.0f)
18 "Neutral displacement value that causes no displacement.\n"
19 "Lower values cause the surface to move inwards, "
20 "higher values push the surface outwards");
21 b.add_input<decl::Float>("Scale").default_value(0.01f).min(0.0f).max(1000.0f).description(
22 "Increase or decrease the amount of displacement");
23 b.add_input<decl::Vector>("Normal").hide_value();
24 b.add_output<decl::Vector>("Displacement");
25}
26
27static void node_shader_init_displacement(bNodeTree * /*ntree*/, bNode *node)
28{
29 node->custom1 = SHD_SPACE_OBJECT; /* space */
30}
31
33 bNode *node,
34 bNodeExecData * /*execdata*/,
37{
38 if (!in[3].link) {
39 GPU_link(mat, "world_normals_get", &in[3].link);
40 }
41
42 if (node->custom1 == SHD_SPACE_OBJECT) {
43 return GPU_stack_link(mat, node, "node_displacement_object", in, out);
44 }
45
46 return GPU_stack_link(mat, node, "node_displacement_world", in, out);
47}
48
50#ifdef WITH_MATERIALX
51{
52 if (to_type_ != NodeItem::Type::DisplacementShader) {
53 return empty();
54 }
55
56 /* NOTE: Normal input and Space feature don't have an implementation in MaterialX. */
57 NodeItem midlevel = get_input_value("Midlevel", NodeItem::Type::Float);
58 NodeItem height = get_input_value("Height", NodeItem::Type::Float) - midlevel;
59 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
60
61 return create_node("displacement",
62 NodeItem::Type::DisplacementShader,
63 {{"displacement", height}, {"scale", scale}});
64}
65#endif
67
68} // namespace blender::nodes::node_shader_displacement_cc
69
70/* node type definition */
72{
74
75 static blender::bke::bNodeType ntype;
76
77 sh_node_type_base(&ntype, "ShaderNodeDisplacement", SH_NODE_DISPLACEMENT);
78 ntype.ui_name = "Displacement";
79 ntype.ui_description = "Displace the surface along the surface normal";
80 ntype.enum_name_legacy = "DISPLACEMENT";
82 ntype.declare = file_ns::node_declare;
83 ntype.initfunc = file_ns::node_shader_init_displacement;
84 ntype.gpu_fn = file_ns::gpu_shader_displacement;
85 ntype.materialx_fn = file_ns::node_shader_materialx;
86
88}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:450
#define SH_NODE_DISPLACEMENT
@ SHD_SPACE_OBJECT
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
bool GPU_link(GPUMaterial *mat, const char *name,...)
#define in
#define out
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_declare(NodeDeclarationBuilder &b)
static int gpu_shader_displacement(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_displacement(bNodeTree *, bNode *node)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_displacement()
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362