visual_shader_particle_nodes.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /**************************************************************************/
  2. /* visual_shader_particle_nodes.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "visual_shader_particle_nodes.h"
  31. #include "scene/resources/image_texture.h"
  32. // VisualShaderNodeParticleEmitter
  33. int VisualShaderNodeParticleEmitter::get_output_port_count() const {
  34. return 1;
  35. }
  36. VisualShaderNodeParticleEmitter::PortType VisualShaderNodeParticleEmitter::get_output_port_type(int p_port) const {
  37. if (mode_2d) {
  38. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  39. }
  40. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  41. }
  42. String VisualShaderNodeParticleEmitter::get_output_port_name(int p_port) const {
  43. if (p_port == 0) {
  44. return "position";
  45. }
  46. return String();
  47. }
  48. bool VisualShaderNodeParticleEmitter::has_output_port_preview(int p_port) const {
  49. return false;
  50. }
  51. void VisualShaderNodeParticleEmitter::set_mode_2d(bool p_enabled) {
  52. if (mode_2d == p_enabled) {
  53. return;
  54. }
  55. mode_2d = p_enabled;
  56. emit_changed();
  57. }
  58. bool VisualShaderNodeParticleEmitter::is_mode_2d() const {
  59. return mode_2d;
  60. }
  61. Vector<StringName> VisualShaderNodeParticleEmitter::get_editable_properties() const {
  62. Vector<StringName> props;
  63. props.push_back("mode_2d");
  64. return props;
  65. }
  66. HashMap<StringName, String> VisualShaderNodeParticleEmitter::get_editable_properties_names() const {
  67. HashMap<StringName, String> names;
  68. names.insert("mode_2d", RTR("2D Mode"));
  69. return names;
  70. }
  71. bool VisualShaderNodeParticleEmitter::is_show_prop_names() const {
  72. return true;
  73. }
  74. void VisualShaderNodeParticleEmitter::_bind_methods() {
  75. ClassDB::bind_method(D_METHOD("set_mode_2d", "enabled"), &VisualShaderNodeParticleEmitter::set_mode_2d);
  76. ClassDB::bind_method(D_METHOD("is_mode_2d"), &VisualShaderNodeParticleEmitter::is_mode_2d);
  77. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_2d"), "set_mode_2d", "is_mode_2d");
  78. }
  79. VisualShaderNodeParticleEmitter::VisualShaderNodeParticleEmitter() {
  80. }
  81. // VisualShaderNodeParticleSphereEmitter
  82. String VisualShaderNodeParticleSphereEmitter::get_caption() const {
  83. return "SphereEmitter";
  84. }
  85. int VisualShaderNodeParticleSphereEmitter::get_input_port_count() const {
  86. return 2;
  87. }
  88. VisualShaderNodeParticleSphereEmitter::PortType VisualShaderNodeParticleSphereEmitter::get_input_port_type(int p_port) const {
  89. return PORT_TYPE_SCALAR;
  90. }
  91. String VisualShaderNodeParticleSphereEmitter::get_input_port_name(int p_port) const {
  92. if (p_port == 0) {
  93. return "radius";
  94. } else if (p_port == 1) {
  95. return "inner_radius";
  96. }
  97. return String();
  98. }
  99. String VisualShaderNodeParticleSphereEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  100. String code;
  101. code += "vec2 __get_random_point_in_circle(inout uint seed, float radius, float inner_radius) {\n";
  102. code += " return __get_random_unit_vec2(seed) * __randf_range(seed, inner_radius, radius);\n";
  103. code += "}\n\n";
  104. code += "vec3 __get_random_point_in_sphere(inout uint seed, float radius, float inner_radius) {\n";
  105. code += " return __get_random_unit_vec3(seed) * __randf_range(seed, inner_radius, radius);\n";
  106. code += "}\n\n";
  107. return code;
  108. }
  109. 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 {
  110. String code;
  111. if (mode_2d) {
  112. 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";
  113. } else {
  114. 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";
  115. }
  116. return code;
  117. }
  118. VisualShaderNodeParticleSphereEmitter::VisualShaderNodeParticleSphereEmitter() {
  119. set_input_port_default_value(0, 10.0);
  120. set_input_port_default_value(1, 0.0);
  121. }
  122. // VisualShaderNodeParticleBoxEmitter
  123. String VisualShaderNodeParticleBoxEmitter::get_caption() const {
  124. return "BoxEmitter";
  125. }
  126. int VisualShaderNodeParticleBoxEmitter::get_input_port_count() const {
  127. return 1;
  128. }
  129. VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleBoxEmitter::get_input_port_type(int p_port) const {
  130. if (p_port == 0) {
  131. if (mode_2d) {
  132. return PORT_TYPE_VECTOR_2D;
  133. }
  134. return PORT_TYPE_VECTOR_3D;
  135. }
  136. return PORT_TYPE_SCALAR;
  137. }
  138. void VisualShaderNodeParticleBoxEmitter::set_mode_2d(bool p_enabled) {
  139. if (mode_2d == p_enabled) {
  140. return;
  141. }
  142. if (p_enabled) {
  143. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  144. } else {
  145. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  146. }
  147. mode_2d = p_enabled;
  148. emit_changed();
  149. }
  150. String VisualShaderNodeParticleBoxEmitter::get_input_port_name(int p_port) const {
  151. if (p_port == 0) {
  152. return "extents";
  153. }
  154. return String();
  155. }
  156. String VisualShaderNodeParticleBoxEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  157. String code;
  158. code += "vec2 __get_random_point_in_box2d(inout uint seed, vec2 extents) {\n";
  159. code += " vec2 half_extents = extents / 2.0;\n";
  160. code += " return vec2(__randf_range(seed, -half_extents.x, half_extents.x), __randf_range(seed, -half_extents.y, half_extents.y));\n";
  161. code += "}\n\n";
  162. code += "vec3 __get_random_point_in_box3d(inout uint seed, vec3 extents) {\n";
  163. code += " vec3 half_extents = extents / 2.0;\n";
  164. 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";
  165. code += "}\n\n";
  166. return code;
  167. }
  168. 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 {
  169. String code;
  170. if (mode_2d) {
  171. 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";
  172. } else {
  173. 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";
  174. }
  175. return code;
  176. }
  177. VisualShaderNodeParticleBoxEmitter::VisualShaderNodeParticleBoxEmitter() {
  178. set_input_port_default_value(0, Vector3(1.0, 1.0, 1.0));
  179. }
  180. // VisualShaderNodeParticleRingEmitter
  181. String VisualShaderNodeParticleRingEmitter::get_caption() const {
  182. return "RingEmitter";
  183. }
  184. int VisualShaderNodeParticleRingEmitter::get_input_port_count() const {
  185. return 3;
  186. }
  187. VisualShaderNodeParticleRingEmitter::PortType VisualShaderNodeParticleRingEmitter::get_input_port_type(int p_port) const {
  188. return PORT_TYPE_SCALAR;
  189. }
  190. String VisualShaderNodeParticleRingEmitter::get_input_port_name(int p_port) const {
  191. if (p_port == 0) {
  192. return "radius";
  193. } else if (p_port == 1) {
  194. return "inner_radius";
  195. } else if (p_port == 2) {
  196. return "height";
  197. }
  198. return String();
  199. }
  200. String VisualShaderNodeParticleRingEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  201. String code;
  202. code += "vec2 __get_random_point_on_ring2d(inout uint seed, float radius, float inner_radius) {\n";
  203. code += " float angle = __rand_from_seed(seed) * TAU;\n";
  204. code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
  205. code += " return vec2(ring.x, ring.y);\n";
  206. code += "}\n\n";
  207. code += "vec3 __get_random_point_on_ring3d(inout uint seed, float radius, float inner_radius, float height) {\n";
  208. code += " float angle = __rand_from_seed(seed) * TAU;\n";
  209. code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
  210. code += " return vec3(ring.x, __randf_range(seed, min(0.0, height), max(0.0, height)), ring.y);\n";
  211. code += "}\n\n";
  212. return code;
  213. }
  214. 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 {
  215. String code;
  216. if (mode_2d) {
  217. 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";
  218. } else {
  219. 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";
  220. }
  221. return code;
  222. }
  223. VisualShaderNodeParticleRingEmitter::VisualShaderNodeParticleRingEmitter() {
  224. set_input_port_default_value(0, 10.0);
  225. set_input_port_default_value(1, 0.0);
  226. set_input_port_default_value(2, 0.0);
  227. }
  228. // VisualShaderNodeParticleMeshEmitter
  229. String VisualShaderNodeParticleMeshEmitter::get_caption() const {
  230. return "MeshEmitter";
  231. }
  232. int VisualShaderNodeParticleMeshEmitter::get_output_port_count() const {
  233. return 6;
  234. }
  235. VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_output_port_type(int p_port) const {
  236. switch (p_port) {
  237. case 0: // position
  238. if (mode_2d) {
  239. return PORT_TYPE_VECTOR_2D;
  240. }
  241. return PORT_TYPE_VECTOR_3D;
  242. case 1: // normal
  243. if (mode_2d) {
  244. return PORT_TYPE_VECTOR_2D;
  245. }
  246. return PORT_TYPE_VECTOR_3D;
  247. case 2: // color
  248. return PORT_TYPE_VECTOR_3D;
  249. case 3: // alpha
  250. return PORT_TYPE_SCALAR;
  251. case 4: // uv
  252. return PORT_TYPE_VECTOR_2D;
  253. case 5: // uv2
  254. return PORT_TYPE_VECTOR_2D;
  255. }
  256. return PORT_TYPE_SCALAR;
  257. }
  258. String VisualShaderNodeParticleMeshEmitter::get_output_port_name(int p_port) const {
  259. switch (p_port) {
  260. case 0:
  261. return "position";
  262. case 1:
  263. return "normal";
  264. case 2:
  265. return "color";
  266. case 3:
  267. return "alpha";
  268. case 4:
  269. return "uv";
  270. case 5:
  271. return "uv2";
  272. }
  273. return String();
  274. }
  275. int VisualShaderNodeParticleMeshEmitter::get_input_port_count() const {
  276. return 0;
  277. }
  278. VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_input_port_type(int p_port) const {
  279. return PORT_TYPE_SCALAR;
  280. }
  281. String VisualShaderNodeParticleMeshEmitter::get_input_port_name(int p_port) const {
  282. return String();
  283. }
  284. String VisualShaderNodeParticleMeshEmitter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  285. String code;
  286. if (is_output_port_connected(0)) { // position
  287. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_vx") + ";\n";
  288. }
  289. if (is_output_port_connected(1)) { // normal
  290. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_nm") + ";\n";
  291. }
  292. if (is_output_port_connected(2) || is_output_port_connected(3)) { // color & alpha
  293. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_col") + ";\n";
  294. }
  295. if (is_output_port_connected(4)) { // uv
  296. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv") + ";\n";
  297. }
  298. if (is_output_port_connected(5)) { // uv2
  299. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv2") + ";\n";
  300. }
  301. return code;
  302. }
  303. 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 {
  304. String code;
  305. if (is_output_port_connected(p_index)) {
  306. switch (p_port_type) {
  307. case PORT_TYPE_VECTOR_2D: {
  308. 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));
  309. } break;
  310. case PORT_TYPE_VECTOR_3D: {
  311. if (mode_2d) {
  312. 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));
  313. } else {
  314. 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));
  315. }
  316. } break;
  317. default:
  318. break;
  319. }
  320. }
  321. return code;
  322. }
  323. 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 {
  324. String code;
  325. code += " {\n";
  326. code += " int __scalar_ibuff = int(__rand_from_seed(__seed) * 65535.0) % " + itos(position_texture->get_width()) + ";\n";
  327. code += _generate_code(p_type, p_id, p_output_vars, 0, "mesh_vx", VisualShaderNode::PORT_TYPE_VECTOR_3D);
  328. code += _generate_code(p_type, p_id, p_output_vars, 1, "mesh_nm", VisualShaderNode::PORT_TYPE_VECTOR_3D);
  329. if (is_output_port_connected(2) || is_output_port_connected(3)) {
  330. code += vformat(" vec4 __vec4_buff = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0);\n", make_unique_id(p_type, p_id, "mesh_col"));
  331. if (is_output_port_connected(2)) {
  332. code += " " + p_output_vars[2] + " = __vec4_buff.rgb;\n";
  333. }
  334. if (is_output_port_connected(3)) {
  335. code += " " + p_output_vars[3] + " = __vec4_buff.a;\n";
  336. }
  337. }
  338. code += _generate_code(p_type, p_id, p_output_vars, 4, "mesh_uv", VisualShaderNode::PORT_TYPE_VECTOR_2D);
  339. code += _generate_code(p_type, p_id, p_output_vars, 5, "mesh_uv2", VisualShaderNode::PORT_TYPE_VECTOR_2D);
  340. code += " }\n";
  341. return code;
  342. }
  343. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeParticleMeshEmitter::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  344. Vector<VisualShader::DefaultTextureParam> ret;
  345. if (is_output_port_connected(0)) {
  346. VisualShader::DefaultTextureParam dtp;
  347. dtp.name = make_unique_id(p_type, p_id, "mesh_vx");
  348. dtp.params.push_back(position_texture);
  349. ret.push_back(dtp);
  350. }
  351. if (is_output_port_connected(1)) {
  352. VisualShader::DefaultTextureParam dtp;
  353. dtp.name = make_unique_id(p_type, p_id, "mesh_nm");
  354. dtp.params.push_back(normal_texture);
  355. ret.push_back(dtp);
  356. }
  357. if (is_output_port_connected(2) || is_output_port_connected(3)) {
  358. VisualShader::DefaultTextureParam dtp;
  359. dtp.name = make_unique_id(p_type, p_id, "mesh_col");
  360. dtp.params.push_back(color_texture);
  361. ret.push_back(dtp);
  362. }
  363. if (is_output_port_connected(4)) {
  364. VisualShader::DefaultTextureParam dtp;
  365. dtp.name = make_unique_id(p_type, p_id, "mesh_uv");
  366. dtp.params.push_back(uv_texture);
  367. ret.push_back(dtp);
  368. }
  369. if (is_output_port_connected(5)) {
  370. VisualShader::DefaultTextureParam dtp;
  371. dtp.name = make_unique_id(p_type, p_id, "mesh_uv2");
  372. dtp.params.push_back(uv2_texture);
  373. ret.push_back(dtp);
  374. }
  375. return ret;
  376. }
  377. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector2> &p_array, Ref<ImageTexture> &r_texture) {
  378. Ref<Image> image;
  379. image.instantiate();
  380. if (p_array.size() == 0) {
  381. image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
  382. } else {
  383. image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
  384. }
  385. for (int i = 0; i < p_array.size(); i++) {
  386. Vector2 v = p_array[i];
  387. image->set_pixel(i, 0, Color(v.x, v.y, 0));
  388. }
  389. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  390. r_texture->set_image(image);
  391. } else {
  392. r_texture->update(image);
  393. }
  394. }
  395. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector3> &p_array, Ref<ImageTexture> &r_texture) {
  396. Ref<Image> image;
  397. image.instantiate();
  398. if (p_array.size() == 0) {
  399. image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
  400. } else {
  401. image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
  402. }
  403. for (int i = 0; i < p_array.size(); i++) {
  404. Vector3 v = p_array[i];
  405. image->set_pixel(i, 0, Color(v.x, v.y, v.z));
  406. }
  407. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  408. r_texture->set_image(image);
  409. } else {
  410. r_texture->update(image);
  411. }
  412. }
  413. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Color> &p_array, Ref<ImageTexture> &r_texture) {
  414. Ref<Image> image;
  415. image.instantiate();
  416. if (p_array.size() == 0) {
  417. image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBA8);
  418. } else {
  419. image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBA8);
  420. }
  421. for (int i = 0; i < p_array.size(); i++) {
  422. image->set_pixel(i, 0, p_array[i]);
  423. }
  424. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  425. r_texture->set_image(image);
  426. } else {
  427. r_texture->update(image);
  428. }
  429. }
  430. void VisualShaderNodeParticleMeshEmitter::_update_textures() {
  431. if (!mesh.is_valid()) {
  432. return;
  433. }
  434. Vector<Vector3> vertices;
  435. Vector<Vector3> normals;
  436. Vector<Color> colors;
  437. Vector<Vector2> uvs;
  438. Vector<Vector2> uvs2;
  439. const int surface_count = mesh->get_surface_count();
  440. if (use_all_surfaces) {
  441. for (int i = 0; i < surface_count; i++) {
  442. const Array surface_arrays = mesh->surface_get_arrays(i);
  443. const int surface_arrays_size = surface_arrays.size();
  444. // position
  445. if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
  446. Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
  447. for (int j = 0; j < vertex_array.size(); j++) {
  448. vertices.push_back((Vector3)vertex_array[j]);
  449. }
  450. }
  451. // normal
  452. if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
  453. Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
  454. for (int j = 0; j < normal_array.size(); j++) {
  455. normals.push_back((Vector3)normal_array[j]);
  456. }
  457. }
  458. // color
  459. if (surface_arrays_size > Mesh::ARRAY_COLOR) {
  460. Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
  461. for (int j = 0; j < color_array.size(); j++) {
  462. colors.push_back((Color)color_array[j]);
  463. }
  464. }
  465. // uv
  466. if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
  467. Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
  468. for (int j = 0; j < uv_array.size(); j++) {
  469. uvs.push_back((Vector2)uv_array[j]);
  470. }
  471. }
  472. // uv2
  473. if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
  474. Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
  475. for (int j = 0; j < uv2_array.size(); j++) {
  476. uvs2.push_back((Vector2)uv2_array[j]);
  477. }
  478. }
  479. }
  480. } else {
  481. if (surface_index >= 0 && surface_index < surface_count) {
  482. const Array surface_arrays = mesh->surface_get_arrays(surface_index);
  483. const int surface_arrays_size = surface_arrays.size();
  484. // position
  485. if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
  486. Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
  487. for (int i = 0; i < vertex_array.size(); i++) {
  488. vertices.push_back((Vector3)vertex_array[i]);
  489. }
  490. }
  491. // normal
  492. if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
  493. Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
  494. for (int i = 0; i < normal_array.size(); i++) {
  495. normals.push_back((Vector3)normal_array[i]);
  496. }
  497. }
  498. // color
  499. if (surface_arrays_size > Mesh::ARRAY_COLOR) {
  500. Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
  501. for (int i = 0; i < color_array.size(); i++) {
  502. colors.push_back((Color)color_array[i]);
  503. }
  504. }
  505. // uv
  506. if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
  507. Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
  508. for (int j = 0; j < uv_array.size(); j++) {
  509. uvs.push_back((Vector2)uv_array[j]);
  510. }
  511. }
  512. // uv2
  513. if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
  514. Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
  515. for (int j = 0; j < uv2_array.size(); j++) {
  516. uvs2.push_back((Vector2)uv2_array[j]);
  517. }
  518. }
  519. }
  520. }
  521. _update_texture(vertices, position_texture);
  522. _update_texture(normals, normal_texture);
  523. _update_texture(colors, color_texture);
  524. _update_texture(uvs, uv_texture);
  525. _update_texture(uvs2, uv2_texture);
  526. }
  527. void VisualShaderNodeParticleMeshEmitter::set_mesh(Ref<Mesh> p_mesh) {
  528. if (mesh == p_mesh) {
  529. return;
  530. }
  531. if (mesh.is_valid()) {
  532. mesh->disconnect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
  533. }
  534. mesh = p_mesh;
  535. if (mesh.is_valid()) {
  536. mesh->connect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
  537. }
  538. emit_changed();
  539. }
  540. Ref<Mesh> VisualShaderNodeParticleMeshEmitter::get_mesh() const {
  541. return mesh;
  542. }
  543. void VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces(bool p_enabled) {
  544. if (use_all_surfaces == p_enabled) {
  545. return;
  546. }
  547. use_all_surfaces = p_enabled;
  548. emit_changed();
  549. }
  550. bool VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces() const {
  551. return use_all_surfaces;
  552. }
  553. void VisualShaderNodeParticleMeshEmitter::set_surface_index(int p_surface_index) {
  554. if (mesh.is_valid()) {
  555. if (mesh->get_surface_count() > 0) {
  556. p_surface_index = CLAMP(p_surface_index, 0, mesh->get_surface_count() - 1);
  557. } else {
  558. p_surface_index = 0;
  559. }
  560. } else if (p_surface_index < 0) {
  561. p_surface_index = 0;
  562. }
  563. if (surface_index == p_surface_index) {
  564. return;
  565. }
  566. surface_index = p_surface_index;
  567. emit_changed();
  568. }
  569. int VisualShaderNodeParticleMeshEmitter::get_surface_index() const {
  570. return surface_index;
  571. }
  572. Vector<StringName> VisualShaderNodeParticleMeshEmitter::get_editable_properties() const {
  573. Vector<StringName> props = VisualShaderNodeParticleEmitter::get_editable_properties();
  574. props.push_back("mesh");
  575. props.push_back("use_all_surfaces");
  576. if (!use_all_surfaces) {
  577. props.push_back("surface_index");
  578. }
  579. return props;
  580. }
  581. HashMap<StringName, String> VisualShaderNodeParticleMeshEmitter::get_editable_properties_names() const {
  582. HashMap<StringName, String> names = VisualShaderNodeParticleEmitter::get_editable_properties_names();
  583. names.insert("mesh", RTR("Mesh"));
  584. names.insert("use_all_surfaces", RTR("Use All Surfaces"));
  585. if (!use_all_surfaces) {
  586. names.insert("surface_index", RTR("Surface Index"));
  587. }
  588. return names;
  589. }
  590. void VisualShaderNodeParticleMeshEmitter::_bind_methods() {
  591. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &VisualShaderNodeParticleMeshEmitter::set_mesh);
  592. ClassDB::bind_method(D_METHOD("get_mesh"), &VisualShaderNodeParticleMeshEmitter::get_mesh);
  593. ClassDB::bind_method(D_METHOD("set_use_all_surfaces", "enabled"), &VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces);
  594. ClassDB::bind_method(D_METHOD("is_use_all_surfaces"), &VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces);
  595. ClassDB::bind_method(D_METHOD("set_surface_index", "surface_index"), &VisualShaderNodeParticleMeshEmitter::set_surface_index);
  596. ClassDB::bind_method(D_METHOD("get_surface_index"), &VisualShaderNodeParticleMeshEmitter::get_surface_index);
  597. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  598. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_all_surfaces"), "set_use_all_surfaces", "is_use_all_surfaces");
  599. ADD_PROPERTY(PropertyInfo(Variant::INT, "surface_index"), "set_surface_index", "get_surface_index");
  600. }
  601. VisualShaderNodeParticleMeshEmitter::VisualShaderNodeParticleMeshEmitter() {
  602. connect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
  603. position_texture.instantiate();
  604. normal_texture.instantiate();
  605. color_texture.instantiate();
  606. uv_texture.instantiate();
  607. uv2_texture.instantiate();
  608. simple_decl = false;
  609. }
  610. // VisualShaderNodeParticleMultiplyByAxisAngle
  611. void VisualShaderNodeParticleMultiplyByAxisAngle::_bind_methods() {
  612. ClassDB::bind_method(D_METHOD("set_degrees_mode", "enabled"), &VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode);
  613. ClassDB::bind_method(D_METHOD("is_degrees_mode"), &VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode);
  614. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "degrees_mode"), "set_degrees_mode", "is_degrees_mode");
  615. }
  616. String VisualShaderNodeParticleMultiplyByAxisAngle::get_caption() const {
  617. return "MultiplyByAxisAngle";
  618. }
  619. int VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_count() const {
  620. return 3;
  621. }
  622. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_type(int p_port) const {
  623. if (p_port == 0 || p_port == 1) { // position, rotation_axis
  624. return PORT_TYPE_VECTOR_3D;
  625. }
  626. return PORT_TYPE_SCALAR; // angle (degrees/radians)
  627. }
  628. String VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_name(int p_port) const {
  629. if (p_port == 0) {
  630. return "position";
  631. }
  632. if (p_port == 1) {
  633. return "axis";
  634. }
  635. if (p_port == 2) {
  636. if (degrees_mode) {
  637. return "angle (degrees)";
  638. } else {
  639. return "angle (radians)";
  640. }
  641. }
  642. return String();
  643. }
  644. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_show_prop_names() const {
  645. return true;
  646. }
  647. int VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_count() const {
  648. return 1;
  649. }
  650. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_type(int p_port) const {
  651. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  652. }
  653. String VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_name(int p_port) const {
  654. return "position";
  655. }
  656. 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 {
  657. String code;
  658. if (degrees_mode) {
  659. 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";
  660. } else {
  661. 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";
  662. }
  663. return code;
  664. }
  665. void VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode(bool p_enabled) {
  666. degrees_mode = p_enabled;
  667. emit_changed();
  668. }
  669. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode() const {
  670. return degrees_mode;
  671. }
  672. Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_properties() const {
  673. Vector<StringName> props;
  674. props.push_back("degrees_mode");
  675. return props;
  676. }
  677. bool VisualShaderNodeParticleMultiplyByAxisAngle::has_output_port_preview(int p_port) const {
  678. return false;
  679. }
  680. VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() {
  681. set_input_port_default_value(1, Vector3(1, 0, 0));
  682. set_input_port_default_value(2, 0.0);
  683. }
  684. // VisualShaderNodeParticleConeVelocity
  685. String VisualShaderNodeParticleConeVelocity::get_caption() const {
  686. return "ConeVelocity";
  687. }
  688. int VisualShaderNodeParticleConeVelocity::get_input_port_count() const {
  689. return 2;
  690. }
  691. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_input_port_type(int p_port) const {
  692. if (p_port == 0) {
  693. return PORT_TYPE_VECTOR_3D;
  694. } else if (p_port == 1) {
  695. return PORT_TYPE_SCALAR;
  696. }
  697. return PORT_TYPE_SCALAR;
  698. }
  699. String VisualShaderNodeParticleConeVelocity::get_input_port_name(int p_port) const {
  700. if (p_port == 0) {
  701. return "direction";
  702. } else if (p_port == 1) {
  703. return "spread(degrees)";
  704. }
  705. return String();
  706. }
  707. int VisualShaderNodeParticleConeVelocity::get_output_port_count() const {
  708. return 1;
  709. }
  710. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_output_port_type(int p_port) const {
  711. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  712. }
  713. String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) const {
  714. if (p_port == 0) {
  715. return "velocity";
  716. }
  717. return String();
  718. }
  719. bool VisualShaderNodeParticleConeVelocity::has_output_port_preview(int p_port) const {
  720. return false;
  721. }
  722. 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 {
  723. String code;
  724. code += " {\n";
  725. code += " float __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
  726. code += " float __scalar_buff1 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  727. code += " float __scalar_buff2 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  728. code += " vec3 __vec3_buff1 = " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + ";\n";
  729. code += " __scalar_buff1 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.x, __vec3_buff1.z) : sign(__vec3_buff1.x) * (PI / 2.0);\n";
  730. 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";
  731. code += " __vec3_buff1 = vec3(sin(__scalar_buff1), 0.0, cos(__scalar_buff1));\n";
  732. code += " vec3 __vec3_buff2 = vec3(0.0, sin(__scalar_buff2), cos(__scalar_buff2));\n";
  733. code += " __vec3_buff2.z = __vec3_buff2.z / max(0.0001, sqrt(abs(__vec3_buff2.z)));\n";
  734. code += " " + p_output_vars[0] + " = normalize(vec3(__vec3_buff1.x * __vec3_buff2.z, __vec3_buff2.y, __vec3_buff1.z * __vec3_buff2.z));\n";
  735. code += " }\n";
  736. return code;
  737. }
  738. VisualShaderNodeParticleConeVelocity::VisualShaderNodeParticleConeVelocity() {
  739. set_input_port_default_value(0, Vector3(1, 0, 0));
  740. set_input_port_default_value(1, 45.0);
  741. simple_decl = false;
  742. }
  743. // VisualShaderNodeParticleRandomness
  744. void VisualShaderNodeParticleRandomness::_bind_methods() {
  745. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeParticleRandomness::set_op_type);
  746. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeParticleRandomness::get_op_type);
  747. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
  748. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  749. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  750. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  751. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  752. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  753. }
  754. Vector<StringName> VisualShaderNodeParticleRandomness::get_editable_properties() const {
  755. Vector<StringName> props;
  756. props.push_back("op_type");
  757. return props;
  758. }
  759. String VisualShaderNodeParticleRandomness::get_caption() const {
  760. return "ParticleRandomness";
  761. }
  762. int VisualShaderNodeParticleRandomness::get_output_port_count() const {
  763. return 1;
  764. }
  765. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_output_port_type(int p_port) const {
  766. switch (op_type) {
  767. case OP_TYPE_VECTOR_2D:
  768. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  769. case OP_TYPE_VECTOR_3D:
  770. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  771. case OP_TYPE_VECTOR_4D:
  772. return p_port == 0 ? PORT_TYPE_VECTOR_4D : PORT_TYPE_SCALAR;
  773. default:
  774. break;
  775. }
  776. return PORT_TYPE_SCALAR;
  777. }
  778. String VisualShaderNodeParticleRandomness::get_output_port_name(int p_port) const {
  779. return "random";
  780. }
  781. int VisualShaderNodeParticleRandomness::get_input_port_count() const {
  782. return 3;
  783. }
  784. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_input_port_type(int p_port) const {
  785. switch (p_port) {
  786. case 0:
  787. return PORT_TYPE_SCALAR_UINT;
  788. case 1:
  789. case 2:
  790. switch (op_type) {
  791. case OP_TYPE_VECTOR_2D:
  792. return PORT_TYPE_VECTOR_2D;
  793. case OP_TYPE_VECTOR_3D:
  794. return PORT_TYPE_VECTOR_3D;
  795. case OP_TYPE_VECTOR_4D:
  796. return PORT_TYPE_VECTOR_4D;
  797. default:
  798. break;
  799. }
  800. break;
  801. }
  802. return PORT_TYPE_SCALAR;
  803. }
  804. String VisualShaderNodeParticleRandomness::get_input_port_name(int p_port) const {
  805. switch (p_port) {
  806. case 0:
  807. return "seed";
  808. case 1:
  809. return "min";
  810. case 2:
  811. return "max";
  812. }
  813. return String();
  814. }
  815. bool VisualShaderNodeParticleRandomness::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  816. return p_port == 0; // seed
  817. }
  818. String VisualShaderNodeParticleRandomness::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  819. String code;
  820. code += "vec2 __randv2_range(inout uint seed, vec2 from, vec2 to) {\n";
  821. code += " return vec2(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y));\n";
  822. code += "}\n\n";
  823. code += "vec3 __randv3_range(inout uint seed, vec3 from, vec3 to) {\n";
  824. code += " return vec3(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y), __randf_range(seed, from.z, to.z));\n";
  825. code += "}\n\n";
  826. code += "vec4 __randv4_range(inout uint seed, vec4 from, vec4 to) {\n";
  827. code += " return vec4(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y), __randf_range(seed, from.z, to.z), __randf_range(seed, from.w, to.w));\n";
  828. code += "}\n\n";
  829. return code;
  830. }
  831. 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 {
  832. static const char *func[(int)OP_TYPE_MAX] = { "__randf_range", "__randv2_range", "__randv3_range", "__randv4_range" };
  833. return vformat(" %s = %s(%s, %s, %s);\n", p_output_vars[0], func[op_type], p_input_vars[0].is_empty() ? "__seed" : 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]);
  834. }
  835. void VisualShaderNodeParticleRandomness::set_op_type(OpType p_op_type) {
  836. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  837. if (op_type == p_op_type) {
  838. return;
  839. }
  840. switch (p_op_type) {
  841. case OP_TYPE_SCALAR: {
  842. set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
  843. set_input_port_default_value(2, 0.0, get_input_port_default_value(2));
  844. } break;
  845. case OP_TYPE_VECTOR_2D: {
  846. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  847. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2));
  848. } break;
  849. case OP_TYPE_VECTOR_3D: {
  850. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  851. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2));
  852. } break;
  853. case OP_TYPE_VECTOR_4D: {
  854. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  855. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2));
  856. } break;
  857. default:
  858. break;
  859. }
  860. op_type = p_op_type;
  861. emit_changed();
  862. }
  863. VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::get_op_type() const {
  864. return op_type;
  865. }
  866. bool VisualShaderNodeParticleRandomness::has_output_port_preview(int p_port) const {
  867. return false;
  868. }
  869. VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() {
  870. set_input_port_default_value(1, -1.0);
  871. set_input_port_default_value(2, 1.0);
  872. }
  873. // VisualShaderNodeParticleAccelerator
  874. void VisualShaderNodeParticleAccelerator::_bind_methods() {
  875. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShaderNodeParticleAccelerator::set_mode);
  876. ClassDB::bind_method(D_METHOD("get_mode"), &VisualShaderNodeParticleAccelerator::get_mode);
  877. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Linear,Radial,Tangential"), "set_mode", "get_mode");
  878. BIND_ENUM_CONSTANT(MODE_LINEAR);
  879. BIND_ENUM_CONSTANT(MODE_RADIAL)
  880. BIND_ENUM_CONSTANT(MODE_TANGENTIAL);
  881. BIND_ENUM_CONSTANT(MODE_MAX);
  882. }
  883. Vector<StringName> VisualShaderNodeParticleAccelerator::get_editable_properties() const {
  884. Vector<StringName> props;
  885. props.push_back("mode");
  886. return props;
  887. }
  888. String VisualShaderNodeParticleAccelerator::get_caption() const {
  889. return "ParticleAccelerator";
  890. }
  891. int VisualShaderNodeParticleAccelerator::get_output_port_count() const {
  892. return 1;
  893. }
  894. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_output_port_type(int p_port) const {
  895. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  896. }
  897. String VisualShaderNodeParticleAccelerator::get_output_port_name(int p_port) const {
  898. return String();
  899. }
  900. int VisualShaderNodeParticleAccelerator::get_input_port_count() const {
  901. return 3;
  902. }
  903. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_input_port_type(int p_port) const {
  904. if (p_port == 0) {
  905. return PORT_TYPE_VECTOR_3D;
  906. } else if (p_port == 1) {
  907. return PORT_TYPE_SCALAR;
  908. } else if (p_port == 2) {
  909. return PORT_TYPE_VECTOR_3D;
  910. }
  911. return PORT_TYPE_SCALAR;
  912. }
  913. String VisualShaderNodeParticleAccelerator::get_input_port_name(int p_port) const {
  914. if (p_port == 0) {
  915. return "amount";
  916. } else if (p_port == 1) {
  917. return "randomness";
  918. } else if (p_port == 2) {
  919. return "axis";
  920. }
  921. return String();
  922. }
  923. 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 {
  924. String code;
  925. code += " {\n";
  926. switch (mode) {
  927. case MODE_LINEAR:
  928. 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";
  929. break;
  930. case MODE_RADIAL:
  931. code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
  932. code += " vec3 __ndiff = normalize(__diff);\n\n";
  933. 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";
  934. break;
  935. case MODE_TANGENTIAL:
  936. code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
  937. code += " vec3 __ndiff = normalize(__diff);\n\n";
  938. 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";
  939. 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";
  940. break;
  941. default:
  942. break;
  943. }
  944. code += " }\n";
  945. return code;
  946. }
  947. void VisualShaderNodeParticleAccelerator::set_mode(Mode p_mode) {
  948. ERR_FAIL_INDEX(int(p_mode), int(MODE_MAX));
  949. if (mode == p_mode) {
  950. return;
  951. }
  952. mode = p_mode;
  953. emit_changed();
  954. }
  955. VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::get_mode() const {
  956. return mode;
  957. }
  958. bool VisualShaderNodeParticleAccelerator::has_output_port_preview(int p_port) const {
  959. return false;
  960. }
  961. VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() {
  962. set_input_port_default_value(0, Vector3(1, 1, 1));
  963. set_input_port_default_value(1, 0.0);
  964. set_input_port_default_value(2, Vector3(0, -9.8, 0));
  965. simple_decl = false;
  966. }
  967. // VisualShaderNodeParticleOutput
  968. String VisualShaderNodeParticleOutput::get_caption() const {
  969. switch (shader_type) {
  970. case VisualShader::TYPE_START:
  971. return "StartOutput";
  972. case VisualShader::TYPE_PROCESS:
  973. return "ProcessOutput";
  974. case VisualShader::TYPE_COLLIDE:
  975. return "CollideOutput";
  976. case VisualShader::TYPE_START_CUSTOM:
  977. return "CustomStartOutput";
  978. case VisualShader::TYPE_PROCESS_CUSTOM:
  979. return "CustomProcessOutput";
  980. default:
  981. ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
  982. return "";
  983. }
  984. }
  985. int VisualShaderNodeParticleOutput::get_input_port_count() const {
  986. switch (shader_type) {
  987. case VisualShader::TYPE_START:
  988. return 8;
  989. case VisualShader::TYPE_PROCESS:
  990. return 7;
  991. case VisualShader::TYPE_COLLIDE:
  992. return 5;
  993. case VisualShader::TYPE_START_CUSTOM:
  994. case VisualShader::TYPE_PROCESS_CUSTOM:
  995. return 6;
  996. default:
  997. ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
  998. return 0;
  999. }
  1000. }
  1001. VisualShaderNodeParticleOutput::PortType VisualShaderNodeParticleOutput::get_input_port_type(int p_port) const {
  1002. switch (p_port) {
  1003. case 0:
  1004. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1005. return PORT_TYPE_VECTOR_3D; // custom.rgb
  1006. }
  1007. return PORT_TYPE_BOOLEAN; // active
  1008. case 1:
  1009. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1010. break; // custom.a (scalar)
  1011. }
  1012. return PORT_TYPE_VECTOR_3D; // velocity
  1013. case 2:
  1014. return PORT_TYPE_VECTOR_3D; // color & velocity
  1015. case 3:
  1016. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1017. return PORT_TYPE_VECTOR_3D; // color
  1018. }
  1019. break; // alpha (scalar)
  1020. case 4:
  1021. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1022. break; // alpha
  1023. }
  1024. if (shader_type == VisualShader::TYPE_PROCESS) {
  1025. break; // scale
  1026. }
  1027. if (shader_type == VisualShader::TYPE_COLLIDE) {
  1028. return PORT_TYPE_TRANSFORM; // transform
  1029. }
  1030. return PORT_TYPE_VECTOR_3D; // position
  1031. case 5:
  1032. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1033. return PORT_TYPE_TRANSFORM; // transform
  1034. }
  1035. if (shader_type == VisualShader::TYPE_PROCESS) {
  1036. return PORT_TYPE_VECTOR_3D; // rotation_axis
  1037. }
  1038. break; // scale (scalar)
  1039. case 6:
  1040. if (shader_type == VisualShader::TYPE_START) {
  1041. return PORT_TYPE_VECTOR_3D; // rotation_axis
  1042. }
  1043. break;
  1044. case 7:
  1045. break; // angle (scalar)
  1046. }
  1047. return PORT_TYPE_SCALAR;
  1048. }
  1049. String VisualShaderNodeParticleOutput::get_input_port_name(int p_port) const {
  1050. String port_name;
  1051. switch (p_port) {
  1052. case 0:
  1053. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1054. port_name = "custom";
  1055. break;
  1056. }
  1057. port_name = "active";
  1058. break;
  1059. case 1:
  1060. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1061. port_name = "custom_alpha";
  1062. break;
  1063. }
  1064. port_name = "velocity";
  1065. break;
  1066. case 2:
  1067. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1068. port_name = "velocity";
  1069. break;
  1070. }
  1071. port_name = "color";
  1072. break;
  1073. case 3:
  1074. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1075. port_name = "color";
  1076. break;
  1077. }
  1078. port_name = "alpha";
  1079. break;
  1080. case 4:
  1081. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1082. port_name = "alpha";
  1083. break;
  1084. }
  1085. if (shader_type == VisualShader::TYPE_PROCESS) {
  1086. port_name = "scale";
  1087. break;
  1088. }
  1089. if (shader_type == VisualShader::TYPE_COLLIDE) {
  1090. port_name = "transform";
  1091. break;
  1092. }
  1093. port_name = "position";
  1094. break;
  1095. case 5:
  1096. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1097. port_name = "transform";
  1098. break;
  1099. }
  1100. if (shader_type == VisualShader::TYPE_PROCESS) {
  1101. port_name = "rotation_axis";
  1102. break;
  1103. }
  1104. port_name = "scale";
  1105. break;
  1106. case 6:
  1107. if (shader_type == VisualShader::TYPE_PROCESS) {
  1108. port_name = "angle_in_radians";
  1109. break;
  1110. }
  1111. port_name = "rotation_axis";
  1112. break;
  1113. case 7:
  1114. port_name = "angle_in_radians";
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. if (!port_name.is_empty()) {
  1120. return port_name.capitalize();
  1121. }
  1122. return String();
  1123. }
  1124. bool VisualShaderNodeParticleOutput::is_port_separator(int p_index) const {
  1125. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  1126. String port_name = get_input_port_name(p_index);
  1127. return bool(port_name == "Scale");
  1128. }
  1129. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1130. String port_name = get_input_port_name(p_index);
  1131. return bool(port_name == "Velocity");
  1132. }
  1133. return false;
  1134. }
  1135. 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 {
  1136. String code;
  1137. String tab = " ";
  1138. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1139. if (!p_input_vars[0].is_empty()) { // custom.rgb
  1140. code += tab + "CUSTOM.rgb = " + p_input_vars[0] + ";\n";
  1141. }
  1142. if (!p_input_vars[1].is_empty()) { // custom.a
  1143. code += tab + "CUSTOM.a = " + p_input_vars[1] + ";\n";
  1144. }
  1145. if (!p_input_vars[2].is_empty()) { // velocity
  1146. code += tab + "VELOCITY = " + p_input_vars[2] + ";\n";
  1147. }
  1148. if (!p_input_vars[3].is_empty()) { // color.rgb
  1149. code += tab + "COLOR.rgb = " + p_input_vars[3] + ";\n";
  1150. }
  1151. if (!p_input_vars[4].is_empty()) { // color.a
  1152. code += tab + "COLOR.a = " + p_input_vars[4] + ";\n";
  1153. }
  1154. if (!p_input_vars[5].is_empty()) { // transform
  1155. code += tab + "TRANSFORM = " + p_input_vars[5] + ";\n";
  1156. }
  1157. } else {
  1158. if (!p_input_vars[0].is_empty()) { // Active (begin).
  1159. code += tab + "ACTIVE = " + p_input_vars[0] + ";\n";
  1160. code += tab + "if(ACTIVE) {\n";
  1161. tab += " ";
  1162. }
  1163. if (!p_input_vars[1].is_empty()) { // velocity
  1164. code += tab + "VELOCITY = " + p_input_vars[1] + ";\n";
  1165. }
  1166. if (!p_input_vars[2].is_empty()) { // color
  1167. code += tab + "COLOR.rgb = " + p_input_vars[2] + ";\n";
  1168. }
  1169. if (!p_input_vars[3].is_empty()) { // alpha
  1170. code += tab + "COLOR.a = " + p_input_vars[3] + ";\n";
  1171. }
  1172. // position
  1173. if (shader_type == VisualShader::TYPE_START) {
  1174. code += tab + "if (RESTART_POSITION) {\n";
  1175. if (!p_input_vars[4].is_empty()) {
  1176. 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";
  1177. } else {
  1178. 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";
  1179. }
  1180. code += tab + " if (RESTART_VELOCITY) {\n";
  1181. code += tab + " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
  1182. code += tab + " }\n";
  1183. code += tab + " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  1184. code += tab + "}\n";
  1185. } else if (shader_type == VisualShader::TYPE_COLLIDE) { // position
  1186. if (!p_input_vars[4].is_empty()) {
  1187. code += tab + "TRANSFORM = " + p_input_vars[4] + ";\n";
  1188. }
  1189. }
  1190. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  1191. int scale = 5;
  1192. int rotation_axis = 6;
  1193. int rotation = 7;
  1194. if (shader_type == VisualShader::TYPE_PROCESS) {
  1195. scale = 4;
  1196. rotation_axis = 5;
  1197. rotation = 6;
  1198. }
  1199. String op;
  1200. if (shader_type == VisualShader::TYPE_START) {
  1201. op = "*=";
  1202. } else {
  1203. op = "=";
  1204. }
  1205. if (!p_input_vars[rotation].is_empty()) { // rotation_axis & angle_in_radians
  1206. String axis;
  1207. if (p_input_vars[rotation_axis].is_empty()) {
  1208. axis = "vec3(0, 1, 0)";
  1209. } else {
  1210. axis = p_input_vars[rotation_axis];
  1211. }
  1212. code += tab + "TRANSFORM " + op + " __build_rotation_mat4(" + axis + ", " + p_input_vars[rotation] + ");\n";
  1213. }
  1214. if (!p_input_vars[scale].is_empty()) { // scale
  1215. 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";
  1216. }
  1217. }
  1218. if (!p_input_vars[0].is_empty()) { // Active (end).
  1219. code += " }\n";
  1220. }
  1221. }
  1222. return code;
  1223. }
  1224. VisualShaderNodeParticleOutput::VisualShaderNodeParticleOutput() {
  1225. }
  1226. // EmitParticle
  1227. Vector<StringName> VisualShaderNodeParticleEmit::get_editable_properties() const {
  1228. Vector<StringName> props;
  1229. props.push_back("flags");
  1230. return props;
  1231. }
  1232. void VisualShaderNodeParticleEmit::_bind_methods() {
  1233. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &VisualShaderNodeParticleEmit::set_flags);
  1234. ClassDB::bind_method(D_METHOD("get_flags"), &VisualShaderNodeParticleEmit::get_flags);
  1235. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Position,RotScale,Velocity,Color,Custom"), "set_flags", "get_flags");
  1236. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  1237. BIND_ENUM_CONSTANT(EMIT_FLAG_ROT_SCALE);
  1238. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  1239. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  1240. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  1241. }
  1242. String VisualShaderNodeParticleEmit::get_caption() const {
  1243. return "EmitParticle";
  1244. }
  1245. int VisualShaderNodeParticleEmit::get_input_port_count() const {
  1246. return 7;
  1247. }
  1248. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_input_port_type(int p_port) const {
  1249. switch (p_port) {
  1250. case 0:
  1251. return PORT_TYPE_BOOLEAN;
  1252. case 1:
  1253. return PORT_TYPE_TRANSFORM;
  1254. case 2:
  1255. return PORT_TYPE_VECTOR_3D;
  1256. case 3:
  1257. return PORT_TYPE_VECTOR_3D;
  1258. case 4:
  1259. return PORT_TYPE_SCALAR;
  1260. case 5:
  1261. return PORT_TYPE_VECTOR_3D;
  1262. case 6:
  1263. return PORT_TYPE_SCALAR;
  1264. }
  1265. return PORT_TYPE_SCALAR;
  1266. }
  1267. String VisualShaderNodeParticleEmit::get_input_port_name(int p_port) const {
  1268. switch (p_port) {
  1269. case 0:
  1270. return "condition";
  1271. case 1:
  1272. return "transform";
  1273. case 2:
  1274. return "velocity";
  1275. case 3:
  1276. return "color";
  1277. case 4:
  1278. return "alpha";
  1279. case 5:
  1280. return "custom";
  1281. case 6:
  1282. return "custom_alpha";
  1283. }
  1284. return String();
  1285. }
  1286. int VisualShaderNodeParticleEmit::get_output_port_count() const {
  1287. return 0;
  1288. }
  1289. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_output_port_type(int p_port) const {
  1290. return PORT_TYPE_SCALAR;
  1291. }
  1292. String VisualShaderNodeParticleEmit::get_output_port_name(int p_port) const {
  1293. return String();
  1294. }
  1295. void VisualShaderNodeParticleEmit::add_flag(EmitFlags p_flag) {
  1296. flags |= p_flag;
  1297. emit_changed();
  1298. }
  1299. bool VisualShaderNodeParticleEmit::has_flag(EmitFlags p_flag) const {
  1300. return flags & p_flag;
  1301. }
  1302. void VisualShaderNodeParticleEmit::set_flags(EmitFlags p_flags) {
  1303. flags = (int)p_flags;
  1304. emit_changed();
  1305. }
  1306. VisualShaderNodeParticleEmit::EmitFlags VisualShaderNodeParticleEmit::get_flags() const {
  1307. return EmitFlags(flags);
  1308. }
  1309. bool VisualShaderNodeParticleEmit::is_show_prop_names() const {
  1310. return true;
  1311. }
  1312. bool VisualShaderNodeParticleEmit::is_generate_input_var(int p_port) const {
  1313. if (p_port == 0) {
  1314. if (!is_input_port_connected(0)) {
  1315. return false;
  1316. }
  1317. }
  1318. return true;
  1319. }
  1320. bool VisualShaderNodeParticleEmit::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1321. switch (p_port) {
  1322. case 1:
  1323. return true;
  1324. case 2:
  1325. return true;
  1326. case 3:
  1327. return true;
  1328. case 4:
  1329. return true;
  1330. case 5:
  1331. return true;
  1332. case 6:
  1333. return true;
  1334. }
  1335. return false;
  1336. }
  1337. 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 {
  1338. String code;
  1339. String tab;
  1340. bool default_condition = false;
  1341. if (!is_input_port_connected(0)) {
  1342. default_condition = true;
  1343. if (get_input_port_default_value(0)) {
  1344. tab = " ";
  1345. } else {
  1346. return code;
  1347. }
  1348. } else {
  1349. tab = " ";
  1350. }
  1351. String transform;
  1352. if (p_input_vars[1].is_empty()) {
  1353. transform = "TRANSFORM";
  1354. } else {
  1355. transform = p_input_vars[1];
  1356. }
  1357. String velocity;
  1358. if (p_input_vars[2].is_empty()) {
  1359. velocity = "VELOCITY";
  1360. } else {
  1361. velocity = p_input_vars[2];
  1362. }
  1363. String color;
  1364. if (p_input_vars[3].is_empty()) {
  1365. color = "COLOR.rgb";
  1366. } else {
  1367. color = p_input_vars[3];
  1368. }
  1369. String alpha;
  1370. if (p_input_vars[4].is_empty()) {
  1371. alpha = "COLOR.a";
  1372. } else {
  1373. alpha = p_input_vars[4];
  1374. }
  1375. String custom;
  1376. if (p_input_vars[5].is_empty()) {
  1377. custom = "CUSTOM.rgb";
  1378. } else {
  1379. custom = p_input_vars[5];
  1380. }
  1381. String custom_alpha;
  1382. if (p_input_vars[6].is_empty()) {
  1383. custom_alpha = "CUSTOM.a";
  1384. } else {
  1385. custom_alpha = p_input_vars[6];
  1386. }
  1387. List<String> flags_arr;
  1388. if (has_flag(EmitFlags::EMIT_FLAG_POSITION)) {
  1389. flags_arr.push_back("FLAG_EMIT_POSITION");
  1390. }
  1391. if (has_flag(EmitFlags::EMIT_FLAG_ROT_SCALE)) {
  1392. flags_arr.push_back("FLAG_EMIT_ROT_SCALE");
  1393. }
  1394. if (has_flag(EmitFlags::EMIT_FLAG_VELOCITY)) {
  1395. flags_arr.push_back("FLAG_EMIT_VELOCITY");
  1396. }
  1397. if (has_flag(EmitFlags::EMIT_FLAG_COLOR)) {
  1398. flags_arr.push_back("FLAG_EMIT_COLOR");
  1399. }
  1400. if (has_flag(EmitFlags::EMIT_FLAG_CUSTOM)) {
  1401. flags_arr.push_back("FLAG_EMIT_CUSTOM");
  1402. }
  1403. String flags_str;
  1404. for (List<String>::ConstIterator itr = flags_arr.begin(); itr != flags_arr.end(); ++itr) {
  1405. if (itr != flags_arr.begin()) {
  1406. flags_str += "|";
  1407. }
  1408. flags_str += *itr;
  1409. }
  1410. if (flags_str.is_empty()) {
  1411. flags_str = "uint(0)";
  1412. }
  1413. if (!default_condition) {
  1414. code += " if (" + p_input_vars[0] + ") {\n";
  1415. }
  1416. code += tab + "emit_subparticle(" + transform + ", " + velocity + ", vec4(" + color + ", " + alpha + "), vec4(" + custom + ", " + custom_alpha + "), " + flags_str + ");\n";
  1417. if (!default_condition) {
  1418. code += " }\n";
  1419. }
  1420. return code;
  1421. }
  1422. VisualShaderNodeParticleEmit::VisualShaderNodeParticleEmit() {
  1423. set_input_port_default_value(0, true);
  1424. }