Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geo_attribute_separate_xyz.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07941f7db796f4adb4d85c2f76d85fa388bc38ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "DNA_material_types.h"

#include "node_geometry_util.hh"

#include "UI_interface.h"
#include "UI_resources.h"

static bNodeSocketTemplate geo_node_attribute_separate_xyz_in[] = {
    {SOCK_GEOMETRY, N_("Geometry")},
    {SOCK_STRING, N_("Vector")},
    {SOCK_VECTOR, N_("Vector"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
    {SOCK_STRING, N_("Result X")},
    {SOCK_STRING, N_("Result Y")},
    {SOCK_STRING, N_("Result Z")},
    {-1, ""},
};

static bNodeSocketTemplate geo_node_attribute_separate_xyz_out[] = {
    {SOCK_GEOMETRY, N_("Geometry")},
    {-1, ""},
};

static void geo_node_attribute_separate_xyz_layout(uiLayout *layout,
                                                   bContext *UNUSED(C),
                                                   PointerRNA *ptr)
{
  uiItemR(layout, ptr, "input_type", 0, IFACE_("Type"), ICON_NONE);
}

namespace blender::nodes {

static void geo_node_attribute_separate_xyz_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeAttributeSeparateXYZ *data = (NodeAttributeSeparateXYZ *)MEM_callocN(
      sizeof(NodeAttributeSeparateXYZ), __func__);
  data->input_type = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
  node->storage = data;
}

static void geo_node_attribute_separate_xyz_update(bNodeTree *UNUSED(ntree), bNode *node)
{
  NodeAttributeSeparateXYZ *node_storage = (NodeAttributeSeparateXYZ *)node->storage;
  update_attribute_input_socket_availabilities(
      *node, "Vector", (GeometryNodeAttributeInputMode)node_storage->input_type);
}

static void extract_input(const int index, const Span<float3> &input, MutableSpan<float> result)
{
  for (const int i : result.index_range()) {
    /* Get the component of the float3. (0: X, 1: Y, 2: Z). */
    const float component = input[i][index];
    result[i] = component;
  }
}

static void separate_attribute(GeometryComponent &component, const GeoNodeExecParams &params)
{
  const std::string result_name_x = params.get_input<std::string>("Result X");
  const std::string result_name_y = params.get_input<std::string>("Result Y");
  const std::string result_name_z = params.get_input<std::string>("Result Z");
  /* The node is only for float3 to float conversions. */
  const CustomDataType input_type = CD_PROP_FLOAT3;
  const CustomDataType result_type = CD_PROP_FLOAT;
  /* The result domain is always point for now. */
  const AttributeDomain result_domain = ATTR_DOMAIN_POINT;

  /* No output to write to. */
  if (result_name_x.empty() && result_name_y.empty() && result_name_z.empty()) {
    return;
  }

  ReadAttributePtr attribute_input = params.get_input_attribute(
      "Vector", component, result_domain, input_type, nullptr);
  if (!attribute_input) {
    return;
  }
  const Span<float3> input_span = attribute_input->get_span<float3>();

  OutputAttributePtr attribute_result_x = component.attribute_try_get_for_output(
      result_name_x, result_domain, result_type);
  OutputAttributePtr attribute_result_y = component.attribute_try_get_for_output(
      result_name_y, result_domain, result_type);
  OutputAttributePtr attribute_result_z = component.attribute_try_get_for_output(
      result_name_z, result_domain, result_type);

  /* Only extract the components for the outputs with a given attribute. */
  if (attribute_result_x) {
    extract_input(0, input_span, attribute_result_x->get_span_for_write_only<float>());
    attribute_result_x.apply_span_and_save();
  }
  if (attribute_result_y) {
    extract_input(1, input_span, attribute_result_y->get_span_for_write_only<float>());
    attribute_result_y.apply_span_and_save();
  }
  if (attribute_result_z) {
    extract_input(2, input_span, attribute_result_z->get_span_for_write_only<float>());
    attribute_result_z.apply_span_and_save();
  }
}

static void geo_node_attribute_separate_xyz_exec(GeoNodeExecParams params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");

  if (geometry_set.has<MeshComponent>()) {
    separate_attribute(geometry_set.get_component_for_write<MeshComponent>(), params);
  }
  if (geometry_set.has<PointCloudComponent>()) {
    separate_attribute(geometry_set.get_component_for_write<PointCloudComponent>(), params);
  }

  params.set_output("Geometry", geometry_set);
}

}  // namespace blender::nodes

void register_node_type_geo_attribute_separate_xyz()
{
  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_ATTRIBUTE_SEPARATE_XYZ, "Attribute Separate XYZ", NODE_CLASS_ATTRIBUTE, 0);
  node_type_socket_templates(
      &ntype, geo_node_attribute_separate_xyz_in, geo_node_attribute_separate_xyz_out);
  node_type_init(&ntype, blender::nodes::geo_node_attribute_separate_xyz_init);
  node_type_update(&ntype, blender::nodes::geo_node_attribute_separate_xyz_update);
  node_type_storage(
      &ntype, "NodeAttributeSeparateXYZ", node_free_standard_storage, node_copy_standard_storage);
  ntype.geometry_node_execute = blender::nodes::geo_node_attribute_separate_xyz_exec;
  ntype.draw_buttons = geo_node_attribute_separate_xyz_layout;
  nodeRegisterType(&ntype);
}