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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
|
/*************************************************************************/
/* visual_shader_particle_nodes.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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 "visual_shader_particle_nodes.h"
#include "core/core_string_names.h"
// VisualShaderNodeParticleEmitter
int VisualShaderNodeParticleEmitter::get_output_port_count() const {
return 1;
}
VisualShaderNodeParticleEmitter::PortType VisualShaderNodeParticleEmitter::get_output_port_type(int p_port) const {
if (mode_2d) {
return PORT_TYPE_VECTOR_2D;
}
return PORT_TYPE_VECTOR_3D;
}
String VisualShaderNodeParticleEmitter::get_output_port_name(int p_port) const {
if (p_port == 0) {
return "position";
}
return String();
}
bool VisualShaderNodeParticleEmitter::has_output_port_preview(int p_port) const {
return false;
}
void VisualShaderNodeParticleEmitter::set_mode_2d(bool p_enabled) {
if (mode_2d == p_enabled) {
return;
}
mode_2d = p_enabled;
emit_changed();
}
bool VisualShaderNodeParticleEmitter::is_mode_2d() const {
return mode_2d;
}
Vector<StringName> VisualShaderNodeParticleEmitter::get_editable_properties() const {
Vector<StringName> props;
props.push_back("mode_2d");
return props;
}
HashMap<StringName, String> VisualShaderNodeParticleEmitter::get_editable_properties_names() const {
HashMap<StringName, String> names;
names.insert("mode_2d", RTR("2D Mode"));
return names;
}
bool VisualShaderNodeParticleEmitter::is_show_prop_names() const {
return true;
}
void VisualShaderNodeParticleEmitter::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mode_2d", "enabled"), &VisualShaderNodeParticleEmitter::set_mode_2d);
ClassDB::bind_method(D_METHOD("is_mode_2d"), &VisualShaderNodeParticleEmitter::is_mode_2d);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_2d"), "set_mode_2d", "is_mode_2d");
}
VisualShaderNodeParticleEmitter::VisualShaderNodeParticleEmitter() {
}
// VisualShaderNodeParticleSphereEmitter
String VisualShaderNodeParticleSphereEmitter::get_caption() const {
return "SphereEmitter";
}
int VisualShaderNodeParticleSphereEmitter::get_input_port_count() const {
return 2;
}
VisualShaderNodeParticleSphereEmitter::PortType VisualShaderNodeParticleSphereEmitter::get_input_port_type(int p_port) const {
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleSphereEmitter::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "radius";
} else if (p_port == 1) {
return "inner_radius";
}
return String();
}
String VisualShaderNodeParticleSphereEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
String code;
code += "vec2 __get_random_point_in_circle(inout uint seed, float radius, float inner_radius) {\n";
code += " return __get_random_unit_vec2(seed) * __randf_range(seed, inner_radius, radius);\n";
code += "}\n\n";
code += "vec3 __get_random_point_in_sphere(inout uint seed, float radius, float inner_radius) {\n";
code += " return __get_random_unit_vec3(seed) * __randf_range(seed, inner_radius, radius);\n";
code += "}\n\n";
return code;
}
String VisualShaderNodeParticleSphereEmitter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
if (mode_2d) {
code += " " + p_output_vars[0] + " = __get_random_point_in_circle(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
} else {
code += " " + p_output_vars[0] + " = __get_random_point_in_sphere(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
}
return code;
}
VisualShaderNodeParticleSphereEmitter::VisualShaderNodeParticleSphereEmitter() {
set_input_port_default_value(0, 10.0);
set_input_port_default_value(1, 0.0);
}
// VisualShaderNodeParticleBoxEmitter
String VisualShaderNodeParticleBoxEmitter::get_caption() const {
return "BoxEmitter";
}
int VisualShaderNodeParticleBoxEmitter::get_input_port_count() const {
return 1;
}
VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleBoxEmitter::get_input_port_type(int p_port) const {
if (p_port == 0) {
if (mode_2d) {
return PORT_TYPE_VECTOR_2D;
}
return PORT_TYPE_VECTOR_3D;
}
return PORT_TYPE_SCALAR;
}
void VisualShaderNodeParticleBoxEmitter::set_mode_2d(bool p_enabled) {
if (mode_2d == p_enabled) {
return;
}
if (p_enabled) {
set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
} else {
set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
}
mode_2d = p_enabled;
emit_changed();
}
String VisualShaderNodeParticleBoxEmitter::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "extents";
}
return String();
}
String VisualShaderNodeParticleBoxEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
String code;
code += "vec2 __get_random_point_in_box2d(inout uint seed, vec2 extents) {\n";
code += " vec2 half_extents = extents / 2.0;\n";
code += " return vec2(__randf_range(seed, -half_extents.x, half_extents.x), __randf_range(seed, -half_extents.y, half_extents.y));\n";
code += "}\n\n";
code += "vec3 __get_random_point_in_box3d(inout uint seed, vec3 extents) {\n";
code += " vec3 half_extents = extents / 2.0;\n";
code += " return vec3(__randf_range(seed, -half_extents.x, half_extents.x), __randf_range(seed, -half_extents.y, half_extents.y), __randf_range(seed, -half_extents.z, half_extents.z));\n";
code += "}\n\n";
return code;
}
String VisualShaderNodeParticleBoxEmitter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
if (mode_2d) {
code += " " + p_output_vars[0] + " = __get_random_point_in_box2d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ");\n";
} else {
code += " " + p_output_vars[0] + " = __get_random_point_in_box3d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ");\n";
}
return code;
}
VisualShaderNodeParticleBoxEmitter::VisualShaderNodeParticleBoxEmitter() {
set_input_port_default_value(0, Vector3(1.0, 1.0, 1.0));
}
// VisualShaderNodeParticleRingEmitter
String VisualShaderNodeParticleRingEmitter::get_caption() const {
return "RingEmitter";
}
int VisualShaderNodeParticleRingEmitter::get_input_port_count() const {
return 3;
}
VisualShaderNodeParticleRingEmitter::PortType VisualShaderNodeParticleRingEmitter::get_input_port_type(int p_port) const {
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleRingEmitter::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "radius";
} else if (p_port == 1) {
return "inner_radius";
} else if (p_port == 2) {
return "height";
}
return String();
}
String VisualShaderNodeParticleRingEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
String code;
code += "vec2 __get_random_point_on_ring2d(inout uint seed, float radius, float inner_radius) {\n";
code += " float angle = __rand_from_seed(seed) * TAU;\n";
code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
code += " return vec2(ring.x, ring.y);\n";
code += "}\n\n";
code += "vec3 __get_random_point_on_ring3d(inout uint seed, float radius, float inner_radius, float height) {\n";
code += " float angle = __rand_from_seed(seed) * TAU;\n";
code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
code += " return vec3(ring.x, __randf_range(seed, min(0.0, height), max(0.0, height)), ring.y);\n";
code += "}\n\n";
return code;
}
String VisualShaderNodeParticleRingEmitter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
if (mode_2d) {
code = " " + p_output_vars[0] + " = __get_random_point_on_ring2d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
} else {
code = " " + p_output_vars[0] + " = __get_random_point_on_ring3d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ", " + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ");\n";
}
return code;
}
VisualShaderNodeParticleRingEmitter::VisualShaderNodeParticleRingEmitter() {
set_input_port_default_value(0, 10.0);
set_input_port_default_value(1, 0.0);
set_input_port_default_value(2, 0.0);
}
// VisualShaderNodeParticleMeshEmitter
String VisualShaderNodeParticleMeshEmitter::get_caption() const {
return "MeshEmitter";
}
int VisualShaderNodeParticleMeshEmitter::get_output_port_count() const {
return 6;
}
VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_output_port_type(int p_port) const {
switch (p_port) {
case 0: // position
if (mode_2d) {
return PORT_TYPE_VECTOR_2D;
}
return PORT_TYPE_VECTOR_3D;
case 1: // normal
if (mode_2d) {
return PORT_TYPE_VECTOR_2D;
}
return PORT_TYPE_VECTOR_3D;
case 2: // color
return PORT_TYPE_VECTOR_3D;
case 3: // alpha
return PORT_TYPE_SCALAR;
case 4: // uv
return PORT_TYPE_VECTOR_2D;
case 5: // uv2
return PORT_TYPE_VECTOR_2D;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleMeshEmitter::get_output_port_name(int p_port) const {
switch (p_port) {
case 0:
return "position";
case 1:
return "normal";
case 2:
return "color";
case 3:
return "alpha";
case 4:
return "uv";
case 5:
return "uv2";
}
return String();
}
int VisualShaderNodeParticleMeshEmitter::get_input_port_count() const {
return 0;
}
VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_input_port_type(int p_port) const {
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleMeshEmitter::get_input_port_name(int p_port) const {
return String();
}
String VisualShaderNodeParticleMeshEmitter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
String code;
if (is_output_port_connected(0)) { // position
code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_vx") + ";\n";
}
if (is_output_port_connected(1)) { // normal
code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_nm") + ";\n";
}
if (is_output_port_connected(2) || is_output_port_connected(3)) { // color & alpha
code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_col") + ";\n";
}
if (is_output_port_connected(4)) { // uv
code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv") + ";\n";
}
if (is_output_port_connected(5)) { // uv2
code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv2") + ";\n";
}
return code;
}
String VisualShaderNodeParticleMeshEmitter::_generate_code(VisualShader::Type p_type, int p_id, const String *p_output_vars, int p_index, const String &p_texture_name, PortType p_port_type) const {
String code;
if (is_output_port_connected(p_index)) {
switch (p_port_type) {
case PORT_TYPE_VECTOR_2D: {
code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xy;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
} break;
case PORT_TYPE_VECTOR_3D: {
if (mode_2d) {
code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xy;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
} else {
code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xyz;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
}
} break;
default:
break;
}
}
return code;
}
String VisualShaderNodeParticleMeshEmitter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
code += " {\n";
code += " int __scalar_ibuff = int(__rand_from_seed(__seed) * 65535.0) % " + itos(position_texture->get_width()) + ";\n";
code += _generate_code(p_type, p_id, p_output_vars, 0, "mesh_vx", VisualShaderNode::PORT_TYPE_VECTOR_3D);
code += _generate_code(p_type, p_id, p_output_vars, 1, "mesh_nm", VisualShaderNode::PORT_TYPE_VECTOR_3D);
if (is_output_port_connected(2) || is_output_port_connected(3)) {
code += vformat(" vec4 __vec4_buff = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0);\n", make_unique_id(p_type, p_id, "mesh_col"));
if (is_output_port_connected(2)) {
code += " " + p_output_vars[2] + " = __vec4_buff.rgb;\n";
}
if (is_output_port_connected(3)) {
code += " " + p_output_vars[3] + " = __vec4_buff.a;\n";
}
}
code += _generate_code(p_type, p_id, p_output_vars, 4, "mesh_uv", VisualShaderNode::PORT_TYPE_VECTOR_2D);
code += _generate_code(p_type, p_id, p_output_vars, 5, "mesh_uv2", VisualShaderNode::PORT_TYPE_VECTOR_2D);
code += " }\n";
return code;
}
Vector<VisualShader::DefaultTextureParam> VisualShaderNodeParticleMeshEmitter::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
Vector<VisualShader::DefaultTextureParam> ret;
if (is_output_port_connected(0)) {
VisualShader::DefaultTextureParam dtp;
dtp.name = make_unique_id(p_type, p_id, "mesh_vx");
dtp.params.push_back(position_texture);
ret.push_back(dtp);
}
if (is_output_port_connected(1)) {
VisualShader::DefaultTextureParam dtp;
dtp.name = make_unique_id(p_type, p_id, "mesh_nm");
dtp.params.push_back(normal_texture);
ret.push_back(dtp);
}
if (is_output_port_connected(2) || is_output_port_connected(3)) {
VisualShader::DefaultTextureParam dtp;
dtp.name = make_unique_id(p_type, p_id, "mesh_col");
dtp.params.push_back(color_texture);
ret.push_back(dtp);
}
if (is_output_port_connected(4)) {
VisualShader::DefaultTextureParam dtp;
dtp.name = make_unique_id(p_type, p_id, "mesh_uv");
dtp.params.push_back(uv_texture);
ret.push_back(dtp);
}
if (is_output_port_connected(5)) {
VisualShader::DefaultTextureParam dtp;
dtp.name = make_unique_id(p_type, p_id, "mesh_uv2");
dtp.params.push_back(uv2_texture);
ret.push_back(dtp);
}
return ret;
}
void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector2> &p_array, Ref<ImageTexture> &r_texture) {
Ref<Image> image;
image.instantiate();
if (p_array.size() == 0) {
image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
} else {
image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
}
for (int i = 0; i < p_array.size(); i++) {
Vector2 v = p_array[i];
image->set_pixel(i, 0, Color(v.x, v.y, 0));
}
if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
r_texture->set_image(image);
} else {
r_texture->update(image);
}
}
void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector3> &p_array, Ref<ImageTexture> &r_texture) {
Ref<Image> image;
image.instantiate();
if (p_array.size() == 0) {
image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
} else {
image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
}
for (int i = 0; i < p_array.size(); i++) {
Vector3 v = p_array[i];
image->set_pixel(i, 0, Color(v.x, v.y, v.z));
}
if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
r_texture->set_image(image);
} else {
r_texture->update(image);
}
}
void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Color> &p_array, Ref<ImageTexture> &r_texture) {
Ref<Image> image;
image.instantiate();
if (p_array.size() == 0) {
image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBA8);
} else {
image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBA8);
}
for (int i = 0; i < p_array.size(); i++) {
image->set_pixel(i, 0, p_array[i]);
}
if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
r_texture->set_image(image);
} else {
r_texture->update(image);
}
}
void VisualShaderNodeParticleMeshEmitter::_update_textures() {
if (!mesh.is_valid()) {
return;
}
Vector<Vector3> vertices;
Vector<Vector3> normals;
Vector<Color> colors;
Vector<Vector2> uvs;
Vector<Vector2> uvs2;
const int surface_count = mesh->get_surface_count();
if (use_all_surfaces) {
for (int i = 0; i < surface_count; i++) {
const Array surface_arrays = mesh->surface_get_arrays(i);
const int surface_arrays_size = surface_arrays.size();
// position
if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
for (int j = 0; j < vertex_array.size(); j++) {
vertices.push_back((Vector3)vertex_array[j]);
}
}
// normal
if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
for (int j = 0; j < normal_array.size(); j++) {
normals.push_back((Vector3)normal_array[j]);
}
}
// color
if (surface_arrays_size > Mesh::ARRAY_COLOR) {
Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
for (int j = 0; j < color_array.size(); j++) {
colors.push_back((Color)color_array[j]);
}
}
// uv
if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
for (int j = 0; j < uv_array.size(); j++) {
uvs.push_back((Vector2)uv_array[j]);
}
}
// uv2
if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
for (int j = 0; j < uv2_array.size(); j++) {
uvs2.push_back((Vector2)uv2_array[j]);
}
}
}
} else {
if (surface_index >= 0 && surface_index < surface_count) {
const Array surface_arrays = mesh->surface_get_arrays(surface_index);
const int surface_arrays_size = surface_arrays.size();
// position
if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
for (int i = 0; i < vertex_array.size(); i++) {
vertices.push_back((Vector3)vertex_array[i]);
}
}
// normal
if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
for (int i = 0; i < normal_array.size(); i++) {
normals.push_back((Vector3)normal_array[i]);
}
}
// color
if (surface_arrays_size > Mesh::ARRAY_COLOR) {
Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
for (int i = 0; i < color_array.size(); i++) {
colors.push_back((Color)color_array[i]);
}
}
// uv
if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
for (int j = 0; j < uv_array.size(); j++) {
uvs.push_back((Vector2)uv_array[j]);
}
}
// uv2
if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
for (int j = 0; j < uv2_array.size(); j++) {
uvs2.push_back((Vector2)uv2_array[j]);
}
}
}
}
_update_texture(vertices, position_texture);
_update_texture(normals, normal_texture);
_update_texture(colors, color_texture);
_update_texture(uvs, uv_texture);
_update_texture(uvs2, uv2_texture);
}
void VisualShaderNodeParticleMeshEmitter::set_mesh(Ref<Mesh> p_mesh) {
if (mesh == p_mesh) {
return;
}
if (mesh.is_valid()) {
Callable callable = callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures);
if (mesh->is_connected(CoreStringNames::get_singleton()->changed, callable)) {
mesh->disconnect(CoreStringNames::get_singleton()->changed, callable);
}
}
mesh = p_mesh;
if (mesh.is_valid()) {
Callable callable = callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures);
if (!mesh->is_connected(CoreStringNames::get_singleton()->changed, callable)) {
mesh->connect(CoreStringNames::get_singleton()->changed, callable);
}
}
emit_changed();
}
Ref<Mesh> VisualShaderNodeParticleMeshEmitter::get_mesh() const {
return mesh;
}
void VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces(bool p_enabled) {
if (use_all_surfaces == p_enabled) {
return;
}
use_all_surfaces = p_enabled;
emit_changed();
}
bool VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces() const {
return use_all_surfaces;
}
void VisualShaderNodeParticleMeshEmitter::set_surface_index(int p_surface_index) {
if (mesh.is_valid()) {
if (mesh->get_surface_count() > 0) {
p_surface_index = CLAMP(p_surface_index, 0, mesh->get_surface_count() - 1);
} else {
p_surface_index = 0;
}
} else if (p_surface_index < 0) {
p_surface_index = 0;
}
if (surface_index == p_surface_index) {
return;
}
surface_index = p_surface_index;
emit_changed();
}
int VisualShaderNodeParticleMeshEmitter::get_surface_index() const {
return surface_index;
}
Vector<StringName> VisualShaderNodeParticleMeshEmitter::get_editable_properties() const {
Vector<StringName> props = VisualShaderNodeParticleEmitter::get_editable_properties();
props.push_back("mesh");
props.push_back("use_all_surfaces");
if (!use_all_surfaces) {
props.push_back("surface_index");
}
return props;
}
HashMap<StringName, String> VisualShaderNodeParticleMeshEmitter::get_editable_properties_names() const {
HashMap<StringName, String> names = VisualShaderNodeParticleEmitter::get_editable_properties_names();
names.insert("mesh", RTR("Mesh"));
names.insert("use_all_surfaces", RTR("Use All Surfaces"));
if (!use_all_surfaces) {
names.insert("surface_index", RTR("Surface Index"));
}
return names;
}
void VisualShaderNodeParticleMeshEmitter::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &VisualShaderNodeParticleMeshEmitter::set_mesh);
ClassDB::bind_method(D_METHOD("get_mesh"), &VisualShaderNodeParticleMeshEmitter::get_mesh);
ClassDB::bind_method(D_METHOD("set_use_all_surfaces", "enabled"), &VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces);
ClassDB::bind_method(D_METHOD("is_use_all_surfaces"), &VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces);
ClassDB::bind_method(D_METHOD("set_surface_index", "surface_index"), &VisualShaderNodeParticleMeshEmitter::set_surface_index);
ClassDB::bind_method(D_METHOD("get_surface_index"), &VisualShaderNodeParticleMeshEmitter::get_surface_index);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_all_surfaces"), "set_use_all_surfaces", "is_use_all_surfaces");
ADD_PROPERTY(PropertyInfo(Variant::INT, "surface_index"), "set_surface_index", "get_surface_index");
}
VisualShaderNodeParticleMeshEmitter::VisualShaderNodeParticleMeshEmitter() {
connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
position_texture.instantiate();
normal_texture.instantiate();
color_texture.instantiate();
uv_texture.instantiate();
uv2_texture.instantiate();
simple_decl = false;
}
// VisualShaderNodeParticleMultiplyByAxisAngle
void VisualShaderNodeParticleMultiplyByAxisAngle::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_degrees_mode", "enabled"), &VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode);
ClassDB::bind_method(D_METHOD("is_degrees_mode"), &VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "degrees_mode"), "set_degrees_mode", "is_degrees_mode");
}
String VisualShaderNodeParticleMultiplyByAxisAngle::get_caption() const {
return "MultiplyByAxisAngle";
}
int VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_count() const {
return 3;
}
VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_type(int p_port) const {
if (p_port == 0 || p_port == 1) { // position, rotation_axis
return PORT_TYPE_VECTOR_3D;
}
return PORT_TYPE_SCALAR; // angle (degrees/radians)
}
String VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "position";
}
if (p_port == 1) {
return "axis";
}
if (p_port == 2) {
if (degrees_mode) {
return "angle (degrees)";
} else {
return "angle (radians)";
}
}
return String();
}
bool VisualShaderNodeParticleMultiplyByAxisAngle::is_show_prop_names() const {
return true;
}
int VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_count() const {
return 1;
}
VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_type(int p_port) const {
return PORT_TYPE_VECTOR_3D;
}
String VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_name(int p_port) const {
return "position";
}
String VisualShaderNodeParticleMultiplyByAxisAngle::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
if (degrees_mode) {
code += " " + p_output_vars[0] + " = __build_rotation_mat3(" + (p_input_vars[1].is_empty() ? ("vec3" + (String)get_input_port_default_value(1)) : p_input_vars[1]) + ", radians(" + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ")) * " + (p_input_vars[0].is_empty() ? "vec3(0.0)" : p_input_vars[0]) + ";\n";
} else {
code += " " + p_output_vars[0] + " = __build_rotation_mat3(" + (p_input_vars[1].is_empty() ? ("vec3" + (String)get_input_port_default_value(1)) : p_input_vars[1]) + ", " + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ") * " + (p_input_vars[0].is_empty() ? "vec3(0.0)" : p_input_vars[0]) + ";\n";
}
return code;
}
void VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode(bool p_enabled) {
degrees_mode = p_enabled;
emit_changed();
}
bool VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode() const {
return degrees_mode;
}
Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_properties() const {
Vector<StringName> props;
props.push_back("degrees_mode");
return props;
}
bool VisualShaderNodeParticleMultiplyByAxisAngle::has_output_port_preview(int p_port) const {
return false;
}
VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() {
set_input_port_default_value(1, Vector3(1, 0, 0));
set_input_port_default_value(2, 0.0);
}
// VisualShaderNodeParticleConeVelocity
String VisualShaderNodeParticleConeVelocity::get_caption() const {
return "ConeVelocity";
}
int VisualShaderNodeParticleConeVelocity::get_input_port_count() const {
return 2;
}
VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_input_port_type(int p_port) const {
if (p_port == 0) {
return PORT_TYPE_VECTOR_3D;
} else if (p_port == 1) {
return PORT_TYPE_SCALAR;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleConeVelocity::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "direction";
} else if (p_port == 1) {
return "spread(degrees)";
}
return String();
}
int VisualShaderNodeParticleConeVelocity::get_output_port_count() const {
return 1;
}
VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_output_port_type(int p_port) const {
return PORT_TYPE_VECTOR_3D;
}
String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) const {
if (p_port == 0) {
return "velocity";
}
return String();
}
bool VisualShaderNodeParticleConeVelocity::has_output_port_preview(int p_port) const {
return false;
}
String VisualShaderNodeParticleConeVelocity::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
code += " {\n";
code += " float __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
code += " float __scalar_buff1 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
code += " float __scalar_buff2 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
code += " vec3 __vec3_buff1 = " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + ";\n";
code += " __scalar_buff1 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.x, __vec3_buff1.z) : sign(__vec3_buff1.x) * (PI / 2.0);\n";
code += " __scalar_buff2 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.y, abs(__vec3_buff1.z)) : (__vec3_buff1.x != 0.0 ? atan(__vec3_buff1.y, abs(__vec3_buff1.x)) : sign(__vec3_buff1.y) * (PI / 2.0));\n";
code += " __vec3_buff1 = vec3(sin(__scalar_buff1), 0.0, cos(__scalar_buff1));\n";
code += " vec3 __vec3_buff2 = vec3(0.0, sin(__scalar_buff2), cos(__scalar_buff2));\n";
code += " __vec3_buff2.z = __vec3_buff2.z / max(0.0001, sqrt(abs(__vec3_buff2.z)));\n";
code += " " + p_output_vars[0] + " = normalize(vec3(__vec3_buff1.x * __vec3_buff2.z, __vec3_buff2.y, __vec3_buff1.z * __vec3_buff2.z));\n";
code += " }\n";
return code;
}
VisualShaderNodeParticleConeVelocity::VisualShaderNodeParticleConeVelocity() {
set_input_port_default_value(0, Vector3(1, 0, 0));
set_input_port_default_value(1, 45.0);
simple_decl = false;
}
// VisualShaderNodeParticleRandomness
void VisualShaderNodeParticleRandomness::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeParticleRandomness::set_op_type);
ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeParticleRandomness::get_op_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3"), "set_op_type", "get_op_type");
BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
BIND_ENUM_CONSTANT(OP_TYPE_MAX);
}
Vector<StringName> VisualShaderNodeParticleRandomness::get_editable_properties() const {
Vector<StringName> props;
props.push_back("op_type");
return props;
}
String VisualShaderNodeParticleRandomness::get_caption() const {
return "ParticleRandomness";
}
int VisualShaderNodeParticleRandomness::get_output_port_count() const {
return 1;
}
VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_output_port_type(int p_port) const {
switch (op_type) {
case OP_TYPE_VECTOR_2D:
return PORT_TYPE_VECTOR_2D;
case OP_TYPE_VECTOR_3D:
return PORT_TYPE_VECTOR_3D;
default:
break;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleRandomness::get_output_port_name(int p_port) const {
return "random";
}
int VisualShaderNodeParticleRandomness::get_input_port_count() const {
return 2;
}
VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_input_port_type(int p_port) const {
switch (op_type) {
case OP_TYPE_VECTOR_2D:
return PORT_TYPE_VECTOR_2D;
case OP_TYPE_VECTOR_3D:
return PORT_TYPE_VECTOR_3D;
default:
break;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleRandomness::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "min";
} else if (p_port == 1) {
return "max";
}
return String();
}
String VisualShaderNodeParticleRandomness::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
switch (op_type) {
case OP_TYPE_SCALAR: {
code += vformat(" %s = __randf_range(__seed, %s, %s);\n", p_output_vars[0], p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0], p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]);
} break;
case OP_TYPE_VECTOR_2D: {
code += vformat(" %s = __randv2_range(__seed, %s, %s);\n", p_output_vars[0], p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0], p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]);
} break;
case OP_TYPE_VECTOR_3D: {
code += vformat(" %s = __randv3_range(__seed, %s, %s);\n", p_output_vars[0], p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0], p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]);
} break;
default:
break;
}
return code;
}
void VisualShaderNodeParticleRandomness::set_op_type(OpType p_op_type) {
ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
if (op_type == p_op_type) {
return;
}
switch (p_op_type) {
case OP_TYPE_SCALAR: {
set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
} break;
case OP_TYPE_VECTOR_2D: {
set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
} break;
case OP_TYPE_VECTOR_3D: {
set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
} break;
default:
break;
}
op_type = p_op_type;
emit_changed();
}
VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::get_op_type() const {
return op_type;
}
bool VisualShaderNodeParticleRandomness::has_output_port_preview(int p_port) const {
return false;
}
VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() {
set_input_port_default_value(0, -1.0);
set_input_port_default_value(1, 1.0);
}
// VisualShaderNodeParticleAccelerator
void VisualShaderNodeParticleAccelerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShaderNodeParticleAccelerator::set_mode);
ClassDB::bind_method(D_METHOD("get_mode"), &VisualShaderNodeParticleAccelerator::get_mode);
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Linear,Radial,Tangential"), "set_mode", "get_mode");
BIND_ENUM_CONSTANT(MODE_LINEAR);
BIND_ENUM_CONSTANT(MODE_RADIAL)
BIND_ENUM_CONSTANT(MODE_TANGENTIAL);
BIND_ENUM_CONSTANT(MODE_MAX);
}
Vector<StringName> VisualShaderNodeParticleAccelerator::get_editable_properties() const {
Vector<StringName> props;
props.push_back("mode");
return props;
}
String VisualShaderNodeParticleAccelerator::get_caption() const {
return "ParticleAccelerator";
}
int VisualShaderNodeParticleAccelerator::get_output_port_count() const {
return 1;
}
VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_output_port_type(int p_port) const {
return PORT_TYPE_VECTOR_3D;
}
String VisualShaderNodeParticleAccelerator::get_output_port_name(int p_port) const {
return String();
}
int VisualShaderNodeParticleAccelerator::get_input_port_count() const {
return 3;
}
VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_input_port_type(int p_port) const {
if (p_port == 0) {
return PORT_TYPE_VECTOR_3D;
} else if (p_port == 1) {
return PORT_TYPE_SCALAR;
} else if (p_port == 2) {
return PORT_TYPE_VECTOR_3D;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleAccelerator::get_input_port_name(int p_port) const {
if (p_port == 0) {
return "amount";
} else if (p_port == 1) {
return "randomness";
} else if (p_port == 2) {
return "axis";
}
return String();
}
String VisualShaderNodeParticleAccelerator::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
code += " {\n";
switch (mode) {
case MODE_LINEAR:
code += " " + p_output_vars[0] + " = length(VELOCITY) > 0.0 ? " + "normalize(VELOCITY) * " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ") : vec3(0.0);\n";
break;
case MODE_RADIAL:
code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
code += " vec3 __ndiff = normalize(__diff);\n\n";
code += " " + p_output_vars[0] + " = length(__diff) > 0.0 ? __ndiff * " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ") : vec3(0.0);\n";
break;
case MODE_TANGENTIAL:
code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
code += " vec3 __ndiff = normalize(__diff);\n\n";
code += " vec3 __vec3_buff1 = cross(__ndiff, normalize(" + (p_input_vars[2].is_empty() ? "vec3" + (String)get_input_port_default_value(2) : p_input_vars[2]) + "));\n";
code += " " + p_output_vars[0] + " = length(__vec3_buff1) > 0.0 ? normalize(__vec3_buff1) * (" + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ")) : vec3(0.0);\n";
break;
default:
break;
}
code += " }\n";
return code;
}
void VisualShaderNodeParticleAccelerator::set_mode(Mode p_mode) {
ERR_FAIL_INDEX(int(p_mode), int(MODE_MAX));
if (mode == p_mode) {
return;
}
mode = p_mode;
emit_changed();
}
VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::get_mode() const {
return mode;
}
bool VisualShaderNodeParticleAccelerator::has_output_port_preview(int p_port) const {
return false;
}
VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() {
set_input_port_default_value(0, Vector3(1, 1, 1));
set_input_port_default_value(1, 0.0);
set_input_port_default_value(2, Vector3(0, -9.8, 0));
simple_decl = false;
}
// VisualShaderNodeParticleOutput
String VisualShaderNodeParticleOutput::get_caption() const {
switch (shader_type) {
case VisualShader::TYPE_START:
return "StartOutput";
case VisualShader::TYPE_PROCESS:
return "ProcessOutput";
case VisualShader::TYPE_COLLIDE:
return "CollideOutput";
case VisualShader::TYPE_START_CUSTOM:
return "CustomStartOutput";
case VisualShader::TYPE_PROCESS_CUSTOM:
return "CustomProcessOutput";
default:
ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
return "";
}
}
int VisualShaderNodeParticleOutput::get_input_port_count() const {
switch (shader_type) {
case VisualShader::TYPE_START:
return 8;
case VisualShader::TYPE_PROCESS:
return 7;
case VisualShader::TYPE_COLLIDE:
return 5;
case VisualShader::TYPE_START_CUSTOM:
case VisualShader::TYPE_PROCESS_CUSTOM:
return 6;
default:
ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
return 0;
}
}
VisualShaderNodeParticleOutput::PortType VisualShaderNodeParticleOutput::get_input_port_type(int p_port) const {
switch (p_port) {
case 0:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
return PORT_TYPE_VECTOR_3D; // custom.rgb
}
return PORT_TYPE_BOOLEAN; // active
case 1:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
break; // custom.a (scalar)
}
return PORT_TYPE_VECTOR_3D; // velocity
case 2:
return PORT_TYPE_VECTOR_3D; // color & velocity
case 3:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
return PORT_TYPE_VECTOR_3D; // color
}
break; // alpha (scalar)
case 4:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
break; // alpha
}
if (shader_type == VisualShader::TYPE_PROCESS) {
break; // scale
}
if (shader_type == VisualShader::TYPE_COLLIDE) {
return PORT_TYPE_TRANSFORM; // transform
}
return PORT_TYPE_VECTOR_3D; // position
case 5:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
return PORT_TYPE_TRANSFORM; // transform
}
if (shader_type == VisualShader::TYPE_PROCESS) {
return PORT_TYPE_VECTOR_3D; // rotation_axis
}
break; // scale (scalar)
case 6:
if (shader_type == VisualShader::TYPE_START) {
return PORT_TYPE_VECTOR_3D; // rotation_axis
}
break;
case 7:
break; // angle (scalar)
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleOutput::get_input_port_name(int p_port) const {
String port_name;
switch (p_port) {
case 0:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "custom";
break;
}
port_name = "active";
break;
case 1:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "custom_alpha";
break;
}
port_name = "velocity";
break;
case 2:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "velocity";
break;
}
port_name = "color";
break;
case 3:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "color";
break;
}
port_name = "alpha";
break;
case 4:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "alpha";
break;
}
if (shader_type == VisualShader::TYPE_PROCESS) {
port_name = "scale";
break;
}
if (shader_type == VisualShader::TYPE_COLLIDE) {
port_name = "transform";
break;
}
port_name = "position";
break;
case 5:
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
port_name = "transform";
break;
}
if (shader_type == VisualShader::TYPE_PROCESS) {
port_name = "rotation_axis";
break;
}
port_name = "scale";
break;
case 6:
if (shader_type == VisualShader::TYPE_PROCESS) {
port_name = "angle_in_radians";
break;
}
port_name = "rotation_axis";
break;
case 7:
port_name = "angle_in_radians";
break;
default:
break;
}
if (!port_name.is_empty()) {
return port_name.capitalize();
}
return String();
}
bool VisualShaderNodeParticleOutput::is_port_separator(int p_index) const {
if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
String port_name = get_input_port_name(p_index);
return bool(port_name == "Scale");
}
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
String port_name = get_input_port_name(p_index);
return bool(port_name == "Velocity");
}
return false;
}
String VisualShaderNodeParticleOutput::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
String tab = " ";
if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
if (!p_input_vars[0].is_empty()) { // custom.rgb
code += tab + "CUSTOM.rgb = " + p_input_vars[0] + ";\n";
}
if (!p_input_vars[1].is_empty()) { // custom.a
code += tab + "CUSTOM.a = " + p_input_vars[1] + ";\n";
}
if (!p_input_vars[2].is_empty()) { // velocity
code += tab + "VELOCITY = " + p_input_vars[2] + ";\n";
}
if (!p_input_vars[3].is_empty()) { // color.rgb
code += tab + "COLOR.rgb = " + p_input_vars[3] + ";\n";
}
if (!p_input_vars[4].is_empty()) { // color.a
code += tab + "COLOR.a = " + p_input_vars[4] + ";\n";
}
if (!p_input_vars[5].is_empty()) { // transform
code += tab + "TRANSFORM = " + p_input_vars[5] + ";\n";
}
} else {
if (!p_input_vars[0].is_empty()) { // Active (begin).
code += tab + "ACTIVE = " + p_input_vars[0] + ";\n";
code += tab + "if(ACTIVE) {\n";
tab += " ";
}
if (!p_input_vars[1].is_empty()) { // velocity
code += tab + "VELOCITY = " + p_input_vars[1] + ";\n";
}
if (!p_input_vars[2].is_empty()) { // color
code += tab + "COLOR.rgb = " + p_input_vars[2] + ";\n";
}
if (!p_input_vars[3].is_empty()) { // alpha
code += tab + "COLOR.a = " + p_input_vars[3] + ";\n";
}
// position
if (shader_type == VisualShader::TYPE_START) {
code += tab + "if (RESTART_POSITION) {\n";
if (!p_input_vars[4].is_empty()) {
code += tab + " TRANSFORM = mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(" + p_input_vars[4] + ", 1.0));\n";
} else {
code += tab + " TRANSFORM = mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
}
code += tab + " if (RESTART_VELOCITY) {\n";
code += tab + " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
code += tab + " }\n";
code += tab + " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
code += tab + "}\n";
} else if (shader_type == VisualShader::TYPE_COLLIDE) { // position
if (!p_input_vars[4].is_empty()) {
code += tab + "TRANSFORM = " + p_input_vars[4] + ";\n";
}
}
if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
int scale = 5;
int rotation_axis = 6;
int rotation = 7;
if (shader_type == VisualShader::TYPE_PROCESS) {
scale = 4;
rotation_axis = 5;
rotation = 6;
}
String op;
if (shader_type == VisualShader::TYPE_START) {
op = "*=";
} else {
op = "=";
}
if (!p_input_vars[rotation].is_empty()) { // rotation_axis & angle_in_radians
String axis;
if (p_input_vars[rotation_axis].is_empty()) {
axis = "vec3(0, 1, 0)";
} else {
axis = p_input_vars[rotation_axis];
}
code += tab + "TRANSFORM " + op + " __build_rotation_mat4(" + axis + ", " + p_input_vars[rotation] + ");\n";
}
if (!p_input_vars[scale].is_empty()) { // scale
code += tab + "TRANSFORM " + op + " mat4(vec4(" + p_input_vars[scale] + ", 0, 0, 0), vec4(0, " + p_input_vars[scale] + ", 0, 0), vec4(0, 0, " + p_input_vars[scale] + ", 0), vec4(0, 0, 0, 1));\n";
}
}
if (!p_input_vars[0].is_empty()) { // Active (end).
code += " }\n";
}
}
return code;
}
VisualShaderNodeParticleOutput::VisualShaderNodeParticleOutput() {
}
// EmitParticle
Vector<StringName> VisualShaderNodeParticleEmit::get_editable_properties() const {
Vector<StringName> props;
props.push_back("flags");
return props;
}
void VisualShaderNodeParticleEmit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_flags", "flags"), &VisualShaderNodeParticleEmit::set_flags);
ClassDB::bind_method(D_METHOD("get_flags"), &VisualShaderNodeParticleEmit::get_flags);
ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Position,RotScale,Velocity,Color,Custom"), "set_flags", "get_flags");
BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
BIND_ENUM_CONSTANT(EMIT_FLAG_ROT_SCALE);
BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
}
String VisualShaderNodeParticleEmit::get_caption() const {
return "EmitParticle";
}
int VisualShaderNodeParticleEmit::get_input_port_count() const {
return 7;
}
VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_input_port_type(int p_port) const {
switch (p_port) {
case 0:
return PORT_TYPE_BOOLEAN;
case 1:
return PORT_TYPE_TRANSFORM;
case 2:
return PORT_TYPE_VECTOR_3D;
case 3:
return PORT_TYPE_VECTOR_3D;
case 4:
return PORT_TYPE_SCALAR;
case 5:
return PORT_TYPE_VECTOR_3D;
case 6:
return PORT_TYPE_SCALAR;
}
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleEmit::get_input_port_name(int p_port) const {
switch (p_port) {
case 0:
return "condition";
case 1:
return "transform";
case 2:
return "velocity";
case 3:
return "color";
case 4:
return "alpha";
case 5:
return "custom";
case 6:
return "custom_alpha";
}
return String();
}
int VisualShaderNodeParticleEmit::get_output_port_count() const {
return 0;
}
VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_output_port_type(int p_port) const {
return PORT_TYPE_SCALAR;
}
String VisualShaderNodeParticleEmit::get_output_port_name(int p_port) const {
return String();
}
void VisualShaderNodeParticleEmit::add_flag(EmitFlags p_flag) {
flags |= p_flag;
emit_changed();
}
bool VisualShaderNodeParticleEmit::has_flag(EmitFlags p_flag) const {
return flags & p_flag;
}
void VisualShaderNodeParticleEmit::set_flags(EmitFlags p_flags) {
flags = (int)p_flags;
emit_changed();
}
VisualShaderNodeParticleEmit::EmitFlags VisualShaderNodeParticleEmit::get_flags() const {
return EmitFlags(flags);
}
bool VisualShaderNodeParticleEmit::is_show_prop_names() const {
return true;
}
bool VisualShaderNodeParticleEmit::is_generate_input_var(int p_port) const {
if (p_port == 0) {
if (!is_input_port_connected(0)) {
return false;
}
}
return true;
}
bool VisualShaderNodeParticleEmit::is_input_port_default(int p_port, Shader::Mode p_mode) const {
switch (p_port) {
case 1:
return true;
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 5:
return true;
case 6:
return true;
}
return false;
}
String VisualShaderNodeParticleEmit::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
String code;
String tab;
bool default_condition = false;
if (!is_input_port_connected(0)) {
default_condition = true;
if (get_input_port_default_value(0)) {
tab = " ";
} else {
return code;
}
} else {
tab = " ";
}
String transform;
if (p_input_vars[1].is_empty()) {
transform = "TRANSFORM";
} else {
transform = p_input_vars[1];
}
String velocity;
if (p_input_vars[2].is_empty()) {
velocity = "VELOCITY";
} else {
velocity = p_input_vars[2];
}
String color;
if (p_input_vars[3].is_empty()) {
color = "COLOR.rgb";
} else {
color = p_input_vars[3];
}
String alpha;
if (p_input_vars[4].is_empty()) {
alpha = "COLOR.a";
} else {
alpha = p_input_vars[4];
}
String custom;
if (p_input_vars[5].is_empty()) {
custom = "CUSTOM.rgb";
} else {
custom = p_input_vars[5];
}
String custom_alpha;
if (p_input_vars[6].is_empty()) {
custom_alpha = "CUSTOM.a";
} else {
custom_alpha = p_input_vars[6];
}
List<String> flags_arr;
if (has_flag(EmitFlags::EMIT_FLAG_POSITION)) {
flags_arr.push_back("FLAG_EMIT_POSITION");
}
if (has_flag(EmitFlags::EMIT_FLAG_ROT_SCALE)) {
flags_arr.push_back("FLAG_EMIT_ROT_SCALE");
}
if (has_flag(EmitFlags::EMIT_FLAG_VELOCITY)) {
flags_arr.push_back("FLAG_EMIT_VELOCITY");
}
if (has_flag(EmitFlags::EMIT_FLAG_COLOR)) {
flags_arr.push_back("FLAG_EMIT_COLOR");
}
if (has_flag(EmitFlags::EMIT_FLAG_CUSTOM)) {
flags_arr.push_back("FLAG_EMIT_CUSTOM");
}
String flags_str;
for (int i = 0; i < flags_arr.size(); i++) {
if (i > 0) {
flags_str += "|";
}
flags_str += flags_arr[i];
}
if (flags_str.is_empty()) {
flags_str = "uint(0)";
}
if (!default_condition) {
code += " if (" + p_input_vars[0] + ") {\n";
}
code += tab + "emit_subparticle(" + transform + ", " + velocity + ", vec4(" + color + ", " + alpha + "), vec4(" + custom + ", " + custom_alpha + "), " + flags_str + ");\n";
if (!default_condition) {
code += " }\n";
}
return code;
}
VisualShaderNodeParticleEmit::VisualShaderNodeParticleEmit() {
set_input_port_default_value(0, true);
}
|