Blender V5.3
node_shader_raycast.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10
11#include "BLI_math_vector_c.hh"
12
13#include "RNA_prototypes.hh"
14
16#include "NOD_shader_raycast.hh"
20
21#include "BLO_read_write.hh"
22
24#include "UI_resources.hh"
25
26namespace blender {
27
29
31
33{
34 const bNode *node = b.node_or_null();
35 const bNodeTree *tree = b.tree_or_null();
36
37 b.use_custom_socket_order();
38 b.allow_any_socket_order();
39
40 b.add_default_layout();
41
42 b.add_output<decl::Float>("Is Hit"_ustr);
43 b.add_output<decl::Float>("Self Hit"_ustr);
44 b.add_output<decl::Float>("Hit Distance"_ustr);
45 b.add_output<decl::Vector>("Hit Position"_ustr);
46 b.add_output<decl::Vector>("Hit Normal"_ustr);
47
48 b.add_input<decl::Vector>("Position"_ustr).hide_value();
49 b.add_input<decl::Vector>("Direction"_ustr).hide_value();
50 b.add_input<decl::Float>("Length"_ustr).default_value(1.0);
51
52 auto &panel = b.add_panel("Attributes"_ustr).default_closed(true);
53 if (node != nullptr) {
54 const NodeShaderRaycast &storage = node_storage(*node);
55 for (const NodeRaycastSampleAttributeItem &item :
56 Span(storage.sample_attribute_items, storage.sample_attribute_items_num))
57 {
58 const UString name(item.name);
59 const eCustomDataType data_type = eCustomDataType(item.data_type);
60 const UString input_identifier(
62 const UString output_identifier(
64 panel.add_input<decl::String>(name, input_identifier)
65 .socket_name_ptr(
68 panel.add_output(data_type, name, output_identifier).align_with_previous();
69
70 /* In shaders color sockets are RGB. To access color alpha, add a separate output socket,
71 * similar to how color access is implemented in the Attribute shader node. */
72 if (item.data_type == CD_PROP_COLOR) {
73 const UString alpha_name(std::string(name.c_str()) + " Alpha");
74 const UString alpha_output_identifier(
76 panel.add_output(CD_PROP_FLOAT, alpha_name, alpha_output_identifier);
77 }
78 }
79 }
80 panel.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
81 .custom_draw(socket_items::ui::draw_extend_socket_fn<RaycastSampleAttributeItemsAccessor>());
82 panel.add_output<decl::Extend>(""_ustr, "__extend__"_ustr).align_with_previous();
83}
84
85static void node_shader_init(bNodeTree * /*ntree*/, bNode *node)
86{
87 NodeShaderRaycast *data = MEM_new<NodeShaderRaycast>(__func__);
88 node->storage = data;
89
90 node->custom1 = 0; /* Only Local */
91}
92
98
99static void node_shader_buts(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
100{
101 layout.prop(ptr, "only_local", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
102}
103
104static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
105{
106 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
107 bNode &node = *static_cast<bNode *>(ptr->data);
108
109 layout.prop(ptr, "only_local", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
110
111 if (ui::Layout *panel = layout.panel(
112 C, "sample_attribute_items", false, IFACE_("Sample Attributes")))
113 {
114 socket_items::ui::draw_items_list_with_operators<RaycastSampleAttributeItemsAccessor>(
115 C, panel, tree, node);
116 socket_items::ui::draw_active_item_props<RaycastSampleAttributeItemsAccessor>(
117 tree, node, [&](PointerRNA *item_ptr) {
118 panel->use_property_split_set(true);
119 panel->use_property_decorate_set(false);
120 panel->prop(item_ptr, "data_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
121 });
122 }
123}
124
125static void node_extra_info(NodeExtraInfoParams &parameters)
126{
127 const Scene *scene = CTX_data_scene(&parameters.C);
129 return;
130 }
131
132 const NodeShaderRaycast &storage = node_storage(parameters.node);
133 if (storage.sample_attribute_items_num == 0) {
134 return;
135 }
136
138 row.text = RPT_("Attributes Not Supported");
139 row.tooltip = TIP_("Accessing attributes is not supported by EEVEE renderer");
140 row.icon = ICON_STATUS_ERROR;
141 parameters.rows.append(std::move(row));
142}
143
148
150{
152 MEM_delete(reinterpret_cast<NodeShaderRaycast *>(node->storage));
153}
154
155static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
156{
157 const NodeShaderRaycast &src_storage = node_storage(*src_node);
158 NodeShaderRaycast *dst_storage = MEM_new<NodeShaderRaycast>(__func__,
159 dna::shallow_copy(src_storage));
160 dst_node->storage = dst_storage;
161
163}
164
165static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
166{
168}
169
174
176 bNode *node,
177 bNodeExecData * /*execdata*/,
180{
182
183 if (!in[0].link) {
184 GPU_link(mat, "world_position_get", GPU_shading_data(), &in[0].link);
185 }
186
187 if (!in[1].link) {
188 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[1].link);
189 }
190
191 /* GPU raycast node does not support attribute sampling. Relink all attribute outputs to a zero
192 * value. */
193 const NodeShaderRaycast &storage = node_storage(*node);
194 for (const NodeRaycastSampleAttributeItem &item :
195 Span(storage.sample_attribute_items, storage.sample_attribute_items_num))
196 {
197 static const float ZERO[4] = {0.0f, 0.0f, 0.0f, 0.0f};
198
199 const UString output_identifier(
201 GPUNodeStack &output = GPU_node_get_output(*node, out, output_identifier.ref());
202 GPU_link(mat, "set_value", GPU_constant(ZERO), &output.link);
203
204 if (item.data_type == CD_PROP_COLOR) {
205 const UString alpha_output_identifier(
207 GPUNodeStack &alpha_output = GPU_node_get_output(*node, out, alpha_output_identifier.ref());
208 GPU_link(mat, "set_value", GPU_constant(ZERO), &alpha_output.link);
209 }
210 }
211
212 const bool only_local = node->custom1;
213 const char *shader_name = only_local ? "node_raycast_only_local" : "node_raycast";
214
215 /* Explicitly link all sockets that are expected by the shader.
216 * Do not rely on the stack linking as it will attempt to link dynamically added attribute
217 * sockets. */
218 return GPU_link(mat,
219 shader_name,
220 GPU_node_get_input_link(*node, in, "Position"),
221 GPU_node_get_input_link(*node, in, "Direction"),
222 GPU_node_get_input_link(*node, in, "Length"),
225 &GPU_node_get_output(*node, out, "Is Hit").link,
226 &GPU_node_get_output(*node, out, "Self Hit").link,
227 &GPU_node_get_output(*node, out, "Hit Distance").link,
228 &GPU_node_get_output(*node, out, "Hit Position").link,
229 &GPU_node_get_output(*node, out, "Hit Normal").link);
230}
231
233#ifdef WITH_MATERIALX
234{
235 return get_output_default(socket_out_->identifier, NodeItem::Type::Any);
236}
237#endif
239
240} // namespace nodes::node_shader_raycast_cc
241
242/* node type definition */
244{
245 namespace file_ns = blender::nodes::node_shader_raycast_cc;
246
247 static blender::bke::bNodeType ntype;
248
249 sh_node_type_base(&ntype, "ShaderNodeRaycast"_ustr, SH_NODE_RAYCAST);
250 ntype.ui_name = "Raycast";
251 ntype.ui_description = "Cast rays and retrieve information from the hit point";
252 ntype.enum_name_legacy = "MATERIAL_RAYCAST";
253 ntype.nclass = NODE_CLASS_INPUT;
254 ntype.initfunc = file_ns::node_shader_init;
256 ntype.insert_link = file_ns::node_insert_link;
257 ntype.draw_buttons = file_ns::node_shader_buts;
258 ntype.draw_buttons_ex = file_ns::node_layout_ex;
259 ntype.register_operators = file_ns::node_operators;
260 ntype.declare = file_ns::node_declare;
261 ntype.gpu_fn = file_ns::node_shader_gpu;
262 ntype.materialx_fn = file_ns::node_shader_materialx;
263 ntype.get_extra_info = file_ns::node_extra_info;
264 ntype.blend_write_storage_content = file_ns::node_blend_write;
265 ntype.blend_data_read_storage_content = file_ns::node_blend_read;
266
268 ntype, "NodeShaderRaycast", file_ns::node_free_storage, file_ns::node_copy_storage);
269
271}
272
273namespace nodes {
274
275StructRNA **RaycastSampleAttributeItemsAccessor::item_srna = &RNA_NodeRaycastSampleAttributeItem;
276
278{
279 writer->write_string(item.name);
280}
281
287
288} // namespace nodes
289
290} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define SH_NODE_RAYCAST
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define ZERO
Definition StrokeRep.cpp:88
#define UI_ITEM_NONE
#define output
constexpr Span()=default
constexpr StringRef()
const char * c_str() const
StringRefNull ref() const
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static void node_shader_buts(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_shader_init(bNodeTree *, bNode *node)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_extra_info(NodeExtraInfoParams &parameters)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static int node_shader_gpu(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
BMesh Iterators.
bool object_shader_nodes_poll(const bContext *C)
GPUNodeLink * GPU_node_get_input_link(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
@ GPU_MATFLAG_RAYCAST
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
const char * RE_engine_id_BLENDER_EEVEE
Definition scene.cc:1717
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
BMesh const char void * data
GPUNodeStack & GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
void register_node_type_sh_raycast()
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
GPUNodeLink * GPU_shading_data()
GPUNodeLink * GPU_kernel_globals()
Scene * CTX_data_scene(const bContext *C)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void write_string(const char *data)
struct RenderData r
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) add_ui_poll(const bContext *C)
Definition BKE_node.hh:341
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
Vector< NodeExtraInfoRow > & rows
static std::string output_socket_identifier_for_item_alpha(const NodeRaycastSampleAttributeItem &item)
static std::string input_socket_identifier_for_item(const NodeRaycastSampleAttributeItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static std::string output_socket_identifier_for_item(const NodeRaycastSampleAttributeItem &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)