-
Notifications
You must be signed in to change notification settings - Fork 6
/
noise.cpp
1311 lines (924 loc) · 41.3 KB
/
noise.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 "noise.h"
#include "core/method_bind_ext.gen.inc"
AccidentalNoise::AccidentalNoise() :
vm(kernel),
eb(kernel) {
function = 0;
prev_function = 0;
mode = SEAMLESS_NONE;
format = FORMAT_HEIGHTMAP;
ranges = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
normalmap_spacing = 1.0;
normalmap_wrapped = true;
normalmap_normalized = true;
bumpmap_spacing = 1.0;
bumpmap_wrapped = true;
bumpmap_light = Vector3(1.0, 1.0, 1.0);
}
void AccidentalNoise::set_mode(MappingModes p_mode) {
mode = p_mode;
emit_changed();
}
AccidentalNoise::MappingModes AccidentalNoise::get_mode() const {
return mode;
}
void AccidentalNoise::set_format(Format p_format) {
format = p_format;
emit_changed();
_change_notify();
}
AccidentalNoise::Format AccidentalNoise::get_format() const {
return format;
}
void AccidentalNoise::set_ranges(const AABB &p_ranges) {
ranges = p_ranges;
emit_changed();
}
AABB AccidentalNoise::get_ranges() const {
return ranges;
}
void AccidentalNoise::set_expression(const String &p_expression) {
if (expression.empty() && p_expression.empty()) {
return;
}
expression = p_expression;
if (!expression.empty()) {
Index exp_index = evaluate(expression);
prev_function = function;
function = exp_index;
} else {
// Resume
function = prev_function;
}
emit_changed();
}
String AccidentalNoise::get_expression() const {
return expression;
}
//------------------------------------------------------------------------------
// Normal/bump map
//------------------------------------------------------------------------------
void AccidentalNoise::set_normalmap_spacing(double p_spacing) {
normalmap_spacing = p_spacing;
if (format == FORMAT_NORMALMAP) {
emit_changed();
}
}
double AccidentalNoise::get_normalmap_spacing() const {
return normalmap_spacing;
}
void AccidentalNoise::set_normalmap_wrapped(bool p_wrapped) {
normalmap_wrapped = p_wrapped;
if (format == FORMAT_NORMALMAP) {
emit_changed();
}
}
bool AccidentalNoise::is_normalmap_wrapped() const {
return normalmap_wrapped;
}
void AccidentalNoise::set_normalmap_normalized(bool p_normalized) {
normalmap_normalized = p_normalized;
if (format == FORMAT_NORMALMAP) {
emit_changed();
}
}
bool AccidentalNoise::is_normalmap_normalized() const {
return normalmap_normalized;
}
void AccidentalNoise::set_bumpmap_spacing(double p_spacing) {
bumpmap_spacing = p_spacing;
if (format == FORMAT_BUMPMAP) {
emit_changed();
}
}
double AccidentalNoise::get_bumpmap_spacing() const {
return bumpmap_spacing;
}
void AccidentalNoise::set_bumpmap_wrapped(bool p_wrapped) {
bumpmap_wrapped = p_wrapped;
if (format == FORMAT_BUMPMAP) {
emit_changed();
}
}
bool AccidentalNoise::is_bumpmap_wrapped() const {
return bumpmap_wrapped;
}
void AccidentalNoise::set_bumpmap_light(const Vector3 &p_light) {
bumpmap_light = p_light;
if (format == FORMAT_BUMPMAP) {
emit_changed();
}
}
Vector3 AccidentalNoise::get_bumpmap_light() const {
return bumpmap_light;
}
//------------------------------------------------------------------------------
// Kernel methods
//------------------------------------------------------------------------------
Index AccidentalNoise::pi() {
auto pi = kernel.pi();
return pi.getIndex();
}
Index AccidentalNoise::e() {
auto e = kernel.e();
return e.getIndex();
}
Index AccidentalNoise::one() {
auto one = kernel.one();
return one.getIndex();
}
Index AccidentalNoise::zero() {
auto zero = kernel.zero();
return zero.getIndex();
}
Index AccidentalNoise::point5() {
auto point5 = kernel.point5();
return point5.getIndex();
}
Index AccidentalNoise::sqrt2() {
auto sqrt2 = kernel.sqrt2();
return sqrt2.getIndex();
}
Index AccidentalNoise::constant(double value) {
auto constant = kernel.constant(value);
return constant.getIndex();
}
Index AccidentalNoise::seed(unsigned int value) {
auto seed = kernel.seed(value);
return seed.getIndex();
}
Index AccidentalNoise::seeder(Index seed, Index src) {
auto seeder = kernel.seeder(kernel[seed], kernel[src]);
return seeder.getIndex();
}
Index AccidentalNoise::value_basis(Index interp, Index seed) {
auto value_basis = kernel.valueBasis(kernel[interp], kernel[seed]);
return value_basis.getIndex();
}
Index AccidentalNoise::gradient_basis(Index interp, Index seed) {
auto gradient_basis = kernel.gradientBasis(kernel[interp], kernel[seed]);
return gradient_basis.getIndex();
}
Index AccidentalNoise::simplex_basis(Index seed) {
auto simplex_basis = kernel.simplexBasis(kernel[seed]);
return simplex_basis.getIndex();
}
Index AccidentalNoise::cellular_basis(Index f1, Index f2, Index f3, Index f4,
Index d1, Index d2, Index d3, Index d4,
Index distance, Index seed) {
auto cellular_basis = kernel.cellularBasis(
kernel[f1], kernel[f2], kernel[f3], kernel[f4],
kernel[d1], kernel[d2], kernel[d3], kernel[d4],
kernel[distance], kernel[seed]);
return cellular_basis.getIndex();
}
Index AccidentalNoise::add(Index src1, Index src2) {
auto add = kernel.add(kernel[src1], kernel[src2]);
return add.getIndex();
}
Index AccidentalNoise::subtract(Index src1, Index src2) {
auto subtract = kernel.subtract(kernel[src1], kernel[src2]);
return subtract.getIndex();
}
Index AccidentalNoise::multiply(Index src1, Index src2) {
auto multiply = kernel.multiply(kernel[src1], kernel[src2]);
return multiply.getIndex();
}
Index AccidentalNoise::divide(Index src1, Index src2) {
auto divide = kernel.divide(kernel[src1], kernel[src2]);
return divide.getIndex();
}
Index AccidentalNoise::maximum(Index src1, Index src2) {
auto maximum = kernel.maximum(kernel[src1], kernel[src2]);
return maximum.getIndex();
}
Index AccidentalNoise::minimum(Index src1, Index src2) {
auto minimum = kernel.minimum(kernel[src1], kernel[src2]);
return minimum.getIndex();
}
Index AccidentalNoise::abs(Index src) {
auto abs = kernel.abs(kernel[src]);
return abs.getIndex();
}
Index AccidentalNoise::pow(Index src1, Index src2) {
auto pow = kernel.pow(kernel[src1], kernel[src2]);
return pow.getIndex();
}
Index AccidentalNoise::bias(Index src1, Index src2) {
auto bias = kernel.bias(kernel[src1], kernel[src2]);
return bias.getIndex();
}
Index AccidentalNoise::gain(Index src1, Index src2) {
auto gain = kernel.gain(kernel[src1], kernel[src2]);
return gain.getIndex();
}
Index AccidentalNoise::scale(Index src, Index p_scale) {
auto scale = kernel.scaleDomain(kernel[src], kernel[p_scale]);
return scale.getIndex();
}
Index AccidentalNoise::scale_x(Index src, Index scale) {
auto scale_x = kernel.scaleX(kernel[src], kernel[scale]);
return scale_x.getIndex();
}
Index AccidentalNoise::scale_y(Index src, Index scale) {
auto scale_y = kernel.scaleY(kernel[src], kernel[scale]);
return scale_y.getIndex();
}
Index AccidentalNoise::scale_z(Index src, Index scale) {
auto scale_z = kernel.scaleZ(kernel[src], kernel[scale]);
return scale_z.getIndex();
}
Index AccidentalNoise::scale_w(Index src, Index scale) {
auto scale_w = kernel.scaleW(kernel[src], kernel[scale]);
return scale_w.getIndex();
}
Index AccidentalNoise::scale_u(Index src, Index scale) {
auto scale_u = kernel.scaleU(kernel[src], kernel[scale]);
return scale_u.getIndex();
}
Index AccidentalNoise::scale_v(Index src, Index scale) {
auto scale_v = kernel.scaleV(kernel[src], kernel[scale]);
return scale_v.getIndex();
}
Index AccidentalNoise::translate(Index src, Index p_translate) {
auto translate = kernel.translateDomain(kernel[src], kernel[p_translate]);
return translate.getIndex();
}
Index AccidentalNoise::translate_x(Index src, Index translate) {
auto translate_x = kernel.translateX(kernel[src], kernel[translate]);
return translate_x.getIndex();
}
Index AccidentalNoise::translate_y(Index src, Index translate) {
auto translate_y = kernel.translateY(kernel[src], kernel[translate]);
return translate_y.getIndex();
}
Index AccidentalNoise::translate_z(Index src, Index translate) {
auto translate_z = kernel.translateZ(kernel[src], kernel[translate]);
return translate_z.getIndex();
}
Index AccidentalNoise::translate_w(Index src, Index translate) {
auto translate_w = kernel.translateW(kernel[src], kernel[translate]);
return translate_w.getIndex();
}
Index AccidentalNoise::translate_u(Index src, Index translate) {
auto translate_u = kernel.translateU(kernel[src], kernel[translate]);
return translate_u.getIndex();
}
Index AccidentalNoise::translate_v(Index src, Index translate) {
auto translate_v = kernel.translateV(kernel[src], kernel[translate]);
return translate_v.getIndex();
}
Index AccidentalNoise::rotate(Index src, Index angle, Index ax, Index ay, Index az) {
auto rotate = kernel.rotateDomain(
kernel[src], kernel[angle], kernel[ax], kernel[ay], kernel[az]);
return rotate.getIndex();
}
Index AccidentalNoise::add_sequence(Index base, unsigned int number, unsigned int stride) {
auto add_sequence = kernel.addSequence(
kernel[base], number, stride);
return add_sequence.getIndex();
}
Index AccidentalNoise::multiply_sequence(Index base, unsigned int number, unsigned int stride) {
auto multiply_sequence = kernel.multiplySequence(
kernel[base], number, stride);
return multiply_sequence.getIndex();
}
Index AccidentalNoise::max_sequence(Index base, unsigned int number, unsigned int stride) {
auto max_sequence = kernel.maxSequence(
kernel[base], number, stride);
return max_sequence.getIndex();
}
Index AccidentalNoise::min_sequence(Index base, unsigned int number, unsigned int stride) {
auto min_sequence = kernel.minSequence(
kernel[base], number, stride);
return min_sequence.getIndex();
}
Index AccidentalNoise::mix(Index low, Index high, Index control) {
auto mix = kernel.mix(kernel[low], kernel[high], kernel[control]);
return mix.getIndex();
}
Index AccidentalNoise::select(Index low, Index high, Index control,
Index threshold, Index falloff) {
auto select = kernel.select(
kernel[low], kernel[high], kernel[control],
kernel[threshold], kernel[falloff]);
return select.getIndex();
}
Index AccidentalNoise::clamp(Index src, Index low, Index high) {
auto clamp = kernel.clamp(kernel[src], kernel[low], kernel[high]);
return clamp.getIndex();
}
Index AccidentalNoise::cos(Index src) {
auto cos = kernel.cos(kernel[src]);
return cos.getIndex();
}
Index AccidentalNoise::sin(Index src) {
auto sin = kernel.sin(kernel[src]);
return sin.getIndex();
}
Index AccidentalNoise::tan(Index src) {
auto tan = kernel.tan(kernel[src]);
return tan.getIndex();
}
Index AccidentalNoise::acos(Index src) {
auto acos = kernel.acos(kernel[src]);
return acos.getIndex();
}
Index AccidentalNoise::asin(Index src) {
auto asin = kernel.asin(kernel[src]);
return asin.getIndex();
}
Index AccidentalNoise::atan(Index src) {
auto atan = kernel.atan(kernel[src]);
return atan.getIndex();
}
Index AccidentalNoise::tiers(Index src, Index num_tiers) {
auto tiers = kernel.tiers(kernel[src], kernel[num_tiers]);
return tiers.getIndex();
}
Index AccidentalNoise::smooth_tiers(Index src, Index num_tiers) {
auto smooth_tiers = kernel.smoothTiers(kernel[src], kernel[num_tiers]);
return smooth_tiers.getIndex();
}
Index AccidentalNoise::x() {
auto x = kernel.x();
return x.getIndex();
}
Index AccidentalNoise::y() {
auto y = kernel.y();
return y.getIndex();
}
Index AccidentalNoise::z() {
auto z = kernel.z();
return z.getIndex();
}
Index AccidentalNoise::w() {
auto w = kernel.w();
return w.getIndex();
}
Index AccidentalNoise::u() {
auto u = kernel.u();
return u.getIndex();
}
Index AccidentalNoise::v() {
auto v = kernel.v();
return v.getIndex();
}
Index AccidentalNoise::dx(Index src, Index spacing) {
auto dx = kernel.dx(kernel[src], kernel[spacing]);
return dx.getIndex();
}
Index AccidentalNoise::dy(Index src, Index spacing) {
auto dy = kernel.dy(kernel[src], kernel[spacing]);
return dy.getIndex();
}
Index AccidentalNoise::dz(Index src, Index spacing) {
auto dz = kernel.dz(kernel[src], kernel[spacing]);
return dz.getIndex();
}
Index AccidentalNoise::dw(Index src, Index spacing) {
auto dw = kernel.dw(kernel[src], kernel[spacing]);
return dw.getIndex();
}
Index AccidentalNoise::du(Index src, Index spacing) {
auto du = kernel.du(kernel[src], kernel[spacing]);
return du.getIndex();
}
Index AccidentalNoise::dv(Index src, Index spacing) {
auto dv = kernel.dv(kernel[src], kernel[spacing]);
return dv.getIndex();
}
Index AccidentalNoise::sigmoid(Index src) {
auto sigmoid = kernel.sigmoid(kernel[src]);
return sigmoid.getIndex();
}
Index AccidentalNoise::radial() {
auto radial = kernel.radial();
return radial.getIndex();
}
Index AccidentalNoise::fractal(Index seed, Index layer,
Index persistence, Index lacunarity, Index numoctaves, Index frequency) {
auto fractal = kernel.fractal(
kernel[seed], kernel[layer],
kernel[persistence], kernel[lacunarity], kernel[numoctaves], kernel[frequency]);
return fractal.getIndex();
}
Index AccidentalNoise::randomize(Index seed, Index low, Index high) {
auto randomize = kernel.randomize(kernel[seed], kernel[low], kernel[high]);
return randomize.getIndex();
}
Index AccidentalNoise::step(Index val, Index control) {
auto step = kernel.step(kernel[val], kernel[control]);
return step.getIndex();
}
Index AccidentalNoise::linear_step(Index low, Index high, Index control) {
auto linear_step = kernel.linearStep(kernel[low], kernel[high], kernel[control]);
return linear_step.getIndex();
}
Index AccidentalNoise::smooth_step(Index low, Index high, Index control) {
auto smooth_step = kernel.smoothStep(kernel[low], kernel[high], kernel[control]);
return smooth_step.getIndex();
}
Index AccidentalNoise::smoother_step(Index low, Index high, Index control) {
auto smoother_step = kernel.smootherStep(kernel[low], kernel[high], kernel[control]);
return smoother_step.getIndex();
}
Index AccidentalNoise::curve_section(Index lowv,
Index t0, Index t1, Index v0, Index v1,
Index control) {
auto curve_section = kernel.curveSection(
kernel[lowv],
kernel[t0], kernel[t1], kernel[v0], kernel[v1],
kernel[control]);
return curve_section.getIndex();
}
// Patterns
Index AccidentalNoise::hex_tile(Index seed) {
auto hex_tile = kernel.hexTile(kernel[seed]);
return hex_tile.getIndex();
}
Index AccidentalNoise::hex_bump() {
auto hex_bump = kernel.hexBump();
return hex_bump.getIndex();
}
Index AccidentalNoise::color(const Color &c) {
auto color = kernel.color(anl::SRGBA(c.r, c.g, c.b, c.a));
return color.getIndex();
}
Index AccidentalNoise::combine_rgba(Index r, Index g, Index b, Index a) {
auto combine_rgba = kernel.combineRGBA(
kernel[r], kernel[g], kernel[b], kernel[a]);
return combine_rgba.getIndex();
}
Index AccidentalNoise::combine_hsva(Index h, Index s, Index v, Index a) {
auto combine_hsva = kernel.combineHSVA(
kernel[h], kernel[s], kernel[v], kernel[a]);
return combine_hsva.getIndex();
}
Index AccidentalNoise::scale_offset(Index src, double scale, double offset) {
auto scale_offset = kernel.scaleOffset(
kernel[src], scale, offset);
return scale_offset.getIndex();
}
Index AccidentalNoise::fractal_layer(BasisTypes basis, Index interp_type,
double scale, double frequency, unsigned int seed, bool rot,
double angle, double ax, double ay, double az) {
auto fractal_layer = kernel.simpleFractalLayer(
basis, kernel[interp_type],
scale, frequency, seed, rot,
angle, ax, ay, az);
return fractal_layer.getIndex();
}
Index AccidentalNoise::ridged_layer(BasisTypes basis, Index interp_type,
double scale, double frequency, unsigned int seed, bool rot,
double angle, double ax, double ay, double az) {
auto ridged_layer = kernel.simpleRidgedLayer(
basis, kernel[interp_type],
scale, frequency, seed, rot,
angle, ax, ay, az);
return ridged_layer.getIndex();
}
Index AccidentalNoise::billow_layer(BasisTypes basis, Index interp_type,
double scale, double frequency, unsigned int seed, bool rot,
double angle, double ax, double ay, double az) {
auto billow_layer = kernel.simpleBillowLayer(
basis, kernel[interp_type],
scale, frequency, seed, rot,
angle, ax, ay, az);
return billow_layer.getIndex();
}
Index AccidentalNoise::fbm(BasisTypes basis, InterpolationTypes interp,
unsigned int numoctaves, double frequency, unsigned int seed, bool rot) {
auto fbm = kernel.simplefBm(
basis, interp,
numoctaves, frequency, seed, rot);
return fbm.getIndex();
}
Index AccidentalNoise::ridged_multifractal(BasisTypes basis, InterpolationTypes interp,
unsigned int numoctaves, double frequency, unsigned int seed, bool rot) {
auto ridged_multifractal = kernel.simpleRidgedMultifractal(
basis, interp,
numoctaves, frequency, seed, rot);
return ridged_multifractal.getIndex();
}
Index AccidentalNoise::billow(BasisTypes basis, InterpolationTypes interp,
unsigned int numoctaves, double frequency, unsigned int seed, bool rot) {
auto billow = kernel.simpleBillow(
basis, interp,
numoctaves, frequency, seed, rot);
return billow.getIndex();
}
void AccidentalNoise::set_var(const String &p_name, double p_value) {
kernel.setVar(p_name.utf8().get_data(), p_value);
}
Index AccidentalNoise::get_var(const String &p_name) {
return kernel.getVar(p_name.utf8().get_data()).getIndex();
}
// Kernel
void AccidentalNoise::set_function(Index p_index) {
// ERR_FAIL_INDEX(p_index, kernel.getKernel()->size());
if (!expression.empty()) {
return;
}
prev_function = function;
function = p_index;
}
Index AccidentalNoise::get_function() {
return function;
}
Index AccidentalNoise::get_last_function() {
return kernel.lastIndex().getIndex();
}
void AccidentalNoise::clear() {
function = 0;
prev_function = 0;
expression = String();
kernel.clear();
}
//------------------------------------------------------------------------------
// NoiseExecutor methods
//------------------------------------------------------------------------------
double AccidentalNoise::get_noise_2d(double x, double y) {
return vm.evaluateScalar(x, y, function);
}
double AccidentalNoise::get_noise_3d(double x, double y, double z) {
return vm.evaluateScalar(x, y, z, function);
}
double AccidentalNoise::get_noise_4d(double x, double y, double z, double w) {
return vm.evaluateScalar(x, y, z, w, function);
}
double AccidentalNoise::get_noise_6d(double x, double y, double z, double w, double u, double v) {
return vm.evaluateScalar(x, y, z, w, u, v, function);
}
Color AccidentalNoise::get_color_2d(double x, double y) {
anl::SRGBA c = vm.evaluateColor(x, y, function);
return Color(c.r, c.g, c.b, c.a);
}
Color AccidentalNoise::get_color_3d(double x, double y, double z) {
anl::SRGBA c = vm.evaluateColor(x, y, z, function);
return Color(c.r, c.g, c.b, c.a);
}
Color AccidentalNoise::get_color_4d(double x, double y, double z, double w) {
anl::SRGBA c = vm.evaluateColor(x, y, z, w, function);
return Color(c.r, c.g, c.b, c.a);
}
Color AccidentalNoise::get_color_6d(double x, double y, double z, double w, double u, double v) {
anl::SRGBA c = vm.evaluateColor(x, y, z, w, u, v, function);
return Color(c.r, c.g, c.b, c.a);
}
//------------------------------------------------------------------------------
// ExpressionBuilder methods
//------------------------------------------------------------------------------
Index AccidentalNoise::evaluate(const String &expression) {
WARN_PRINT("ExpressionBuilder is unstable, use at your own discretion.");
auto function = eb.eval(expression.utf8().get_data());
return function.getIndex();
}
//------------------------------------------------------------------------------
// Image methods
//------------------------------------------------------------------------------
Ref<Image> AccidentalNoise::get_image(int p_width, int p_height) {
return _map_to_image(p_width, p_height, function, mode, format, ranges);
}
Ref<Image> AccidentalNoise::get_seamless_image(int p_width, int p_height) {
// Returns seamless image regardless of mapping mode
return _map_to_image(p_width, p_height, function, SEAMLESS_XY, format, ranges);
}
Ref<Texture> AccidentalNoise::get_texture(int p_width, int p_height) {
const Ref<Image> &image = get_image(p_width, p_height);
Ref<ImageTexture> texture = memnew(ImageTexture);
texture->create_from_image(image);
return texture;
}
Vector<Ref<Image> > AccidentalNoise::get_image_3d(int p_width, int p_height, int p_depth) {
return _map_to_image_3d(p_width, p_height, p_depth, function, mode, format, ranges);
}
Vector<Ref<Image> > AccidentalNoise::get_seamless_image_3d(int p_width, int p_height, int p_depth) {
// Returns seamless 3D image regardless of mapping mode
return _map_to_image_3d(p_width, p_height, p_depth, function, SEAMLESS_XY, format, ranges);
}
Ref<Image> AccidentalNoise::_map_to_image(int p_width, int p_height, Index p_index, MappingModes p_mode, Format p_format, const AABB &p_ranges) {
anl::SMappingRanges ranges(
p_ranges.position.x, p_ranges.position.x + p_ranges.size.x,
p_ranges.position.y, p_ranges.position.y + p_ranges.size.y,
p_ranges.position.z, p_ranges.position.z + p_ranges.size.z);
PoolVector<uint8_t> dest_data;
const int SIZE = p_width * p_height;
Image::Format image_format = Image::Format::FORMAT_L8;
switch (p_format) {
case FORMAT_HEIGHTMAP: {
image_format = Image::Format::FORMAT_L8;
anl::CArray2Dd img(p_width, p_height);
anl::map2DNoZ(p_mode, img, kernel, ranges, p_index);
auto src_data = img.getData();
dest_data.resize(SIZE);
PoolVector<uint8_t>::Write w = dest_data.write();
for (int i = 0; i < SIZE; ++i) {
w[i] = (uint8_t)(src_data[i] * 255);
}
} break;
case FORMAT_NORMALMAP: {
image_format = Image::Format::FORMAT_RGBA8;
anl::CArray2Dd img(p_width, p_height);
anl::map2DNoZ(p_mode, img, kernel, ranges, p_index);
anl::CArray2Drgba normal_img(p_width, p_height);
anl::calcNormalMap(&img, &normal_img, normalmap_spacing, normalmap_normalized, normalmap_wrapped);
auto src_data = normal_img.getData();
dest_data.resize(SIZE * 4);
PoolVector<uint8_t>::Write w = dest_data.write();
for (int i = 0; i < SIZE; ++i) {
w[i * 4 + 0] = (uint8_t)(src_data[i].r * 255);
w[i * 4 + 1] = (uint8_t)(src_data[i].g * 255);
w[i * 4 + 2] = (uint8_t)(src_data[i].b * 255);
w[i * 4 + 3] = 255;
}
} break;
case FORMAT_BUMPMAP: {
image_format = Image::Format::FORMAT_L8;
anl::CArray2Dd img(p_width, p_height);
anl::map2DNoZ(p_mode, img, kernel, ranges, p_index);
anl::CArray2Dd bump_img(p_width, p_height);
float light[3] = { bumpmap_light.x, bumpmap_light.y, bumpmap_light.z };
anl::calcBumpMap(&img, &bump_img, light, bumpmap_spacing, bumpmap_wrapped);
auto src_data = bump_img.getData();
dest_data.resize(SIZE);
PoolVector<uint8_t>::Write w = dest_data.write();
for (int i = 0; i < SIZE; ++i) {
w[i] = (uint8_t)(src_data[i] * 255);
}
} break;
case FORMAT_TEXTURE: {
image_format = Image::Format::FORMAT_RGBA8;
anl::CArray2Drgba img(p_width, p_height);
anl::mapRGBA2DNoZ(p_mode, img, kernel, ranges, p_index);
auto src_data = img.getData();
dest_data.resize(SIZE * 4);
PoolVector<uint8_t>::Write w = dest_data.write();
for (int i = 0; i < SIZE; ++i) {
w[i * 4 + 0] = (uint8_t)(src_data[i].r * 255);
w[i * 4 + 1] = (uint8_t)(src_data[i].g * 255);
w[i * 4 + 2] = (uint8_t)(src_data[i].b * 255);
w[i * 4 + 3] = (uint8_t)(src_data[i].a * 255);
}
} break;
}
Ref<Image> noise = memnew(Image);
noise->create(p_width, p_height, 0, image_format, dest_data);
return noise;
}
Vector<Ref<Image> > AccidentalNoise::_map_to_image_3d(int p_width, int p_height, int p_depth, Index p_index, MappingModes p_mode, Format p_format, const AABB &p_ranges) {
anl::SMappingRanges ranges(
p_ranges.position.x, p_ranges.position.x + p_ranges.size.x,
p_ranges.position.y, p_ranges.position.y + p_ranges.size.y,
p_ranges.position.z, p_ranges.position.z + p_ranges.size.z);
Vector<PoolVector<uint8_t> > dest_data;
const int SIZE = p_width * p_height;
Image::Format image_format = Image::Format::FORMAT_L8;
switch (p_format) {
case FORMAT_HEIGHTMAP: { // more like depth map?
image_format = Image::Format::FORMAT_L8;
anl::CArray3Dd img(p_width, p_height, p_depth);
anl::map3D(p_mode, img, kernel, ranges, p_index);
for (int k = 0; k < p_depth; k++) {
PoolVector<uint8_t> dest_data_image;
dest_data_image.resize(SIZE);
PoolVector<uint8_t>::Write w = dest_data_image.write();
auto src_data = img.getData();
for (int i = SIZE * k; i < SIZE * (k + 1); i++) {
w[i] = (uint8_t)(src_data[i] * 255);
}