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

node_geo_legacy_point_instance.cc « legacy « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: faf0b1a5fe704ea955499da9b9d9f333450f1975 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * 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_collection_types.h"

#include "BLI_hash.h"
#include "BLI_task.hh"

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

#include "node_geometry_util.hh"

namespace blender::nodes::node_geo_legacy_point_instance_cc {

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Geometry"));
  b.add_input<decl::Object>(N_("Object")).hide_label();
  b.add_input<decl::Collection>(N_("Collection")).hide_label();
  b.add_input<decl::Geometry>(N_("Instance Geometry"));
  b.add_input<decl::Int>(N_("Seed")).min(-10000).max(10000);
  b.add_output<decl::Geometry>(N_("Geometry"));
}

static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
  uiItemR(layout, ptr, "instance_type", 0, "", ICON_NONE);
  if (RNA_enum_get(ptr, "instance_type") == GEO_NODE_POINT_INSTANCE_TYPE_COLLECTION) {
    uiItemR(layout, ptr, "use_whole_collection", 0, nullptr, ICON_NONE);
  }
}

static void node_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeGeometryPointInstance *data = MEM_cnew<NodeGeometryPointInstance>(__func__);
  data->instance_type = GEO_NODE_POINT_INSTANCE_TYPE_OBJECT;
  data->flag |= GEO_NODE_POINT_INSTANCE_WHOLE_COLLECTION;
  node->storage = data;
}

static void node_update(bNodeTree *ntree, bNode *node)
{
  bNodeSocket *object_socket = (bNodeSocket *)BLI_findlink(&node->inputs, 1);
  bNodeSocket *collection_socket = object_socket->next;
  bNodeSocket *instance_geometry_socket = collection_socket->next;
  bNodeSocket *seed_socket = instance_geometry_socket->next;

  NodeGeometryPointInstance *node_storage = (NodeGeometryPointInstance *)node->storage;
  GeometryNodePointInstanceType type = (GeometryNodePointInstanceType)node_storage->instance_type;
  const bool use_whole_collection = (node_storage->flag &
                                     GEO_NODE_POINT_INSTANCE_WHOLE_COLLECTION) != 0;

  nodeSetSocketAvailability(ntree, object_socket, type == GEO_NODE_POINT_INSTANCE_TYPE_OBJECT);
  nodeSetSocketAvailability(
      ntree, collection_socket, type == GEO_NODE_POINT_INSTANCE_TYPE_COLLECTION);
  nodeSetSocketAvailability(
      ntree, instance_geometry_socket, type == GEO_NODE_POINT_INSTANCE_TYPE_GEOMETRY);
  nodeSetSocketAvailability(ntree,
                            seed_socket,
                            type == GEO_NODE_POINT_INSTANCE_TYPE_COLLECTION &&
                                !use_whole_collection);
}

static Vector<InstanceReference> get_instance_references__object(GeoNodeExecParams &params)
{
  Object *object = params.extract_input<Object *>("Object");
  if (object == params.self_object()) {
    return {};
  }
  if (object != nullptr) {
    return {*object};
  }
  return {};
}

static Vector<InstanceReference> get_instance_references__collection(GeoNodeExecParams &params)
{
  const bNode &node = params.node();
  NodeGeometryPointInstance *node_storage = (NodeGeometryPointInstance *)node.storage;

  Collection *collection = params.get_input<Collection *>("Collection");
  if (collection == nullptr) {
    return {};
  }

  if (BLI_listbase_is_empty(&collection->children) &&
      BLI_listbase_is_empty(&collection->gobject)) {
    params.error_message_add(NodeWarningType::Info, TIP_("Collection is empty"));
    return {};
  }

  if (node_storage->flag & GEO_NODE_POINT_INSTANCE_WHOLE_COLLECTION) {
    return {*collection};
  }

  Vector<InstanceReference> references;
  /* Direct child objects are instanced as objects. */
  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
    references.append(*cob->ob);
  }
  /* Direct child collections are instanced as collections. */
  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
    references.append(*child->collection);
  }

  return references;
}

static Vector<InstanceReference> get_instance_references__geometry(GeoNodeExecParams &params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Instance Geometry");
  geometry_set.ensure_owns_direct_data();
  return {std::move(geometry_set)};
}

static Vector<InstanceReference> get_instance_references(GeoNodeExecParams &params)
{
  const bNode &node = params.node();
  NodeGeometryPointInstance *node_storage = (NodeGeometryPointInstance *)node.storage;
  const GeometryNodePointInstanceType type = (GeometryNodePointInstanceType)
                                                 node_storage->instance_type;

  switch (type) {
    case GEO_NODE_POINT_INSTANCE_TYPE_OBJECT: {
      return get_instance_references__object(params);
    }
    case GEO_NODE_POINT_INSTANCE_TYPE_COLLECTION: {
      return get_instance_references__collection(params);
    }
    case GEO_NODE_POINT_INSTANCE_TYPE_GEOMETRY: {
      return get_instance_references__geometry(params);
    }
  }
  return {};
}

