-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
voxel_instance_generator.cpp
1046 lines (859 loc) · 32.5 KB
/
voxel_instance_generator.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "voxel_instance_generator.h"
#include "../../constants/voxel_string_names.h"
#include "../../util/containers/container_funcs.h"
#include "../../util/godot/classes/array_mesh.h"
#include "../../util/godot/classes/engine.h"
#include "../../util/godot/core/array.h"
#include "../../util/godot/core/random_pcg.h"
#include "../../util/math/conv.h"
#include "../../util/math/triangle.h"
#include "../../util/profiling.h"
namespace zylann::voxel {
namespace {
// This cap is for sanity, to prevent potential crashing.
const float MAX_DENSITY = 10.f;
// We expose a slider going below max density as it should not often be needed, but we allow greater if really necessary
const char *DENSITY_HINT_STRING = "0.0, 1.0, 0.01, or_greater";
} // namespace
void VoxelInstanceGenerator::generate_transforms(
StdVector<Transform3f> &out_transforms,
Vector3i grid_position,
// TODO `lod_index` has become unused, remove?
int lod_index,
int layer_id,
Array surface_arrays,
UpMode up_mode,
uint8_t octant_mask,
float block_size
) {
ZN_PROFILE_SCOPE();
if (surface_arrays.size() < ArrayMesh::ARRAY_VERTEX && surface_arrays.size() < ArrayMesh::ARRAY_NORMAL &&
surface_arrays.size() < ArrayMesh::ARRAY_INDEX) {
return;
}
PackedVector3Array vertices = surface_arrays[ArrayMesh::ARRAY_VERTEX];
if (vertices.size() == 0) {
return;
}
if (_density <= 0.f) {
return;
}
PackedVector3Array normals = surface_arrays[ArrayMesh::ARRAY_NORMAL];
ERR_FAIL_COND(normals.size() == 0);
PackedInt32Array indices = surface_arrays[ArrayMesh::ARRAY_INDEX];
ERR_FAIL_COND(indices.size() == 0);
ERR_FAIL_COND(indices.size() % 3 != 0);
const uint32_t block_pos_hash = Vector3iHasher::hash(grid_position);
Vector3f global_up(0.f, 1.f, 0.f);
// Using different number generators so changing parameters affecting one doesn't affect the other
const uint64_t seed = block_pos_hash + layer_id;
RandomPCG pcg0;
pcg0.seed(seed);
RandomPCG pcg1;
pcg1.seed(seed + 1);
out_transforms.clear();
// TODO This part might be moved to the meshing thread if it turns out to be too heavy
static thread_local StdVector<Vector3f> g_vertex_cache;
static thread_local StdVector<Vector3f> g_normal_cache;
static thread_local StdVector<float> g_noise_cache;
// static thread_local StdVector<float> g_noise_graph_output_cache;
static thread_local StdVector<float> g_noise_graph_x_cache;
static thread_local StdVector<float> g_noise_graph_y_cache;
static thread_local StdVector<float> g_noise_graph_z_cache;
StdVector<Vector3f> &vertex_cache = g_vertex_cache;
StdVector<Vector3f> &normal_cache = g_normal_cache;
vertex_cache.clear();
normal_cache.clear();
// Pick random points
{
ZN_PROFILE_SCOPE_NAMED("mesh to points");
// PackedVector3Array::Read vertices_r = vertices.read();
// PackedVector3Array::Read normals_r = normals.read();
// Generate base positions
switch (_emit_mode) {
case EMIT_FROM_VERTICES: {
// Density is interpreted differently here,
// so it's possible a different emit mode will produce different amounts of instances.
// I had to use `uint64` and clamp it because floats can't contain `0xffffffff` accurately. Instead
// it results in `0x100000000`, one unit above.
const float density = math::clamp(_density, 0.f, 1.f);
static constexpr float max_density = 1.f;
const uint32_t density_u32 =
math::min(uint64_t(double(0xffffffff) * density / max_density), uint64_t(0xffffffff));
const int size = vertices.size();
const float margin = block_size - block_size * 0.01f;
for (int i = 0; i < size; ++i) {
// TODO We could actually generate indexes and pick those,
// rather than iterating them all and rejecting
if (pcg0.rand() >= density_u32) {
continue;
}
// Ignore vertices located on the positive faces of the block. They are usually shared with the
// neighbor block, which causes a density bias and overlapping instances
const Vector3f pos = to_vec3f(vertices[i]);
if (pos.x > margin || pos.y > margin || pos.z > margin) {
continue;
}
vertex_cache.push_back(pos);
normal_cache.push_back(to_vec3f(normals[i]));
}
} break;
case EMIT_FROM_FACES_FAST: {
// PoolIntArray::Read indices_r = indices.read();
const int triangle_count = indices.size() / 3;
// Assumes triangles are all roughly under the same size, and Transvoxel ones do (when not simplified),
// so we can use number of triangles as a metric proportional to the number of instances
const int instance_count = _density * triangle_count;
vertex_cache.resize(instance_count);
normal_cache.resize(instance_count);
for (int instance_index = 0; instance_index < instance_count; ++instance_index) {
// Pick a random triangle
const uint32_t ii = (pcg0.rand() % triangle_count) * 3;
const int ia = indices[ii];
const int ib = indices[ii + 1];
const int ic = indices[ii + 2];
const Vector3 &pa = vertices[ia];
const Vector3 &pb = vertices[ib];
const Vector3 &pc = vertices[ic];
const Vector3 &na = normals[ia];
const Vector3 &nb = normals[ib];
const Vector3 &nc = normals[ic];
const float t0 = pcg1.randf();
const float t1 = pcg1.randf();
// This formula gives pretty uniform distribution but involves a square root
// const Vector3 p = pa.linear_interpolate(pb, t0).linear_interpolate(pc, 1.f - sqrt(t1));
// This is an approximation
const Vector3 p = pa.lerp(pb, t0).lerp(pc, t1);
const Vector3 n = na.lerp(nb, t0).lerp(nc, t1);
vertex_cache[instance_index] = to_vec3f(p);
normal_cache[instance_index] = to_vec3f(n);
}
} break;
case EMIT_FROM_FACES: {
// PackedInt32Array::Read indices_r = indices.read();
const int triangle_count = indices.size() / 3;
// static thread_local StdVector<float> g_area_cache;
// StdVector<float> &area_cache = g_area_cache;
// area_cache.resize(triangle_count);
// Does not assume triangles have the same size, so instead a "unit size" is used,
// and more instances will be placed in triangles larger than this.
// This is roughly the size of one voxel's triangle
// const float unit_area = 0.5f * squared(block_size / 32.f);
float accumulator = 0.f;
const float inv_density = 1.f / _density;
for (int triangle_index = 0; triangle_index < triangle_count; ++triangle_index) {
const uint32_t ii = triangle_index * 3;
const int ia = indices[ii];
const int ib = indices[ii + 1];
const int ic = indices[ii + 2];
const Vector3 &pa = vertices[ia];
const Vector3 &pb = vertices[ib];
const Vector3 &pc = vertices[ic];
const Vector3 &na = normals[ia];
const Vector3 &nb = normals[ib];
const Vector3 &nc = normals[ic];
const float triangle_area = math::get_triangle_area(pa, pb, pc);
accumulator += triangle_area;
const int count_in_triangle = int(accumulator * _density);
for (int i = 0; i < count_in_triangle; ++i) {
const float t0 = pcg1.randf();
const float t1 = pcg1.randf();
// This formula gives pretty uniform distribution but involves a square root
// const Vector3 p = pa.linear_interpolate(pb, t0).linear_interpolate(pc, 1.f - sqrt(t1));
// This is an approximation
const Vector3 p = pa.lerp(pb, t0).lerp(pc, t1);
const Vector3 n = na.lerp(nb, t0).lerp(nc, t1);
vertex_cache.push_back(to_vec3f(p));
normal_cache.push_back(to_vec3f(n));
}
accumulator -= count_in_triangle * inv_density;
}
} break;
default:
CRASH_NOW();
}
}
// Filter out by octants
// This is done so some octants can be filled with user-edited data instead,
// because mesh size may not necessarily match data block size
if ((octant_mask & 0xff) != 0xff) {
ZN_PROFILE_SCOPE_NAMED("octant filter");
const float h = block_size / 2.f;
for (unsigned int i = 0; i < vertex_cache.size(); ++i) {
const Vector3f &pos = vertex_cache[i];
const uint8_t octant_index = get_octant_index(pos, h);
if ((octant_mask & (1 << octant_index)) == 0) {
unordered_remove(vertex_cache, i);
unordered_remove(normal_cache, i);
--i;
}
}
}
// Position of the block relative to the instancer node.
// Use full-precision here because we deal with potentially large coordinates
const Vector3 mesh_block_origin_d = grid_position * block_size;
// Don't directly access member vars because they can be modified by the editor thread (the resources themselves can
// get modified with relatively no harm, but the pointers can't)
Ref<pg::VoxelGraphFunction> noise_graph;
Ref<Noise> noise;
{
ShortLockScope slock(_ptr_settings_lock);
noise = _noise;
noise_graph = _noise_graph;
}
// Filter out by noise graph
if (noise_graph.is_valid()) {
ZN_PROFILE_SCOPE_NAMED("Noise graph filter");
StdVector<float> &out_buffer = g_noise_cache;
out_buffer.resize(vertex_cache.size());
// Check noise graph validity
std::shared_ptr<pg::VoxelGraphFunction::CompiledGraph> compiled_graph = noise_graph->get_compiled_graph();
if (compiled_graph != nullptr) {
const int input_count = compiled_graph->runtime.get_input_count();
const int output_count = compiled_graph->runtime.get_output_count();
bool valid = (output_count == 1);
switch (_noise_dimension) {
case DIMENSION_2D:
if (input_count != 2) {
valid = false;
}
break;
case DIMENSION_3D:
if (input_count != 3) {
valid = false;
}
break;
default:
ERR_FAIL();
}
if (!valid) {
compiled_graph = nullptr;
}
}
if (compiled_graph != nullptr) {
// Execute graph
StdVector<float> &x_buffer = g_noise_graph_x_cache;
StdVector<float> &z_buffer = g_noise_graph_z_cache;
x_buffer.resize(vertex_cache.size());
z_buffer.resize(vertex_cache.size());
FixedArray<Span<float>, 1> outputs;
outputs[0] = to_span(out_buffer);
switch (_noise_dimension) {
case DIMENSION_2D: {
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
x_buffer[i] = pos.x;
z_buffer[i] = pos.z;
}
FixedArray<Span<float>, 2> inputs;
inputs[0] = to_span(x_buffer);
inputs[1] = to_span(z_buffer);
noise_graph->execute(to_span(inputs), to_span(outputs));
} break;
case DIMENSION_3D: {
StdVector<float> &y_buffer = g_noise_graph_y_cache;
y_buffer.resize(vertex_cache.size());
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
x_buffer[i] = pos.x;
y_buffer[i] = pos.y;
z_buffer[i] = pos.z;
}
FixedArray<Span<float>, 3> inputs;
inputs[0] = to_span(x_buffer);
inputs[1] = to_span(y_buffer);
inputs[2] = to_span(z_buffer);
noise_graph->execute(to_span(inputs), to_span(outputs));
} break;
default:
ERR_FAIL();
}
} else {
// Error fallback
for (float &v : out_buffer) {
v = 0.f;
}
}
}
StdVector<float> &noise_cache = g_noise_cache;
// Legacy noise (noise graph is more versatile, but this remains for compatibility)
if (noise.is_valid()) {
noise_cache.resize(vertex_cache.size());
switch (_noise_dimension) {
case DIMENSION_2D: {
if (noise_graph.is_valid()) {
// Multiply output of noise graph
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
// Casting to float because Noise returns `real_t`, which is `double` in 64-bit float builds,
// but we don't need doubles for noise in this context...
noise_cache[i] *= math::max(float(noise->get_noise_2d(pos.x, pos.z)), 0.f);
}
} else {
// Use noise directly
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
noise_cache[i] = noise->get_noise_2d(pos.x, pos.z);
}
}
} break;
case DIMENSION_3D: {
if (noise_graph.is_valid()) {
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
noise_cache[i] *= math::max(float(noise->get_noise_3d(pos.x, pos.y, pos.z)), 0.f);
}
} else {
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const Vector3 &pos = to_vec3(vertex_cache[i]) + mesh_block_origin_d;
noise_cache[i] = noise->get_noise_3d(pos.x, pos.y, pos.z);
}
}
} break;
default:
ERR_FAIL();
}
}
const bool use_noise = noise.is_valid() || noise_graph.is_valid();
// Filter out by noise
if (use_noise) {
ZN_PROFILE_SCOPE_NAMED("Noise filter");
for (size_t i = 0; i < vertex_cache.size(); ++i) {
const float n = noise_cache[i];
if (n <= 0) {
unordered_remove(vertex_cache, i);
unordered_remove(normal_cache, i);
unordered_remove(noise_cache, i);
--i;
}
}
}
const float vertical_alignment = _vertical_alignment;
const float scale_min = _min_scale;
const float scale_range = _max_scale - _min_scale;
const bool random_vertical_flip = _random_vertical_flip;
const float offset_along_normal = _offset_along_normal;
const float normal_min_y = _min_surface_normal_y;
const float normal_max_y = _max_surface_normal_y;
const bool slope_filter = normal_min_y != -1.f || normal_max_y != 1.f;
const bool height_filter =
_min_height != std::numeric_limits<float>::min() || _max_height != std::numeric_limits<float>::max();
const float min_height = _min_height;
const float max_height = _max_height;
const Vector3f fixed_look_axis = up_mode == UP_MODE_POSITIVE_Y ? Vector3f(1, 0, 0) : Vector3f(0, 1, 0);
const Vector3f fixed_look_axis_alternative = up_mode == UP_MODE_POSITIVE_Y ? Vector3f(0, 1, 0) : Vector3f(1, 0, 0);
const Vector3f mesh_block_origin = to_vec3f(grid_position * block_size);
// Calculate orientations and scales
for (size_t vertex_index = 0; vertex_index < vertex_cache.size(); ++vertex_index) {
Transform3f t;
t.origin = vertex_cache[vertex_index];
// Warning: sometimes mesh normals are not perfectly normalized.
// The cause is for meshing speed on CPU. It's normalized on GPU anyways.
Vector3f surface_normal = normal_cache[vertex_index];
Vector3f axis_y;
bool surface_normal_is_normalized = false;
bool sphere_up_is_computed = false;
bool sphere_distance_is_computed = false;
float sphere_distance;
if (vertical_alignment == 0.f) {
surface_normal = math::normalized(surface_normal);
surface_normal_is_normalized = true;
axis_y = surface_normal;
} else {
if (up_mode == UP_MODE_SPHERE) {
global_up = math::normalized(mesh_block_origin + t.origin, sphere_distance);
sphere_up_is_computed = true;
sphere_distance_is_computed = true;
}
if (vertical_alignment < 1.f) {
axis_y = math::normalized(math::lerp(surface_normal, global_up, vertical_alignment));
} else {
axis_y = global_up;
}
}
if (slope_filter) {
if (!surface_normal_is_normalized) {
surface_normal = math::normalized(surface_normal);
}
float ny = surface_normal.y;
if (up_mode == UP_MODE_SPHERE) {
if (!sphere_up_is_computed) {
global_up = math::normalized(mesh_block_origin + t.origin, sphere_distance);
sphere_up_is_computed = true;
sphere_distance_is_computed = true;
}
ny = math::dot(surface_normal, global_up);
}
if (ny < normal_min_y || ny > normal_max_y) {
// Discard
continue;
}
}
if (height_filter) {
float y = mesh_block_origin.y + t.origin.y;
if (up_mode == UP_MODE_SPHERE) {
if (!sphere_distance_is_computed) {
sphere_distance = math::length(mesh_block_origin + t.origin);
sphere_distance_is_computed = true;
}
y = sphere_distance;
}
if (y < min_height || y > max_height) {
continue;
}
}
t.origin += offset_along_normal * axis_y;
// Allows to use two faces of a single rock to create variety in the same layer
if (random_vertical_flip && (pcg1.rand() & 1) == 1) {
axis_y = -axis_y;
// TODO Should have to flip another axis as well?
}
// Pick a random rotation from the floor's normal.
// We may check for cases too close to Y to avoid broken basis due to float precision limits,
// even if that could differ from the expected result
Vector3f dir;
if (_random_rotation) {
do {
// TODO Optimization: a pool of precomputed random directions would do the job too? Or would it waste
// the cache?
dir = math::normalized(Vector3f(pcg1.randf() - 0.5f, pcg1.randf() - 0.5f, pcg1.randf() - 0.5f));
// TODO Any way to check if the two vectors are close to aligned without normalizing `dir`?
} while (Math::abs(math::dot(dir, axis_y)) > 0.9999f);
} else {
// If the surface is aligned with this axis, it will create a "pole" where all instances are looking at.
// When getting too close to it, we may pick a different axis.
dir = fixed_look_axis;
if (Math::abs(math::dot(dir, axis_y)) > 0.9999f) {
dir = fixed_look_axis_alternative;
}
}
const Vector3f axis_x = math::normalized(math::cross(axis_y, dir));
const Vector3f axis_z = math::cross(axis_x, axis_y);
// In Godot 3, the Basis constructor expected 3 rows, but in Godot 4 it was changed to take 3 columns...
// t.basis = Basis3f(Vector3f(axis_x.x, axis_y.x, axis_z.x), Vector3f(axis_x.y, axis_y.y, axis_z.y),
// Vector3f(axis_x.z, axis_y.z, axis_z.z));
t.basis = Basis3f(axis_x, axis_y, axis_z);
if (scale_range > 0.f) {
float r = pcg1.randf();
switch (_scale_distribution) {
case DISTRIBUTION_QUADRATIC:
r = r * r;
break;
case DISTRIBUTION_CUBIC:
r = r * r * r;
break;
case DISTRIBUTION_QUINTIC:
r = r * r * r * r * r;
break;
default:
break;
}
if (use_noise && _noise_on_scale > 0.f) {
#ifdef DEBUG_ENABLED
CRASH_COND(vertex_index >= noise_cache.size());
#endif
// Multiplied noise because it gives more pronounced results
const float n = math::clamp(noise_cache[vertex_index] * 2.f, 0.f, 1.f);
r *= Math::lerp(1.f, n, _noise_on_scale);
}
const float scale = scale_min + scale_range * r;
t.basis.scale(scale);
} else if (scale_min != 1.f) {
t.basis.scale(scale_min);
}
out_transforms.push_back(t);
}
// TODO Investigate if this helps (won't help with authored terrain)
// if (graph_generator.is_valid()) {
// for (size_t i = 0; i < _transform_cache.size(); ++i) {
// Transform &t = _transform_cache[i];
// const Vector3 up = t.get_basis().get_axis(Vector3::AXIS_Y);
// t.origin = graph_generator->approximate_surface(t.origin, up * 0.5f);
// }
// }
}
void VoxelInstanceGenerator::set_density(float density) {
density = math::clamp(density, 0.f, MAX_DENSITY);
if (density == _density) {
return;
}
_density = density;
emit_changed();
}
float VoxelInstanceGenerator::get_density() const {
return _density;
}
void VoxelInstanceGenerator::set_emit_mode(EmitMode mode) {
ERR_FAIL_INDEX(mode, EMIT_MODE_COUNT);
if (_emit_mode == mode) {
return;
}
_emit_mode = mode;
emit_changed();
}
VoxelInstanceGenerator::EmitMode VoxelInstanceGenerator::get_emit_mode() const {
return _emit_mode;
}
void VoxelInstanceGenerator::set_min_scale(float min_scale) {
if (_min_scale == min_scale) {
return;
}
_min_scale = min_scale;
emit_changed();
}
float VoxelInstanceGenerator::get_min_scale() const {
return _min_scale;
}
void VoxelInstanceGenerator::set_max_scale(float max_scale) {
if (max_scale == _max_scale) {
return;
}
_max_scale = max_scale;
emit_changed();
}
float VoxelInstanceGenerator::get_max_scale() const {
return _max_scale;
}
void VoxelInstanceGenerator::set_scale_distribution(Distribution distribution) {
ERR_FAIL_INDEX(distribution, DISTRIBUTION_COUNT);
if (distribution == _scale_distribution) {
return;
}
_scale_distribution = distribution;
emit_changed();
}
VoxelInstanceGenerator::Distribution VoxelInstanceGenerator::get_scale_distribution() const {
return _scale_distribution;
}
void VoxelInstanceGenerator::set_vertical_alignment(float amount) {
amount = math::clamp(amount, 0.f, 1.f);
if (_vertical_alignment == amount) {
return;
}
_vertical_alignment = amount;
emit_changed();
}
float VoxelInstanceGenerator::get_vertical_alignment() const {
return _vertical_alignment;
}
void VoxelInstanceGenerator::set_offset_along_normal(float offset) {
if (_offset_along_normal == offset) {
return;
}
_offset_along_normal = offset;
emit_changed();
}
float VoxelInstanceGenerator::get_offset_along_normal() const {
return _offset_along_normal;
}
void VoxelInstanceGenerator::set_min_slope_degrees(float degrees) {
_min_slope_degrees = math::clamp(degrees, 0.f, 180.f);
const float max_surface_normal_y = math::min(1.f, Math::cos(math::deg_to_rad(_min_slope_degrees)));
if (max_surface_normal_y == _max_surface_normal_y) {
return;
}
_max_surface_normal_y = max_surface_normal_y;
emit_changed();
}
float VoxelInstanceGenerator::get_min_slope_degrees() const {
return _min_slope_degrees;
}
void VoxelInstanceGenerator::set_max_slope_degrees(float degrees) {
_max_slope_degrees = math::clamp(degrees, 0.f, 180.f);
const float min_surface_normal_y = math::max(-1.f, Math::cos(math::deg_to_rad(_max_slope_degrees)));
if (min_surface_normal_y == _min_surface_normal_y) {
return;
}
_min_surface_normal_y = min_surface_normal_y;
emit_changed();
}
float VoxelInstanceGenerator::get_max_slope_degrees() const {
return _max_slope_degrees;
}
void VoxelInstanceGenerator::set_min_height(float h) {
if (h == _min_height) {
return;
}
_min_height = h;
emit_changed();
}
float VoxelInstanceGenerator::get_min_height() const {
return _min_height;
}
void VoxelInstanceGenerator::set_max_height(float h) {
if (_max_height == h) {
return;
}
_max_height = h;
emit_changed();
}
float VoxelInstanceGenerator::get_max_height() const {
return _max_height;
}
void VoxelInstanceGenerator::set_random_vertical_flip(bool flip_enabled) {
if (flip_enabled == _random_vertical_flip) {
return;
}
_random_vertical_flip = flip_enabled;
emit_changed();
}
bool VoxelInstanceGenerator::get_random_vertical_flip() const {
return _random_vertical_flip;
}
void VoxelInstanceGenerator::set_random_rotation(bool enabled) {
if (enabled != _random_rotation) {
_random_rotation = enabled;
emit_changed();
}
}
bool VoxelInstanceGenerator::get_random_rotation() const {
return _random_rotation;
}
void VoxelInstanceGenerator::set_noise(Ref<Noise> noise) {
{
ShortLockScope slock(_ptr_settings_lock);
if (_noise == noise) {
return;
}
if (_noise.is_valid()) {
_noise->disconnect(
VoxelStringNames::get_singleton().changed,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_changed)
);
}
_noise = noise;
if (_noise.is_valid()) {
_noise->connect(
VoxelStringNames::get_singleton().changed,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_changed)
);
}
}
// Emit signal outside of the locked region to avoid eventual deadlocks if handlers want to access the property
emit_changed();
}
Ref<Noise> VoxelInstanceGenerator::get_noise() const {
ShortLockScope slock(_ptr_settings_lock);
return _noise;
}
void VoxelInstanceGenerator::set_noise_graph(Ref<pg::VoxelGraphFunction> func) {
{
ShortLockScope slock(_ptr_settings_lock);
if (_noise_graph == func) {
return;
}
if (_noise_graph.is_valid()) {
_noise_graph->disconnect(
VoxelStringNames::get_singleton().changed,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_graph_changed)
);
_noise_graph->disconnect(
VoxelStringNames::get_singleton().compiled,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_graph_changed)
);
}
_noise_graph = func;
if (_noise_graph.is_valid()) {
// Compile on assignment because there isn't really a good place to do it...
func->compile(Engine::get_singleton()->is_editor_hint());
_noise_graph->connect(
VoxelStringNames::get_singleton().changed,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_graph_changed)
);
_noise_graph->connect(
VoxelStringNames::get_singleton().compiled,
callable_mp(this, &VoxelInstanceGenerator::_on_noise_graph_changed)
);
}
}
// Emit signal outside of the locked region to avoid eventual deadlocks if handlers want to access the property
emit_changed();
}
Ref<pg::VoxelGraphFunction> VoxelInstanceGenerator::get_noise_graph() const {
ShortLockScope slock(_ptr_settings_lock);
return _noise_graph;
}
void VoxelInstanceGenerator::set_noise_dimension(Dimension dim) {
ERR_FAIL_INDEX(dim, DIMENSION_COUNT);
if (dim == _noise_dimension) {
return;
}
_noise_dimension = dim;
emit_changed();
}
VoxelInstanceGenerator::Dimension VoxelInstanceGenerator::get_noise_dimension() const {
return _noise_dimension;
}
void VoxelInstanceGenerator::set_noise_on_scale(float amount) {
amount = math::clamp(amount, 0.f, 1.f);
if (amount == _noise_on_scale) {
return;
}
_noise_on_scale = amount;
emit_changed();
}
float VoxelInstanceGenerator::get_noise_on_scale() const {
return _noise_on_scale;
}
void VoxelInstanceGenerator::_on_noise_changed() {
emit_changed();
}
void VoxelInstanceGenerator::_on_noise_graph_changed() {
emit_changed();
}
#ifdef TOOLS_ENABLED
void VoxelInstanceGenerator::get_configuration_warnings(PackedStringArray &warnings) const {
Ref<pg::VoxelGraphFunction> noise_graph = get_noise_graph();
if (noise_graph.is_valid()) {
// Graph compiles?
godot::get_resource_configuration_warnings(**noise_graph, warnings, []() { return "noise_graph: "; });
// Check I/Os
const int expected_input_count = (_noise_dimension == DIMENSION_2D ? 2 : 3);
const int expected_output_count = 1;
const int input_count = noise_graph->get_input_definitions().size();
const int output_count = noise_graph->get_output_definitions().size();
if (input_count != expected_input_count) {
warnings.append(String("The noise graph has an invalid number of inputs. Expected {0}, found {1}")
.format(varray(expected_input_count, input_count)));
}
if (output_count != expected_output_count) {
warnings.append(String("The noise graph has an invalid number of outputs. Expected {0}, found {1}")
.format(varray(expected_output_count, output_count)));
}
}
}
#endif
void VoxelInstanceGenerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_density", "density"), &VoxelInstanceGenerator::set_density);
ClassDB::bind_method(D_METHOD("get_density"), &VoxelInstanceGenerator::get_density);
ClassDB::bind_method(D_METHOD("set_emit_mode", "density"), &VoxelInstanceGenerator::set_emit_mode);
ClassDB::bind_method(D_METHOD("get_emit_mode"), &VoxelInstanceGenerator::get_emit_mode);
ClassDB::bind_method(D_METHOD("set_min_scale", "min_scale"), &VoxelInstanceGenerator::set_min_scale);
ClassDB::bind_method(D_METHOD("get_min_scale"), &VoxelInstanceGenerator::get_min_scale);
ClassDB::bind_method(D_METHOD("set_max_scale", "max_scale"), &VoxelInstanceGenerator::set_max_scale);
ClassDB::bind_method(D_METHOD("get_max_scale"), &VoxelInstanceGenerator::get_max_scale);
ClassDB::bind_method(
D_METHOD("set_scale_distribution", "distribution"), &VoxelInstanceGenerator::set_scale_distribution
);
ClassDB::bind_method(D_METHOD("get_scale_distribution"), &VoxelInstanceGenerator::get_scale_distribution);
ClassDB::bind_method(D_METHOD("set_vertical_alignment", "amount"), &VoxelInstanceGenerator::set_vertical_alignment);
ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &VoxelInstanceGenerator::get_vertical_alignment);
ClassDB::bind_method(
D_METHOD("set_offset_along_normal", "offset"), &VoxelInstanceGenerator::set_offset_along_normal
);
ClassDB::bind_method(D_METHOD("get_offset_along_normal"), &VoxelInstanceGenerator::get_offset_along_normal);
ClassDB::bind_method(D_METHOD("set_min_slope_degrees", "degrees"), &VoxelInstanceGenerator::set_min_slope_degrees);
ClassDB::bind_method(D_METHOD("get_min_slope_degrees"), &VoxelInstanceGenerator::get_min_slope_degrees);
ClassDB::bind_method(D_METHOD("set_max_slope_degrees", "degrees"), &VoxelInstanceGenerator::set_max_slope_degrees);
ClassDB::bind_method(D_METHOD("get_max_slope_degrees"), &VoxelInstanceGenerator::get_max_slope_degrees);
ClassDB::bind_method(D_METHOD("set_min_height", "height"), &VoxelInstanceGenerator::set_min_height);
ClassDB::bind_method(D_METHOD("get_min_height"), &VoxelInstanceGenerator::get_min_height);
ClassDB::bind_method(D_METHOD("set_max_height", "height"), &VoxelInstanceGenerator::set_max_height);
ClassDB::bind_method(D_METHOD("get_max_height"), &VoxelInstanceGenerator::get_max_height);
ClassDB::bind_method(
D_METHOD("set_random_vertical_flip", "enabled"), &VoxelInstanceGenerator::set_random_vertical_flip
);
ClassDB::bind_method(D_METHOD("get_random_vertical_flip"), &VoxelInstanceGenerator::get_random_vertical_flip);
ClassDB::bind_method(D_METHOD("set_random_rotation", "enabled"), &VoxelInstanceGenerator::set_random_rotation);
ClassDB::bind_method(D_METHOD("get_random_rotation"), &VoxelInstanceGenerator::get_random_rotation);
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &VoxelInstanceGenerator::set_noise);
ClassDB::bind_method(D_METHOD("get_noise"), &VoxelInstanceGenerator::get_noise);
ClassDB::bind_method(D_METHOD("set_noise_graph", "graph"), &VoxelInstanceGenerator::set_noise_graph);
ClassDB::bind_method(D_METHOD("get_noise_graph"), &VoxelInstanceGenerator::get_noise_graph);
ClassDB::bind_method(D_METHOD("set_noise_dimension", "dim"), &VoxelInstanceGenerator::set_noise_dimension);
ClassDB::bind_method(D_METHOD("get_noise_dimension"), &VoxelInstanceGenerator::get_noise_dimension);
ClassDB::bind_method(D_METHOD("set_noise_on_scale", "amount"), &VoxelInstanceGenerator::set_noise_on_scale);
ClassDB::bind_method(D_METHOD("get_noise_on_scale"), &VoxelInstanceGenerator::get_noise_on_scale);
ADD_GROUP("Emission", "");
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "density", PROPERTY_HINT_RANGE, DENSITY_HINT_STRING),
"set_density",
"get_density"
);
ADD_PROPERTY(
PropertyInfo(Variant::INT, "emit_mode", PROPERTY_HINT_ENUM, "Vertices,FacesFast,Faces"),
"set_emit_mode",
"get_emit_mode"
);
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "min_slope_degrees", PROPERTY_HINT_RANGE, "0.0, 180.0, 0.1"),
"set_min_slope_degrees",
"get_min_slope_degrees"
);
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "max_slope_degrees", PROPERTY_HINT_RANGE, "0.0, 180.0, 0.1"),
"set_max_slope_degrees",
"get_max_slope_degrees"
);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_height"), "set_min_height", "get_min_height");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_height"), "set_max_height", "get_max_height");
ADD_GROUP("Scale", "");
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "min_scale", PROPERTY_HINT_RANGE, "0.0, 10.0, 0.01"),
"set_min_scale",
"get_min_scale"
);
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "max_scale", PROPERTY_HINT_RANGE, "0.0, 10.0, 0.01"),
"set_max_scale",
"get_max_scale"
);
ADD_PROPERTY(
PropertyInfo(Variant::INT, "scale_distribution", PROPERTY_HINT_ENUM, "Linear,Quadratic,Cubic,Quintic"),
"set_scale_distribution",
"get_scale_distribution"
);
ADD_GROUP("Rotation", "");
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "vertical_alignment", PROPERTY_HINT_RANGE, "0.0, 1.0, 0.01"),
"set_vertical_alignment",
"get_vertical_alignment"
);
ADD_PROPERTY(
PropertyInfo(Variant::BOOL, "random_vertical_flip"), "set_random_vertical_flip", "get_random_vertical_flip"
);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "random_rotation"), "set_random_rotation", "get_random_rotation");
ADD_GROUP("Offset", "");
ADD_PROPERTY(
PropertyInfo(Variant::FLOAT, "offset_along_normal"), "set_offset_along_normal", "get_offset_along_normal"
);