visual_shader_particle_nodes.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/core_string_names.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 PORT_TYPE_VECTOR_2D;
  39. }
  40. return PORT_TYPE_VECTOR_3D;
  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 += " __scalar_ibuff = int(__rand_from_seed(__seed) * 65535.0) % " + itos(position_texture->get_width()) + ";\n";
  326. code += _generate_code(p_type, p_id, p_output_vars, 0, "mesh_vx", VisualShaderNode::PORT_TYPE_VECTOR_3D);
  327. code += _generate_code(p_type, p_id, p_output_vars, 1, "mesh_nm", VisualShaderNode::PORT_TYPE_VECTOR_3D);
  328. if (is_output_port_connected(2) || is_output_port_connected(3)) {
  329. code += vformat(" __vec4_buff = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0);\n", make_unique_id(p_type, p_id, "mesh_col"));
  330. if (is_output_port_connected(2)) {
  331. code += " " + p_output_vars[2] + " = __vec4_buff.rgb;\n";
  332. }
  333. if (is_output_port_connected(3)) {
  334. code += " " + p_output_vars[3] + " = __vec4_buff.a;\n";
  335. }
  336. }
  337. code += _generate_code(p_type, p_id, p_output_vars, 4, "mesh_uv", VisualShaderNode::PORT_TYPE_VECTOR_2D);
  338. code += _generate_code(p_type, p_id, p_output_vars, 5, "mesh_uv2", VisualShaderNode::PORT_TYPE_VECTOR_2D);
  339. return code;
  340. }
  341. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeParticleMeshEmitter::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  342. Vector<VisualShader::DefaultTextureParam> ret;
  343. if (is_output_port_connected(0)) {
  344. VisualShader::DefaultTextureParam dtp;
  345. dtp.name = make_unique_id(p_type, p_id, "mesh_vx");
  346. dtp.params.push_back(position_texture);
  347. ret.push_back(dtp);
  348. }
  349. if (is_output_port_connected(1)) {
  350. VisualShader::DefaultTextureParam dtp;
  351. dtp.name = make_unique_id(p_type, p_id, "mesh_nm");
  352. dtp.params.push_back(normal_texture);
  353. ret.push_back(dtp);
  354. }
  355. if (is_output_port_connected(2) || is_output_port_connected(3)) {
  356. VisualShader::DefaultTextureParam dtp;
  357. dtp.name = make_unique_id(p_type, p_id, "mesh_col");
  358. dtp.params.push_back(color_texture);
  359. ret.push_back(dtp);
  360. }
  361. if (is_output_port_connected(4)) {
  362. VisualShader::DefaultTextureParam dtp;
  363. dtp.name = make_unique_id(p_type, p_id, "mesh_uv");
  364. dtp.params.push_back(uv_texture);
  365. ret.push_back(dtp);
  366. }
  367. if (is_output_port_connected(5)) {
  368. VisualShader::DefaultTextureParam dtp;
  369. dtp.name = make_unique_id(p_type, p_id, "mesh_uv2");
  370. dtp.params.push_back(uv2_texture);
  371. ret.push_back(dtp);
  372. }
  373. return ret;
  374. }
  375. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector2> &p_array, Ref<ImageTexture> &r_texture) {
  376. Ref<Image> image;
  377. image.instantiate();
  378. if (p_array.size() == 0) {
  379. image->create(1, 1, false, Image::Format::FORMAT_RGBF);
  380. } else {
  381. image->create(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
  382. }
  383. for (int i = 0; i < p_array.size(); i++) {
  384. Vector2 v = p_array[i];
  385. image->set_pixel(i, 0, Color(v.x, v.y, 0));
  386. }
  387. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  388. r_texture->set_image(image);
  389. } else {
  390. r_texture->update(image);
  391. }
  392. }
  393. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector3> &p_array, Ref<ImageTexture> &r_texture) {
  394. Ref<Image> image;
  395. image.instantiate();
  396. if (p_array.size() == 0) {
  397. image->create(1, 1, false, Image::Format::FORMAT_RGBF);
  398. } else {
  399. image->create(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
  400. }
  401. for (int i = 0; i < p_array.size(); i++) {
  402. Vector3 v = p_array[i];
  403. image->set_pixel(i, 0, Color(v.x, v.y, v.z));
  404. }
  405. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  406. r_texture->set_image(image);
  407. } else {
  408. r_texture->update(image);
  409. }
  410. }
  411. void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Color> &p_array, Ref<ImageTexture> &r_texture) {
  412. Ref<Image> image;
  413. image.instantiate();
  414. if (p_array.size() == 0) {
  415. image->create(1, 1, false, Image::Format::FORMAT_RGBA8);
  416. } else {
  417. image->create(p_array.size(), 1, false, Image::Format::FORMAT_RGBA8);
  418. }
  419. for (int i = 0; i < p_array.size(); i++) {
  420. image->set_pixel(i, 0, p_array[i]);
  421. }
  422. if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
  423. r_texture->set_image(image);
  424. } else {
  425. r_texture->update(image);
  426. }
  427. }
  428. void VisualShaderNodeParticleMeshEmitter::_update_textures() {
  429. if (!mesh.is_valid()) {
  430. return;
  431. }
  432. Vector<Vector3> vertices;
  433. Vector<Vector3> normals;
  434. Vector<Color> colors;
  435. Vector<Vector2> uvs;
  436. Vector<Vector2> uvs2;
  437. const int surface_count = mesh->get_surface_count();
  438. if (use_all_surfaces) {
  439. for (int i = 0; i < surface_count; i++) {
  440. const Array surface_arrays = mesh->surface_get_arrays(i);
  441. const int surface_arrays_size = surface_arrays.size();
  442. // position
  443. if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
  444. Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
  445. for (int j = 0; j < vertex_array.size(); j++) {
  446. vertices.push_back((Vector3)vertex_array[j]);
  447. }
  448. }
  449. // normal
  450. if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
  451. Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
  452. for (int j = 0; j < normal_array.size(); j++) {
  453. normals.push_back((Vector3)normal_array[j]);
  454. }
  455. }
  456. // color
  457. if (surface_arrays_size > Mesh::ARRAY_COLOR) {
  458. Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
  459. for (int j = 0; j < color_array.size(); j++) {
  460. colors.push_back((Color)color_array[j]);
  461. }
  462. }
  463. // uv
  464. if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
  465. Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
  466. for (int j = 0; j < uv_array.size(); j++) {
  467. uvs.push_back((Vector2)uv_array[j]);
  468. }
  469. }
  470. // uv2
  471. if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
  472. Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
  473. for (int j = 0; j < uv2_array.size(); j++) {
  474. uvs2.push_back((Vector2)uv2_array[j]);
  475. }
  476. }
  477. }
  478. } else {
  479. if (surface_index >= 0 && surface_index < surface_count) {
  480. const Array surface_arrays = mesh->surface_get_arrays(surface_index);
  481. const int surface_arrays_size = surface_arrays.size();
  482. // position
  483. if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
  484. Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
  485. for (int i = 0; i < vertex_array.size(); i++) {
  486. vertices.push_back((Vector3)vertex_array[i]);
  487. }
  488. }
  489. // normal
  490. if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
  491. Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
  492. for (int i = 0; i < normal_array.size(); i++) {
  493. normals.push_back((Vector3)normal_array[i]);
  494. }
  495. }
  496. // color
  497. if (surface_arrays_size > Mesh::ARRAY_COLOR) {
  498. Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
  499. for (int i = 0; i < color_array.size(); i++) {
  500. colors.push_back((Color)color_array[i]);
  501. }
  502. }
  503. // uv
  504. if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
  505. Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
  506. for (int j = 0; j < uv_array.size(); j++) {
  507. uvs.push_back((Vector2)uv_array[j]);
  508. }
  509. }
  510. // uv2
  511. if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
  512. Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
  513. for (int j = 0; j < uv2_array.size(); j++) {
  514. uvs2.push_back((Vector2)uv2_array[j]);
  515. }
  516. }
  517. }
  518. }
  519. _update_texture(vertices, position_texture);
  520. _update_texture(normals, normal_texture);
  521. _update_texture(colors, color_texture);
  522. _update_texture(uvs, uv_texture);
  523. _update_texture(uvs2, uv2_texture);
  524. }
  525. void VisualShaderNodeParticleMeshEmitter::set_mesh(Ref<Mesh> p_mesh) {
  526. if (mesh == p_mesh) {
  527. return;
  528. }
  529. if (mesh.is_valid()) {
  530. Callable callable = callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures);
  531. if (mesh->is_connected(CoreStringNames::get_singleton()->changed, callable)) {
  532. mesh->disconnect(CoreStringNames::get_singleton()->changed, callable);
  533. }
  534. }
  535. mesh = p_mesh;
  536. if (mesh.is_valid()) {
  537. Callable callable = callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures);
  538. if (!mesh->is_connected(CoreStringNames::get_singleton()->changed, callable)) {
  539. mesh->connect(CoreStringNames::get_singleton()->changed, callable);
  540. }
  541. }
  542. emit_changed();
  543. }
  544. Ref<Mesh> VisualShaderNodeParticleMeshEmitter::get_mesh() const {
  545. return mesh;
  546. }
  547. void VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces(bool p_enabled) {
  548. if (use_all_surfaces == p_enabled) {
  549. return;
  550. }
  551. use_all_surfaces = p_enabled;
  552. emit_changed();
  553. }
  554. bool VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces() const {
  555. return use_all_surfaces;
  556. }
  557. void VisualShaderNodeParticleMeshEmitter::set_surface_index(int p_surface_index) {
  558. if (mesh.is_valid()) {
  559. if (mesh->get_surface_count() > 0) {
  560. p_surface_index = CLAMP(p_surface_index, 0, mesh->get_surface_count() - 1);
  561. } else {
  562. p_surface_index = 0;
  563. }
  564. } else if (p_surface_index < 0) {
  565. p_surface_index = 0;
  566. }
  567. if (surface_index == p_surface_index) {
  568. return;
  569. }
  570. surface_index = p_surface_index;
  571. emit_changed();
  572. }
  573. int VisualShaderNodeParticleMeshEmitter::get_surface_index() const {
  574. return surface_index;
  575. }
  576. Vector<StringName> VisualShaderNodeParticleMeshEmitter::get_editable_properties() const {
  577. Vector<StringName> props = VisualShaderNodeParticleEmitter::get_editable_properties();
  578. props.push_back("mesh");
  579. props.push_back("use_all_surfaces");
  580. if (!use_all_surfaces) {
  581. props.push_back("surface_index");
  582. }
  583. return props;
  584. }
  585. HashMap<StringName, String> VisualShaderNodeParticleMeshEmitter::get_editable_properties_names() const {
  586. HashMap<StringName, String> names = VisualShaderNodeParticleEmitter::get_editable_properties_names();
  587. names.insert("mesh", RTR("Mesh"));
  588. names.insert("use_all_surfaces", RTR("Use All Surfaces"));
  589. if (!use_all_surfaces) {
  590. names.insert("surface_index", RTR("Surface Index"));
  591. }
  592. return names;
  593. }
  594. void VisualShaderNodeParticleMeshEmitter::_bind_methods() {
  595. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &VisualShaderNodeParticleMeshEmitter::set_mesh);
  596. ClassDB::bind_method(D_METHOD("get_mesh"), &VisualShaderNodeParticleMeshEmitter::get_mesh);
  597. ClassDB::bind_method(D_METHOD("set_use_all_surfaces", "enabled"), &VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces);
  598. ClassDB::bind_method(D_METHOD("is_use_all_surfaces"), &VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces);
  599. ClassDB::bind_method(D_METHOD("set_surface_index", "surface_index"), &VisualShaderNodeParticleMeshEmitter::set_surface_index);
  600. ClassDB::bind_method(D_METHOD("get_surface_index"), &VisualShaderNodeParticleMeshEmitter::get_surface_index);
  601. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  602. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_all_surfaces"), "set_use_all_surfaces", "is_use_all_surfaces");
  603. ADD_PROPERTY(PropertyInfo(Variant::INT, "surface_index"), "set_surface_index", "get_surface_index");
  604. }
  605. VisualShaderNodeParticleMeshEmitter::VisualShaderNodeParticleMeshEmitter() {
  606. connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
  607. position_texture.instantiate();
  608. normal_texture.instantiate();
  609. color_texture.instantiate();
  610. uv_texture.instantiate();
  611. uv2_texture.instantiate();
  612. }
  613. // VisualShaderNodeParticleMultiplyByAxisAngle
  614. void VisualShaderNodeParticleMultiplyByAxisAngle::_bind_methods() {
  615. ClassDB::bind_method(D_METHOD("set_degrees_mode", "enabled"), &VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode);
  616. ClassDB::bind_method(D_METHOD("is_degrees_mode"), &VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode);
  617. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "degrees_mode"), "set_degrees_mode", "is_degrees_mode");
  618. }
  619. String VisualShaderNodeParticleMultiplyByAxisAngle::get_caption() const {
  620. return "MultiplyByAxisAngle";
  621. }
  622. int VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_count() const {
  623. return 3;
  624. }
  625. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_type(int p_port) const {
  626. if (p_port == 0 || p_port == 1) { // position, rotation_axis
  627. return PORT_TYPE_VECTOR_3D;
  628. }
  629. return PORT_TYPE_SCALAR; // angle (degrees/radians)
  630. }
  631. String VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_name(int p_port) const {
  632. if (p_port == 0) {
  633. return "position";
  634. }
  635. if (p_port == 1) {
  636. return "axis";
  637. }
  638. if (p_port == 2) {
  639. if (degrees_mode) {
  640. return "angle (degrees)";
  641. } else {
  642. return "angle (radians)";
  643. }
  644. }
  645. return String();
  646. }
  647. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_show_prop_names() const {
  648. return true;
  649. }
  650. int VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_count() const {
  651. return 1;
  652. }
  653. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_type(int p_port) const {
  654. return PORT_TYPE_VECTOR_3D;
  655. }
  656. String VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_name(int p_port) const {
  657. return "position";
  658. }
  659. 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 {
  660. String code;
  661. if (degrees_mode) {
  662. 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";
  663. } else {
  664. 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";
  665. }
  666. return code;
  667. }
  668. void VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode(bool p_enabled) {
  669. degrees_mode = p_enabled;
  670. emit_changed();
  671. }
  672. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode() const {
  673. return degrees_mode;
  674. }
  675. Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_properties() const {
  676. Vector<StringName> props;
  677. props.push_back("degrees_mode");
  678. return props;
  679. }
  680. bool VisualShaderNodeParticleMultiplyByAxisAngle::has_output_port_preview(int p_port) const {
  681. return false;
  682. }
  683. VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() {
  684. set_input_port_default_value(1, Vector3(1, 0, 0));
  685. set_input_port_default_value(2, 0.0);
  686. }
  687. // VisualShaderNodeParticleConeVelocity
  688. String VisualShaderNodeParticleConeVelocity::get_caption() const {
  689. return "ConeVelocity";
  690. }
  691. int VisualShaderNodeParticleConeVelocity::get_input_port_count() const {
  692. return 2;
  693. }
  694. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_input_port_type(int p_port) const {
  695. if (p_port == 0) {
  696. return PORT_TYPE_VECTOR_3D;
  697. } else if (p_port == 1) {
  698. return PORT_TYPE_SCALAR;
  699. }
  700. return PORT_TYPE_SCALAR;
  701. }
  702. String VisualShaderNodeParticleConeVelocity::get_input_port_name(int p_port) const {
  703. if (p_port == 0) {
  704. return "direction";
  705. } else if (p_port == 1) {
  706. return "spread(degrees)";
  707. }
  708. return String();
  709. }
  710. int VisualShaderNodeParticleConeVelocity::get_output_port_count() const {
  711. return 1;
  712. }
  713. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_output_port_type(int p_port) const {
  714. return PORT_TYPE_VECTOR_3D;
  715. }
  716. String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) const {
  717. if (p_port == 0) {
  718. return "velocity";
  719. }
  720. return String();
  721. }
  722. bool VisualShaderNodeParticleConeVelocity::has_output_port_preview(int p_port) const {
  723. return false;
  724. }
  725. 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 {
  726. String code;
  727. code += " __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
  728. code += " __scalar_buff1 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  729. code += " __scalar_buff2 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  730. code += " __vec3_buff1 = " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + ";\n";
  731. code += " __scalar_buff1 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.x, __vec3_buff1.z) : sign(__vec3_buff1.x) * (PI / 2.0);\n";
  732. 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";
  733. code += " __vec3_buff1 = vec3(sin(__scalar_buff1), 0.0, cos(__scalar_buff1));\n";
  734. code += " __vec3_buff2 = vec3(0.0, sin(__scalar_buff2), cos(__scalar_buff2));\n";
  735. code += " __vec3_buff2.z = __vec3_buff2.z / max(0.0001, sqrt(abs(__vec3_buff2.z)));\n";
  736. code += " " + p_output_vars[0] + " = normalize(vec3(__vec3_buff1.x * __vec3_buff2.z, __vec3_buff2.y, __vec3_buff1.z * __vec3_buff2.z));\n";
  737. return code;
  738. }
  739. VisualShaderNodeParticleConeVelocity::VisualShaderNodeParticleConeVelocity() {
  740. set_input_port_default_value(0, Vector3(1, 0, 0));
  741. set_input_port_default_value(1, 45.0);
  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"), "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_MAX);
  752. }
  753. Vector<StringName> VisualShaderNodeParticleRandomness::get_editable_properties() const {
  754. Vector<StringName> props;
  755. props.push_back("op_type");
  756. return props;
  757. }
  758. String VisualShaderNodeParticleRandomness::get_caption() const {
  759. return "ParticleRandomness";
  760. }
  761. int VisualShaderNodeParticleRandomness::get_output_port_count() const {
  762. return 1;
  763. }
  764. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_output_port_type(int p_port) const {
  765. switch (op_type) {
  766. case OP_TYPE_VECTOR_2D:
  767. return PORT_TYPE_VECTOR_2D;
  768. case OP_TYPE_VECTOR_3D:
  769. return PORT_TYPE_VECTOR_3D;
  770. default:
  771. break;
  772. }
  773. return PORT_TYPE_SCALAR;
  774. }
  775. String VisualShaderNodeParticleRandomness::get_output_port_name(int p_port) const {
  776. return "random";
  777. }
  778. int VisualShaderNodeParticleRandomness::get_input_port_count() const {
  779. return 2;
  780. }
  781. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_input_port_type(int p_port) const {
  782. switch (op_type) {
  783. case OP_TYPE_VECTOR_2D:
  784. return PORT_TYPE_VECTOR_2D;
  785. case OP_TYPE_VECTOR_3D:
  786. return PORT_TYPE_VECTOR_3D;
  787. default:
  788. break;
  789. }
  790. return PORT_TYPE_SCALAR;
  791. }
  792. String VisualShaderNodeParticleRandomness::get_input_port_name(int p_port) const {
  793. if (p_port == 0) {
  794. return "min";
  795. } else if (p_port == 1) {
  796. return "max";
  797. }
  798. return String();
  799. }
  800. 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 {
  801. String code;
  802. switch (op_type) {
  803. case OP_TYPE_SCALAR: {
  804. 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]);
  805. } break;
  806. case OP_TYPE_VECTOR_2D: {
  807. 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]);
  808. } break;
  809. case OP_TYPE_VECTOR_3D: {
  810. 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]);
  811. } break;
  812. default:
  813. break;
  814. }
  815. return code;
  816. }
  817. void VisualShaderNodeParticleRandomness::set_op_type(OpType p_op_type) {
  818. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  819. if (op_type == p_op_type) {
  820. return;
  821. }
  822. switch (p_op_type) {
  823. case OP_TYPE_SCALAR: {
  824. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  825. set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
  826. } break;
  827. case OP_TYPE_VECTOR_2D: {
  828. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  829. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  830. } break;
  831. case OP_TYPE_VECTOR_3D: {
  832. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  833. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  834. } break;
  835. default:
  836. break;
  837. }
  838. op_type = p_op_type;
  839. emit_changed();
  840. }
  841. VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::get_op_type() const {
  842. return op_type;
  843. }
  844. bool VisualShaderNodeParticleRandomness::has_output_port_preview(int p_port) const {
  845. return false;
  846. }
  847. VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() {
  848. set_input_port_default_value(0, -1.0);
  849. set_input_port_default_value(1, 1.0);
  850. }
  851. // VisualShaderNodeParticleAccelerator
  852. void VisualShaderNodeParticleAccelerator::_bind_methods() {
  853. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShaderNodeParticleAccelerator::set_mode);
  854. ClassDB::bind_method(D_METHOD("get_mode"), &VisualShaderNodeParticleAccelerator::get_mode);
  855. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Linear,Radial,Tangential"), "set_mode", "get_mode");
  856. BIND_ENUM_CONSTANT(MODE_LINEAR);
  857. BIND_ENUM_CONSTANT(MODE_RADIAL)
  858. BIND_ENUM_CONSTANT(MODE_TANGENTIAL);
  859. BIND_ENUM_CONSTANT(MODE_MAX);
  860. }
  861. Vector<StringName> VisualShaderNodeParticleAccelerator::get_editable_properties() const {
  862. Vector<StringName> props;
  863. props.push_back("mode");
  864. return props;
  865. }
  866. String VisualShaderNodeParticleAccelerator::get_caption() const {
  867. return "ParticleAccelerator";
  868. }
  869. int VisualShaderNodeParticleAccelerator::get_output_port_count() const {
  870. return 1;
  871. }
  872. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_output_port_type(int p_port) const {
  873. return PORT_TYPE_VECTOR_3D;
  874. }
  875. String VisualShaderNodeParticleAccelerator::get_output_port_name(int p_port) const {
  876. return String();
  877. }
  878. int VisualShaderNodeParticleAccelerator::get_input_port_count() const {
  879. return 3;
  880. }
  881. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_input_port_type(int p_port) const {
  882. if (p_port == 0) {
  883. return PORT_TYPE_VECTOR_3D;
  884. } else if (p_port == 1) {
  885. return PORT_TYPE_SCALAR;
  886. } else if (p_port == 2) {
  887. return PORT_TYPE_VECTOR_3D;
  888. }
  889. return PORT_TYPE_SCALAR;
  890. }
  891. String VisualShaderNodeParticleAccelerator::get_input_port_name(int p_port) const {
  892. if (p_port == 0) {
  893. return "amount";
  894. } else if (p_port == 1) {
  895. return "randomness";
  896. } else if (p_port == 2) {
  897. return "axis";
  898. }
  899. return String();
  900. }
  901. 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 {
  902. String code;
  903. switch (mode) {
  904. case MODE_LINEAR:
  905. 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";
  906. break;
  907. case MODE_RADIAL:
  908. 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";
  909. break;
  910. case MODE_TANGENTIAL:
  911. code += " __vec3_buff1 = cross(__ndiff, normalize(" + (p_input_vars[2].is_empty() ? "vec3" + (String)get_input_port_default_value(2) : p_input_vars[2]) + "));\n";
  912. 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";
  913. break;
  914. default:
  915. break;
  916. }
  917. return code;
  918. }
  919. void VisualShaderNodeParticleAccelerator::set_mode(Mode p_mode) {
  920. ERR_FAIL_INDEX(int(p_mode), int(MODE_MAX));
  921. if (mode == p_mode) {
  922. return;
  923. }
  924. mode = p_mode;
  925. emit_changed();
  926. }
  927. VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::get_mode() const {
  928. return mode;
  929. }
  930. bool VisualShaderNodeParticleAccelerator::has_output_port_preview(int p_port) const {
  931. return false;
  932. }
  933. VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() {
  934. set_input_port_default_value(0, Vector3(1, 1, 1));
  935. set_input_port_default_value(1, 0.0);
  936. set_input_port_default_value(2, Vector3(0, -9.8, 0));
  937. }
  938. // VisualShaderNodeParticleOutput
  939. String VisualShaderNodeParticleOutput::get_caption() const {
  940. if (shader_type == VisualShader::TYPE_START) {
  941. return "StartOutput";
  942. } else if (shader_type == VisualShader::TYPE_PROCESS) {
  943. return "ProcessOutput";
  944. } else if (shader_type == VisualShader::TYPE_COLLIDE) {
  945. return "CollideOutput";
  946. } else if (shader_type == VisualShader::TYPE_START_CUSTOM) {
  947. return "CustomStartOutput";
  948. } else if (shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  949. return "CustomProcessOutput";
  950. }
  951. return String();
  952. }
  953. int VisualShaderNodeParticleOutput::get_input_port_count() const {
  954. if (shader_type == VisualShader::TYPE_START) {
  955. return 8;
  956. } else if (shader_type == VisualShader::TYPE_COLLIDE) {
  957. return 5;
  958. } else if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  959. return 6;
  960. } else { // TYPE_PROCESS
  961. return 7;
  962. }
  963. return 0;
  964. }
  965. VisualShaderNodeParticleOutput::PortType VisualShaderNodeParticleOutput::get_input_port_type(int p_port) const {
  966. switch (p_port) {
  967. case 0:
  968. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  969. return PORT_TYPE_VECTOR_3D; // custom.rgb
  970. }
  971. return PORT_TYPE_BOOLEAN; // active
  972. case 1:
  973. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  974. break; // custom.a (scalar)
  975. }
  976. return PORT_TYPE_VECTOR_3D; // velocity
  977. case 2:
  978. return PORT_TYPE_VECTOR_3D; // color & velocity
  979. case 3:
  980. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  981. return PORT_TYPE_VECTOR_3D; // color
  982. }
  983. break; // alpha (scalar)
  984. case 4:
  985. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  986. break; // alpha
  987. }
  988. if (shader_type == VisualShader::TYPE_PROCESS) {
  989. break; // scale
  990. }
  991. if (shader_type == VisualShader::TYPE_COLLIDE) {
  992. return PORT_TYPE_TRANSFORM; // transform
  993. }
  994. return PORT_TYPE_VECTOR_3D; // position
  995. case 5:
  996. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  997. return PORT_TYPE_TRANSFORM; // transform
  998. }
  999. if (shader_type == VisualShader::TYPE_PROCESS) {
  1000. return PORT_TYPE_VECTOR_3D; // rotation_axis
  1001. }
  1002. break; // scale (scalar)
  1003. case 6:
  1004. if (shader_type == VisualShader::TYPE_START) {
  1005. return PORT_TYPE_VECTOR_3D; // rotation_axis
  1006. }
  1007. break;
  1008. case 7:
  1009. break; // angle (scalar)
  1010. }
  1011. return PORT_TYPE_SCALAR;
  1012. }
  1013. String VisualShaderNodeParticleOutput::get_input_port_name(int p_port) const {
  1014. String port_name;
  1015. switch (p_port) {
  1016. case 0:
  1017. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1018. port_name = "custom";
  1019. break;
  1020. }
  1021. port_name = "active";
  1022. break;
  1023. case 1:
  1024. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1025. port_name = "custom_alpha";
  1026. break;
  1027. }
  1028. port_name = "velocity";
  1029. break;
  1030. case 2:
  1031. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1032. port_name = "velocity";
  1033. break;
  1034. }
  1035. port_name = "color";
  1036. break;
  1037. case 3:
  1038. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1039. port_name = "color";
  1040. break;
  1041. }
  1042. port_name = "alpha";
  1043. break;
  1044. case 4:
  1045. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1046. port_name = "alpha";
  1047. break;
  1048. }
  1049. if (shader_type == VisualShader::TYPE_PROCESS) {
  1050. port_name = "scale";
  1051. break;
  1052. }
  1053. if (shader_type == VisualShader::TYPE_COLLIDE) {
  1054. port_name = "transform";
  1055. break;
  1056. }
  1057. port_name = "position";
  1058. break;
  1059. case 5:
  1060. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1061. port_name = "transform";
  1062. break;
  1063. }
  1064. if (shader_type == VisualShader::TYPE_PROCESS) {
  1065. port_name = "rotation_axis";
  1066. break;
  1067. }
  1068. port_name = "scale";
  1069. break;
  1070. case 6:
  1071. if (shader_type == VisualShader::TYPE_PROCESS) {
  1072. port_name = "angle_in_radians";
  1073. break;
  1074. }
  1075. port_name = "rotation_axis";
  1076. break;
  1077. case 7:
  1078. port_name = "angle_in_radians";
  1079. break;
  1080. default:
  1081. break;
  1082. }
  1083. if (!port_name.is_empty()) {
  1084. return port_name.capitalize();
  1085. }
  1086. return String();
  1087. }
  1088. bool VisualShaderNodeParticleOutput::is_port_separator(int p_index) const {
  1089. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  1090. String name = get_input_port_name(p_index);
  1091. return bool(name == "Scale");
  1092. }
  1093. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1094. String name = get_input_port_name(p_index);
  1095. return bool(name == "Velocity");
  1096. }
  1097. return false;
  1098. }
  1099. 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 {
  1100. String code;
  1101. String tab = " ";
  1102. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  1103. if (!p_input_vars[0].is_empty()) { // custom.rgb
  1104. code += tab + "CUSTOM.rgb = " + p_input_vars[0] + ";\n";
  1105. }
  1106. if (!p_input_vars[1].is_empty()) { // custom.a
  1107. code += tab + "CUSTOM.a = " + p_input_vars[1] + ";\n";
  1108. }
  1109. if (!p_input_vars[2].is_empty()) { // velocity
  1110. code += tab + "VELOCITY = " + p_input_vars[2] + ";\n";
  1111. }
  1112. if (!p_input_vars[3].is_empty()) { // color.rgb
  1113. code += tab + "COLOR.rgb = " + p_input_vars[3] + ";\n";
  1114. }
  1115. if (!p_input_vars[4].is_empty()) { // color.a
  1116. code += tab + "COLOR.a = " + p_input_vars[4] + ";\n";
  1117. }
  1118. if (!p_input_vars[5].is_empty()) { // transform
  1119. code += tab + "TRANSFORM = " + p_input_vars[5] + ";\n";
  1120. }
  1121. } else {
  1122. if (!p_input_vars[0].is_empty()) { // Active (begin).
  1123. code += tab + "ACTIVE = " + p_input_vars[0] + ";\n";
  1124. code += tab + "if(ACTIVE) {\n";
  1125. tab += " ";
  1126. }
  1127. if (!p_input_vars[1].is_empty()) { // velocity
  1128. code += tab + "VELOCITY = " + p_input_vars[1] + ";\n";
  1129. }
  1130. if (!p_input_vars[2].is_empty()) { // color
  1131. code += tab + "COLOR.rgb = " + p_input_vars[2] + ";\n";
  1132. }
  1133. if (!p_input_vars[3].is_empty()) { // alpha
  1134. code += tab + "COLOR.a = " + p_input_vars[3] + ";\n";
  1135. }
  1136. // position
  1137. if (shader_type == VisualShader::TYPE_START) {
  1138. code += tab + "if (RESTART_POSITION) {\n";
  1139. if (!p_input_vars[4].is_empty()) {
  1140. 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";
  1141. } else {
  1142. 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";
  1143. }
  1144. code += tab + " if (RESTART_VELOCITY) {\n";
  1145. code += tab + " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
  1146. code += tab + " }\n";
  1147. code += tab + " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  1148. code += tab + "}\n";
  1149. } else if (shader_type == VisualShader::TYPE_COLLIDE) { // position
  1150. if (!p_input_vars[4].is_empty()) {
  1151. code += tab + "TRANSFORM = " + p_input_vars[4] + ";\n";
  1152. }
  1153. }
  1154. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  1155. int scale = 5;
  1156. int rotation_axis = 6;
  1157. int rotation = 7;
  1158. if (shader_type == VisualShader::TYPE_PROCESS) {
  1159. scale = 4;
  1160. rotation_axis = 5;
  1161. rotation = 6;
  1162. }
  1163. String op;
  1164. if (shader_type == VisualShader::TYPE_START) {
  1165. op = "*=";
  1166. } else {
  1167. op = "=";
  1168. }
  1169. if (!p_input_vars[rotation].is_empty()) { // rotation_axis & angle_in_radians
  1170. String axis;
  1171. if (p_input_vars[rotation_axis].is_empty()) {
  1172. axis = "vec3(0, 1, 0)";
  1173. } else {
  1174. axis = p_input_vars[rotation_axis];
  1175. }
  1176. code += tab + "TRANSFORM " + op + " __build_rotation_mat4(" + axis + ", " + p_input_vars[rotation] + ");\n";
  1177. }
  1178. if (!p_input_vars[scale].is_empty()) { // scale
  1179. 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";
  1180. }
  1181. }
  1182. if (!p_input_vars[0].is_empty()) { // Active (end).
  1183. code += " }\n";
  1184. }
  1185. }
  1186. return code;
  1187. }
  1188. VisualShaderNodeParticleOutput::VisualShaderNodeParticleOutput() {
  1189. }
  1190. // EmitParticle
  1191. Vector<StringName> VisualShaderNodeParticleEmit::get_editable_properties() const {
  1192. Vector<StringName> props;
  1193. props.push_back("flags");
  1194. return props;
  1195. }
  1196. void VisualShaderNodeParticleEmit::_bind_methods() {
  1197. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &VisualShaderNodeParticleEmit::set_flags);
  1198. ClassDB::bind_method(D_METHOD("get_flags"), &VisualShaderNodeParticleEmit::get_flags);
  1199. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Position,RotScale,Velocity,Color,Custom"), "set_flags", "get_flags");
  1200. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  1201. BIND_ENUM_CONSTANT(EMIT_FLAG_ROT_SCALE);
  1202. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  1203. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  1204. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  1205. }
  1206. String VisualShaderNodeParticleEmit::get_caption() const {
  1207. return "EmitParticle";
  1208. }
  1209. int VisualShaderNodeParticleEmit::get_input_port_count() const {
  1210. return 7;
  1211. }
  1212. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_input_port_type(int p_port) const {
  1213. switch (p_port) {
  1214. case 0:
  1215. return PORT_TYPE_BOOLEAN;
  1216. case 1:
  1217. return PORT_TYPE_TRANSFORM;
  1218. case 2:
  1219. return PORT_TYPE_VECTOR_3D;
  1220. case 3:
  1221. return PORT_TYPE_VECTOR_3D;
  1222. case 4:
  1223. return PORT_TYPE_SCALAR;
  1224. case 5:
  1225. return PORT_TYPE_VECTOR_3D;
  1226. case 6:
  1227. return PORT_TYPE_SCALAR;
  1228. }
  1229. return PORT_TYPE_SCALAR;
  1230. }
  1231. String VisualShaderNodeParticleEmit::get_input_port_name(int p_port) const {
  1232. switch (p_port) {
  1233. case 0:
  1234. return "condition";
  1235. case 1:
  1236. return "transform";
  1237. case 2:
  1238. return "velocity";
  1239. case 3:
  1240. return "color";
  1241. case 4:
  1242. return "alpha";
  1243. case 5:
  1244. return "custom";
  1245. case 6:
  1246. return "custom_alpha";
  1247. }
  1248. return String();
  1249. }
  1250. int VisualShaderNodeParticleEmit::get_output_port_count() const {
  1251. return 0;
  1252. }
  1253. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_output_port_type(int p_port) const {
  1254. return PORT_TYPE_SCALAR;
  1255. }
  1256. String VisualShaderNodeParticleEmit::get_output_port_name(int p_port) const {
  1257. return String();
  1258. }
  1259. void VisualShaderNodeParticleEmit::add_flag(EmitFlags p_flag) {
  1260. flags |= p_flag;
  1261. emit_changed();
  1262. }
  1263. bool VisualShaderNodeParticleEmit::has_flag(EmitFlags p_flag) const {
  1264. return flags & p_flag;
  1265. }
  1266. void VisualShaderNodeParticleEmit::set_flags(EmitFlags p_flags) {
  1267. flags = (int)p_flags;
  1268. emit_changed();
  1269. }
  1270. VisualShaderNodeParticleEmit::EmitFlags VisualShaderNodeParticleEmit::get_flags() const {
  1271. return EmitFlags(flags);
  1272. }
  1273. bool VisualShaderNodeParticleEmit::is_show_prop_names() const {
  1274. return true;
  1275. }
  1276. bool VisualShaderNodeParticleEmit::is_generate_input_var(int p_port) const {
  1277. if (p_port == 0) {
  1278. if (!is_input_port_connected(0)) {
  1279. return false;
  1280. }
  1281. }
  1282. return true;
  1283. }
  1284. bool VisualShaderNodeParticleEmit::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1285. switch (p_port) {
  1286. case 1:
  1287. return true;
  1288. case 2:
  1289. return true;
  1290. case 3:
  1291. return true;
  1292. case 4:
  1293. return true;
  1294. case 5:
  1295. return true;
  1296. case 6:
  1297. return true;
  1298. }
  1299. return false;
  1300. }
  1301. 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 {
  1302. String code;
  1303. String tab;
  1304. bool default_condition = false;
  1305. if (!is_input_port_connected(0)) {
  1306. default_condition = true;
  1307. if (get_input_port_default_value(0)) {
  1308. tab = " ";
  1309. } else {
  1310. return code;
  1311. }
  1312. } else {
  1313. tab = " ";
  1314. }
  1315. String transform;
  1316. if (p_input_vars[1].is_empty()) {
  1317. transform = "TRANSFORM";
  1318. } else {
  1319. transform = p_input_vars[1];
  1320. }
  1321. String velocity;
  1322. if (p_input_vars[2].is_empty()) {
  1323. velocity = "VELOCITY";
  1324. } else {
  1325. velocity = p_input_vars[2];
  1326. }
  1327. String color;
  1328. if (p_input_vars[3].is_empty()) {
  1329. color = "COLOR.rgb";
  1330. } else {
  1331. color = p_input_vars[3];
  1332. }
  1333. String alpha;
  1334. if (p_input_vars[4].is_empty()) {
  1335. alpha = "COLOR.a";
  1336. } else {
  1337. alpha = p_input_vars[4];
  1338. }
  1339. String custom;
  1340. if (p_input_vars[5].is_empty()) {
  1341. custom = "CUSTOM.rgb";
  1342. } else {
  1343. custom = p_input_vars[5];
  1344. }
  1345. String custom_alpha;
  1346. if (p_input_vars[6].is_empty()) {
  1347. custom_alpha = "CUSTOM.a";
  1348. } else {
  1349. custom_alpha = p_input_vars[6];
  1350. }
  1351. List<String> flags_arr;
  1352. if (has_flag(EmitFlags::EMIT_FLAG_POSITION)) {
  1353. flags_arr.push_back("FLAG_EMIT_POSITION");
  1354. }
  1355. if (has_flag(EmitFlags::EMIT_FLAG_ROT_SCALE)) {
  1356. flags_arr.push_back("FLAG_EMIT_ROT_SCALE");
  1357. }
  1358. if (has_flag(EmitFlags::EMIT_FLAG_VELOCITY)) {
  1359. flags_arr.push_back("FLAG_EMIT_VELOCITY");
  1360. }
  1361. if (has_flag(EmitFlags::EMIT_FLAG_COLOR)) {
  1362. flags_arr.push_back("FLAG_EMIT_COLOR");
  1363. }
  1364. if (has_flag(EmitFlags::EMIT_FLAG_CUSTOM)) {
  1365. flags_arr.push_back("FLAG_EMIT_CUSTOM");
  1366. }
  1367. String flags;
  1368. for (int i = 0; i < flags_arr.size(); i++) {
  1369. if (i > 0) {
  1370. flags += "|";
  1371. }
  1372. flags += flags_arr[i];
  1373. }
  1374. if (flags.is_empty()) {
  1375. flags = "uint(0)";
  1376. }
  1377. if (!default_condition) {
  1378. code += " if (" + p_input_vars[0] + ") {\n";
  1379. }
  1380. code += tab + "emit_subparticle(" + transform + ", " + velocity + ", vec4(" + color + ", " + alpha + "), vec4(" + custom + ", " + custom_alpha + "), " + flags + ");\n";
  1381. if (!default_condition) {
  1382. code += " }\n";
  1383. }
  1384. return code;
  1385. }
  1386. VisualShaderNodeParticleEmit::VisualShaderNodeParticleEmit() {
  1387. set_input_port_default_value(0, true);
  1388. }