/**
 * Add the instance references to the component as a separate step from actually creating the
 * instances in order to avoid a map lookup for every transform. While this might add some
 * unnecessary references if they are not chosen while adding transforms, in the common cases
 * there are many more transforms than there are references, so that isn't likely.
 */
static Array<int> add_instance_references(InstancesComponent &instance_component,
                                          Span<InstanceReference> possible_references)
{
  Array<int> possible_handles(possible_references.size());
  for (const int i : possible_references.index_range()) {
    possible_handles[i] = instance_component.add_reference(possible_references[i]);
  }
  return possible_handles;
}

static void add_instances_from_component(InstancesComponent &instances,
                                         const GeometryComponent &src_geometry,
                                         Span<int> possible_handles,
                                         const GeoNodeExecParams &params)
{
  const AttributeDomain domain = ATTR_DOMAIN_POINT;

  const int domain_size = src_geometry.attribute_domain_size(domain);
  if (domain_size == 0) {
    return;
  }

  VArray<float3> positions = src_geometry.attribute_get_for_read<float3>(
      "position", domain, {0, 0, 0});
  VArray<float3> rotations = src_geometry.attribute_get_for_read<float3>(
      "rotation", domain, {0, 0, 0});
  VArray<float3> scales = src_geometry.attribute_get_for_read<float3>("scale", domain, {1, 1, 1});
  VArray<int> id_attribute = src_geometry.attribute_get_for_read<int>("id", domain, -1);

  /* The initial size of the component might be non-zero if there are two component types. */
  const int start_len = instances.instances_amount();
  instances.resize(start_len + domain_size);
  MutableSpan<int> handles = instances.instance_reference_handles().slice(start_len, domain_size);
  MutableSpan<float4x4> transforms = instances.instance_transforms().slice(start_len, domain_size);
  OutputAttribute_Typed<int> instance_id_attribute =
      instances.attribute_try_get_for_output_only<int>("id", ATTR_DOMAIN_INSTANCE);
  MutableSpan<int> instance_ids = instance_id_attribute.as_span();

  /* Skip all of the randomness handling if there is only a single possible instance
   * (anything except for collection mode with "Whole Collection" turned off). */
  if (possible_handles.size() == 1) {
    const int handle = possible_handles.first();
    threading::parallel_for(IndexRange(domain_size), 1024, [&](IndexRange range) {
      for (const int i : range) {
        handles[i] = handle;
        transforms[i] = float4x4::from_loc_eul_scale(positions[i], rotations[i], scales[i]);
        instance_ids[i] = id_attribute[i];
      }
    });
  }
  else {
    const int seed = params.get_input<int>("Seed");
    Array<uint32_t> ids = get_geometry_element_ids_as_uints(src_geometry, ATTR_DOMAIN_POINT);
    threading::parallel_for(IndexRange(domain_size), 1024, [&](IndexRange range) {
      for (const int i : range) {
        const int index = BLI_hash_int_2d(ids[i], seed) % possible_handles.size();
        const int handle = possible_handles[index];
        handles[i] = handle;
        transforms[i] = float4x4::from_loc_eul_scale(positions[i], rotations[i], scales[i]);
        instance_ids[i] = id_attribute[i];
      }
    });
  }

  instance_id_attribute.save();
}

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

  /* TODO: This node should be able to instance on the input instances component
   * rather than making the entire input geometry set real. */
  geometry_set = geometry::realize_instances_legacy(geometry_set);

  const Vector<InstanceReference> possible_references = get_instance_references(params);
  if (possible_references.is_empty()) {
    params.set_output("Geometry", std::move(geometry_set_out));
    return;
  }

  InstancesComponent &instances = geometry_set_out.get_component_for_write<InstancesComponent>();
  Array<int> possible_handles = add_instance_references(instances, possible_references);

  if (geometry_set.has<MeshComponent>()) {
    add_instances_from_component(instances,
                                 *geometry_set.get_component_for_read<MeshComponent>(),
                                 possible_handles,
                                 params);
  }
  if (geometry_set.has<PointCloudComponent>()) {
    add_instances_from_component(instances,
                                 *geometry_set.get_component_for_read<PointCloudComponent>(),
                                 possible_handles,
                                 params);
  }
  if (geometry_set.has<CurveComponent>()) {
    add_instances_from_component(instances,
                                 *geometry_set.get_component_for_read<CurveComponent>(),
                                 possible_handles,
                                 params);
  }

  params.set_output("Geometry", std::move(geometry_set_out));
}

}  // namespace blender::nodes::node_geo_legacy_point_instance_cc

void register_node_type_geo_point_instance()
{
  namespace file_ns = blender::nodes::node_geo_legacy_point_instance_cc;

  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_LEGACY_POINT_INSTANCE, "Point Instance", NODE_CLASS_GEOMETRY);
  node_type_init(&ntype, file_ns::node_init);
  node_type_storage(
      &ntype, "NodeGeometryPointInstance", node_free_standard_storage, node_copy_standard_storage);
  ntype.declare = file_ns::node_declare;
  ntype.draw_buttons = file_ns::node_layout;
  node_type_update(&ntype, file_ns::node_update);
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  nodeRegisterType(&ntype);
}