Blender  V2.93
node_shader_normal_map.c
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 
20 #include "../node_shader_util.h"
21 
22 /* **************** OUTPUT ******************** */
23 
25  {SOCK_FLOAT, N_("Strength"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 10.0f},
26  {SOCK_RGBA, N_("Color"), 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f},
27  {-1, ""},
28 };
29 
31  {SOCK_VECTOR, N_("Normal"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
32  {-1, ""},
33 };
34 
36 {
37  NodeShaderNormalMap *attr = MEM_callocN(sizeof(NodeShaderNormalMap), "NodeShaderNormalMap");
38  node->storage = attr;
39 }
40 
42  int UNUSED(thread),
43  bNode *UNUSED(node),
44  bNodeExecData *UNUSED(execdata),
45  bNodeStack **UNUSED(in),
46  bNodeStack **UNUSED(out))
47 {
48 }
49 
51  bNode *node,
52  bNodeExecData *UNUSED(execdata),
53  GPUNodeStack *in,
54  GPUNodeStack *out)
55 {
56  NodeShaderNormalMap *nm = node->storage;
57 
58  GPUNodeLink *strength;
59  if (in[0].link) {
60  strength = in[0].link;
61  }
62  else if (node->original) {
63  bNodeSocket *socket = BLI_findlink(&node->original->inputs, 0);
64  bNodeSocketValueFloat *socket_data = socket->default_value;
65  strength = GPU_uniform(&socket_data->value);
66  }
67  else {
68  strength = GPU_constant(in[0].vec);
69  }
70 
71  GPUNodeLink *newnormal;
72  if (in[1].link) {
73  newnormal = in[1].link;
74  }
75  else if (node->original) {
76  bNodeSocket *socket = BLI_findlink(&node->original->inputs, 1);
77  bNodeSocketValueRGBA *socket_data = socket->default_value;
78  newnormal = GPU_uniform(socket_data->value);
79  }
80  else {
81  newnormal = GPU_constant(in[1].vec);
82  }
83 
84  const char *color_to_normal_fnc_name = "color_to_normal_new_shading";
86  color_to_normal_fnc_name = "color_to_blender_normal_new_shading";
87  }
88 
89  GPU_link(mat, color_to_normal_fnc_name, newnormal, &newnormal);
90  switch (nm->space) {
91  case SHD_SPACE_TANGENT:
92  GPU_link(mat,
93  "node_normal_map",
95  GPU_attribute(mat, CD_TANGENT, nm->uv_map),
97  newnormal,
98  &newnormal);
99  break;
100  case SHD_SPACE_OBJECT:
102  GPU_link(
103  mat, "direction_transform_m4v3", newnormal, GPU_builtin(GPU_OBJECT_MATRIX), &newnormal);
104  break;
105  case SHD_SPACE_WORLD:
107  /* Nothing to do. */
108  break;
109  }
110 
112  GPU_link(mat, "node_normal_map_mix", strength, newnormal, oldnormal, &out[0].link);
113 
114  return true;
115 }
116 
117 /* node type definition */
119 {
120  static bNodeType ntype;
121 
122  sh_node_type_base(&ntype, SH_NODE_NORMAL_MAP, "Normal Map", NODE_CLASS_OP_VECTOR, 0);
127  &ntype, "NodeShaderNormalMap", node_free_standard_storage, node_copy_standard_storage);
130 
131  nodeRegisterType(&ntype);
132 }
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
#define SH_NODE_NORMAL_MAP
Definition: BKE_node.h:1040
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_OP_VECTOR
Definition: BKE_node.h:337
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4635
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_MIDDLE
Definition: BKE_node.h:372
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define UNUSED(x)
#define ELEM(...)
#define N_(msgid)
@ CD_TANGENT
#define SHD_SPACE_WORLD
#define SHD_SPACE_TANGENT
@ SOCK_VECTOR
@ SOCK_FLOAT
@ SOCK_RGBA
#define SHD_SPACE_BLENDER_OBJECT
#define SHD_SPACE_BLENDER_WORLD
#define SHD_SPACE_OBJECT
GPUNodeLink * GPU_builtin(eGPUBuiltin builtin)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, CustomDataType type, const char *name)
GPUNodeLink * GPU_constant(const float *num)
@ GPU_WORLD_NORMAL
Definition: GPU_material.h:105
@ GPU_OBJECT_MATRIX
Definition: GPU_material.h:88
@ GPU_OBJECT_INFO
Definition: GPU_material.h:102
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
void register_node_type_sh_normal_map(void)
static bNodeSocketTemplate sh_node_normal_map_in[]
static void node_shader_init_normal_map(bNodeTree *UNUSED(ntree), bNode *node)
static bNodeSocketTemplate sh_node_normal_map_out[]
static void node_shader_exec_normal_map(void *UNUSED(data), int UNUSED(thread), bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), bNodeStack **UNUSED(in), bNodeStack **UNUSED(out))
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
struct GPUNodeLink * link
Definition: GPU_material.h:119
Compact definition of a node socket.
Definition: BKE_node.h:95
void * default_value
Defines a node type.
Definition: BKE_node.h:221