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

node_geo_attribute_capture.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8a33205de4a7f0ae8786d8bf36b68eb9856490c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * 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 "UI_interface.h"
#include "UI_resources.h"

#include "BKE_attribute_math.hh"

#include "node_geometry_util.hh"

namespace blender::nodes {

static void geo_node_attribute_capture_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>("Geometry");
  b.add_input<decl::Vector>("Value");
  b.add_input<decl::Float>("Value", "Value_001");
  b.add_input<decl::Color>("Value", "Value_002");
  b.add_input<decl::Bool>("Value", "Value_003");
  b.add_input<decl::Int>("Value", "Value_004");

  b.add_output<decl::Geometry>("Geometry");
  b.add_output<decl::Vector>("Attribute");
  b.add_output<decl::Float>("Attribute", "Attribute_001");
  b.add_output<decl::Color>("Attribute", "Attribute_002");
  b.add_output<decl::Bool>("Attribute", "Attribute_003");
  b.add_output<decl::Int>("Attribute", "Attribute_004");
}

static void geo_node_attribute_capture_layout(uiLayout *layout,
                                              bContext *UNUSED(C),
                                              PointerRNA *ptr)
{
  uiLayoutSetPropSep(layout, true);
  uiLayoutSetPropDecorate(layout, false);
  uiItemR(layout, ptr, "domain", 0, "", ICON_NONE);
  uiItemR(layout, ptr, "data_type", 0, "", ICON_NONE);
}

static void geo_node_attribute_capture_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeGeometryAttributeCapture *data = (NodeGeometryAttributeCapture *)MEM_callocN(
      sizeof(NodeGeometryAttributeCapture), __func__);
  data->data_type = CD_PROP_FLOAT;
  data->domain = ATTR_DOMAIN_POINT;

  node->storage = data;
}

static void geo_node_attribute_capture_update(bNodeTree *UNUSED(ntree), bNode *node)
{
  const NodeGeometryAttributeCapture &storage = *(const NodeGeometryAttributeCapture *)
                                                     node->storage;
  const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);

  bNodeSocket *socket_value_geometry = (bNodeSocket *)node->inputs.first;
  bNodeSocket *socket_value_vector = socket_value_geometry->next;
  bNodeSocket *socket_value_float = socket_value_vector->next;
  bNodeSocket *socket_value_color4f = socket_value_float->next;
  bNodeSocket *socket_value_boolean = socket_value_color4f->next;
  bNodeSocket *socket_value_int32 = socket_value_boolean->next;

  nodeSetSocketAvailability(socket_value_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(socket_value_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(socket_value_color4f, data_type == CD_PROP_COLOR);
  nodeSetSocketAvailability(socket_value_boolean, data_type == CD_PROP_BOOL);
  nodeSetSocketAvailability(socket_value_int32, data_type == CD_PROP_INT32);

  bNodeSocket *out_socket_value_geometry = (bNodeSocket *)node->outputs.first;
  bNodeSocket *out_socket_value_vector = out_socket_value_geometry->next;
  bNodeSocket *out_socket_value_float = out_socket_value_vector->next;
  bNodeSocket *out_socket_value_color4f = out_socket_value_float->next;
  bNodeSocket *out_socket_value_boolean = out_socket_value_color4f->next;
  bNodeSocket *out_socket_value_int32 = out_socket_value_boolean->next;

  nodeSetSocketAvailability(out_socket_value_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(out_socket_value_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(out_socket_value_color4f, data_type == CD_PROP_COLOR);
  nodeSetSocketAvailability(out_socket_value_boolean, data_type == CD_PROP_BOOL);
  nodeSetSocketAvailability(out_socket_value_int32, data_type == CD_PROP_INT32);
}

static void try_capture_field_on_geometry(GeometryComponent &component,
                                          const AttributeIDRef &attribute_id,
                                          const AttributeDomain domain,
                                          const GField &field)
{
  GeometryComponentFieldContext field_context{component, domain};
  const int domain_size = component.attribute_domain_size(domain);
  const IndexMask mask{IndexMask(domain_size)};

  const CustomDataType data_type = bke::cpp_type_to_custom_data_type(field.cpp_type());
  OutputAttribute output_attribute = component.attribute_try_get_for_output_only(
      attribute_id, domain, data_type);

  fn::FieldEvaluator evaluator{field_context, &mask};
  evaluator.add_with_destination(field, output_attribute.varray());
  evaluator.evaluate();

  output_attribute.save();
}

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

  geometry_set = bke::geometry_set_realize_instances(geometry_set);

  const bNode &node = params.node();
  const NodeGeometryAttributeCapture &storage = *(const NodeGeometryAttributeCapture *)
                                                     node.storage;
  const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
  const AttributeDomain domain = static_cast<AttributeDomain>(storage.domain);

  GField field;
  switch (data_type) {
    case CD_PROP_FLOAT:
      field = params.get_input<Field<float>>("Value_001");
      break;
    case CD_PROP_FLOAT3:
      field = params.get_input<Field<float3>>("Value");
      break;
    case CD_PROP_COLOR:
      field = params.get_input<Field<ColorGeometry4f>>("Value_002");
      break;
    case CD_PROP_BOOL:
      field = params.get_input<Field<bool>>("Value_003");
      break;
    case CD_PROP_INT32:
      field = params.get_input<Field<int>>("Value_004");
      break;
    default:
      break;
  }

  WeakAnonymousAttributeID anonymous_id{"Attribute Capture"};
  const CPPType &type = field.cpp_type();

  static const Array<GeometryComponentType> types = {
      GEO_COMPONENT_TYPE_MESH, GEO_COMPONENT_TYPE_POINT_CLOUD, GEO_COMPONENT_TYPE_CURVE};
  for (const GeometryComponentType type : types) {
    if (geometry_set.has(type)) {
      GeometryComponent &component = geometry_set.get_component_for_write(type);
      try_capture_field_on_geometry(component, anonymous_id.get(), domain, field);
    }
  }

  GField output_field{
      std::make_shared<bke::AnonymousAttributeFieldInput>(std::move(anonymous_id), type)};

  switch (data_type) {
    case CD_PROP_FLOAT: {
      params.set_output("Attribute_001", Field<float>(output_field));
      break;
    }
    case CD_PROP_FLOAT3: {
      params.set_output("Attribute", Field<float3>(output_field));
      break;
    }
    case CD_PROP_COLOR: {
      params.set_output("Attribute_002", Field<ColorGeometry4f>(output_field));
      break;
    }
    case CD_PROP_BOOL: {
      params.set_output("Attribute_003", Field<bool>(output_field));
      break;
    }
    case CD_PROP_INT32: {
      params.set_output("Attribute_004", Field<int>(output_field));
      break;
    }
    default:
      break;
  }

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

}  // namespace blender::nodes

void register_node_type_geo_attribute_capture()
{
  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_ATTRIBUTE_CAPTURE, "Attribute Capture", NODE_CLASS_ATTRIBUTE, 0);
  node_type_storage(&ntype,
                    "NodeGeometryAttributeCapture",
                    node_free_standard_storage,
                    node_copy_standard_storage);
  node_type_init(&ntype, blender::nodes::geo_node_attribute_capture_init);
  node_type_update(&ntype, blender::nodes::geo_node_attribute_capture_update);
  ntype.declare = blender::nodes::geo_node_attribute_capture_declare;
  ntype.geometry_node_execute = blender::nodes::geo_node_attribute_capture_exec;
  ntype.draw_buttons = blender::nodes::geo_node_attribute_capture_layout;
  nodeRegisterType(&ntype);
}