Blender V4.3
node_shader_vector_transform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "UI_interface.hh"
13#include "UI_resources.hh"
14
16
18{
19 b.add_input<decl::Vector>("Vector")
20 .default_value({0.5f, 0.5f, 0.5f})
21 .min(-10000.0f)
22 .max(10000.0f)
23 .description("Vector, point, or normal which will be used for convertion between spaces");
24 b.add_output<decl::Vector>("Vector");
25}
26
28{
29 uiItemR(layout,
30 ptr,
31 "vector_type",
33 nullptr,
34 ICON_NONE);
35 uiItemR(layout, ptr, "convert_from", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
36 uiItemR(layout, ptr, "convert_to", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
37}
38
39static void node_shader_init_vect_transform(bNodeTree * /*ntree*/, bNode *node)
40{
41 NodeShaderVectTransform *vect = MEM_cnew<NodeShaderVectTransform>("NodeShaderVectTransform");
42
43 /* Convert World into Object Space per default */
44 vect->convert_to = 1;
45
46 node->storage = vect;
47}
48
49static const char *get_gpufn_name_from_to(short from, short to, bool is_direction)
50{
51 switch (from) {
53 switch (to) {
55 return nullptr;
57 return is_direction ? "direction_transform_object_to_world" :
58 "point_transform_object_to_world";
60 return is_direction ? "direction_transform_object_to_view" :
61 "point_transform_object_to_view";
62 }
63 break;
65 switch (to) {
67 return nullptr;
69 return is_direction ? "direction_transform_world_to_view" :
70 "point_transform_world_to_view";
72 return is_direction ? "direction_transform_world_to_object" :
73 "point_transform_world_to_object";
74 }
75 break;
77 switch (to) {
79 return nullptr;
81 return is_direction ? "direction_transform_view_to_world" :
82 "point_transform_view_to_world";
84 return is_direction ? "direction_transform_view_to_object" :
85 "point_transform_view_to_object";
86 }
87 break;
88 }
89 return nullptr;
90}
91
93 bNode *node,
94 bNodeExecData * /*execdata*/,
95 GPUNodeStack *in,
96 GPUNodeStack *out)
97{
98 GPUNodeLink *inputlink;
99
100 NodeShaderVectTransform *nodeprop = (NodeShaderVectTransform *)node->storage;
101
102 if (in[0].hasinput) {
103 inputlink = in[0].link;
104 }
105 else {
106 inputlink = GPU_constant(in[0].vec);
107 }
108
109 const bool is_direction = (nodeprop->type != SHD_VECT_TRANSFORM_TYPE_POINT);
110 const char *func_name = get_gpufn_name_from_to(
111 nodeprop->convert_from, nodeprop->convert_to, is_direction);
112
113 if (func_name) {
114 /* For cycles we have inverted Z */
115 /* TODO: pass here the correct matrices */
118 {
119 GPU_link(mat, "invert_z", inputlink, &inputlink);
120 }
121
122 GPU_link(mat, func_name, inputlink, &out[0].link);
123
126 {
127 GPU_link(mat, "invert_z", out[0].link, &out[0].link);
128 }
129 }
130 else {
131 GPU_link(mat, "set_rgb", inputlink, &out[0].link);
132 }
133
134 if (nodeprop->type == SHD_VECT_TRANSFORM_TYPE_NORMAL) {
135 GPU_link(mat, "vector_normalize", out[0].link, &out[0].link);
136 }
137
138 return true;
139}
140
142#ifdef WITH_MATERIALX
143{
144 NodeItem res = empty();
145 NodeShaderVectTransform *nodeprop = (NodeShaderVectTransform *)node_->storage;
146 std::string fromspace;
147 std::string tospace;
148 std::string category;
149 NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
150
151 switch (nodeprop->convert_from) {
153 fromspace = "world";
154 break;
156 fromspace = "object";
157 break;
158 default:
159 /* NOTE: SHD_VECT_TRANSFORM_SPACE_CAMERA don't have an implementation in MaterialX. */
161 return vector;
162 }
163
164 switch (nodeprop->convert_to) {
166 tospace = "world";
167 break;
169 tospace = "object";
170 break;
171 default:
172 /* NOTE: SHD_VECT_TRANSFORM_SPACE_CAMERA don't have an implementation in MaterialX. */
174 return vector;
175 }
176
177 if (fromspace == tospace) {
178 return vector;
179 }
180
181 switch (nodeprop->type) {
183 category = "transformpoint";
184 break;
186 category = "transformnormal";
187 break;
189 category = "transformvector";
190 break;
191 default:
193 return vector;
194 }
195
196 return create_node(category,
197 NodeItem::Type::Vector3,
198 {{"in", vector}, {"fromspace", val(fromspace)}, {"tospace", val(tospace)}});
199}
200#endif
202
203} // namespace blender::nodes::node_shader_vector_transform_cc
204
206{
208
209 static blender::bke::bNodeType ntype;
210
212 ntype.declare = file_ns::node_declare;
213 ntype.draw_buttons = file_ns::node_shader_buts_vect_transform;
214 ntype.initfunc = file_ns::node_shader_init_vect_transform;
216 &ntype, "NodeShaderVectTransform", node_free_standard_storage, node_copy_standard_storage);
217 ntype.gpu_fn = file_ns::gpu_shader_vect_transform;
218 ntype.materialx_fn = file_ns::node_shader_materialx;
219
221}
#define SH_NODE_VECT_TRANSFORM
Definition BKE_node.hh:965
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:407
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
@ SHD_VECT_TRANSFORM_SPACE_WORLD
@ SHD_VECT_TRANSFORM_SPACE_OBJECT
@ SHD_VECT_TRANSFORM_SPACE_CAMERA
@ SHD_VECT_TRANSFORM_TYPE_VECTOR
@ SHD_VECT_TRANSFORM_TYPE_NORMAL
@ SHD_VECT_TRANSFORM_TYPE_POINT
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a vector
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
@ UI_ITEM_R_EXPAND
local_group_size(16, 16) .push_constant(Type b
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static int gpu_shader_vect_transform(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_vect_transform(uiLayout *layout, bContext *, PointerRNA *ptr)
static const char * get_gpufn_name_from_to(short from, short to, bool is_direction)
static void node_shader_init_vect_transform(bNodeTree *, bNode *node)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void sh_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void register_node_type_sh_vect_transform()
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
#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
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
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