Blender  V2.93
node_shader_clamp.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "node_shader_util.h"
25 
26 /* **************** Clamp ******************** */
28  {SOCK_FLOAT, N_("Value"), 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, PROP_NONE},
29  {SOCK_FLOAT, N_("Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE},
30  {SOCK_FLOAT, N_("Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE},
31  {-1, ""},
32 };
34  {SOCK_FLOAT, N_("Result")},
35  {-1, ""},
36 };
37 
39 {
40  node->custom1 = NODE_CLAMP_MINMAX; /* clamp type */
41 }
42 
43 static int gpu_shader_clamp(GPUMaterial *mat,
44  bNode *node,
45  bNodeExecData *UNUSED(execdata),
46  GPUNodeStack *in,
47  GPUNodeStack *out)
48 {
49  return (node->custom1 == NODE_CLAMP_MINMAX) ?
50  GPU_stack_link(mat, node, "clamp_minmax", in, out) :
51  GPU_stack_link(mat, node, "clamp_range", in, out);
52 }
53 
55 {
57  "Clamp (Min Max)",
58  [](float value, float min, float max) { return std::min(std::max(value, min), max); }};
60  "Clamp (Range)", [](float value, float a, float b) {
61  if (a < b) {
62  return clamp_f(value, a, b);
63  }
64 
65  return clamp_f(value, b, a);
66  }};
67 
68  int clamp_type = builder.bnode().custom1;
69  if (clamp_type == NODE_CLAMP_MINMAX) {
70  builder.set_matching_fn(minmax_fn);
71  }
72  else {
73  builder.set_matching_fn(range_fn);
74  }
75 }
76 
78 {
79  static bNodeType ntype;
80 
86 
87  nodeRegisterType(&ntype);
88 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
#define NODE_CLASS_CONVERTOR
Definition: BKE_node.h:341
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
MINLINE float clamp_f(float value, float min, float max)
#define UNUSED(x)
#define N_(msgid)
@ NODE_CLAMP_MINMAX
@ SOCK_FLOAT
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
Group RGB to Bright Vector Camera SH_NODE_CLAMP
@ PROP_NONE
Definition: RNA_types.h:113
void set_matching_fn(const fn::MultiFunction &function)
OperationNode * node
bNodeTree * ntree
static unsigned a[3]
Definition: RandGen.cpp:92
static void sh_node_clamp_expand_in_mf_network(blender::nodes::NodeMFNetworkBuilder &builder)
static void node_shader_init_clamp(bNodeTree *UNUSED(ntree), bNode *node)
void register_node_type_sh_clamp(void)
static bNodeSocketTemplate sh_node_clamp_in[]
static bNodeSocketTemplate sh_node_clamp_out[]
static int gpu_shader_clamp(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
#define min(a, b)
Definition: sort.c:51
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeExpandInMFNetworkFunction expand_in_mf_network
Definition: BKE_node.h:324
short custom1
float max