forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gd_water_2d.cpp
1250 lines (1106 loc) · 43.7 KB
/
gd_water_2d.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
/**************************************************************************/
/* gd_water_2d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gd_water_2d.h"
#include "common/gd_core.h"
#include "common/gd_pack.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "scene/main/viewport.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <string>
// define some constants
#ifndef M_PI
#define M_PI Math_PI
#endif
template <typename T>
T create_poolarray(size_t presize) {
T data;
data.resize(presize);
return data;
}
template <typename T>
PoolVector<T> create_poolarray(T *buf_ptr, size_t buf_size) {
PoolVector<T> data;
data.resize(buf_size);
memcpy(data.write().ptr(), buf_ptr, buf_size * sizeof(T));
return data;
}
// https://www.khronos.org/opengl/wiki/Texture_Combiners
// https://www.gamedev.net/tutorials/programming/graphics/creating-a-glsl-library-r2428
// GL_DECAL: C = Cf * (1 – At) + Ct * At ; A = Af
// clang-format off
const char *_material_shaders[] = { R"(
shader_type canvas_item;
uniform sampler2D envmap;
float map(float value, float min1, float max1, float min2, float max2) {
return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}
void fragment() {
vec4 skin = texture(TEXTURE, UV);
vec4 env = texture(envmap, UV2);
vec3 color = mix(skin.rgb, env.rgb, map(env.a, 0.5, 1.0, 0.1, 1));
COLOR *= vec4(color, skin.a);
}
)", R"(
shader_type canvas_item;
uniform sampler2D envmap;
void fragment() {
vec4 skin = texture(TEXTURE, UV);
vec4 env = texture(envmap, UV2);
vec3 modulate = skin.rgb * env.rgb;
COLOR *= vec4(mix(modulate.rgb, skin.rgb, env.a), skin.a);
}
)", R"(
shader_type canvas_item;
uniform sampler2D envmap;
void fragment() {
vec4 skin = texture(TEXTURE, UV);
vec4 env = texture(envmap, UV2);
vec3 color = mix(skin.rgb, env.rgb, env.a);
COLOR *= vec4(color, skin.a);
}
)", R"(
shader_type canvas_item;
uniform sampler2D envmap;
float map(float value, float min1, float max1, float min2, float max2) {
return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}
void fragment() {
vec4 skin = texture(TEXTURE, UV);
vec4 env = texture(envmap, UV2);
if (UV.x > 0.66) {
vec3 color = mix(skin.rgb, env.rgb, map(env.a, 0.5, 1.0, 0.1, 1));
COLOR *= vec4(color, skin.a);
} else if (UV.x > 0.33) {
vec3 modulate = skin.rgb * env.rgb;
COLOR *= vec4(mix(modulate.rgb, skin.rgb, env.a), skin.a);
} else {
vec3 color = mix(skin.rgb, env.rgb, env.a);
COLOR *= vec4(color, skin.a);
}
}
)" };
// clang-format on
/// Private methods
// https://blog.demofox.org/2017/05/29/when-random-numbers-are-too-random-low-discrepancy-sequences/
static Vector<Point2> _get_samples(int num, size_t basex, size_t basey, const Rect2 &view) {
// calculate the sample points
Vector<Point2> ret;
ret.resize(num);
auto samples = ret.write;
for (size_t i = 0; i < num; ++i) {
samples[i].x = 0; // x axis
{
real_t denominator = basex;
size_t n = i;
while (n > 0) {
const size_t multiplier = n % basex;
samples[i].x += multiplier / denominator;
n = n / basex;
denominator *= basex;
}
}
samples[i].y = 0; // y axis
{
real_t denominator = basey;
size_t n = i;
while (n > 0) {
const size_t multiplier = n % basey;
samples[i].y += multiplier / denominator;
n = n / basey;
denominator *= basey;
}
}
}
for (size_t i = 0; i < num; ++i) {
samples[i].x = view.position.x + samples[i].x * view.size.width;
samples[i].y = view.position.y + samples[i].y * view.size.height;
}
return ret;
}
static Ref<Texture> make_texture_from_data(const uint8_t *p_data, size_t p_data_len, int p_flags, const String &p_name) {
String fn = "user://__water2d_" + String::num(p_flags) + "_" + p_name; // cached texture
if (ResourceLoader::exists(fn + ".tex")) {
return ResourceLoader::load(fn + ".tex", "Texture");
} else {
Ref<Image> image = memnew(Image(p_data, p_data_len));
Ref<ImageTexture> texture = memnew(ImageTexture);
texture->create_from_image(image, p_flags);
texture->set_name(p_name);
ResourceSaver::save(fn + ".tex", texture);
return texture;
}
}
#define IRAND() Math::rand()
#define TS() OS::get_singleton()->get_ticks_msec()
namespace {
enum {
ARRAY_MESH_VERTEX,
ARRAY_MESH_SKIN_UV,
ARRAY_MESH_ENVMAP_UV,
ARRAY_MESH_INDEX,
ARRAY_MESH_WIREFRAME_COLOR,
ARRAY_MESH_WIREFRAME_INDEX,
ARRAY_MESH_MAX,
};
}
// initial conditions: every heights at zero
template <int WaterSize>
void WaterRipples<WaterSize>::init() {
memset(*p1, 0, BufferSpace);
memset(*p2, 0, BufferSpace);
memset(smooth, 0, BufferSpace);
prebuild_water(); // prebuild geometric model
}
// trace a hole at normalized [0..1] coordinates
template <int WaterSize>
void WaterRipples<WaterSize>::set_wave(real_t p_x, real_t p_y, int p_amp) {
const int x = Math::fposmod(p_x, 1) * WaterSize;
const int y = Math::fposmod(p_y, 1) * WaterSize;
print_verbose(vformat("(WaterRipples) set wave at cell. %dx%d", x, y));
(*p1)[x][y] -= p_amp;
}
// trace a hole following parametric curves
template <int WaterSize>
void WaterRipples<WaterSize>::run_wave(real_t p_phase, real_t p_cos, real_t p_sin, int p_amp) {
const real_t r = (angle * M_PI) / 1024;
// [TODO] Ripple types:
// https://github.com/lequangios/Ripple-Cocos2dx/blob/e7e0a379ed20c2a033c802e0edc3309e7757eea5/CCRippleSprite.cpp#L290
const int x = Math::fposmod(Math::cos(p_cos * r + p_phase), 1) * WaterSize;
const int y = Math::fposmod(Math::sin(p_sin * r + p_phase), 1) * WaterSize;
print_verbose(vformat("(WaterRipples) run wave at cell. %dx%d", x, y));
(*p1)[x][y] -= p_amp;
}
// trace a random hole
template <int WaterSize>
void WaterRipples<WaterSize>::random_wave() {
const int x = IRAND() % WaterSize + 1;
const int y = IRAND() % WaterSize + 1;
const int amp = IRAND() & 127;
print_verbose(vformat("(WaterRipples) ranom wave at cell. %dx%d (amp: %d)", x, y, amp));
(*p1)[IRAND() % WaterSize + 1][IRAND() % WaterSize + 1] -= amp;
}
// update to next state of fluid model
template <int WaterSize>
void WaterRipples<WaterSize>::update() {
angle = (angle + 2) & 1023; // new angle for parametric curves
new_water(); // fluid update
smooth_water(); // smoothing
}
// physical calculus for fluid model
template <int WaterSize>
void WaterRipples<WaterSize>::new_water() {
// discretized differential equation
for (int x = 1; x <= WaterSize; x++) {
for (int y = 0; y <= WaterSize; y++) {
(*p1)[x][y] = (((*p2)[x - 1][y] + (*p2)[x + 1][y] + (*p2)[x][y - 1] + (*p2)[x][y + 1]) >> 1) - (*p1)[x][y];
(*p1)[x][y] -= (*p1)[x][y] >> 4;
}
}
// copy borders to make the map periodic
memcpy(&(*p1)[0][0], &(*p1)[1][0], BufferRowSpace);
memcpy(&(*p1)[WaterSize + 1][0], &(*p1)[1][0], BufferRowSpace);
for (int x = 0, *ptr = &(*p1)[0][0]; x <= WaterSize; x++, ptr += BufferSize) {
ptr[0] = ptr[1];
ptr[WaterSize + 1] = ptr[1];
}
// swap buffers t and t-1, we advance in time
std::swap(p1, p2);
}
// filter and smooth producted values
template <int WaterSize>
void WaterRipples<WaterSize>::smooth_water() {
for (int x = 1; x < WaterSize + 1; x++) {
for (int y = 1; y < WaterSize + 1; y++) {
smooth[x][y] = (3 * (*p1)[x][y] + 2 * (*p1)[x + 1][y] + 2 * (*p1)[x][y + 1] + (*p1)[x + 1][y + 1]) >> 3;
}
}
for (int i = 1; i < 4; i++) {
for (int x = 1; x < WaterSize + 1; x++) {
for (int y = 1; y < WaterSize + 1; y++) {
smooth[x][y] = (3 * smooth[x][y] + 2 * smooth[x + 1][y] + 2 * smooth[x][y + 1] + smooth[x + 1][y + 1]) >> 3;
}
}
}
}
// pre-building of a geometric model
template <int WaterSize>
void WaterRipples<WaterSize>::prebuild_water(void) {
print_verbose("(WaterRipples) prebuilding water geometry ..");
// vertices calculus -> we already know x and y
// calculus of background texture coordinates
for (int x = 0; x < GridSize; x++) {
const real_t xmin = x / 2.0;
for (int y = 0; y < GridSize; y++) {
const real_t ymin = y / 2.0;
sommet[x][y] = { xmin, ymin, 0 };
uvmap[x][y] = { x * CellSize, y * CellSize };
}
}
// normals to faces calculus: z component is constant
for (int x = 0; x < GridSize; x++) {
for (int y = 0; y < GridSize; y++) {
normal[x][y].z = NormZ;
}
}
// calculate normals to vertices (z component only)
for (int x = 1; x < GridLast; x++) {
for (int y = 1; y < GridLast; y++) {
// snormal[x][y].z = normal[x - 1][y].z + normal[x + 1][y].z + normal[x][y -1].z + normal[x][y + 1].z;
// snormal[x][y].z = 0.01 + 0.01 + 0.01 + 0.01 = 0.04;
snormal[x][y].z = NormZ * 4;
}
}
// copy borders of the map (z component only) for periodicity
for (int x = 0; x < GridSize; x++) {
snormal[x][0].z = normal[x][0].z;
snormal[x][GridLast].z = normal[x][GridLast].z;
}
memcpy((void *)&snormal[0][0], (const void *)&normal[0][0], GridRowSpace);
memcpy((void *)&snormal[GridLast][0], (const void *)&normal[GridLast][0], GridRowSpace);
}
// construction of a geometric model
template <int WaterSize>
void WaterRipples<WaterSize>::build_water() {
const real_t zf = size_factor;
// calculate vertices: z component
for (int x = 0; x < WaterSize; x++) {
for (int y = 0; y < WaterSize; y++) {
const real_t h1 = smooth[x + 1][y + 1] / zf;
sommet[x * 2][y * 2].z = (h1 < 0) ? 0 : h1;
}
}
// construct vertices in-between
for (int x = 0; x <= GridLast; x += 2) { // even rows
for (int y = 1; y <= GridLast - 1; y += 2) { // odd columns
sommet[x][y].z = (sommet[x][y - 1].z + sommet[x][y + 1].z) / 2;
}
}
// construct vertices in-between
for (int x = 1; x <= GridLast - 1; x += 2) { // even rows
for (int y = 0; y <= GridLast; y++) { // every columns
sommet[x][y].z = (sommet[x - 1][y].z + sommet[x + 1][y].z) / 2;
}
}
// calculate normals to faces : components x and y
// -> simplified cross product knowing that we have a distance of 1.0 between
// each fluid cells.
for (int x = 0; x < GridLast; x++) {
for (int y = 0; y < GridSize - 1; y++) {
normal[x][y].x = 0.1 * (sommet[x][y].z - sommet[x + 1][y].z);
normal[x][y].y = 0.1 * (sommet[x][y].z - sommet[x][y + 1].z);
}
}
// copy map borders(components x and y only) for periodicity
memcpy((void *)&normal[GridLast][0], (void *)&normal[GridLast - 1][0], GridRowSpace);
for (int x = 0; x < GridSize; x++) {
normal[x][GridLast].x = normal[x][GridLast - 1].x;
normal[x][GridLast].y = normal[x][GridLast - 1].y;
}
// calculate normals to vertices (components X and Y only)
for (int x = 1; x < GridLast; x++) {
for (int y = 1; y < GridLast; y++) {
snormal[x][y].x = normal[x - 1][y].x + normal[x + 1][y].x + normal[x][y - 1].x + normal[x][y + 1].x;
snormal[x][y].y = normal[x - 1][y].y + normal[x + 1][y].y + normal[x][y - 1].y + normal[x][y + 1].y;
}
}
// copy map borders (components x and y only)
for (int x = 0; x < GridSize; x++) {
snormal[x][0].x = normal[x][0].x;
snormal[x][0].y = normal[x][0].y;
snormal[x][GridLast].x = normal[x][GridLast].x;
snormal[x][GridLast].y = normal[x][GridLast].y;
}
memcpy((void *)&snormal[0][0], (const void *)&normal[0][0], GridRowSpace);
memcpy((void *)&snormal[GridLast][0], (const void *)&normal[GridLast][0], GridRowSpace);
constexpr real_t SNormZ = NormZ * 4;
// calculate ourself normalization
for (int x = 0; x < GridSize; x++) {
for (int y = 0; y < GridSize; y++) {
const real_t sqroot = Math::sqrt(snormal[x][y].x * snormal[x][y].x + snormal[x][y].y * snormal[x][y].y + SNormZ * SNormZ);
snormaln[x][y].x = snormal[x][y].x / sqroot;
snormaln[x][y].y = snormal[x][y].y / sqroot;
snormaln[x][y].z = SNormZ / sqroot;
}
}
// really simple version of a fake envmap generator
for (int x = 0; x < GridSize; x++) {
for (int y = 0; y < GridSize; y++) {
// perturbate coordinates of background mapping with the components x,y of normals...
// simulate refraction
newuvmap[x][y].x = uvmap[x][y].x + 0.05 * snormaln[x][y].x;
newuvmap[x][y].y = uvmap[x][y].y + 0.05 * snormaln[x][y].y;
// trick: xy projection of normals -> assume reflection in direction of the normals
// looks ok for non-plane surfaces
envmap[x][y].x = 0.5 + snormaln[x][y].x * 0.45;
envmap[x][y].y = 0.5 + snormaln[x][y].y * 0.45;
}
}
}
// build strip index for vertex arrays
template <int WaterSize>
void WaterRipples<WaterSize>::build_strip_index() {
// array is (WaterSize * 2) x (WaterSize * 2)
const int strip_width = GridSize - 2; // n points define n-2 triangles in a strip
int *sindex_ptr = &sindex[0];
// build index list
for (int x = 0; x < strip_width; x++) { // vertical index in array
*sindex_ptr++ = ((x + 1) * GridSize) + 1; // strip_width + 1 triangle strips
for (int y = 1; y < strip_width; y++) { // horizontal index in array
*sindex_ptr++ = (x * GridSize) + y;
*sindex_ptr++ = ((x + 1) * GridSize) + y + 1;
}
*sindex_ptr++ = (x * GridSize) + strip_width;
}
}
// build triangles index for vertex arrays
template <int WaterSize>
void WaterRipples<WaterSize>::build_tri_index() {
int *sindex_ptr = &sindex[0];
// build index list (column order)
for (int y = 0; y < GridSize - 1; y++) {
for (int x = 0; x < GridSize - 1; x++) {
*sindex_ptr++ = y * GridSize + x;
*sindex_ptr++ = y * GridSize + (x + 1);
*sindex_ptr++ = (y + 1) * GridSize + x;
*sindex_ptr++ = (y + 1) * GridSize + (x + 1);
*sindex_ptr++ = (y + 1) * GridSize + x;
*sindex_ptr++ = y * GridSize + (x + 1);
}
}
// int l0 = 0;
// int l1 = WaterSize * 4;
// int l2 = l1;
//
// for (int y = 0; y < GridSize; y += 2) {
// for (int x = 0; x < GridSize; x++) { // n-2 triangles (n points in 2 array lines)
// if ((x & 1) == 1) {
// *sindex_ptr++ = l1++;
// l1++;
// } else {
// *sindex_ptr++ = l0++;
// l0++;
// }
// }
//
// l0 = l2;
// l1 = l2 + WaterSize * 4;
// l2 = l1;
// }
}
// build and display geometric model
template <int WaterSize>
Array WaterRipples<WaterSize>::build_mesh_data(const Rect2 *p_skin_region, const Rect2 *p_envmap_region, bool p_with_wireframe) {
build_water();
build_tri_index();
const size_t indexes_num = (GridSize - 1) * (GridSize - 1) * 6;
Array mesh_data;
mesh_data.resize(ARRAY_MESH_MAX);
mesh_data[ARRAY_MESH_VERTEX] = create_poolarray(&sommet[0][0], GridArea);
if (p_with_wireframe) {
// wireframe from triangles:
PoolIntArray windex;
windex.resize(indexes_num * 2);
auto _windex = windex.write();
for (int i = 0, j = 0; i < indexes_num; i += 3, j += 6) {
_windex[j + 0] = sindex[i + 0];
_windex[j + 1] = sindex[i + 1];
_windex[j + 2] = sindex[i + 1];
_windex[j + 3] = sindex[i + 2];
_windex[j + 4] = sindex[i + 2];
_windex[j + 5] = sindex[i + 0];
}
_windex.release();
Color colors[GridSize][GridSize];
for (int x = 0; x < GridSize; x++) {
for (int y = 0; y < GridSize; y++) {
if (sommet[x][y].z != 0) {
const real_t z = CLAMP(sommet[x][y].z * 10, 0, 1);
colors[x][y] = Color(z, 1 - z, 1 - z, 0.6);
} else {
colors[x][y] = Color(0, 1, 1, 0.3);
}
}
}
mesh_data[ARRAY_MESH_WIREFRAME_COLOR] = create_poolarray(&colors[0][0], GridArea);
mesh_data[ARRAY_MESH_WIREFRAME_INDEX] = windex;
}
// normalize uv for mask texture atlas
Vector2(*_newuvmap)[GridSize] = newuvmap;
Vector2(*_envmap)[GridSize] = envmap;
if (p_skin_region || p_envmap_region) {
if (p_skin_region) {
_newuvmap = new Vector2[GridSize][GridSize];
}
if (p_envmap_region) {
_envmap = new Vector2[GridSize][GridSize];
}
for (int x = 0; x < GridSize; x++) {
for (int y = 0; y < GridSize; y++) {
// normalize uv coordinates for texture atlas
if (p_skin_region) {
_newuvmap[x][y] = p_skin_region->position + newuvmap[x][y] * p_skin_region->size;
}
if (p_envmap_region) {
_envmap[x][y] = p_envmap_region->position + envmap[x][y] * p_envmap_region->size;
}
}
}
}
mesh_data[ARRAY_MESH_SKIN_UV] = create_poolarray(&_newuvmap[0][0], GridArea);
mesh_data[ARRAY_MESH_ENVMAP_UV] = create_poolarray(&_envmap[0][0], GridArea);
mesh_data[ARRAY_MESH_INDEX] = create_poolarray(sindex, indexes_num);
return mesh_data;
}
template <int WaterSize>
int WaterRipples<WaterSize>::get_grid_size() const {
return GridSize;
}
template <int WaterSize>
void WaterRipples<WaterSize>::set_size_factor(int p_factor) {
size_factor = p_factor;
}
template <int WaterSize>
int WaterRipples<WaterSize>::get_size_factor() const {
return size_factor;
}
// define GridSize x GridSize columns of fluid
template <int WaterSize>
WaterRipples<WaterSize>::WaterRipples() {
size_factor = 100;
angle = 0;
p1 = &water1; // FRONT map
p2 = &water2; // BACK map
// allocate buffers
sommet = new Vector3[GridSize][GridSize]; // vertices vector
normal = new Vector3[GridSize][GridSize]; // quads normals
snormal = new Vector3[GridSize][GridSize]; // vertices normals (average)
snormaln = new Vector3[GridSize][GridSize]; // normalized vertices normals
uvmap = new Vector2[GridSize][GridSize]; // background texture coordinates
newuvmap = new Vector2[GridSize][GridSize]; // perturbated background coordinates -> refraction
envmap = new Vector2[GridSize][GridSize]; // envmap coordinates
sindex = new int[(GridSize - 1) * (GridSize - 1) * 6]; // vertex array index
}
template <int WaterSize>
WaterRipples<WaterSize>::~WaterRipples(void) {
delete[] sommet; // vertices vector
delete[] normal; // quads normals
delete[] snormal; // vertices normals (average)
delete[] snormaln; // normalized vertices normals
delete[] uvmap; // background texture coordinates
delete[] newuvmap; // perturbated background coordinates -> refraction
delete[] envmap; // envmap coordinates
delete[] sindex; // vertex array index
}
/// Godot node
#ifdef TOOLS_ENABLED
Dictionary Water2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["view_size"] = get_view_size();
return state;
}
void Water2D::_edit_set_state(const Dictionary &p_state) {
Node2D::_edit_set_state(p_state);
set_view_size(p_state["view_size"]);
}
Rect2 Water2D::_edit_get_rect() const {
return Rect2(Point2(), get_view_size());
}
void Water2D::_edit_set_rect(const Rect2 &p_rect) {
set_view_size(p_rect.size);
_change_notify();
}
bool Water2D::_edit_use_rect() const {
return true;
}
#endif
#define run_water(cmd) \
switch (level_quality) { \
case 0: \
water0->cmd; \
break; \
case 1: \
water1->cmd; \
break; \
default: \
case 2: \
water2->cmd; \
break; \
}
#define run_water_r(cmd) \
switch (level_quality) { \
case 0: \
return water0->cmd; \
case 1: \
return water1->cmd; \
default: \
case 2: \
return water2->cmd; \
}
void Water2D::set_active(bool p_state) {
active = p_state;
if (is_visible_in_tree()) {
set_process_internal(active);
}
}
bool Water2D::is_active() const {
return active;
}
void Water2D::set_quality_level(int p_quality) {
level_quality = p_quality;
update();
}
int Water2D::get_quality_level() const {
return level_quality;
}
void Water2D::set_skin_texture(const Ref<Texture> &p_texture) {
if (texture_skin != p_texture) {
texture_skin = p_texture;
_update_material |= UPDATE_WATER;
update();
}
}
Ref<Texture> Water2D::get_skin_texture() const {
return texture_skin;
}
void Water2D::set_mask_texture(const Ref<Texture> &p_texture) {
if (texture_mask != p_texture) {
texture_mask = p_texture;
_update_material |= UPDATE_ALL;
update();
}
}
Ref<Texture> Water2D::get_mask_texture() const {
return texture_mask;
}
void Water2D::set_details_map(bool p_state) {
details_map = p_state;
_update_material |= UPDATE_DETAILS;
update();
}
bool Water2D::is_details_map() const {
return details_map;
}
void Water2D::set_caustics(bool p_state) {
caustics = p_state;
_update_material |= UPDATE_CAUSTICS;
update();
}
bool Water2D::is_caustics() const {
return caustics;
}
void Water2D::set_wireframe(bool p_state) {
if (wireframe != p_state) {
wireframe = p_state;
update();
}
}
bool Water2D::is_wireframe() const {
return wireframe;
}
void Water2D::set_blend_mode(int p_mode) {
ERR_FAIL_INDEX(p_mode, BLEND_MODES_NUM);
if (blend_variant != p_mode) {
blend_variant = p_mode;
_update_material |= UPDATE_WATER;
}
}
int Water2D::get_blend_mode() const {
return blend_variant;
}
void Water2D::set_wave_speed_rate(real_t p_rate) {
wave_speed_rate = p_rate;
}
real_t Water2D::get_wave_speed_rate() const {
return wave_speed_rate;
}
void Water2D::set_wave_size_factor(int p_factor) {
run_water(set_size_factor(p_factor));
}
int Water2D::get_wave_size_factor() const {
run_water_r(get_size_factor());
}
void Water2D::set_caustics_speed_rate(real_t p_rate) {
caustics_speed_rate = p_rate;
}
real_t Water2D::get_caustics_speed_rate() const {
return caustics_speed_rate;
}
void Water2D::set_caustics_alpha(real_t p_alpha) {
ERR_FAIL_COND(p_alpha <= 0 || p_alpha > 1);
caustics_alpha = p_alpha;
_update_material |= UPDATE_CAUSTICS;
update();
}
real_t Water2D::get_caustics_alpha() const {
return caustics_alpha;
}
void Water2D::set_wave(real_t p_x, real_t p_y, int p_amp) {
run_water(set_wave(p_x, p_y, p_amp));
}
void Water2D::run_wave(real_t p_phase, real_t p_cos, real_t p_sin, int p_amp) {
run_water(run_wave(p_phase, p_cos, p_sin, p_amp));
}
void Water2D::random_wave() {
run_water(random_wave());
}
extern "C" const uint8_t envmap_png_data[];
extern "C" const size_t envmap_png_size;
extern "C" const uint8_t *causticsmaps[];
extern "C" const uint8_t *noisemaps[];
_FORCE_INLINE_ static std::pair<bool, Rect2> get_texture_region(const Ref<Texture> &p_texture) {
Ref<AtlasTexture> atlas = p_texture;
if (atlas.is_valid()) {
return std::make_pair(true, atlas->get_region()); // true region
}
return std::make_pair(false, Rect2(0, 0, 1, 1)); // full rect
}
void Water2D::_changed_callback(Object *p_changed, const char *p_prop) {
#ifdef TOOLS_ENABLED
if (p_changed == this) {
if (strcmp(p_prop, "z-index") == 0) {
if (mesh_item.is_valid()) {
VS::get_singleton()->canvas_item_set_draw_index(mesh_item, get_index());
}
if (caustics_item.is_valid()) {
VS::get_singleton()->canvas_item_set_draw_index(caustics_item, get_index());
}
}
}
#endif
}
void Water2D::_notification(int p_notification) {
switch (p_notification) {
case NOTIFICATION_READY: {
run_water(init());
texture_envmap = make_texture_from_data(envmap_png_data, envmap_png_size, Texture::FLAGS_DEFAULT, "envmap");
mesh_item = RID_PRIME(VS::get_singleton()->canvas_item_create());
VS::get_singleton()->canvas_item_set_parent(mesh_item, get_canvas_item());
VS::get_singleton()->canvas_item_set_draw_index(mesh_item, get_index());
_update_material = UPDATE_ALL;
} break;
case NOTIFICATION_ENTER_TREE: {
if (active) {
set_process_internal(true);
}
} break;
case NOTIFICATION_EXIT_TREE: {
set_process_internal(false);
} break;
case NOTIFICATION_DRAW: {
std::pair<bool, Rect2> skin_reg = get_texture_region(texture_skin);
std::pair<bool, Rect2> envmap_reg = get_texture_region(texture_envmap);
auto build_mesh_data = [=](const Rect2 *p_skin_region, const Rect2 *p_envmap_region, bool p_with_wireframe) {
run_water_r(build_mesh_data(skin_reg.first ? &skin_reg.second : nullptr, envmap_reg.first ? &envmap_reg.second : nullptr, wireframe));
};
Array mesh_data = build_mesh_data(skin_reg.first ? &skin_reg.second : nullptr, envmap_reg.first ? &envmap_reg.second : nullptr, wireframe);
if (wireframe) { // wireframe
if (!mesh_wireframe.is_valid()) {
mesh_wireframe = newref(ArrayMesh);
}
Array w;
w.resize(VS::ARRAY_MAX);
w[VisualServer::ARRAY_VERTEX] = mesh_data[ARRAY_MESH_VERTEX];
w[VisualServer::ARRAY_COLOR] = mesh_data[ARRAY_MESH_WIREFRAME_COLOR];
w[VisualServer::ARRAY_INDEX] = mesh_data[ARRAY_MESH_WIREFRAME_INDEX];
mesh_wireframe->clear_mesh();
mesh_wireframe->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, w);
draw_mesh(mesh_wireframe, Ref<Texture>(), texture_mask);
}
{ // skin and envmap
if (details_map && !animation_details.is_valid()) {
// load maps
for (int m = 0; m < NUM_NORMALS; m++) {
animation_details.frames.push_back(make_texture_from_data(noisemaps[m], -1, Texture::FLAGS_DEFAULT, "details" + String::num(m)));
}
}
Array d;
d.resize(VS::ARRAY_MAX);
d[VisualServer::ARRAY_VERTEX] = mesh_data[ARRAY_MESH_VERTEX];
d[VisualServer::ARRAY_TEX_UV] = mesh_data[ARRAY_MESH_SKIN_UV];
d[VisualServer::ARRAY_TEX_UV2] = mesh_data[ARRAY_MESH_ENVMAP_UV];
d[VisualServer::ARRAY_INDEX] = mesh_data[ARRAY_MESH_INDEX];
mesh->clear_mesh();
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d);
if (_update_material & UPDATE_WATER || _update_material & UPDATE_DETAILS) {
materials[blend_variant]->set_shader_param("envmap", texture_envmap);
VS::get_singleton()->canvas_item_clear(mesh_item);
VS::get_singleton()->canvas_item_set_material(mesh_item, materials[blend_variant]->get_rid());
RID _skin = texture_skin ? texture_skin->get_rid() : RID();
RID _mask = texture_mask ? texture_mask->get_rid() : RID();
RID _details = (details_map && animation_details.is_valid()) ? animation_details.texture()->get_rid() : RID();
VS::get_singleton()->canvas_item_add_mesh(mesh_item, mesh->get_rid(), Transform2D(), Color(1, 1, 1, 1), _skin, _details, _mask);
}
}
if (caustics) { // caustics
if (!animation_caustics.is_valid()) {
// load maps
for (int c = 0; c < NUM_CAUSTICS; c++) {
animation_caustics.frames.push_back(make_texture_from_data(causticsmaps[c], -1, Texture::FLAGS_DEFAULT, "caust" + String::num(c)));
}
}
if (!caustics_item.is_valid()) {
caustics_item = RID_PRIME(VS::get_singleton()->canvas_item_create());
VS::get_singleton()->canvas_item_set_parent(caustics_item, get_canvas_item());
VS::get_singleton()->canvas_item_set_draw_index(caustics_item, get_index());
}
if (!mesh_caustics.is_valid()) {
mesh_caustics = newref(ArrayMesh);
}
if (!material_caustics.is_valid()) {
material_caustics = newref(CanvasItemMaterial);
material_caustics->set_blend_mode(CanvasItemMaterial::BLEND_MODE_ADD);
VS::get_singleton()->canvas_item_set_material(caustics_item, material_caustics->get_rid());
}
Array d;
d.resize(VS::ARRAY_MAX);
d[VisualServer::ARRAY_VERTEX] = mesh_data[ARRAY_MESH_VERTEX];
d[VisualServer::ARRAY_TEX_UV] = mesh_data[ARRAY_MESH_SKIN_UV];
d[VisualServer::ARRAY_INDEX] = mesh_data[ARRAY_MESH_INDEX];
mesh_caustics->clear_mesh();
mesh_caustics->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d);
if (_update_material & UPDATE_CAUSTICS) {
VS::get_singleton()->canvas_item_clear(caustics_item);
VS::get_singleton()->canvas_item_set_visible(caustics_item, true);
RID _caustics = animation_caustics.is_valid() ? animation_caustics.texture()->get_rid() : RID();
RID _mask = texture_mask ? texture_mask->get_rid() : RID();
VS::get_singleton()->canvas_item_add_mesh(caustics_item, mesh_caustics->get_rid(), Transform2D(), Color(1, 1, 1, caustics_alpha), _caustics, RID(), _mask);
}
} else {
if (_update_material & UPDATE_CAUSTICS) {
VS::get_singleton()->canvas_item_set_visible(caustics_item, false);
}
}
_update_material = 0;
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
set_process_internal(active && is_visible());
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
static float water_progress = 0;
const float delta = get_process_delta_time();
water_progress += delta;
if (water_progress > wave_speed_rate) {
run_water(update());
update();
water_progress = 0;
}
if (caustics && animation_caustics.next(delta, caustics_speed_rate)) {
_update_material |= UPDATE_CAUSTICS;
update();
}
if (details_map && animation_details.next(delta, details_speed_rate)) {
_update_material |= UPDATE_DETAILS;
update();
}
} break;
}
}
#ifdef TOOLS_ENABLED
void Water2D::toogle_demo_mode() {
set_blend_mode(BLEND_MODE_DEMO);
}
#endif
void Water2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_active"), &Water2D::set_active);
ClassDB::bind_method(D_METHOD("is_active"), &Water2D::is_active);
ClassDB::bind_method(D_METHOD("set_quality_level"), &Water2D::set_quality_level);
ClassDB::bind_method(D_METHOD("get_quality_level"), &Water2D::get_quality_level);
ClassDB::bind_method(D_METHOD("set_skin_texture"), &Water2D::set_skin_texture);
ClassDB::bind_method(D_METHOD("get_skin_texture"), &Water2D::get_skin_texture);
ClassDB::bind_method(D_METHOD("set_mask_texture"), &Water2D::set_mask_texture);
ClassDB::bind_method(D_METHOD("get_mask_texture"), &Water2D::get_mask_texture);
ClassDB::bind_method(D_METHOD("set_wireframe"), &Water2D::set_wireframe);
ClassDB::bind_method(D_METHOD("is_wireframe"), &Water2D::is_wireframe);
ClassDB::bind_method(D_METHOD("set_blend_mode"), &Water2D::set_blend_mode);
ClassDB::bind_method(D_METHOD("get_blend_mode"), &Water2D::get_blend_mode);
ClassDB::bind_method(D_METHOD("set_details_map"), &Water2D::set_details_map);
ClassDB::bind_method(D_METHOD("is_details_map"), &Water2D::is_details_map);
ClassDB::bind_method(D_METHOD("set_wave_speed_rate"), &Water2D::set_wave_speed_rate);
ClassDB::bind_method(D_METHOD("get_wave_speed_rate"), &Water2D::get_wave_speed_rate);
ClassDB::bind_method(D_METHOD("set_wave_size_factor"), &Water2D::set_wave_size_factor);
ClassDB::bind_method(D_METHOD("get_wave_size_factor"), &Water2D::get_wave_size_factor);
ClassDB::bind_method(D_METHOD("set_caustics"), &Water2D::set_caustics);
ClassDB::bind_method(D_METHOD("is_caustics"), &Water2D::is_caustics);
ClassDB::bind_method(D_METHOD("set_caustics_speed_rate"), &Water2D::set_caustics_speed_rate);
ClassDB::bind_method(D_METHOD("get_caustics_speed_rate"), &Water2D::get_caustics_speed_rate);
ClassDB::bind_method(D_METHOD("set_caustics_alpha"), &Water2D::set_caustics_alpha);
ClassDB::bind_method(D_METHOD("get_caustics_alpha"), &Water2D::get_caustics_alpha);
ClassDB::bind_method(D_METHOD("set_wave", "x", "y", "amp"), &Water2D::set_wave);
ClassDB::bind_method(D_METHOD("run_wave", "phase", "cos", "sin", "amp"), &Water2D::run_wave);
ClassDB::bind_method(D_METHOD("random_wave"), &Water2D::random_wave);