visual_shader_particle_nodes.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. // VisualShaderNodeParticleEmitter
  32. int VisualShaderNodeParticleEmitter::get_output_port_count() const {
  33. return 1;
  34. }
  35. VisualShaderNodeParticleEmitter::PortType VisualShaderNodeParticleEmitter::get_output_port_type(int p_port) const {
  36. return PORT_TYPE_VECTOR;
  37. }
  38. String VisualShaderNodeParticleEmitter::get_output_port_name(int p_port) const {
  39. if (p_port == 0) {
  40. return "position";
  41. }
  42. return String();
  43. }
  44. VisualShaderNodeParticleEmitter::VisualShaderNodeParticleEmitter() {
  45. }
  46. // VisualShaderNodeParticleSphereEmitter
  47. String VisualShaderNodeParticleSphereEmitter::get_caption() const {
  48. return "SphereEmitter";
  49. }
  50. int VisualShaderNodeParticleSphereEmitter::get_input_port_count() const {
  51. return 2;
  52. }
  53. VisualShaderNodeParticleSphereEmitter::PortType VisualShaderNodeParticleSphereEmitter::get_input_port_type(int p_port) const {
  54. return PORT_TYPE_SCALAR;
  55. }
  56. String VisualShaderNodeParticleSphereEmitter::get_input_port_name(int p_port) const {
  57. if (p_port == 0) {
  58. return "radius";
  59. } else if (p_port == 1) {
  60. return "inner_radius";
  61. }
  62. return String();
  63. }
  64. String VisualShaderNodeParticleSphereEmitter::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  65. String code;
  66. code += "vec3 __get_random_point_in_sphere(inout uint seed, float radius, float inner_radius) {\n";
  67. code += " return __get_random_unit_vec3(seed) * __randf_range(seed, inner_radius, radius);\n";
  68. code += "}\n\n";
  69. return code;
  70. }
  71. 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 {
  72. String code;
  73. 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";
  74. return code;
  75. }
  76. VisualShaderNodeParticleSphereEmitter::VisualShaderNodeParticleSphereEmitter() {
  77. set_input_port_default_value(0, 10.0);
  78. set_input_port_default_value(1, 0.0);
  79. }
  80. // VisualShaderNodeParticleBoxEmitter
  81. String VisualShaderNodeParticleBoxEmitter::get_caption() const {
  82. return "BoxEmitter";
  83. }
  84. int VisualShaderNodeParticleBoxEmitter::get_input_port_count() const {
  85. return 1;
  86. }
  87. VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleBoxEmitter::get_input_port_type(int p_port) const {
  88. if (p_port == 0) {
  89. return PORT_TYPE_VECTOR;
  90. }
  91. return PORT_TYPE_SCALAR;
  92. }
  93. String VisualShaderNodeParticleBoxEmitter::get_input_port_name(int p_port) const {
  94. if (p_port == 0) {
  95. return "extents";
  96. }
  97. return String();
  98. }
  99. String VisualShaderNodeParticleBoxEmitter::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  100. String code;
  101. code += "vec3 __get_random_point_in_box(inout uint seed, vec3 extents) {\n";
  102. code += " vec3 half_extents = extents / 2.0;\n";
  103. 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";
  104. code += "}\n\n";
  105. return code;
  106. }
  107. 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 {
  108. String code;
  109. code += " " + p_output_vars[0] + " = __get_random_point_in_box(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ");\n";
  110. return code;
  111. }
  112. VisualShaderNodeParticleBoxEmitter::VisualShaderNodeParticleBoxEmitter() {
  113. set_input_port_default_value(0, Vector3(1.0, 1.0, 1.0));
  114. }
  115. // VisualShaderNodeParticleRingEmitter
  116. String VisualShaderNodeParticleRingEmitter::get_caption() const {
  117. return "RingEmitter";
  118. }
  119. int VisualShaderNodeParticleRingEmitter::get_input_port_count() const {
  120. return 3;
  121. }
  122. VisualShaderNodeParticleRingEmitter::PortType VisualShaderNodeParticleRingEmitter::get_input_port_type(int p_port) const {
  123. return PORT_TYPE_SCALAR;
  124. }
  125. String VisualShaderNodeParticleRingEmitter::get_input_port_name(int p_port) const {
  126. if (p_port == 0) {
  127. return "radius";
  128. } else if (p_port == 1) {
  129. return "inner_radius";
  130. } else if (p_port == 2) {
  131. return "height";
  132. }
  133. return String();
  134. }
  135. String VisualShaderNodeParticleRingEmitter::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  136. String code;
  137. code += "vec3 __get_random_point_on_ring(inout uint seed, float radius, float inner_radius, float height) {\n";
  138. code += " float angle = __rand_from_seed(seed) * PI * 2.0;\n";
  139. code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
  140. code += " return vec3(ring.x, __randf_range(seed, min(0.0, height), max(0.0, height)), ring.y);\n";
  141. code += "}\n\n";
  142. return code;
  143. }
  144. 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 {
  145. String code;
  146. code = " " + p_output_vars[0] + " = __get_random_point_on_ring(__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";
  147. return code;
  148. }
  149. VisualShaderNodeParticleRingEmitter::VisualShaderNodeParticleRingEmitter() {
  150. set_input_port_default_value(0, 10.0);
  151. set_input_port_default_value(1, 0.0);
  152. set_input_port_default_value(2, 0.0);
  153. }
  154. // VisualShaderNodeParticleMultiplyByAxisAngle
  155. void VisualShaderNodeParticleMultiplyByAxisAngle::_bind_methods() {
  156. ClassDB::bind_method(D_METHOD("set_degrees_mode", "enabled"), &VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode);
  157. ClassDB::bind_method(D_METHOD("is_degrees_mode"), &VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode);
  158. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "degrees_mode"), "set_degrees_mode", "is_degrees_mode");
  159. }
  160. String VisualShaderNodeParticleMultiplyByAxisAngle::get_caption() const {
  161. return "MultiplyByAxisAngle";
  162. }
  163. int VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_count() const {
  164. return 3;
  165. }
  166. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_type(int p_port) const {
  167. if (p_port == 0 || p_port == 1) { // position, rotation_axis
  168. return PORT_TYPE_VECTOR;
  169. }
  170. return PORT_TYPE_SCALAR; // angle (degrees/radians)
  171. }
  172. String VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_name(int p_port) const {
  173. if (p_port == 0) {
  174. return "position";
  175. }
  176. if (p_port == 1) {
  177. return "axis";
  178. }
  179. if (p_port == 2) {
  180. if (degrees_mode) {
  181. return "angle (degrees)";
  182. } else {
  183. return "angle (radians)";
  184. }
  185. }
  186. return String();
  187. }
  188. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_show_prop_names() const {
  189. return true;
  190. }
  191. int VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_count() const {
  192. return 1;
  193. }
  194. VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_type(int p_port) const {
  195. return PORT_TYPE_VECTOR;
  196. }
  197. String VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_name(int p_port) const {
  198. return "position";
  199. }
  200. 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 {
  201. String code;
  202. if (degrees_mode) {
  203. 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";
  204. } else {
  205. 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";
  206. }
  207. return code;
  208. }
  209. void VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode(bool p_enabled) {
  210. degrees_mode = p_enabled;
  211. emit_changed();
  212. }
  213. bool VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode() const {
  214. return degrees_mode;
  215. }
  216. Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_properties() const {
  217. Vector<StringName> props;
  218. props.push_back("degrees_mode");
  219. props.push_back("axis_amount");
  220. return props;
  221. }
  222. VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() {
  223. set_input_port_default_value(1, Vector3(1, 0, 0));
  224. set_input_port_default_value(2, 0.0);
  225. }
  226. // VisualShaderNodeParticleConeVelocity
  227. String VisualShaderNodeParticleConeVelocity::get_caption() const {
  228. return "ConeVelocity";
  229. }
  230. int VisualShaderNodeParticleConeVelocity::get_input_port_count() const {
  231. return 2;
  232. }
  233. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_input_port_type(int p_port) const {
  234. if (p_port == 0) {
  235. return PORT_TYPE_VECTOR;
  236. } else if (p_port == 1) {
  237. return PORT_TYPE_SCALAR;
  238. }
  239. return PORT_TYPE_SCALAR;
  240. }
  241. String VisualShaderNodeParticleConeVelocity::get_input_port_name(int p_port) const {
  242. if (p_port == 0) {
  243. return "direction";
  244. } else if (p_port == 1) {
  245. return "spread(degrees)";
  246. }
  247. return String();
  248. }
  249. int VisualShaderNodeParticleConeVelocity::get_output_port_count() const {
  250. return 1;
  251. }
  252. VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_output_port_type(int p_port) const {
  253. return PORT_TYPE_VECTOR;
  254. }
  255. String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) const {
  256. if (p_port == 0) {
  257. return "velocity";
  258. }
  259. return String();
  260. }
  261. 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 {
  262. String code;
  263. code += " __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
  264. code += " __scalar_buff1 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  265. code += " __scalar_buff2 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
  266. code += " __vec3_buff1 = " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + ";\n";
  267. code += " __scalar_buff1 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.x, __vec3_buff1.z) : sign(__vec3_buff1.x) * (PI / 2.0);\n";
  268. 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";
  269. code += " __vec3_buff1 = vec3(sin(__scalar_buff1), 0.0, cos(__scalar_buff1));\n";
  270. code += " __vec3_buff2 = vec3(0.0, sin(__scalar_buff2), cos(__scalar_buff2));\n";
  271. code += " __vec3_buff2.z = __vec3_buff2.z / max(0.0001, sqrt(abs(__vec3_buff2.z)));\n";
  272. code += " " + p_output_vars[0] + " = normalize(vec3(__vec3_buff1.x * __vec3_buff2.z, __vec3_buff2.y, __vec3_buff1.z * __vec3_buff2.z));\n";
  273. return code;
  274. }
  275. VisualShaderNodeParticleConeVelocity::VisualShaderNodeParticleConeVelocity() {
  276. set_input_port_default_value(0, Vector3(1, 0, 0));
  277. set_input_port_default_value(1, 45.0);
  278. }
  279. // VisualShaderNodeParticleRandomness
  280. void VisualShaderNodeParticleRandomness::_bind_methods() {
  281. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeParticleRandomness::set_op_type);
  282. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeParticleRandomness::get_op_type);
  283. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector"), "set_op_type", "get_op_type");
  284. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  285. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR);
  286. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  287. }
  288. Vector<StringName> VisualShaderNodeParticleRandomness::get_editable_properties() const {
  289. Vector<StringName> props;
  290. props.push_back("op_type");
  291. return props;
  292. }
  293. String VisualShaderNodeParticleRandomness::get_caption() const {
  294. return "ParticleRandomness";
  295. }
  296. int VisualShaderNodeParticleRandomness::get_output_port_count() const {
  297. return 1;
  298. }
  299. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_output_port_type(int p_port) const {
  300. if (op_type == OP_TYPE_VECTOR) {
  301. return PORT_TYPE_VECTOR;
  302. }
  303. return PORT_TYPE_SCALAR;
  304. }
  305. String VisualShaderNodeParticleRandomness::get_output_port_name(int p_port) const {
  306. return "random";
  307. }
  308. int VisualShaderNodeParticleRandomness::get_input_port_count() const {
  309. return 2;
  310. }
  311. VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_input_port_type(int p_port) const {
  312. if (op_type == OP_TYPE_VECTOR) {
  313. return PORT_TYPE_VECTOR;
  314. }
  315. return PORT_TYPE_SCALAR;
  316. }
  317. String VisualShaderNodeParticleRandomness::get_input_port_name(int p_port) const {
  318. if (p_port == 0) {
  319. return "min";
  320. } else if (p_port == 1) {
  321. return "max";
  322. }
  323. return String();
  324. }
  325. 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 {
  326. String code;
  327. if (op_type == OP_TYPE_SCALAR) {
  328. 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]);
  329. } else if (op_type == OP_TYPE_VECTOR) {
  330. code += vformat(" %s = __randv_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]);
  331. }
  332. return code;
  333. }
  334. void VisualShaderNodeParticleRandomness::set_op_type(OpType p_op_type) {
  335. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  336. if (op_type == p_op_type) {
  337. return;
  338. }
  339. if (p_op_type == OP_TYPE_SCALAR) {
  340. set_input_port_default_value(0, 0.0);
  341. set_input_port_default_value(1, 1.0);
  342. } else {
  343. set_input_port_default_value(0, Vector3(-1.0, -1.0, -1.0));
  344. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0));
  345. }
  346. op_type = p_op_type;
  347. emit_changed();
  348. }
  349. VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::get_op_type() const {
  350. return op_type;
  351. }
  352. VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() {
  353. set_input_port_default_value(0, 0.0);
  354. set_input_port_default_value(1, 1.0);
  355. }
  356. // VisualShaderNodeParticleAccelerator
  357. void VisualShaderNodeParticleAccelerator::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShaderNodeParticleAccelerator::set_mode);
  359. ClassDB::bind_method(D_METHOD("get_mode"), &VisualShaderNodeParticleAccelerator::get_mode);
  360. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Linear,Radial,Tangential"), "set_mode", "get_mode");
  361. BIND_ENUM_CONSTANT(MODE_LINEAR);
  362. BIND_ENUM_CONSTANT(MODE_RADIAL)
  363. BIND_ENUM_CONSTANT(MODE_TANGENTIAL);
  364. BIND_ENUM_CONSTANT(MODE_MAX);
  365. }
  366. Vector<StringName> VisualShaderNodeParticleAccelerator::get_editable_properties() const {
  367. Vector<StringName> props;
  368. props.push_back("mode");
  369. return props;
  370. }
  371. String VisualShaderNodeParticleAccelerator::get_caption() const {
  372. return "ParticleAccelerator";
  373. }
  374. int VisualShaderNodeParticleAccelerator::get_output_port_count() const {
  375. return 1;
  376. }
  377. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_output_port_type(int p_port) const {
  378. return PORT_TYPE_VECTOR;
  379. }
  380. String VisualShaderNodeParticleAccelerator::get_output_port_name(int p_port) const {
  381. return String();
  382. }
  383. int VisualShaderNodeParticleAccelerator::get_input_port_count() const {
  384. return 3;
  385. }
  386. VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_input_port_type(int p_port) const {
  387. if (p_port == 0) {
  388. return PORT_TYPE_VECTOR;
  389. } else if (p_port == 1) {
  390. return PORT_TYPE_SCALAR;
  391. } else if (p_port == 2) {
  392. return PORT_TYPE_VECTOR;
  393. }
  394. return PORT_TYPE_SCALAR;
  395. }
  396. String VisualShaderNodeParticleAccelerator::get_input_port_name(int p_port) const {
  397. if (p_port == 0) {
  398. return "amount";
  399. } else if (p_port == 1) {
  400. return "randomness";
  401. } else if (p_port == 2) {
  402. return "axis";
  403. }
  404. return String();
  405. }
  406. 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 {
  407. String code;
  408. switch (mode) {
  409. case MODE_LINEAR:
  410. 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";
  411. break;
  412. case MODE_RADIAL:
  413. 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";
  414. break;
  415. case MODE_TANGENTIAL:
  416. 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";
  417. 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";
  418. break;
  419. default:
  420. break;
  421. }
  422. return code;
  423. }
  424. void VisualShaderNodeParticleAccelerator::set_mode(Mode p_mode) {
  425. ERR_FAIL_INDEX(int(p_mode), int(MODE_MAX));
  426. if (mode == p_mode) {
  427. return;
  428. }
  429. mode = p_mode;
  430. emit_changed();
  431. }
  432. VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::get_mode() const {
  433. return mode;
  434. }
  435. VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() {
  436. set_input_port_default_value(0, Vector3(1, 1, 1));
  437. set_input_port_default_value(1, 0.0);
  438. set_input_port_default_value(2, Vector3(0, -9.8, 0));
  439. }
  440. // VisualShaderNodeParticleOutput
  441. String VisualShaderNodeParticleOutput::get_caption() const {
  442. if (shader_type == VisualShader::TYPE_START) {
  443. return "StartOutput";
  444. } else if (shader_type == VisualShader::TYPE_PROCESS) {
  445. return "ProcessOutput";
  446. } else if (shader_type == VisualShader::TYPE_COLLIDE) {
  447. return "CollideOutput";
  448. } else if (shader_type == VisualShader::TYPE_START_CUSTOM) {
  449. return "CustomStartOutput";
  450. } else if (shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  451. return "CustomProcessOutput";
  452. }
  453. return String();
  454. }
  455. int VisualShaderNodeParticleOutput::get_input_port_count() const {
  456. if (shader_type == VisualShader::TYPE_START) {
  457. return 8;
  458. } else if (shader_type == VisualShader::TYPE_COLLIDE) {
  459. return 5;
  460. } else if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  461. return 6;
  462. } else { // TYPE_PROCESS
  463. return 7;
  464. }
  465. return 0;
  466. }
  467. VisualShaderNodeParticleOutput::PortType VisualShaderNodeParticleOutput::get_input_port_type(int p_port) const {
  468. switch (p_port) {
  469. case 0:
  470. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  471. return PORT_TYPE_VECTOR; // custom.rgb
  472. }
  473. return PORT_TYPE_BOOLEAN; // active
  474. case 1:
  475. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  476. break; // custom.a (scalar)
  477. }
  478. return PORT_TYPE_VECTOR; // velocity
  479. case 2:
  480. return PORT_TYPE_VECTOR; // color & velocity
  481. case 3:
  482. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  483. return PORT_TYPE_VECTOR; // color
  484. }
  485. break; // alpha (scalar)
  486. case 4:
  487. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  488. break; // alpha
  489. }
  490. if (shader_type == VisualShader::TYPE_PROCESS) {
  491. break; // scale
  492. }
  493. if (shader_type == VisualShader::TYPE_COLLIDE) {
  494. return PORT_TYPE_TRANSFORM; // transform
  495. }
  496. return PORT_TYPE_VECTOR; // position
  497. case 5:
  498. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  499. return PORT_TYPE_TRANSFORM; // transform
  500. }
  501. if (shader_type == VisualShader::TYPE_PROCESS) {
  502. return PORT_TYPE_VECTOR; // rotation_axis
  503. }
  504. break; // scale (scalar)
  505. case 6:
  506. if (shader_type == VisualShader::TYPE_START) {
  507. return PORT_TYPE_VECTOR; // rotation_axis
  508. }
  509. break;
  510. case 7:
  511. break; // angle (scalar)
  512. }
  513. return PORT_TYPE_SCALAR;
  514. }
  515. String VisualShaderNodeParticleOutput::get_input_port_name(int p_port) const {
  516. String port_name;
  517. switch (p_port) {
  518. case 0:
  519. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  520. port_name = "custom";
  521. break;
  522. }
  523. port_name = "active";
  524. break;
  525. case 1:
  526. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  527. port_name = "custom_alpha";
  528. break;
  529. }
  530. port_name = "velocity";
  531. break;
  532. case 2:
  533. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  534. port_name = "velocity";
  535. break;
  536. }
  537. port_name = "color";
  538. break;
  539. case 3:
  540. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  541. port_name = "color";
  542. break;
  543. }
  544. port_name = "alpha";
  545. break;
  546. case 4:
  547. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  548. port_name = "alpha";
  549. break;
  550. }
  551. if (shader_type == VisualShader::TYPE_PROCESS) {
  552. port_name = "scale";
  553. break;
  554. }
  555. if (shader_type == VisualShader::TYPE_COLLIDE) {
  556. port_name = "transform";
  557. break;
  558. }
  559. port_name = "position";
  560. break;
  561. case 5:
  562. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  563. port_name = "transform";
  564. break;
  565. }
  566. if (shader_type == VisualShader::TYPE_PROCESS) {
  567. port_name = "rotation_axis";
  568. break;
  569. }
  570. port_name = "scale";
  571. break;
  572. case 6:
  573. if (shader_type == VisualShader::TYPE_PROCESS) {
  574. port_name = "angle_in_radians";
  575. break;
  576. }
  577. port_name = "rotation_axis";
  578. break;
  579. case 7:
  580. port_name = "angle_in_radians";
  581. break;
  582. default:
  583. break;
  584. }
  585. if (!port_name.is_empty()) {
  586. return port_name.capitalize();
  587. }
  588. return String();
  589. }
  590. bool VisualShaderNodeParticleOutput::is_port_separator(int p_index) const {
  591. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  592. String name = get_input_port_name(p_index);
  593. return bool(name == "Scale");
  594. }
  595. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  596. String name = get_input_port_name(p_index);
  597. return bool(name == "Velocity");
  598. }
  599. return false;
  600. }
  601. 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 {
  602. String code;
  603. String tab = " ";
  604. if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
  605. if (!p_input_vars[0].is_empty()) { // custom.rgb
  606. code += tab + "CUSTOM.rgb = " + p_input_vars[0] + ";\n";
  607. }
  608. if (!p_input_vars[1].is_empty()) { // custom.a
  609. code += tab + "CUSTOM.a = " + p_input_vars[1] + ";\n";
  610. }
  611. if (!p_input_vars[2].is_empty()) { // velocity
  612. code += tab + "VELOCITY = " + p_input_vars[2] + ";\n";
  613. }
  614. if (!p_input_vars[3].is_empty()) { // color.rgb
  615. code += tab + "COLOR.rgb = " + p_input_vars[3] + ";\n";
  616. }
  617. if (!p_input_vars[4].is_empty()) { // color.a
  618. code += tab + "COLOR.a = " + p_input_vars[4] + ";\n";
  619. }
  620. if (!p_input_vars[5].is_empty()) { // transform
  621. code += tab + "TRANSFORM = " + p_input_vars[5] + ";\n";
  622. }
  623. } else {
  624. if (!p_input_vars[0].is_empty()) { // active (begin)
  625. code += tab + "ACTIVE = " + p_input_vars[0] + ";\n";
  626. code += tab + "if(ACTIVE) {\n";
  627. tab += " ";
  628. }
  629. if (!p_input_vars[1].is_empty()) { // velocity
  630. code += tab + "VELOCITY = " + p_input_vars[1] + ";\n";
  631. }
  632. if (!p_input_vars[2].is_empty()) { // color
  633. code += tab + "COLOR.rgb = " + p_input_vars[2] + ";\n";
  634. }
  635. if (!p_input_vars[3].is_empty()) { // alpha
  636. code += tab + "COLOR.a = " + p_input_vars[3] + ";\n";
  637. }
  638. // position
  639. if (shader_type == VisualShader::TYPE_START) {
  640. code += tab + "if (RESTART_POSITION) {\n";
  641. if (!p_input_vars[4].is_empty()) {
  642. 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";
  643. } else {
  644. 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";
  645. }
  646. code += tab + " if (RESTART_VELOCITY) {\n";
  647. code += tab + " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
  648. code += tab + " }\n";
  649. code += tab + " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  650. code += tab + "}\n";
  651. } else if (shader_type == VisualShader::TYPE_COLLIDE) { // position
  652. if (!p_input_vars[4].is_empty()) {
  653. code += tab + "TRANSFORM = " + p_input_vars[4] + ";\n";
  654. }
  655. }
  656. if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
  657. int scale = 5;
  658. int rotation_axis = 6;
  659. int rotation = 7;
  660. if (shader_type == VisualShader::TYPE_PROCESS) {
  661. scale = 4;
  662. rotation_axis = 5;
  663. rotation = 6;
  664. }
  665. String op;
  666. if (shader_type == VisualShader::TYPE_START) {
  667. op = "*=";
  668. } else {
  669. op = "=";
  670. }
  671. if (!p_input_vars[rotation].is_empty()) { // rotation_axis & angle_in_radians
  672. String axis;
  673. if (p_input_vars[rotation_axis].is_empty()) {
  674. axis = "vec3(0, 1, 0)";
  675. } else {
  676. axis = p_input_vars[rotation_axis];
  677. }
  678. code += tab + "TRANSFORM " + op + " __build_rotation_mat4(" + axis + ", " + p_input_vars[rotation] + ");\n";
  679. }
  680. if (!p_input_vars[scale].is_empty()) { // scale
  681. 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";
  682. }
  683. }
  684. if (!p_input_vars[0].is_empty()) { // active (end)
  685. code += " }\n";
  686. }
  687. }
  688. return code;
  689. }
  690. VisualShaderNodeParticleOutput::VisualShaderNodeParticleOutput() {
  691. }
  692. // EmitParticle
  693. Vector<StringName> VisualShaderNodeParticleEmit::get_editable_properties() const {
  694. Vector<StringName> props;
  695. props.push_back("flags");
  696. return props;
  697. }
  698. void VisualShaderNodeParticleEmit::_bind_methods() {
  699. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &VisualShaderNodeParticleEmit::set_flags);
  700. ClassDB::bind_method(D_METHOD("get_flags"), &VisualShaderNodeParticleEmit::get_flags);
  701. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Position,RotScale,Velocity,Color,Custom"), "set_flags", "get_flags");
  702. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  703. BIND_ENUM_CONSTANT(EMIT_FLAG_ROT_SCALE);
  704. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  705. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  706. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  707. }
  708. String VisualShaderNodeParticleEmit::get_caption() const {
  709. return "EmitParticle";
  710. }
  711. int VisualShaderNodeParticleEmit::get_input_port_count() const {
  712. return 7;
  713. }
  714. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_input_port_type(int p_port) const {
  715. switch (p_port) {
  716. case 0:
  717. return PORT_TYPE_BOOLEAN;
  718. case 1:
  719. return PORT_TYPE_TRANSFORM;
  720. case 2:
  721. return PORT_TYPE_VECTOR;
  722. case 3:
  723. return PORT_TYPE_VECTOR;
  724. case 4:
  725. return PORT_TYPE_SCALAR;
  726. case 5:
  727. return PORT_TYPE_VECTOR;
  728. case 6:
  729. return PORT_TYPE_SCALAR;
  730. }
  731. return PORT_TYPE_SCALAR;
  732. }
  733. String VisualShaderNodeParticleEmit::get_input_port_name(int p_port) const {
  734. switch (p_port) {
  735. case 0:
  736. return "condition";
  737. case 1:
  738. return "transform";
  739. case 2:
  740. return "velocity";
  741. case 3:
  742. return "color";
  743. case 4:
  744. return "alpha";
  745. case 5:
  746. return "custom";
  747. case 6:
  748. return "custom_alpha";
  749. }
  750. return String();
  751. }
  752. int VisualShaderNodeParticleEmit::get_output_port_count() const {
  753. return 0;
  754. }
  755. VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_output_port_type(int p_port) const {
  756. return PORT_TYPE_SCALAR;
  757. }
  758. String VisualShaderNodeParticleEmit::get_output_port_name(int p_port) const {
  759. return String();
  760. }
  761. void VisualShaderNodeParticleEmit::add_flag(EmitFlags p_flag) {
  762. flags |= p_flag;
  763. emit_changed();
  764. }
  765. bool VisualShaderNodeParticleEmit::has_flag(EmitFlags p_flag) const {
  766. return flags & p_flag;
  767. }
  768. void VisualShaderNodeParticleEmit::set_flags(EmitFlags p_flags) {
  769. flags = (int)p_flags;
  770. emit_changed();
  771. }
  772. VisualShaderNodeParticleEmit::EmitFlags VisualShaderNodeParticleEmit::get_flags() const {
  773. return EmitFlags(flags);
  774. }
  775. bool VisualShaderNodeParticleEmit::is_show_prop_names() const {
  776. return true;
  777. }
  778. bool VisualShaderNodeParticleEmit::is_generate_input_var(int p_port) const {
  779. if (p_port == 0) {
  780. if (!is_input_port_connected(0)) {
  781. return false;
  782. }
  783. }
  784. return true;
  785. }
  786. String VisualShaderNodeParticleEmit::get_input_port_default_hint(int p_port) const {
  787. switch (p_port) {
  788. case 1:
  789. return "default";
  790. case 2:
  791. return "default";
  792. case 3:
  793. return "default";
  794. case 4:
  795. return "default";
  796. case 5:
  797. return "default";
  798. case 6:
  799. return "default";
  800. }
  801. return String();
  802. }
  803. 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 {
  804. String code;
  805. String tab;
  806. bool default_condition = false;
  807. if (!is_input_port_connected(0)) {
  808. default_condition = true;
  809. if (get_input_port_default_value(0)) {
  810. tab = " ";
  811. } else {
  812. return code;
  813. }
  814. } else {
  815. tab = " ";
  816. }
  817. String transform;
  818. if (p_input_vars[1].is_empty()) {
  819. transform = "TRANSFORM";
  820. } else {
  821. transform = p_input_vars[1];
  822. }
  823. String velocity;
  824. if (p_input_vars[2].is_empty()) {
  825. velocity = "VELOCITY";
  826. } else {
  827. velocity = p_input_vars[2];
  828. }
  829. String color;
  830. if (p_input_vars[3].is_empty()) {
  831. color = "COLOR.rgb";
  832. } else {
  833. color = p_input_vars[3];
  834. }
  835. String alpha;
  836. if (p_input_vars[4].is_empty()) {
  837. alpha = "COLOR.a";
  838. } else {
  839. alpha = p_input_vars[4];
  840. }
  841. String custom;
  842. if (p_input_vars[5].is_empty()) {
  843. custom = "CUSTOM.rgb";
  844. } else {
  845. custom = p_input_vars[5];
  846. }
  847. String custom_alpha;
  848. if (p_input_vars[6].is_empty()) {
  849. custom_alpha = "CUSTOM.a";
  850. } else {
  851. custom_alpha = p_input_vars[6];
  852. }
  853. List<String> flags_arr;
  854. if (has_flag(EmitFlags::EMIT_FLAG_POSITION)) {
  855. flags_arr.push_back("FLAG_EMIT_POSITION");
  856. }
  857. if (has_flag(EmitFlags::EMIT_FLAG_ROT_SCALE)) {
  858. flags_arr.push_back("FLAG_EMIT_ROT_SCALE");
  859. }
  860. if (has_flag(EmitFlags::EMIT_FLAG_VELOCITY)) {
  861. flags_arr.push_back("FLAG_EMIT_VELOCITY");
  862. }
  863. if (has_flag(EmitFlags::EMIT_FLAG_COLOR)) {
  864. flags_arr.push_back("FLAG_EMIT_COLOR");
  865. }
  866. if (has_flag(EmitFlags::EMIT_FLAG_CUSTOM)) {
  867. flags_arr.push_back("FLAG_EMIT_CUSTOM");
  868. }
  869. String flags;
  870. for (int i = 0; i < flags_arr.size(); i++) {
  871. if (i > 0) {
  872. flags += "|";
  873. }
  874. flags += flags_arr[i];
  875. }
  876. if (flags.is_empty()) {
  877. flags = "uint(0)";
  878. }
  879. if (!default_condition) {
  880. code += " if (" + p_input_vars[0] + ") {\n";
  881. }
  882. code += tab + "emit_subparticle(" + transform + ", " + velocity + ", vec4(" + color + ", " + alpha + "), vec4(" + custom + ", " + custom_alpha + "), " + flags + ");\n";
  883. if (!default_condition) {
  884. code += " }\n";
  885. }
  886. return code;
  887. }
  888. VisualShaderNodeParticleEmit::VisualShaderNodeParticleEmit() {
  889. set_input_port_default_value(0, true);
  890. }