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

node_geo_attribute_randomize.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c3acfc97358001c41c183eaade6678c74c88c77 (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
/*
 * 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 "node_geometry_util.hh"

#include "BLI_hash.h"
#include "BLI_rand.hh"

#include "DNA_mesh_types.h"
#include "DNA_pointcloud_types.h"

static bNodeSocketTemplate geo_node_attribute_randomize_in[] = {
    {SOCK_GEOMETRY, N_("Geometry")},
    {SOCK_STRING, N_("Attribute")},
    {SOCK_VECTOR, N_("Min"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
    {SOCK_VECTOR, N_("Max"), 1.0f, 1.0f, 1.0f, 0.0f, -FLT_MAX, FLT_MAX},
    {SOCK_FLOAT, N_("Min"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
    {SOCK_FLOAT, N_("Max"), 1.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
    {SOCK_INT, N_("Seed"), 0, 0, 0, 0, -10000, 10000},
    {-1, ""},
};

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

static void geo_node_attribute_randomize_init(bNodeTree *UNUSED(tree), bNode *node)
{
  node->custom1 = CD_PROP_FLOAT;
}

static void geo_node_attribute_randomize_update(bNodeTree *UNUSED(ntree), bNode *node)
{
  bNodeSocket *sock_min_vector = (bNodeSocket *)BLI_findlink(&node->inputs, 2);
  bNodeSocket *sock_max_vector = sock_min_vector->next;
  bNodeSocket *sock_min_float = sock_max_vector->next;
  bNodeSocket *sock_max_float = sock_min_float->next;

  const int data_type = node->custom1;

  nodeSetSocketAvailability(sock_min_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(sock_max_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(sock_min_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(sock_max_float, data_type == CD_PROP_FLOAT);
}

namespace blender::nodes {

/** Rehash to combine the seed with the "id" hash and a mutator for each dimension. */
static float noise_from_index_and_mutator(const int seed, const int hash, const int mutator)
{
  const int combined_hash = BLI_hash_int_3d(seed, hash, mutator);
  return BLI_hash_int_01(combined_hash);
}

/** Rehash to combine the seed with the "id" hash. */
static float noise_from_index(const int seed, const int hash)
{
  const int combined_hash = BLI_hash_int_2d(seed, hash);
  return BLI_hash_int_01(combined_hash);
}

static void randomize_attribute(BooleanWriteAttribute &attribute, Span<int> hashes, const int seed)
{
  MutableSpan<bool> attribute_span = attribute.get_span();
  for (const int i : IndexRange(attribute.size())) {
    const bool value = noise_from_index(seed, hashes[i]) > 0.5f;
    attribute_span[i] = value;
  }
  attribute.apply_span();
}

static void randomize_attribute(
    FloatWriteAttribute &attribute, float min, float max, Span<int> hashes, const int seed)
{
  MutableSpan<float> attribute_span = attribute.get_span();
  for (const int i : IndexRange(attribute.size())) {
    const float value = noise_from_index(seed, hashes[i]) * (max - min) + min;
    attribute_span[i] = value;
  }
  attribute.apply_span();
}

static void randomize_attribute(
    Float3WriteAttribute &attribute, float3 min, float3 max, Span<int> hashes, const int seed)
{
  MutableSpan<float3> attribute_span = attribute.get_span();
  for (const int i : IndexRange(attribute.size())) {
    const float x = noise_from_index_and_mutator(seed, hashes[i], 47);
    const float y = noise_from_index_and_mutator(seed, hashes[i], 8);
    const float z = noise_from_index_and_mutator(seed, hashes[i], 64);
    const float3 value = float3(x, y, z) * (max - min) + min;
    attribute_span[i] = value;
  }
  attribute.apply_span();
}

static Array<int> get_element_hashes(GeometryComponent &component,
                                     const AttributeDomain domain,
                                     const int attribute_size)
{
  /* Hash the reserved name attribute "id" as a (hopefully) stable seed for each point. */
  ReadAttributePtr hash_attribute = component.attribute_try_get_for_read("id", domain);
  Array<int> hashes(attribute_size);
  if (hash_attribute) {
    BLI_assert(hashes.size() == hash_attribute->size());
    const CPPType &cpp_type = hash_attribute->cpp_type();
    fn::GSpan items = hash_attribute->get_span();
    for (const int i : hashes.index_range()) {
      hashes[i] = (int)cpp_type.hash(items[i]);
    }
  }
  else {
    /* If there is no "id" attribute for per-point variation, just create it here. */
    RandomNumberGenerator rng;
    rng.seed(0);
    for (const int i : hashes.index_range()) {
      hashes[i] = rng.get_int32();
    }
  }

  return hashes;
}

static void randomize_attribute(GeometryComponent &component,
                                const GeoNodeExecParams &params,
                                const int seed)
{
  const bNode &node = params.node();
  const CustomDataType data_type = static_cast<CustomDataType>(node.custom1);
  const AttributeDomain domain = static_cast<AttributeDomain>(node.custom2);
  const std::string attribute_name = params.get_input<std::string>("Attribute");
  if (attribute_name.empty()) {
    return;
  }

  WriteAttributePtr attribute = component.attribute_try_ensure_for_write(
      attribute_name, domain, data_type);
  if (!attribute) {
    return;
  }

  Array<int> hashes = get_element_hashes(component, domain, attribute->size());

  switch (data_type) {
    case CD_PROP_FLOAT: {
      FloatWriteAttribute float_attribute = std::move(attribute);
      const float min_value = params.get_input<float>("Min_001");
      const float max_value = params.get_input<float>("Max_001");
      randomize_attribute(float_attribute, min_value, max_value, hashes, seed);
      break;
    }
    case CD_PROP_FLOAT3: {
      Float3WriteAttribute float3_attribute = std::move(attribute);
      const float3 min_value = params.get_input<float3>("Min");
      const float3 max_value = params.get_input<float3>("Max");
      randomize_attribute(float3_attribute, min_value, max_value, hashes, seed);
      break;
    }
    case CD_PROP_BOOL: {
      BooleanWriteAttribute boolean_attribute = std::move(attribute);
      randomize_attribute(boolean_attribute, hashes, seed);
      break;
    }
    default:
      break;
  }
}

static void geo_node_random_attribute_exec(GeoNodeExecParams params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
  const int seed = params.get_input<int>("Seed");

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

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

}  // namespace blender::nodes

void register_node_type_geo_attribute_randomize()
{
  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_ATTRIBUTE_RANDOMIZE, "Attribute Randomize", NODE_CLASS_ATTRIBUTE, 0);
  node_type_socket_templates(
      &ntype, geo_node_attribute_randomize_in, geo_node_attribute_randomize_out);
  node_type_init(&ntype, geo_node_attribute_randomize_init);
  node_type_update(&ntype, geo_node_attribute_randomize_update);
  ntype.geometry_node_execute = blender::nodes::geo_node_random_attribute_exec;
  nodeRegisterType(&ntype);
}