particles.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*************************************************************************/
  2. /* particles.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "particles.h"
  31. #include "scene/resources/surface_tool.h"
  32. #include "servers/visual_server.h"
  33. Rect3 Particles::get_aabb() const {
  34. return Rect3();
  35. }
  36. PoolVector<Face3> Particles::get_faces(uint32_t p_usage_flags) const {
  37. return PoolVector<Face3>();
  38. }
  39. void Particles::set_emitting(bool p_emitting) {
  40. emitting = p_emitting;
  41. VS::get_singleton()->particles_set_emitting(particles, emitting);
  42. }
  43. void Particles::set_amount(int p_amount) {
  44. ERR_FAIL_COND(p_amount < 1);
  45. amount = p_amount;
  46. VS::get_singleton()->particles_set_amount(particles, amount);
  47. }
  48. void Particles::set_lifetime(float p_lifetime) {
  49. ERR_FAIL_COND(p_lifetime <= 0);
  50. lifetime = p_lifetime;
  51. VS::get_singleton()->particles_set_lifetime(particles, lifetime);
  52. }
  53. void Particles::set_one_shot(bool p_one_shot) {
  54. one_shot = p_one_shot;
  55. VS::get_singleton()->particles_set_one_shot(particles, one_shot);
  56. if (!one_shot && emitting)
  57. VisualServer::get_singleton()->particles_restart(particles);
  58. }
  59. void Particles::set_pre_process_time(float p_time) {
  60. pre_process_time = p_time;
  61. VS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  62. }
  63. void Particles::set_explosiveness_ratio(float p_ratio) {
  64. explosiveness_ratio = p_ratio;
  65. VS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  66. }
  67. void Particles::set_randomness_ratio(float p_ratio) {
  68. randomness_ratio = p_ratio;
  69. VS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  70. }
  71. void Particles::set_visibility_aabb(const Rect3 &p_aabb) {
  72. visibility_aabb = p_aabb;
  73. VS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
  74. update_gizmo();
  75. _change_notify("visibility_aabb");
  76. }
  77. void Particles::set_use_local_coordinates(bool p_enable) {
  78. local_coords = p_enable;
  79. VS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  80. }
  81. void Particles::set_process_material(const Ref<Material> &p_material) {
  82. process_material = p_material;
  83. RID material_rid;
  84. if (process_material.is_valid())
  85. material_rid = process_material->get_rid();
  86. VS::get_singleton()->particles_set_process_material(particles, material_rid);
  87. update_configuration_warning();
  88. }
  89. void Particles::set_speed_scale(float p_scale) {
  90. speed_scale = p_scale;
  91. VS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  92. }
  93. bool Particles::is_emitting() const {
  94. return emitting;
  95. }
  96. int Particles::get_amount() const {
  97. return amount;
  98. }
  99. float Particles::get_lifetime() const {
  100. return lifetime;
  101. }
  102. bool Particles::get_one_shot() const {
  103. return one_shot;
  104. }
  105. float Particles::get_pre_process_time() const {
  106. return pre_process_time;
  107. }
  108. float Particles::get_explosiveness_ratio() const {
  109. return explosiveness_ratio;
  110. }
  111. float Particles::get_randomness_ratio() const {
  112. return randomness_ratio;
  113. }
  114. Rect3 Particles::get_visibility_aabb() const {
  115. return visibility_aabb;
  116. }
  117. bool Particles::get_use_local_coordinates() const {
  118. return local_coords;
  119. }
  120. Ref<Material> Particles::get_process_material() const {
  121. return process_material;
  122. }
  123. float Particles::get_speed_scale() const {
  124. return speed_scale;
  125. }
  126. void Particles::set_draw_order(DrawOrder p_order) {
  127. draw_order = p_order;
  128. VS::get_singleton()->particles_set_draw_order(particles, VS::ParticlesDrawOrder(p_order));
  129. }
  130. Particles::DrawOrder Particles::get_draw_order() const {
  131. return draw_order;
  132. }
  133. void Particles::set_draw_passes(int p_count) {
  134. ERR_FAIL_COND(p_count < 1);
  135. draw_passes.resize(p_count);
  136. VS::get_singleton()->particles_set_draw_passes(particles, p_count);
  137. _change_notify();
  138. }
  139. int Particles::get_draw_passes() const {
  140. return draw_passes.size();
  141. }
  142. void Particles::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
  143. ERR_FAIL_INDEX(p_pass, draw_passes.size());
  144. draw_passes[p_pass] = p_mesh;
  145. RID mesh_rid;
  146. if (p_mesh.is_valid())
  147. mesh_rid = p_mesh->get_rid();
  148. VS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
  149. update_configuration_warning();
  150. }
  151. Ref<Mesh> Particles::get_draw_pass_mesh(int p_pass) const {
  152. ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
  153. return draw_passes[p_pass];
  154. }
  155. void Particles::set_fixed_fps(int p_count) {
  156. fixed_fps = p_count;
  157. VS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  158. }
  159. int Particles::get_fixed_fps() const {
  160. return fixed_fps;
  161. }
  162. void Particles::set_fractional_delta(bool p_enable) {
  163. fractional_delta = p_enable;
  164. VS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  165. }
  166. bool Particles::get_fractional_delta() const {
  167. return fractional_delta;
  168. }
  169. String Particles::get_configuration_warning() const {
  170. String warnings;
  171. bool meshes_found = false;
  172. for (int i = 0; i < draw_passes.size(); i++) {
  173. if (draw_passes[i].is_valid()) {
  174. meshes_found = true;
  175. break;
  176. }
  177. }
  178. if (!meshes_found) {
  179. warnings += "- " + TTR("Nothing is visible because meshes have not been assigned to draw passes.");
  180. }
  181. if (process_material.is_null()) {
  182. if (warnings != String())
  183. warnings += "\n";
  184. warnings += "- " + TTR("A material to process the particles is not assigned, so no behavior is imprinted.");
  185. }
  186. return warnings;
  187. }
  188. void Particles::restart() {
  189. VisualServer::get_singleton()->particles_restart(particles);
  190. }
  191. Rect3 Particles::capture_aabb() const {
  192. return VS::get_singleton()->particles_get_current_aabb(particles);
  193. }
  194. void Particles::_validate_property(PropertyInfo &property) const {
  195. if (property.name.begins_with("draw_pass_")) {
  196. int index = property.name.get_slicec('_', 2).to_int() - 1;
  197. if (index >= draw_passes.size()) {
  198. property.usage = 0;
  199. return;
  200. }
  201. }
  202. }
  203. void Particles::_notification(int p_what) {
  204. if (p_what == NOTIFICATION_PAUSED || p_what == NOTIFICATION_UNPAUSED) {
  205. if (can_process()) {
  206. VS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  207. } else {
  208. VS::get_singleton()->particles_set_speed_scale(particles, 0);
  209. }
  210. }
  211. }
  212. void Particles::_bind_methods() {
  213. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &Particles::set_emitting);
  214. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &Particles::set_amount);
  215. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &Particles::set_lifetime);
  216. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Particles::set_one_shot);
  217. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &Particles::set_pre_process_time);
  218. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &Particles::set_explosiveness_ratio);
  219. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &Particles::set_randomness_ratio);
  220. ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &Particles::set_visibility_aabb);
  221. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &Particles::set_use_local_coordinates);
  222. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &Particles::set_fixed_fps);
  223. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &Particles::set_fractional_delta);
  224. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &Particles::set_process_material);
  225. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &Particles::set_speed_scale);
  226. ClassDB::bind_method(D_METHOD("is_emitting"), &Particles::is_emitting);
  227. ClassDB::bind_method(D_METHOD("get_amount"), &Particles::get_amount);
  228. ClassDB::bind_method(D_METHOD("get_lifetime"), &Particles::get_lifetime);
  229. ClassDB::bind_method(D_METHOD("get_one_shot"), &Particles::get_one_shot);
  230. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &Particles::get_pre_process_time);
  231. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &Particles::get_explosiveness_ratio);
  232. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &Particles::get_randomness_ratio);
  233. ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &Particles::get_visibility_aabb);
  234. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &Particles::get_use_local_coordinates);
  235. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &Particles::get_fixed_fps);
  236. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &Particles::get_fractional_delta);
  237. ClassDB::bind_method(D_METHOD("get_process_material"), &Particles::get_process_material);
  238. ClassDB::bind_method(D_METHOD("get_speed_scale"), &Particles::get_speed_scale);
  239. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &Particles::set_draw_order);
  240. ClassDB::bind_method(D_METHOD("get_draw_order"), &Particles::get_draw_order);
  241. ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &Particles::set_draw_passes);
  242. ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &Particles::set_draw_pass_mesh);
  243. ClassDB::bind_method(D_METHOD("get_draw_passes"), &Particles::get_draw_passes);
  244. ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &Particles::get_draw_pass_mesh);
  245. ClassDB::bind_method(D_METHOD("restart"), &Particles::restart);
  246. ClassDB::bind_method(D_METHOD("capture_aabb"), &Particles::capture_aabb);
  247. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  248. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,100000,1"), "set_amount", "get_amount");
  249. ADD_GROUP("Time", "");
  250. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01"), "set_lifetime", "get_lifetime");
  251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  252. ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  253. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_speed_scale", "get_speed_scale");
  254. ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  255. ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  256. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  257. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  258. ADD_GROUP("Drawing", "");
  259. ADD_PROPERTY(PropertyInfo(Variant::RECT3, "visibility_aabb"), "set_visibility_aabb", "get_visibility_aabb");
  260. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  261. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  262. ADD_GROUP("Process Material", "");
  263. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticlesMaterial"), "set_process_material", "get_process_material");
  264. ADD_GROUP("Draw Passes", "draw_");
  265. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
  266. for (int i = 0; i < MAX_DRAW_PASSES; i++) {
  267. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
  268. }
  269. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  270. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  271. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  272. BIND_CONSTANT(MAX_DRAW_PASSES);
  273. }
  274. Particles::Particles() {
  275. particles = VS::get_singleton()->particles_create();
  276. set_base(particles);
  277. set_emitting(true);
  278. set_one_shot(false);
  279. set_amount(8);
  280. set_lifetime(1);
  281. set_fixed_fps(0);
  282. set_fractional_delta(true);
  283. set_pre_process_time(0);
  284. set_explosiveness_ratio(0);
  285. set_randomness_ratio(0);
  286. set_visibility_aabb(Rect3(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
  287. set_use_local_coordinates(true);
  288. set_draw_passes(1);
  289. set_draw_order(DRAW_ORDER_INDEX);
  290. set_speed_scale(1);
  291. }
  292. Particles::~Particles() {
  293. VS::get_singleton()->free(particles);
  294. }
  295. //////////////////////////////////////
  296. Mutex *ParticlesMaterial::material_mutex = NULL;
  297. SelfList<ParticlesMaterial>::List ParticlesMaterial::dirty_materials;
  298. Map<ParticlesMaterial::MaterialKey, ParticlesMaterial::ShaderData> ParticlesMaterial::shader_map;
  299. ParticlesMaterial::ShaderNames *ParticlesMaterial::shader_names = NULL;
  300. void ParticlesMaterial::init_shaders() {
  301. #ifndef NO_THREADS
  302. material_mutex = Mutex::create();
  303. #endif
  304. shader_names = memnew(ShaderNames);
  305. shader_names->spread = "spread";
  306. shader_names->flatness = "flatness";
  307. shader_names->initial_linear_velocity = "initial_linear_velocity";
  308. shader_names->initial_angle = "initial_angle";
  309. shader_names->angular_velocity = "angular_velocity";
  310. shader_names->orbit_velocity = "orbit_velocity";
  311. shader_names->linear_accel = "linear_accel";
  312. shader_names->radial_accel = "radial_accel";
  313. shader_names->tangent_accel = "tangent_accel";
  314. shader_names->damping = "damping";
  315. shader_names->scale = "scale";
  316. shader_names->hue_variation = "hue_variation";
  317. shader_names->anim_speed = "anim_speed";
  318. shader_names->anim_offset = "anim_offset";
  319. shader_names->initial_linear_velocity_random = "initial_linear_velocity_random";
  320. shader_names->initial_angle_random = "initial_angle_random";
  321. shader_names->angular_velocity_random = "angular_velocity_random";
  322. shader_names->orbit_velocity_random = "orbit_velocity_random";
  323. shader_names->linear_accel_random = "linear_accel_random";
  324. shader_names->radial_accel_random = "radial_accel_random";
  325. shader_names->tangent_accel_random = "tangent_accel_random";
  326. shader_names->damping_random = "damping_random";
  327. shader_names->scale_random = "scale_random";
  328. shader_names->hue_variation_random = "hue_variation_random";
  329. shader_names->anim_speed_random = "anim_speed_random";
  330. shader_names->anim_offset_random = "anim_offset_random";
  331. shader_names->angle_texture = "angle_texture";
  332. shader_names->angular_velocity_texture = "angular_velocity_texture";
  333. shader_names->orbit_velocity_texture = "orbit_velocity_texture";
  334. shader_names->linear_accel_texture = "linear_accel_texture";
  335. shader_names->radial_accel_texture = "radial_accel_texture";
  336. shader_names->tangent_accel_texture = "tangent_accel_texture";
  337. shader_names->damping_texture = "damping_texture";
  338. shader_names->scale_texture = "scale_texture";
  339. shader_names->hue_variation_texture = "hue_variation_texture";
  340. shader_names->anim_speed_texture = "anim_speed_texture";
  341. shader_names->anim_offset_texture = "anim_offset_texture";
  342. shader_names->color = "color_value";
  343. shader_names->color_ramp = "color_ramp";
  344. shader_names->emission_sphere_radius = "emission_sphere_radius";
  345. shader_names->emission_box_extents = "emission_box_extents";
  346. shader_names->emission_texture_point_count = "emission_texture_point_count";
  347. shader_names->emission_texture_points = "emission_texture_points";
  348. shader_names->emission_texture_normal = "emission_texture_normal";
  349. shader_names->emission_texture_color = "emission_texture_color";
  350. shader_names->trail_divisor = "trail_divisor";
  351. shader_names->trail_size_modifier = "trail_size_modifier";
  352. shader_names->trail_color_modifier = "trail_color_modifier";
  353. shader_names->gravity = "gravity";
  354. }
  355. void ParticlesMaterial::finish_shaders() {
  356. #ifndef NO_THREADS
  357. memdelete(material_mutex);
  358. #endif
  359. memdelete(shader_names);
  360. }
  361. void ParticlesMaterial::_update_shader() {
  362. dirty_materials.remove(&element);
  363. MaterialKey mk = _compute_key();
  364. if (mk.key == current_key.key)
  365. return; //no update required in the end
  366. if (shader_map.has(current_key)) {
  367. shader_map[current_key].users--;
  368. if (shader_map[current_key].users == 0) {
  369. //deallocate shader, as it's no longer in use
  370. VS::get_singleton()->free(shader_map[current_key].shader);
  371. shader_map.erase(current_key);
  372. }
  373. }
  374. current_key = mk;
  375. if (shader_map.has(mk)) {
  376. VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  377. shader_map[mk].users++;
  378. return;
  379. }
  380. //must create a shader!
  381. String code = "shader_type particles;\n";
  382. code += "uniform float spread;\n";
  383. code += "uniform float flatness;\n";
  384. code += "uniform float initial_linear_velocity;\n";
  385. code += "uniform float initial_angle;\n";
  386. code += "uniform float angular_velocity;\n";
  387. code += "uniform float orbit_velocity;\n";
  388. code += "uniform float linear_accel;\n";
  389. code += "uniform float radial_accel;\n";
  390. code += "uniform float tangent_accel;\n";
  391. code += "uniform float damping;\n";
  392. code += "uniform float scale;\n";
  393. code += "uniform float hue_variation;\n";
  394. code += "uniform float anim_speed;\n";
  395. code += "uniform float anim_offset;\n";
  396. code += "uniform float initial_linear_velocity_random;\n";
  397. code += "uniform float initial_angle_random;\n";
  398. code += "uniform float angular_velocity_random;\n";
  399. code += "uniform float orbit_velocity_random;\n";
  400. code += "uniform float linear_accel_random;\n";
  401. code += "uniform float radial_accel_random;\n";
  402. code += "uniform float tangent_accel_random;\n";
  403. code += "uniform float damping_random;\n";
  404. code += "uniform float scale_random;\n";
  405. code += "uniform float hue_variation_random;\n";
  406. code += "uniform float anim_speed_random;\n";
  407. code += "uniform float anim_offset_random;\n";
  408. switch (emission_shape) {
  409. case EMISSION_SHAPE_POINT: {
  410. //do none
  411. } break;
  412. case EMISSION_SHAPE_SPHERE: {
  413. code += "uniform float emission_sphere_radius;\n";
  414. } break;
  415. case EMISSION_SHAPE_BOX: {
  416. code += "uniform vec3 emission_box_extents;\n";
  417. } break;
  418. case EMISSION_SHAPE_DIRECTED_POINTS: {
  419. code += "uniform sampler2D emission_texture_normal : hint_black;\n";
  420. } //fallthrough
  421. case EMISSION_SHAPE_POINTS: {
  422. code += "uniform sampler2D emission_texture_points : hint_black;\n";
  423. code += "uniform int emission_texture_point_count;\n";
  424. if (emission_color_texture.is_valid()) {
  425. code += "uniform sampler2D emission_texture_color : hint_white;\n";
  426. }
  427. } break;
  428. }
  429. code += "uniform vec4 color_value : hint_color;\n";
  430. code += "uniform int trail_divisor;\n";
  431. code += "uniform vec3 gravity;\n";
  432. if (color_ramp.is_valid())
  433. code += "uniform sampler2D color_ramp;\n";
  434. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  435. code += "uniform sampler2D linear_velocity_texture;\n";
  436. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid())
  437. code += "uniform sampler2D orbit_velocity_texture;\n";
  438. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid())
  439. code += "uniform sampler2D angular_velocity_texture;\n";
  440. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid())
  441. code += "uniform sampler2D linear_accel_texture;\n";
  442. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid())
  443. code += "uniform sampler2D radial_accel_texture;\n";
  444. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid())
  445. code += "uniform sampler2D tangent_accel_texture;\n";
  446. if (tex_parameters[PARAM_DAMPING].is_valid())
  447. code += "uniform sampler2D damping_texture;\n";
  448. if (tex_parameters[PARAM_ANGLE].is_valid())
  449. code += "uniform sampler2D angle_texture;\n";
  450. if (tex_parameters[PARAM_SCALE].is_valid())
  451. code += "uniform sampler2D scale_texture;\n";
  452. if (tex_parameters[PARAM_HUE_VARIATION].is_valid())
  453. code += "uniform sampler2D hue_variation_texture;\n";
  454. if (tex_parameters[PARAM_ANIM_SPEED].is_valid())
  455. code += "uniform sampler2D anim_speed_texture;\n";
  456. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  457. code += "uniform sampler2D anim_offset_texture;\n";
  458. if (trail_size_modifier.is_valid()) {
  459. code += "uniform sampler2D trail_size_modifier;\n";
  460. }
  461. if (trail_color_modifier.is_valid()) {
  462. code += "uniform sampler2D trail_color_modifier;\n";
  463. }
  464. //need a random function
  465. code += "\n\n";
  466. code += "float rand_from_seed(inout uint seed) {\n";
  467. code += " int k;\n";
  468. code += " int s = int(seed);\n";
  469. code += " if (s == 0)\n";
  470. code += " s = 305420679;\n";
  471. code += " k = s / 127773;\n";
  472. code += " s = 16807 * (s - k * 127773) - 2836 * k;\n";
  473. code += " if (s < 0)\n";
  474. code += " s += 2147483647;\n";
  475. code += " seed = uint(s);\n";
  476. code += " return float(seed % uint(65536))/65535.0;\n";
  477. code += "}\n";
  478. code += "\n";
  479. //improve seed quality
  480. code += "uint hash(uint x) {\n";
  481. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  482. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  483. code += " x = (x >> uint(16)) ^ x;\n";
  484. code += " return x;\n";
  485. code += "}\n";
  486. code += "\n";
  487. code += "void vertex() {\n";
  488. code += " uint base_number = NUMBER/uint(trail_divisor);\n";
  489. code += " uint alt_seed = hash(base_number+uint(1)+RANDOM_SEED);\n";
  490. code += " float angle_rand = rand_from_seed(alt_seed);\n";
  491. code += " float scale_rand = rand_from_seed(alt_seed);\n";
  492. code += " float hue_rot_rand = rand_from_seed(alt_seed);\n";
  493. code += " float anim_offset_rand = rand_from_seed(alt_seed);\n";
  494. code += "\n";
  495. if (emission_shape >= EMISSION_SHAPE_POINTS) {
  496. code += " int point = min(emission_texture_point_count-1,int(rand_from_seed(alt_seed) * float(emission_texture_point_count)));\n";
  497. code += " ivec2 emission_tex_size = textureSize( emission_texture_points, 0 );\n";
  498. code += " ivec2 emission_tex_ofs = ivec2( point % emission_tex_size.x, point / emission_tex_size.x );\n";
  499. }
  500. code += " if (RESTART) {\n";
  501. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  502. code += " float tex_linear_velocity = textureLod(linear_velocity_texture,vec2(0.0,0.0),0.0).r;\n";
  503. else
  504. code += " float tex_linear_velocity = 0.0;\n";
  505. if (tex_parameters[PARAM_ANGLE].is_valid())
  506. code += " float tex_angle = textureLod(angle_texture,vec2(0.0,0.0),0.0).r;\n";
  507. else
  508. code += " float tex_angle = 0.0;\n";
  509. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  510. code += " float tex_anim_offset = textureLod(anim_offset_texture,vec2(0.0,0.0),0.0).r;\n";
  511. else
  512. code += " float tex_anim_offset = 0.0;\n";
  513. if (flags[FLAG_DISABLE_Z]) {
  514. code += " float angle1 = (rand_from_seed(alt_seed)*2.0-1.0)*spread/180.0*3.1416;\n";
  515. code += " vec3 rot = vec3( cos(angle1), sin(angle1),0.0 );\n";
  516. code += " VELOCITY = rot*initial_linear_velocity*mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
  517. } else {
  518. //initiate velocity spread in 3D
  519. code += " float angle1 = rand_from_seed(alt_seed)*spread*3.1416;\n";
  520. code += " float angle2 = rand_from_seed(alt_seed)*20.0*3.1416; // make it more random like\n";
  521. code += " vec3 rot_xz = vec3( sin(angle1), 0.0, cos(angle1) );\n";
  522. code += " vec3 rot = vec3( cos(angle2)*rot_xz.x,sin(angle2)*rot_xz.x, rot_xz.z);\n";
  523. code += " VELOCITY = rot*initial_linear_velocity*mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
  524. }
  525. code += " float base_angle = (initial_angle+tex_angle)*mix(1.0,angle_rand,initial_angle_random);\n";
  526. code += " CUSTOM.x = base_angle*3.1416/180.0;\n"; //angle
  527. code += " CUSTOM.y = 0.0;\n"; //phase
  528. code += " CUSTOM.z = (anim_offset+tex_anim_offset)*mix(1.0,anim_offset_rand,anim_offset_random);\n"; //animation offset (0-1)
  529. switch (emission_shape) {
  530. case EMISSION_SHAPE_POINT: {
  531. //do none
  532. } break;
  533. case EMISSION_SHAPE_SPHERE: {
  534. code += " TRANSFORM[3].xyz = normalize(vec3(rand_from_seed(alt_seed) * 2.0 - 1.0, rand_from_seed(alt_seed) * 2.0-1.0, rand_from_seed(alt_seed) * 2.0-1.0 ))*emission_sphere_radius;\n";
  535. } break;
  536. case EMISSION_SHAPE_BOX: {
  537. code += " TRANSFORM[3].xyz = vec3(rand_from_seed(alt_seed) * 2.0 - 1.0, rand_from_seed(alt_seed) * 2.0-1.0, rand_from_seed(alt_seed) * 2.0-1.0)*emission_box_extents;\n";
  538. } break;
  539. case EMISSION_SHAPE_POINTS:
  540. case EMISSION_SHAPE_DIRECTED_POINTS: {
  541. code += " TRANSFORM[3].xyz = texelFetch(emission_texture_points, emission_tex_ofs,0).xyz;\n";
  542. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  543. if (flags[FLAG_DISABLE_Z]) {
  544. code += " mat2 rotm;";
  545. code += " rotm[0] = texelFetch(emission_texture_normal, emission_tex_ofs,0).xy;\n";
  546. code += " rotm[1] = rotm[0].yx * vec2(1.0,-1.0);\n";
  547. code += " VELOCITY.xy = rotm * VELOCITY.xy;\n";
  548. } else {
  549. code += " vec3 normal = texelFetch(emission_texture_normal, emission_tex_ofs,0).xyz;\n";
  550. code += " vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0, 1.0, 0.0);\n";
  551. code += " vec3 tangent = normalize(cross(v0, normal));\n";
  552. code += " vec3 bitangent = normalize(cross(tangent, normal));\n";
  553. code += " VELOCITY = mat3(tangent,bitangent,normal) * VELOCITY;\n";
  554. }
  555. }
  556. } break;
  557. }
  558. code += " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY,0.0)).xyz;\n";
  559. code += " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  560. if (flags[FLAG_DISABLE_Z]) {
  561. code += " VELOCITY.z = 0.0;\n";
  562. code += " TRANSFORM[3].z = 0.0;\n";
  563. }
  564. code += " } else {\n";
  565. code += " CUSTOM.y += DELTA/LIFETIME;\n";
  566. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  567. code += " float tex_linear_velocity = textureLod(linear_velocity_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  568. else
  569. code += " float tex_linear_velocity = 0.0;\n";
  570. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid())
  571. code += " float tex_orbit_velocity = textureLod(orbit_velocity_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  572. else
  573. code += " float tex_orbit_velocity = 0.0;\n";
  574. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid())
  575. code += " float tex_angular_velocity = textureLod(angular_velocity_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  576. else
  577. code += " float tex_angular_velocity = 0.0;\n";
  578. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid())
  579. code += " float tex_linear_accel = textureLod(linear_accel_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  580. else
  581. code += " float tex_linear_accel = 0.0;\n";
  582. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid())
  583. code += " float tex_radial_accel = textureLod(radial_accel_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  584. else
  585. code += " float tex_radial_accel = 0.0;\n";
  586. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid())
  587. code += " float tex_tangent_accel = textureLod(tangent_accel_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  588. else
  589. code += " float tex_tangent_accel = 0.0;\n";
  590. if (tex_parameters[PARAM_DAMPING].is_valid())
  591. code += " float tex_damping = textureLod(damping_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  592. else
  593. code += " float tex_damping = 0.0;\n";
  594. if (tex_parameters[PARAM_ANGLE].is_valid())
  595. code += " float tex_angle = textureLod(angle_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  596. else
  597. code += " float tex_angle = 0.0;\n";
  598. if (tex_parameters[PARAM_ANIM_SPEED].is_valid())
  599. code += " float tex_anim_speed = textureLod(anim_speed_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  600. else
  601. code += " float tex_anim_speed = 0.0;\n";
  602. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  603. code += " float tex_anim_offset = textureLod(anim_offset_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  604. else
  605. code += " float tex_anim_offset = 0.0;\n";
  606. code += " vec3 force = gravity; \n";
  607. code += " vec3 pos = TRANSFORM[3].xyz; \n";
  608. if (flags[FLAG_DISABLE_Z]) {
  609. code += " pos.z = 0.0; \n";
  610. }
  611. code += " //apply linear acceleration\n";
  612. code += " force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * (linear_accel+tex_linear_accel)*mix(1.0,rand_from_seed(alt_seed),linear_accel_random) : vec3(0.0);\n";
  613. code += " //apply radial acceleration\n";
  614. code += " vec3 org = vec3(0.0);\n";
  615. code += " vec3 diff = pos-org;\n";
  616. code += " force += length(diff) > 0.0 ? normalize(diff) * (radial_accel+tex_radial_accel)*mix(1.0,rand_from_seed(alt_seed),radial_accel_random) : vec3(0.0);\n";
  617. code += " //apply tangential acceleration;\n";
  618. if (flags[FLAG_DISABLE_Z]) {
  619. code += " force += length(diff.yx) > 0.0 ? vec3(normalize(diff.yx * vec2(-1.0,1.0)),0.0) * ((tangent_accel+tex_tangent_accel)*mix(1.0,rand_from_seed(alt_seed),radial_accel_random)) : vec3(0.0);\n";
  620. } else {
  621. code += " vec3 crossDiff = cross(normalize(diff),normalize(gravity));\n";
  622. code += " force += length(crossDiff) > 0.0 ? normalize(crossDiff) * ((tangent_accel+tex_tangent_accel)*mix(1.0,rand_from_seed(alt_seed),radial_accel_random)) : vec3(0.0);\n";
  623. }
  624. code += " //apply attractor forces\n";
  625. code += " VELOCITY += force * DELTA;\n";
  626. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  627. code += " VELOCITY = normalize(VELOCITY)*tex_linear_velocity;\n";
  628. }
  629. code += " if (damping + tex_damping > 0.0) {\n";
  630. code += " \n";
  631. code += " float v = length(VELOCITY);\n";
  632. code += " float damp = (damping+tex_damping)*mix(1.0,rand_from_seed(alt_seed),damping_random);\n";
  633. code += " v -= damp * DELTA;\n";
  634. code += " if (v < 0.0) {\n";
  635. code += " VELOCITY = vec3(0.0);\n";
  636. code += " } else {\n";
  637. code += " VELOCITY = normalize(VELOCITY) * v;\n";
  638. code += " }\n";
  639. code += " }\n";
  640. code += " float base_angle = (initial_angle+tex_angle)*mix(1.0,angle_rand,initial_angle_random);\n";
  641. code += " base_angle += CUSTOM.y*LIFETIME*(angular_velocity+tex_angular_velocity)*mix(1.0,rand_from_seed(alt_seed)*2.0-1.0,angular_velocity_random);\n";
  642. code += " CUSTOM.x = base_angle*3.1416/180.0;\n"; //angle
  643. code += " CUSTOM.z = (anim_offset+tex_anim_offset)*mix(1.0,anim_offset_rand,anim_offset_random)+CUSTOM.y*(anim_speed+tex_anim_speed)*mix(1.0,rand_from_seed(alt_seed),anim_speed_random);\n"; //angle
  644. if (flags[FLAG_ANIM_LOOP]) {
  645. code += " CUSTOM.z = mod(CUSTOM.z,1.0);\n"; //loop
  646. } else {
  647. code += " CUSTOM.z = clamp(CUSTOM.z,0.0,1.0);\n"; //0 to 1 only
  648. }
  649. code += " }\n";
  650. //apply color
  651. //apply hue rotation
  652. if (tex_parameters[PARAM_SCALE].is_valid())
  653. code += " float tex_scale = textureLod(scale_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  654. else
  655. code += " float tex_scale = 1.0;\n";
  656. if (tex_parameters[PARAM_HUE_VARIATION].is_valid())
  657. code += " float tex_hue_variation = textureLod(hue_variation_texture,vec2(CUSTOM.y,0.0),0.0).r;\n";
  658. else
  659. code += " float tex_hue_variation = 0.0;\n";
  660. code += " float hue_rot_angle = (hue_variation+tex_hue_variation)*3.1416*2.0*mix(1.0,hue_rot_rand*2.0-1.0,hue_variation_random);\n";
  661. code += " float hue_rot_c = cos(hue_rot_angle);\n";
  662. code += " float hue_rot_s = sin(hue_rot_angle);\n";
  663. code += " mat4 hue_rot_mat = mat4( vec4(0.299, 0.587, 0.114, 0.0),\n";
  664. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  665. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  666. code += " vec4(0.000, 0.000, 0.000, 1.0)) +\n";
  667. code += " mat4( vec4(0.701, -0.587, -0.114, 0.0),\n";
  668. code += " vec4(-0.299, 0.413, -0.114, 0.0),\n";
  669. code += " vec4(-0.300, -0.588, 0.886, 0.0),\n";
  670. code += " vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_c +\n";
  671. code += " mat4( vec4(0.168, 0.330, -0.497, 0.0),\n";
  672. code += " vec4(-0.328, 0.035, 0.292, 0.0),\n";
  673. code += " vec4(1.250, -1.050, -0.203, 0.0),\n";
  674. code += " vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_s;\n";
  675. if (color_ramp.is_valid()) {
  676. code += " COLOR = textureLod(color_ramp,vec2(CUSTOM.y,0.0),0.0) * hue_rot_mat;\n";
  677. } else {
  678. code += " COLOR = color_value * hue_rot_mat;\n";
  679. }
  680. if (emission_color_texture.is_valid() && emission_shape >= EMISSION_SHAPE_POINTS) {
  681. code += " COLOR*= texelFetch(emission_texture_color,emission_tex_ofs,0);\n";
  682. }
  683. if (trail_color_modifier.is_valid()) {
  684. code += " if (trail_divisor > 1) { COLOR *= textureLod(trail_color_modifier,vec2(float(int(NUMBER)%trail_divisor)/float(trail_divisor-1),0.0),0.0); }\n";
  685. }
  686. code += "\n";
  687. if (flags[FLAG_DISABLE_Z]) {
  688. code += " TRANSFORM[0] = vec4(cos(CUSTOM.x),-sin(CUSTOM.x),0.0,0.0);\n";
  689. code += " TRANSFORM[1] = vec4(sin(CUSTOM.x),cos(CUSTOM.x),0.0,0.0);\n";
  690. code += " TRANSFORM[2] = vec4(0.0,0.0,1.0,0.0);\n";
  691. } else {
  692. //orient particle Y towards velocity
  693. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  694. code += " if (length(VELOCITY) > 0.0) { TRANSFORM[1].xyz = normalize(VELOCITY); } else { TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz); }\n";
  695. code += " if (TRANSFORM[1].xyz == normalize(TRANSFORM[0].xyz)) {\n";
  696. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
  697. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
  698. code += " } else {\n";
  699. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
  700. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
  701. code += " }\n";
  702. } else {
  703. code += " TRANSFORM[0].xyz = normalize(TRANSFORM[0].xyz);\n";
  704. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  705. code += " TRANSFORM[2].xyz = normalize(TRANSFORM[2].xyz);\n";
  706. }
  707. //turn particle by rotation in Y
  708. if (flags[FLAG_ROTATE_Y]) {
  709. code += " TRANSFORM = TRANSFORM * mat4( vec4(cos(CUSTOM.x),0.0,-sin(CUSTOM.x),0.0), vec4(0.0,1.0,0.0,0.0),vec4(sin(CUSTOM.x),0.0,cos(CUSTOM.x),0.0),vec4(0.0,0.0,0.0,1.0));\n";
  710. }
  711. }
  712. //scale by scale
  713. code += " float base_scale = mix(scale*tex_scale,1.0,scale_random*scale_rand);\n";
  714. if (trail_size_modifier.is_valid()) {
  715. code += " if (trail_divisor > 1) { base_scale *= textureLod(trail_size_modifier,vec2(float(int(NUMBER)%trail_divisor)/float(trail_divisor-1),0.0),0.0).r; } \n";
  716. }
  717. code += " TRANSFORM[0].xyz *= base_scale;\n";
  718. code += " TRANSFORM[1].xyz *= base_scale;\n";
  719. code += " TRANSFORM[2].xyz *= base_scale;\n";
  720. if (flags[FLAG_DISABLE_Z]) {
  721. code += " VELOCITY.z = 0.0;\n";
  722. code += " TRANSFORM[3].z = 0.0;\n";
  723. }
  724. code += "}\n";
  725. code += "\n";
  726. ShaderData shader_data;
  727. shader_data.shader = VS::get_singleton()->shader_create();
  728. shader_data.users = 1;
  729. VS::get_singleton()->shader_set_code(shader_data.shader, code);
  730. shader_map[mk] = shader_data;
  731. VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  732. }
  733. void ParticlesMaterial::flush_changes() {
  734. if (material_mutex)
  735. material_mutex->lock();
  736. while (dirty_materials.first()) {
  737. dirty_materials.first()->self()->_update_shader();
  738. }
  739. if (material_mutex)
  740. material_mutex->unlock();
  741. }
  742. void ParticlesMaterial::_queue_shader_change() {
  743. if (material_mutex)
  744. material_mutex->lock();
  745. if (!element.in_list()) {
  746. dirty_materials.add(&element);
  747. }
  748. if (material_mutex)
  749. material_mutex->unlock();
  750. }
  751. bool ParticlesMaterial::_is_shader_dirty() const {
  752. bool dirty = false;
  753. if (material_mutex)
  754. material_mutex->lock();
  755. dirty = element.in_list();
  756. if (material_mutex)
  757. material_mutex->unlock();
  758. return dirty;
  759. }
  760. void ParticlesMaterial::set_spread(float p_spread) {
  761. spread = p_spread;
  762. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->spread, p_spread);
  763. }
  764. float ParticlesMaterial::get_spread() const {
  765. return spread;
  766. }
  767. void ParticlesMaterial::set_flatness(float p_flatness) {
  768. flatness = p_flatness;
  769. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->flatness, p_flatness);
  770. }
  771. float ParticlesMaterial::get_flatness() const {
  772. return flatness;
  773. }
  774. void ParticlesMaterial::set_param(Parameter p_param, float p_value) {
  775. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  776. parameters[p_param] = p_value;
  777. switch (p_param) {
  778. case PARAM_INITIAL_LINEAR_VELOCITY: {
  779. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity, p_value);
  780. } break;
  781. case PARAM_ANGULAR_VELOCITY: {
  782. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity, p_value);
  783. } break;
  784. case PARAM_ORBIT_VELOCITY: {
  785. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity, p_value);
  786. } break;
  787. case PARAM_LINEAR_ACCEL: {
  788. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel, p_value);
  789. } break;
  790. case PARAM_RADIAL_ACCEL: {
  791. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel, p_value);
  792. } break;
  793. case PARAM_TANGENTIAL_ACCEL: {
  794. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel, p_value);
  795. } break;
  796. case PARAM_DAMPING: {
  797. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping, p_value);
  798. } break;
  799. case PARAM_ANGLE: {
  800. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle, p_value);
  801. } break;
  802. case PARAM_SCALE: {
  803. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale, p_value);
  804. } break;
  805. case PARAM_HUE_VARIATION: {
  806. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation, p_value);
  807. } break;
  808. case PARAM_ANIM_SPEED: {
  809. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed, p_value);
  810. } break;
  811. case PARAM_ANIM_OFFSET: {
  812. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset, p_value);
  813. } break;
  814. case PARAM_MAX: {
  815. };
  816. }
  817. }
  818. float ParticlesMaterial::get_param(Parameter p_param) const {
  819. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  820. return parameters[p_param];
  821. }
  822. void ParticlesMaterial::set_param_randomness(Parameter p_param, float p_value) {
  823. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  824. randomness[p_param] = p_value;
  825. switch (p_param) {
  826. case PARAM_INITIAL_LINEAR_VELOCITY: {
  827. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity_random, p_value);
  828. } break;
  829. case PARAM_ANGULAR_VELOCITY: {
  830. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_random, p_value);
  831. } break;
  832. case PARAM_ORBIT_VELOCITY: {
  833. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_random, p_value);
  834. } break;
  835. case PARAM_LINEAR_ACCEL: {
  836. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_random, p_value);
  837. } break;
  838. case PARAM_RADIAL_ACCEL: {
  839. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_random, p_value);
  840. } break;
  841. case PARAM_TANGENTIAL_ACCEL: {
  842. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_random, p_value);
  843. } break;
  844. case PARAM_DAMPING: {
  845. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_random, p_value);
  846. } break;
  847. case PARAM_ANGLE: {
  848. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle_random, p_value);
  849. } break;
  850. case PARAM_SCALE: {
  851. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_random, p_value);
  852. } break;
  853. case PARAM_HUE_VARIATION: {
  854. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_random, p_value);
  855. } break;
  856. case PARAM_ANIM_SPEED: {
  857. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_random, p_value);
  858. } break;
  859. case PARAM_ANIM_OFFSET: {
  860. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_random, p_value);
  861. } break;
  862. case PARAM_MAX: {
  863. };
  864. }
  865. }
  866. float ParticlesMaterial::get_param_randomness(Parameter p_param) const {
  867. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  868. return randomness[p_param];
  869. }
  870. static void _adjust_curve_range(const Ref<Texture> &p_texture, float p_min, float p_max) {
  871. Ref<CurveTexture> curve_tex = p_texture;
  872. if (!curve_tex.is_valid())
  873. return;
  874. curve_tex->ensure_default_setup(p_min, p_max);
  875. }
  876. void ParticlesMaterial::set_param_texture(Parameter p_param, const Ref<Texture> &p_texture) {
  877. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  878. tex_parameters[p_param] = p_texture;
  879. switch (p_param) {
  880. case PARAM_INITIAL_LINEAR_VELOCITY: {
  881. //do none for this one
  882. } break;
  883. case PARAM_ANGULAR_VELOCITY: {
  884. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_texture, p_texture);
  885. _adjust_curve_range(p_texture, -360, 360);
  886. } break;
  887. case PARAM_ORBIT_VELOCITY: {
  888. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_texture, p_texture);
  889. _adjust_curve_range(p_texture, -500, 500);
  890. } break;
  891. case PARAM_LINEAR_ACCEL: {
  892. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_texture, p_texture);
  893. _adjust_curve_range(p_texture, -200, 200);
  894. } break;
  895. case PARAM_RADIAL_ACCEL: {
  896. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_texture, p_texture);
  897. _adjust_curve_range(p_texture, -200, 200);
  898. } break;
  899. case PARAM_TANGENTIAL_ACCEL: {
  900. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_texture, p_texture);
  901. _adjust_curve_range(p_texture, -200, 200);
  902. } break;
  903. case PARAM_DAMPING: {
  904. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_texture, p_texture);
  905. _adjust_curve_range(p_texture, 0, 100);
  906. } break;
  907. case PARAM_ANGLE: {
  908. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angle_texture, p_texture);
  909. _adjust_curve_range(p_texture, -360, 360);
  910. } break;
  911. case PARAM_SCALE: {
  912. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_texture, p_texture);
  913. Ref<CurveTexture> curve_tex = p_texture;
  914. if (curve_tex.is_valid()) {
  915. curve_tex->ensure_default_setup();
  916. }
  917. } break;
  918. case PARAM_HUE_VARIATION: {
  919. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_texture, p_texture);
  920. _adjust_curve_range(p_texture, -1, 1);
  921. } break;
  922. case PARAM_ANIM_SPEED: {
  923. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_texture, p_texture);
  924. _adjust_curve_range(p_texture, 0, 200);
  925. } break;
  926. case PARAM_ANIM_OFFSET: {
  927. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_texture, p_texture);
  928. } break;
  929. case PARAM_MAX: {
  930. };
  931. }
  932. _queue_shader_change();
  933. }
  934. Ref<Texture> ParticlesMaterial::get_param_texture(Parameter p_param) const {
  935. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Texture>());
  936. return tex_parameters[p_param];
  937. }
  938. void ParticlesMaterial::set_color(const Color &p_color) {
  939. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->color, p_color);
  940. color = p_color;
  941. }
  942. Color ParticlesMaterial::get_color() const {
  943. return color;
  944. }
  945. void ParticlesMaterial::set_color_ramp(const Ref<Texture> &p_texture) {
  946. color_ramp = p_texture;
  947. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->color_ramp, p_texture);
  948. _queue_shader_change();
  949. _change_notify();
  950. }
  951. Ref<Texture> ParticlesMaterial::get_color_ramp() const {
  952. return color_ramp;
  953. }
  954. void ParticlesMaterial::set_flag(Flags p_flag, bool p_enable) {
  955. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  956. flags[p_flag] = p_enable;
  957. _queue_shader_change();
  958. }
  959. bool ParticlesMaterial::get_flag(Flags p_flag) const {
  960. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  961. return flags[p_flag];
  962. }
  963. void ParticlesMaterial::set_emission_shape(EmissionShape p_shape) {
  964. emission_shape = p_shape;
  965. _change_notify();
  966. _queue_shader_change();
  967. }
  968. void ParticlesMaterial::set_emission_sphere_radius(float p_radius) {
  969. emission_sphere_radius = p_radius;
  970. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_sphere_radius, p_radius);
  971. }
  972. void ParticlesMaterial::set_emission_box_extents(Vector3 p_extents) {
  973. emission_box_extents = p_extents;
  974. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_box_extents, p_extents);
  975. }
  976. void ParticlesMaterial::set_emission_point_texture(const Ref<Texture> &p_points) {
  977. emission_point_texture = p_points;
  978. RID texture;
  979. if (p_points.is_valid())
  980. texture = p_points->get_rid();
  981. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_points, texture);
  982. }
  983. void ParticlesMaterial::set_emission_normal_texture(const Ref<Texture> &p_normals) {
  984. emission_normal_texture = p_normals;
  985. RID texture;
  986. if (p_normals.is_valid())
  987. texture = p_normals->get_rid();
  988. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_normal, texture);
  989. }
  990. void ParticlesMaterial::set_emission_color_texture(const Ref<Texture> &p_colors) {
  991. emission_color_texture = p_colors;
  992. RID texture;
  993. if (p_colors.is_valid())
  994. texture = p_colors->get_rid();
  995. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_color, texture);
  996. _queue_shader_change();
  997. }
  998. void ParticlesMaterial::set_emission_point_count(int p_count) {
  999. emission_point_count = p_count;
  1000. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_point_count, p_count);
  1001. }
  1002. ParticlesMaterial::EmissionShape ParticlesMaterial::get_emission_shape() const {
  1003. return emission_shape;
  1004. }
  1005. float ParticlesMaterial::get_emission_sphere_radius() const {
  1006. return emission_sphere_radius;
  1007. }
  1008. Vector3 ParticlesMaterial::get_emission_box_extents() const {
  1009. return emission_box_extents;
  1010. }
  1011. Ref<Texture> ParticlesMaterial::get_emission_point_texture() const {
  1012. return emission_point_texture;
  1013. }
  1014. Ref<Texture> ParticlesMaterial::get_emission_normal_texture() const {
  1015. return emission_normal_texture;
  1016. }
  1017. Ref<Texture> ParticlesMaterial::get_emission_color_texture() const {
  1018. return emission_color_texture;
  1019. }
  1020. int ParticlesMaterial::get_emission_point_count() const {
  1021. return emission_point_count;
  1022. }
  1023. void ParticlesMaterial::set_trail_divisor(int p_divisor) {
  1024. trail_divisor = p_divisor;
  1025. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_divisor, p_divisor);
  1026. }
  1027. int ParticlesMaterial::get_trail_divisor() const {
  1028. return trail_divisor;
  1029. }
  1030. void ParticlesMaterial::set_trail_size_modifier(const Ref<CurveTexture> &p_trail_size_modifier) {
  1031. trail_size_modifier = p_trail_size_modifier;
  1032. Ref<CurveTexture> curve = trail_size_modifier;
  1033. if (curve.is_valid()) {
  1034. curve->ensure_default_setup();
  1035. }
  1036. RID texture;
  1037. if (p_trail_size_modifier.is_valid())
  1038. texture = p_trail_size_modifier->get_rid();
  1039. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_size_modifier, texture);
  1040. _queue_shader_change();
  1041. }
  1042. Ref<CurveTexture> ParticlesMaterial::get_trail_size_modifier() const {
  1043. return trail_size_modifier;
  1044. }
  1045. void ParticlesMaterial::set_trail_color_modifier(const Ref<GradientTexture> &p_trail_color_modifier) {
  1046. trail_color_modifier = p_trail_color_modifier;
  1047. RID texture;
  1048. if (p_trail_color_modifier.is_valid())
  1049. texture = p_trail_color_modifier->get_rid();
  1050. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_color_modifier, texture);
  1051. _queue_shader_change();
  1052. }
  1053. Ref<GradientTexture> ParticlesMaterial::get_trail_color_modifier() const {
  1054. return trail_color_modifier;
  1055. }
  1056. void ParticlesMaterial::set_gravity(const Vector3 &p_gravity) {
  1057. gravity = p_gravity;
  1058. Vector3 gset = gravity;
  1059. if (gset == Vector3()) {
  1060. gset = Vector3(0, -0.000001, 0); //as gravity is used as upvector in some calculations
  1061. }
  1062. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->gravity, gset);
  1063. }
  1064. Vector3 ParticlesMaterial::get_gravity() const {
  1065. return gravity;
  1066. }
  1067. RID ParticlesMaterial::get_shader_rid() const {
  1068. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  1069. return shader_map[current_key].shader;
  1070. }
  1071. void ParticlesMaterial::_validate_property(PropertyInfo &property) const {
  1072. if (property.name == "color" && color_ramp.is_valid()) {
  1073. property.usage = 0;
  1074. }
  1075. if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
  1076. property.usage = 0;
  1077. }
  1078. if (property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
  1079. property.usage = 0;
  1080. }
  1081. if ((property.name == "emission_point_texture" || property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  1082. property.usage = 0;
  1083. }
  1084. if (property.name == "emission_normal_texture" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  1085. property.usage = 0;
  1086. }
  1087. if (property.name == "emission_point_count" && (emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS)) {
  1088. property.usage = 0;
  1089. }
  1090. }
  1091. void ParticlesMaterial::_bind_methods() {
  1092. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &ParticlesMaterial::set_spread);
  1093. ClassDB::bind_method(D_METHOD("get_spread"), &ParticlesMaterial::get_spread);
  1094. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &ParticlesMaterial::set_flatness);
  1095. ClassDB::bind_method(D_METHOD("get_flatness"), &ParticlesMaterial::get_flatness);
  1096. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &ParticlesMaterial::set_param);
  1097. ClassDB::bind_method(D_METHOD("get_param", "param"), &ParticlesMaterial::get_param);
  1098. ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &ParticlesMaterial::set_param_randomness);
  1099. ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &ParticlesMaterial::get_param_randomness);
  1100. ClassDB::bind_method(D_METHOD("set_param_texture", "param", "texture"), &ParticlesMaterial::set_param_texture);
  1101. ClassDB::bind_method(D_METHOD("get_param_texture", "param"), &ParticlesMaterial::get_param_texture);
  1102. ClassDB::bind_method(D_METHOD("set_color", "color"), &ParticlesMaterial::set_color);
  1103. ClassDB::bind_method(D_METHOD("get_color"), &ParticlesMaterial::get_color);
  1104. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &ParticlesMaterial::set_color_ramp);
  1105. ClassDB::bind_method(D_METHOD("get_color_ramp"), &ParticlesMaterial::get_color_ramp);
  1106. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enable"), &ParticlesMaterial::set_flag);
  1107. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &ParticlesMaterial::get_flag);
  1108. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &ParticlesMaterial::set_emission_shape);
  1109. ClassDB::bind_method(D_METHOD("get_emission_shape"), &ParticlesMaterial::get_emission_shape);
  1110. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &ParticlesMaterial::set_emission_sphere_radius);
  1111. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &ParticlesMaterial::get_emission_sphere_radius);
  1112. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &ParticlesMaterial::set_emission_box_extents);
  1113. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &ParticlesMaterial::get_emission_box_extents);
  1114. ClassDB::bind_method(D_METHOD("set_emission_point_texture", "texture"), &ParticlesMaterial::set_emission_point_texture);
  1115. ClassDB::bind_method(D_METHOD("get_emission_point_texture"), &ParticlesMaterial::get_emission_point_texture);
  1116. ClassDB::bind_method(D_METHOD("set_emission_normal_texture", "texture"), &ParticlesMaterial::set_emission_normal_texture);
  1117. ClassDB::bind_method(D_METHOD("get_emission_normal_texture"), &ParticlesMaterial::get_emission_normal_texture);
  1118. ClassDB::bind_method(D_METHOD("set_emission_color_texture", "texture"), &ParticlesMaterial::set_emission_color_texture);
  1119. ClassDB::bind_method(D_METHOD("get_emission_color_texture"), &ParticlesMaterial::get_emission_color_texture);
  1120. ClassDB::bind_method(D_METHOD("set_emission_point_count", "point_count"), &ParticlesMaterial::set_emission_point_count);
  1121. ClassDB::bind_method(D_METHOD("get_emission_point_count"), &ParticlesMaterial::get_emission_point_count);
  1122. ClassDB::bind_method(D_METHOD("set_trail_divisor", "divisor"), &ParticlesMaterial::set_trail_divisor);
  1123. ClassDB::bind_method(D_METHOD("get_trail_divisor"), &ParticlesMaterial::get_trail_divisor);
  1124. ClassDB::bind_method(D_METHOD("set_trail_size_modifier", "texture"), &ParticlesMaterial::set_trail_size_modifier);
  1125. ClassDB::bind_method(D_METHOD("get_trail_size_modifier"), &ParticlesMaterial::get_trail_size_modifier);
  1126. ClassDB::bind_method(D_METHOD("set_trail_color_modifier", "texture"), &ParticlesMaterial::set_trail_color_modifier);
  1127. ClassDB::bind_method(D_METHOD("get_trail_color_modifier"), &ParticlesMaterial::get_trail_color_modifier);
  1128. ClassDB::bind_method(D_METHOD("get_gravity"), &ParticlesMaterial::get_gravity);
  1129. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &ParticlesMaterial::set_gravity);
  1130. ADD_GROUP("Trail", "trail_");
  1131. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_divisor", PROPERTY_HINT_RANGE, "1,1000000,1"), "set_trail_divisor", "get_trail_divisor");
  1132. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trail_size_modifier", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_trail_size_modifier", "get_trail_size_modifier");
  1133. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trail_color_modifier", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_trail_color_modifier", "get_trail_color_modifier");
  1134. ADD_GROUP("Emission Shape", "emission_");
  1135. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");
  1136. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1137. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  1138. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_point_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_point_texture", "get_emission_point_texture");
  1139. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_normal_texture", "get_emission_normal_texture");
  1140. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_color_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_color_texture", "get_emission_color_texture");
  1141. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_point_count", PROPERTY_HINT_RANGE, "0,1000000,1"), "set_emission_point_count", "get_emission_point_count");
  1142. ADD_GROUP("Flags", "flag_");
  1143. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_flag", "get_flag", FLAG_ALIGN_Y_TO_VELOCITY);
  1144. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_rotate_y"), "set_flag", "get_flag", FLAG_ROTATE_Y);
  1145. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_disable_z"), "set_flag", "get_flag", FLAG_DISABLE_Z);
  1146. ADD_GROUP("Spread", "");
  1147. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1148. ADD_PROPERTY(PropertyInfo(Variant::REAL, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
  1149. ADD_GROUP("Gravity", "");
  1150. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  1151. ADD_GROUP("Initial Velocity", "initial_");
  1152. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
  1153. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_INITIAL_LINEAR_VELOCITY);
  1154. ADD_GROUP("Angular Velocity", "angular_");
  1155. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity", PROPERTY_HINT_RANGE, "-360,360,0.01"), "set_param", "get_param", PARAM_ANGULAR_VELOCITY);
  1156. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
  1157. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGULAR_VELOCITY);
  1158. ADD_GROUP("Orbit Velocity", "orbit_");
  1159. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity", PROPERTY_HINT_RANGE, "-1000,1000,0.01"), "set_param", "get_param", PARAM_ORBIT_VELOCITY);
  1160. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
  1161. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ORBIT_VELOCITY);
  1162. ADD_GROUP("Linear Accel", "linear_");
  1163. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel", PROPERTY_HINT_RANGE, "-100,100,0.01"), "set_param", "get_param", PARAM_LINEAR_ACCEL);
  1164. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
  1165. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_LINEAR_ACCEL);
  1166. ADD_GROUP("Radial Accel", "radial_");
  1167. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel", PROPERTY_HINT_RANGE, "-100,100,0.01"), "set_param", "get_param", PARAM_RADIAL_ACCEL);
  1168. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
  1169. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_RADIAL_ACCEL);
  1170. ADD_GROUP("Tangential Accel", "tangential_");
  1171. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel", PROPERTY_HINT_RANGE, "-100,100,0.01"), "set_param", "get_param", PARAM_TANGENTIAL_ACCEL);
  1172. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
  1173. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_TANGENTIAL_ACCEL);
  1174. ADD_GROUP("Damping", "");
  1175. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_param", "get_param", PARAM_DAMPING);
  1176. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
  1177. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_DAMPING);
  1178. ADD_GROUP("Angle", "");
  1179. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1"), "set_param", "get_param", PARAM_ANGLE);
  1180. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
  1181. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGLE);
  1182. ADD_GROUP("Scale", "");
  1183. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale", PROPERTY_HINT_RANGE, "0,1000,0.01"), "set_param", "get_param", PARAM_SCALE);
  1184. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
  1185. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_SCALE);
  1186. ADD_GROUP("Color", "");
  1187. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1188. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_color_ramp", "get_color_ramp");
  1189. ADD_GROUP("Hue Variation", "hue_");
  1190. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.1"), "set_param", "get_param", PARAM_HUE_VARIATION);
  1191. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
  1192. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_HUE_VARIATION);
  1193. ADD_GROUP("Animation", "anim_");
  1194. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_param", "get_param", PARAM_ANIM_SPEED);
  1195. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
  1196. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_SPEED);
  1197. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
  1198. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
  1199. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_OFFSET);
  1200. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "anim_loop"), "set_flag", "get_flag", FLAG_ANIM_LOOP);
  1201. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1202. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1203. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1204. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1205. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1206. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1207. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1208. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1209. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1210. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1211. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1212. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1213. BIND_ENUM_CONSTANT(PARAM_MAX);
  1214. BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
  1215. BIND_ENUM_CONSTANT(FLAG_ROTATE_Y);
  1216. BIND_ENUM_CONSTANT(FLAG_MAX);
  1217. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1218. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1219. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  1220. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1221. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1222. }
  1223. ParticlesMaterial::ParticlesMaterial()
  1224. : element(this) {
  1225. set_spread(45);
  1226. set_flatness(0);
  1227. set_param(PARAM_INITIAL_LINEAR_VELOCITY, 1);
  1228. set_param(PARAM_ORBIT_VELOCITY, 0);
  1229. set_param(PARAM_LINEAR_ACCEL, 0);
  1230. set_param(PARAM_RADIAL_ACCEL, 0);
  1231. set_param(PARAM_TANGENTIAL_ACCEL, 0);
  1232. set_param(PARAM_DAMPING, 0);
  1233. set_param(PARAM_ANGLE, 0);
  1234. set_param(PARAM_SCALE, 1);
  1235. set_param(PARAM_HUE_VARIATION, 0);
  1236. set_param(PARAM_ANIM_SPEED, 0);
  1237. set_param(PARAM_ANIM_OFFSET, 0);
  1238. set_emission_shape(EMISSION_SHAPE_POINT);
  1239. set_emission_sphere_radius(1);
  1240. set_emission_box_extents(Vector3(1, 1, 1));
  1241. set_trail_divisor(1);
  1242. set_gravity(Vector3(0, -9.8, 0));
  1243. emission_point_count = 1;
  1244. for (int i = 0; i < PARAM_MAX; i++) {
  1245. set_param_randomness(Parameter(i), 0);
  1246. }
  1247. for (int i = 0; i < FLAG_MAX; i++) {
  1248. flags[i] = false;
  1249. }
  1250. set_color(Color(1, 1, 1, 1));
  1251. current_key.key = 0;
  1252. current_key.invalid_key = 1;
  1253. _queue_shader_change();
  1254. }
  1255. ParticlesMaterial::~ParticlesMaterial() {
  1256. }