particle_process_material.cpp 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  1. /**************************************************************************/
  2. /* particle_process_material.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "particle_process_material.h"
  31. #include "core/version.h"
  32. Mutex ParticleProcessMaterial::dirty_materials_mutex;
  33. SelfList<ParticleProcessMaterial>::List ParticleProcessMaterial::dirty_materials;
  34. Mutex ParticleProcessMaterial::shader_map_mutex;
  35. HashMap<ParticleProcessMaterial::MaterialKey, ParticleProcessMaterial::ShaderData, ParticleProcessMaterial::MaterialKey> ParticleProcessMaterial::shader_map;
  36. RBSet<String> ParticleProcessMaterial::min_max_properties;
  37. ParticleProcessMaterial::ShaderNames *ParticleProcessMaterial::shader_names = nullptr;
  38. void ParticleProcessMaterial::init_shaders() {
  39. shader_names = memnew(ShaderNames);
  40. shader_names->direction = "direction";
  41. shader_names->spread = "spread";
  42. shader_names->flatness = "flatness";
  43. shader_names->initial_linear_velocity_min = "initial_linear_velocity_min";
  44. shader_names->initial_angle_min = "initial_angle_min";
  45. shader_names->angular_velocity_min = "angular_velocity_min";
  46. shader_names->orbit_velocity_min = "orbit_velocity_min";
  47. shader_names->radial_velocity_min = "radial_velocity_min";
  48. shader_names->linear_accel_min = "linear_accel_min";
  49. shader_names->radial_accel_min = "radial_accel_min";
  50. shader_names->tangent_accel_min = "tangent_accel_min";
  51. shader_names->damping_min = "damping_min";
  52. shader_names->scale_min = "scale_min";
  53. shader_names->hue_variation_min = "hue_variation_min";
  54. shader_names->anim_speed_min = "anim_speed_min";
  55. shader_names->anim_offset_min = "anim_offset_min";
  56. shader_names->directional_velocity_min = "directional_velocity_min";
  57. shader_names->scale_over_velocity_min = "scale_over_velocity_min";
  58. shader_names->initial_linear_velocity_max = "initial_linear_velocity_max";
  59. shader_names->initial_angle_max = "initial_angle_max";
  60. shader_names->angular_velocity_max = "angular_velocity_max";
  61. shader_names->orbit_velocity_max = "orbit_velocity_max";
  62. shader_names->radial_velocity_max = "radial_velocity_max";
  63. shader_names->linear_accel_max = "linear_accel_max";
  64. shader_names->radial_accel_max = "radial_accel_max";
  65. shader_names->tangent_accel_max = "tangent_accel_max";
  66. shader_names->damping_max = "damping_max";
  67. shader_names->scale_max = "scale_max";
  68. shader_names->hue_variation_max = "hue_variation_max";
  69. shader_names->anim_speed_max = "anim_speed_max";
  70. shader_names->anim_offset_max = "anim_offset_max";
  71. shader_names->directional_velocity_max = "directional_velocity_max";
  72. shader_names->scale_over_velocity_max = "scale_over_velocity_max";
  73. shader_names->angle_texture = "angle_texture";
  74. shader_names->angular_velocity_texture = "angular_velocity_texture";
  75. shader_names->orbit_velocity_texture = "orbit_velocity_curve";
  76. shader_names->radial_velocity_texture = "radial_velocity_curve";
  77. shader_names->linear_accel_texture = "linear_accel_texture";
  78. shader_names->radial_accel_texture = "radial_accel_texture";
  79. shader_names->tangent_accel_texture = "tangent_accel_texture";
  80. shader_names->damping_texture = "damping_texture";
  81. shader_names->scale_texture = "scale_curve";
  82. shader_names->hue_variation_texture = "hue_rot_curve";
  83. shader_names->anim_speed_texture = "animation_speed_curve";
  84. shader_names->anim_offset_texture = "animation_offset_curve";
  85. shader_names->directional_velocity_texture = "directional_velocity_curve";
  86. shader_names->scale_over_velocity_texture = "scale_over_velocity_curve";
  87. shader_names->color = "color_value";
  88. shader_names->color_ramp = "color_ramp";
  89. shader_names->alpha_ramp = "alpha_curve";
  90. shader_names->emission_ramp = "emission_curve";
  91. shader_names->color_initial_ramp = "color_initial_ramp";
  92. shader_names->velocity_limit_curve = "velocity_limit_curve";
  93. shader_names->inherit_emitter_velocity_ratio = "inherit_emitter_velocity_ratio";
  94. shader_names->velocity_pivot = "velocity_pivot";
  95. shader_names->emission_sphere_radius = "emission_sphere_radius";
  96. shader_names->emission_box_extents = "emission_box_extents";
  97. shader_names->emission_texture_point_count = "emission_texture_point_count";
  98. shader_names->emission_texture_points = "emission_texture_points";
  99. shader_names->emission_texture_normal = "emission_texture_normal";
  100. shader_names->emission_texture_color = "emission_texture_color";
  101. shader_names->emission_ring_axis = "emission_ring_axis";
  102. shader_names->emission_ring_height = "emission_ring_height";
  103. shader_names->emission_ring_radius = "emission_ring_radius";
  104. shader_names->emission_ring_inner_radius = "emission_ring_inner_radius";
  105. shader_names->emission_ring_cone_angle = "emission_ring_cone_angle";
  106. shader_names->emission_shape_offset = "emission_shape_offset";
  107. shader_names->emission_shape_scale = "emission_shape_scale";
  108. shader_names->turbulence_enabled = "turbulence_enabled";
  109. shader_names->turbulence_noise_strength = "turbulence_noise_strength";
  110. shader_names->turbulence_noise_scale = "turbulence_noise_scale";
  111. shader_names->turbulence_noise_speed = "turbulence_noise_speed";
  112. shader_names->turbulence_noise_speed_random = "turbulence_noise_speed_random";
  113. shader_names->turbulence_influence_over_life = "turbulence_influence_over_life";
  114. shader_names->turbulence_influence_min = "turbulence_influence_min";
  115. shader_names->turbulence_influence_max = "turbulence_influence_max";
  116. shader_names->turbulence_initial_displacement_min = "turbulence_initial_displacement_min";
  117. shader_names->turbulence_initial_displacement_max = "turbulence_initial_displacement_max";
  118. shader_names->gravity = "gravity";
  119. shader_names->lifetime_randomness = "lifetime_randomness";
  120. shader_names->sub_emitter_frequency = "sub_emitter_frequency";
  121. shader_names->sub_emitter_amount_at_end = "sub_emitter_amount_at_end";
  122. shader_names->sub_emitter_amount_at_collision = "sub_emitter_amount_at_collision";
  123. shader_names->sub_emitter_amount_at_start = "sub_emitter_amount_at_start";
  124. shader_names->sub_emitter_keep_velocity = "sub_emitter_keep_velocity";
  125. shader_names->collision_friction = "collision_friction";
  126. shader_names->collision_bounce = "collision_bounce";
  127. }
  128. void ParticleProcessMaterial::finish_shaders() {
  129. dirty_materials.clear();
  130. memdelete(shader_names);
  131. shader_names = nullptr;
  132. }
  133. void ParticleProcessMaterial::_update_shader() {
  134. if (!_is_initialized()) {
  135. _mark_ready();
  136. }
  137. MaterialKey mk = _compute_key();
  138. if (mk == current_key) {
  139. return; // No update required in the end.
  140. }
  141. {
  142. MutexLock lock(shader_map_mutex);
  143. ShaderData *v = shader_map.getptr(current_key);
  144. if (v) {
  145. v->users--;
  146. if (v->users == 0) {
  147. // Deallocate shader, as it's no longer in use.
  148. RS::get_singleton()->free(v->shader);
  149. shader_map.erase(current_key);
  150. shader_rid = RID();
  151. }
  152. }
  153. current_key = mk;
  154. v = shader_map.getptr(mk);
  155. if (v) {
  156. shader_rid = v->shader;
  157. RS::get_singleton()->material_set_shader(_get_material(), shader_rid);
  158. v->users++;
  159. return;
  160. }
  161. }
  162. // No pre-existing shader, create one.
  163. // Add a comment to describe the shader origin (useful when converting to ShaderMaterial).
  164. String code = "// NOTE: Shader automatically converted from " GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG "'s ParticleProcessMaterial.\n\n";
  165. code += "shader_type particles;\n";
  166. code += "render_mode disable_velocity;\n";
  167. if (collision_scale) {
  168. code += "render_mode collision_use_scale;\n";
  169. }
  170. code += "\n";
  171. // Define uniforms.
  172. code += "uniform vec3 direction;\n";
  173. code += "uniform float spread;\n";
  174. code += "uniform float flatness;\n";
  175. code += "uniform float inherit_emitter_velocity_ratio = 0.0;\n";
  176. code += "uniform float initial_linear_velocity_min;\n";
  177. code += "uniform float initial_linear_velocity_max;\n";
  178. code += "uniform float directional_velocity_min;\n";
  179. code += "uniform float directional_velocity_max;\n";
  180. code += "uniform float angular_velocity_min;\n";
  181. code += "uniform float angular_velocity_max;\n";
  182. code += "uniform float orbit_velocity_min;\n";
  183. code += "uniform float orbit_velocity_max;\n";
  184. code += "uniform float radial_velocity_min;\n";
  185. code += "uniform float radial_velocity_max;\n";
  186. code += "uniform float linear_accel_min;\n";
  187. code += "uniform float linear_accel_max;\n";
  188. code += "uniform float radial_accel_min;\n";
  189. code += "uniform float radial_accel_max;\n";
  190. code += "uniform float tangent_accel_min;\n";
  191. code += "uniform float tangent_accel_max;\n";
  192. code += "uniform float damping_min;\n";
  193. code += "uniform float damping_max;\n";
  194. code += "uniform float initial_angle_min;\n";
  195. code += "uniform float initial_angle_max;\n";
  196. code += "uniform float scale_min;\n";
  197. code += "uniform float scale_max;\n";
  198. code += "uniform float hue_variation_min;\n";
  199. code += "uniform float hue_variation_max;\n";
  200. code += "uniform float anim_speed_min;\n";
  201. code += "uniform float anim_speed_max;\n";
  202. code += "uniform float anim_offset_min;\n";
  203. code += "uniform float anim_offset_max;\n";
  204. code += "uniform float lifetime_randomness;\n";
  205. code += "uniform vec3 emission_shape_offset = vec3(0.0);\n";
  206. code += "uniform vec3 emission_shape_scale = vec3(1.0);\n";
  207. code += "uniform vec3 velocity_pivot = vec3(0.0);\n";
  208. if (tex_parameters[PARAM_SCALE_OVER_VELOCITY].is_valid()) {
  209. code += "uniform float scale_over_velocity_min = 0.0;\n";
  210. code += "uniform float scale_over_velocity_max = 5.0;\n";
  211. }
  212. switch (emission_shape) {
  213. case EMISSION_SHAPE_POINT: {
  214. //do none
  215. } break;
  216. case EMISSION_SHAPE_SPHERE: {
  217. code += "uniform float emission_sphere_radius;\n";
  218. } break;
  219. case EMISSION_SHAPE_SPHERE_SURFACE: {
  220. code += "uniform float emission_sphere_radius;\n";
  221. } break;
  222. case EMISSION_SHAPE_BOX: {
  223. code += "uniform vec3 emission_box_extents;\n";
  224. } break;
  225. case EMISSION_SHAPE_DIRECTED_POINTS: {
  226. code += "uniform sampler2D emission_texture_normal : hint_default_black;\n";
  227. [[fallthrough]];
  228. }
  229. case EMISSION_SHAPE_POINTS: {
  230. code += "uniform sampler2D emission_texture_points : hint_default_black;\n";
  231. code += "uniform int emission_texture_point_count;\n";
  232. if (emission_color_texture.is_valid()) {
  233. code += "uniform sampler2D emission_texture_color : hint_default_white;\n";
  234. }
  235. } break;
  236. case EMISSION_SHAPE_RING: {
  237. code += "uniform vec3 " + shader_names->emission_ring_axis + ";\n";
  238. code += "uniform float " + shader_names->emission_ring_height + ";\n";
  239. code += "uniform float " + shader_names->emission_ring_radius + ";\n";
  240. code += "uniform float " + shader_names->emission_ring_inner_radius + ";\n";
  241. code += "uniform float " + shader_names->emission_ring_cone_angle + ";\n";
  242. } break;
  243. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  244. break;
  245. }
  246. }
  247. if (sub_emitter_mode != SUB_EMITTER_DISABLED && !RenderingServer::get_singleton()->is_low_end()) {
  248. if (sub_emitter_mode == SUB_EMITTER_CONSTANT) {
  249. code += "uniform float sub_emitter_frequency;\n";
  250. }
  251. if (sub_emitter_mode == SUB_EMITTER_AT_END) {
  252. code += "uniform int sub_emitter_amount_at_end;\n";
  253. }
  254. if (sub_emitter_mode == SUB_EMITTER_AT_COLLISION) {
  255. code += "uniform int sub_emitter_amount_at_collision;\n";
  256. }
  257. if (sub_emitter_mode == SUB_EMITTER_AT_START) {
  258. code += "uniform int sub_emitter_amount_at_start;\n";
  259. }
  260. code += "uniform bool sub_emitter_keep_velocity;\n";
  261. }
  262. code += "uniform vec4 color_value : source_color;\n";
  263. code += "uniform vec3 gravity;\n";
  264. if (color_ramp.is_valid()) {
  265. code += "uniform sampler2D color_ramp : repeat_disable;\n";
  266. }
  267. if (color_initial_ramp.is_valid()) {
  268. code += "uniform sampler2D color_initial_ramp : repeat_disable;\n";
  269. }
  270. if (alpha_curve.is_valid()) {
  271. code += "uniform sampler2D alpha_curve : repeat_disable;\n";
  272. }
  273. if (emission_curve.is_valid()) {
  274. code += "uniform sampler2D emission_curve : repeat_disable;\n";
  275. }
  276. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  277. code += "uniform sampler2D linear_velocity_texture : repeat_disable;\n";
  278. }
  279. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  280. code += "uniform sampler2D orbit_velocity_curve : repeat_disable;\n";
  281. }
  282. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  283. code += "uniform sampler2D angular_velocity_texture : repeat_disable;\n";
  284. }
  285. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  286. code += "uniform sampler2D linear_accel_texture : repeat_disable;\n";
  287. }
  288. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  289. code += "uniform sampler2D radial_accel_texture : repeat_disable;\n";
  290. }
  291. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  292. code += "uniform sampler2D tangent_accel_texture : repeat_disable;\n";
  293. }
  294. if (tex_parameters[PARAM_DAMPING].is_valid()) {
  295. code += "uniform sampler2D damping_texture : repeat_disable;\n";
  296. }
  297. if (tex_parameters[PARAM_ANGLE].is_valid()) {
  298. code += "uniform sampler2D angle_texture : repeat_disable;\n";
  299. }
  300. if (tex_parameters[PARAM_SCALE].is_valid()) {
  301. code += "uniform sampler2D scale_curve : repeat_disable;\n";
  302. }
  303. if (tex_parameters[PARAM_HUE_VARIATION].is_valid()) {
  304. code += "uniform sampler2D hue_rot_curve : repeat_disable;\n";
  305. }
  306. if (tex_parameters[PARAM_ANIM_SPEED].is_valid()) {
  307. code += "uniform sampler2D animation_speed_curve : repeat_disable;\n";
  308. }
  309. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  310. code += "uniform sampler2D animation_offset_curve : repeat_disable;\n";
  311. }
  312. if (tex_parameters[PARAM_RADIAL_VELOCITY].is_valid()) {
  313. code += "uniform sampler2D radial_velocity_curve : repeat_disable;\n";
  314. }
  315. if (tex_parameters[PARAM_SCALE_OVER_VELOCITY].is_valid()) {
  316. code += "uniform sampler2D scale_over_velocity_curve : repeat_disable;\n";
  317. }
  318. if (tex_parameters[PARAM_DIRECTIONAL_VELOCITY].is_valid()) {
  319. code += "uniform sampler2D directional_velocity_curve : repeat_disable;\n";
  320. }
  321. if (velocity_limit_curve.is_valid()) {
  322. code += "uniform sampler2D velocity_limit_curve : repeat_disable;\n";
  323. }
  324. if (collision_mode == COLLISION_RIGID) {
  325. code += "uniform float collision_friction;\n";
  326. code += "uniform float collision_bounce;\n";
  327. }
  328. if (turbulence_enabled) {
  329. code += "uniform float turbulence_noise_strength;\n";
  330. code += "uniform float turbulence_noise_scale;\n";
  331. code += "uniform float turbulence_influence_min;\n";
  332. code += "uniform float turbulence_influence_max;\n";
  333. code += "uniform float turbulence_initial_displacement_min;\n";
  334. code += "uniform float turbulence_initial_displacement_max;\n";
  335. code += "uniform float turbulence_noise_speed_random;\n";
  336. code += "uniform vec3 turbulence_noise_speed = vec3(1.0, 1.0, 1.0);\n";
  337. if (tex_parameters[PARAM_TURB_INFLUENCE_OVER_LIFE].is_valid()) {
  338. code += "uniform sampler2D turbulence_influence_over_life;\n";
  339. }
  340. if (turbulence_color_ramp.is_valid()) {
  341. code += "uniform sampler2D turbulence_color_ramp;\n";
  342. }
  343. }
  344. code += "\n";
  345. // Define helper functions.
  346. if (turbulence_enabled) {
  347. // Functions for 3D noise / turbulence.
  348. code += "// Functions for 3D noise / turbulence.\n";
  349. code += "vec4 grad(vec4 p) {\n";
  350. code += " p = fract(vec4(\n";
  351. code += " dot(p, vec4(0.143081, 0.001724, 0.280166, 0.262771)),\n";
  352. code += " dot(p, vec4(0.645401, -0.047791, -0.146698, 0.595016)),\n";
  353. code += " dot(p, vec4(-0.499665, -0.095734, 0.425674, -0.207367)),\n";
  354. code += " dot(p, vec4(-0.013596, -0.848588, 0.423736, 0.17044))));\n";
  355. code += " return fract((p.xyzw * p.yzwx) * 2365.952041) * 2.0 - 1.0;\n";
  356. code += "}\n\n";
  357. code += "float noise(vec4 coord) {\n";
  358. code += " // Domain rotation to improve the look of XYZ slices + animation patterns.\n";
  359. code += " coord = vec4(\n";
  360. code += " coord.xyz + dot(coord, vec4(vec3(-0.1666667), -0.5)),\n";
  361. code += " dot(coord, vec4(0.5)));\n\n";
  362. code += " vec4 base = floor(coord), delta = coord - base;\n\n";
  363. code += " vec4 grad_0000 = grad(base + vec4(0.0, 0.0, 0.0, 0.0)), grad_1000 = grad(base + vec4(1.0, 0.0, 0.0, 0.0));\n";
  364. code += " vec4 grad_0100 = grad(base + vec4(0.0, 1.0, 0.0, 0.0)), grad_1100 = grad(base + vec4(1.0, 1.0, 0.0, 0.0));\n";
  365. code += " vec4 grad_0010 = grad(base + vec4(0.0, 0.0, 1.0, 0.0)), grad_1010 = grad(base + vec4(1.0, 0.0, 1.0, 0.0));\n";
  366. code += " vec4 grad_0110 = grad(base + vec4(0.0, 1.0, 1.0, 0.0)), grad_1110 = grad(base + vec4(1.0, 1.0, 1.0, 0.0));\n";
  367. code += " vec4 grad_0001 = grad(base + vec4(0.0, 0.0, 0.0, 1.0)), grad_1001 = grad(base + vec4(1.0, 0.0, 0.0, 1.0));\n";
  368. code += " vec4 grad_0101 = grad(base + vec4(0.0, 1.0, 0.0, 1.0)), grad_1101 = grad(base + vec4(1.0, 1.0, 0.0, 1.0));\n";
  369. code += " vec4 grad_0011 = grad(base + vec4(0.0, 0.0, 1.0, 1.0)), grad_1011 = grad(base + vec4(1.0, 0.0, 1.0, 1.0));\n";
  370. code += " vec4 grad_0111 = grad(base + vec4(0.0, 1.0, 1.0, 1.0)), grad_1111 = grad(base + vec4(1.0, 1.0, 1.0, 1.0));\n\n";
  371. code += " vec4 result_0123 = vec4(\n";
  372. code += " dot(delta - vec4(0.0, 0.0, 0.0, 0.0), grad_0000), dot(delta - vec4(1.0, 0.0, 0.0, 0.0), grad_1000),\n";
  373. code += " dot(delta - vec4(0.0, 1.0, 0.0, 0.0), grad_0100), dot(delta - vec4(1.0, 1.0, 0.0, 0.0), grad_1100));\n";
  374. code += " vec4 result_4567 = vec4(\n";
  375. code += " dot(delta - vec4(0.0, 0.0, 1.0, 0.0), grad_0010), dot(delta - vec4(1.0, 0.0, 1.0, 0.0), grad_1010),\n";
  376. code += " dot(delta - vec4(0.0, 1.0, 1.0, 0.0), grad_0110), dot(delta - vec4(1.0, 1.0, 1.0, 0.0), grad_1110));\n";
  377. code += " vec4 result_89AB = vec4(\n";
  378. code += " dot(delta - vec4(0.0, 0.0, 0.0, 1.0), grad_0001), dot(delta - vec4(1.0, 0.0, 0.0, 1.0), grad_1001),\n";
  379. code += " dot(delta - vec4(0.0, 1.0, 0.0, 1.0), grad_0101), dot(delta - vec4(1.0, 1.0, 0.0, 1.0), grad_1101));\n";
  380. code += " vec4 result_CDEF = vec4(\n";
  381. code += " dot(delta - vec4(0.0, 0.0, 1.0, 1.0), grad_0011), dot(delta - vec4(1.0, 0.0, 1.0, 1.0), grad_1011),\n";
  382. code += " dot(delta - vec4(0.0, 1.0, 1.0, 1.0), grad_0111), dot(delta - vec4(1.0, 1.0, 1.0, 1.0), grad_1111));\n\n";
  383. code += " vec4 fade = delta * delta * delta * (10.0 + delta * (-15.0 + delta * 6.0));\n";
  384. code += " vec4 result_W0 = mix(result_0123, result_89AB, fade.w), result_W1 = mix(result_4567, result_CDEF, fade.w);\n";
  385. code += " vec4 result_WZ = mix(result_W0, result_W1, fade.z);\n";
  386. code += " vec2 result_WZY = mix(result_WZ.xy, result_WZ.zw, fade.y);\n";
  387. code += " return mix(result_WZY.x, result_WZY.y, fade.x);\n";
  388. code += "}\n\n";
  389. code += "// Curl 3D and three-noise function with friendly permission by Isaac Cohen.\n";
  390. code += "// Modified to accept 4D noise.\n";
  391. code += "vec3 noise_3x(vec4 p) {\n";
  392. code += " float s = noise(p);\n";
  393. code += " float s1 = noise(p + vec4(vec3(0.0), 1.7320508 * 2048.333333));\n";
  394. code += " float s2 = noise(p - vec4(vec3(0.0), 1.7320508 * 2048.333333));\n";
  395. code += " vec3 c = vec3(s, s1, s2);\n";
  396. code += " return c;\n";
  397. code += "}\n\n";
  398. code += "vec3 curl_3d(vec4 p, float c) {\n";
  399. code += " float epsilon = 0.001 + c;\n";
  400. code += " vec4 dx = vec4(epsilon, 0.0, 0.0, 0.0);\n";
  401. code += " vec4 dy = vec4(0.0, epsilon, 0.0, 0.0);\n";
  402. code += " vec4 dz = vec4(0.0, 0.0, epsilon, 0.0);\n";
  403. code += " vec3 x0 = noise_3x(p - dx).xyz;\n";
  404. code += " vec3 x1 = noise_3x(p + dx).xyz;\n";
  405. code += " vec3 y0 = noise_3x(p - dy).xyz;\n";
  406. code += " vec3 y1 = noise_3x(p + dy).xyz;\n";
  407. code += " vec3 z0 = noise_3x(p - dz).xyz;\n";
  408. code += " vec3 z1 = noise_3x(p + dz).xyz;\n";
  409. code += " float x = (y1.z - y0.z) - (z1.y - z0.y);\n";
  410. code += " float y = (z1.x - z0.x) - (x1.z - x0.z);\n";
  411. code += " float z = (x1.y - x0.y) - (y1.x - y0.x);\n";
  412. code += " return normalize(vec3(x, y, z));\n";
  413. code += "}\n\n";
  414. code += "vec3 get_noise_direction(vec3 pos) {\n";
  415. code += " float adj_contrast = max((turbulence_noise_strength - 1.0), 0.0) * 70.0;\n";
  416. code += " vec4 noise_time = TIME * vec4(turbulence_noise_speed, turbulence_noise_speed_random);\n";
  417. code += " vec4 noise_pos = vec4(pos * turbulence_noise_scale, 0.0);\n";
  418. code += " vec3 noise_direction = curl_3d(noise_pos + noise_time, adj_contrast);\n";
  419. code += " noise_direction = mix(0.9 * noise_direction, noise_direction, turbulence_noise_strength - 9.0);\n";
  420. code += " return noise_direction;\n";
  421. code += "}\n\n";
  422. }
  423. code += "vec4 rotate_hue(vec4 current_color, float hue_rot_angle) {\n";
  424. code += " float hue_rot_c = cos(hue_rot_angle);\n";
  425. code += " float hue_rot_s = sin(hue_rot_angle);\n";
  426. code += " mat4 hue_rot_mat =\n";
  427. code += " mat4(vec4(0.299, 0.587, 0.114, 0.0),\n";
  428. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  429. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  430. code += " vec4(0.000, 0.000, 0.000, 1.0)) +\n";
  431. code += " mat4(vec4(0.701, -0.587, -0.114, 0.0),\n";
  432. code += " vec4(-0.299, 0.413, -0.114, 0.0),\n";
  433. code += " vec4(-0.300, -0.588, 0.886, 0.0),\n";
  434. code += " vec4(0.000, 0.000, 0.000, 0.0)) *\n";
  435. code += " hue_rot_c +\n";
  436. code += " mat4(vec4(0.168, 0.330, -0.497, 0.0),\n";
  437. code += " vec4(-0.328, 0.035, 0.292, 0.0),\n";
  438. code += " vec4(1.250, -1.050, -0.203, 0.0),\n";
  439. code += " vec4(0.000, 0.000, 0.000, 0.0)) *\n";
  440. code += " hue_rot_s;\n";
  441. code += " return hue_rot_mat * current_color;\n";
  442. code += "}\n\n";
  443. // Random functions.
  444. code += "float rand_from_seed(inout uint seed) {\n";
  445. code += " int k;\n";
  446. code += " int s = int(seed);\n";
  447. code += " if (s == 0) {\n";
  448. code += " s = 305420679;\n";
  449. code += " }\n";
  450. code += " k = s / 127773;\n";
  451. code += " s = 16807 * (s - k * 127773) - 2836 * k;\n";
  452. code += " if (s < 0) {\n";
  453. code += " s += 2147483647;\n";
  454. code += " }\n";
  455. code += " seed = uint(s);\n";
  456. code += " return float(seed % uint(65536)) / 65535.0;\n";
  457. code += "}\n\n";
  458. code += "float rand_from_seed_m1_p1(inout uint seed) {\n";
  459. code += " return rand_from_seed(seed) * 2.0 - 1.0;\n";
  460. code += "}\n\n";
  461. // Improve seed quality.
  462. code += "uint hash(uint x) {\n";
  463. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  464. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  465. code += " x = (x >> uint(16)) ^ x;\n";
  466. code += " return x;\n";
  467. code += "}\n\n";
  468. // Define structs.
  469. code += "struct DisplayParameters {\n";
  470. code += " vec3 scale;\n";
  471. code += " float hue_rotation;\n";
  472. code += " float animation_speed;\n";
  473. code += " float animation_offset;\n";
  474. code += " float lifetime;\n";
  475. code += " vec4 color;\n";
  476. code += " float emission_texture_position;\n";
  477. code += "};\n\n";
  478. code += "struct DynamicsParameters {\n";
  479. code += " float angle;\n";
  480. code += " float angular_velocity;\n";
  481. code += " float initial_velocity_multiplier;\n";
  482. code += " float directional_velocity;\n";
  483. code += " float radial_velocity;\n";
  484. code += " float orbit_velocity;\n";
  485. if (turbulence_enabled) {
  486. code += " float turb_influence;\n";
  487. }
  488. code += "};\n\n";
  489. code += "struct PhysicalParameters {\n";
  490. code += " float linear_accel;\n";
  491. code += " float radial_accel;\n";
  492. code += " float tangent_accel;\n";
  493. code += " float damping;\n";
  494. code += "};\n\n";
  495. code += "void calculate_initial_physical_params(inout PhysicalParameters params, inout uint alt_seed) {\n";
  496. code += " params.linear_accel = mix(linear_accel_min, linear_accel_max, rand_from_seed(alt_seed));\n";
  497. code += " params.radial_accel = mix(radial_accel_min, radial_accel_max, rand_from_seed(alt_seed));\n";
  498. code += " params.tangent_accel = mix(tangent_accel_min, tangent_accel_max, rand_from_seed(alt_seed));\n";
  499. code += " params.damping = mix(damping_min, damping_max, rand_from_seed(alt_seed));\n";
  500. code += "}\n\n";
  501. code += "void calculate_initial_dynamics_params(inout DynamicsParameters params, inout uint alt_seed) {\n";
  502. code += " // -------------------- DO NOT REORDER OPERATIONS, IT BREAKS VISUAL COMPATIBILITY\n";
  503. code += " // -------------------- ADD NEW OPERATIONS AT THE BOTTOM\n";
  504. code += " params.angle = mix(initial_angle_min, initial_angle_max, rand_from_seed(alt_seed));\n";
  505. code += " params.angular_velocity = mix(angular_velocity_min, angular_velocity_max, rand_from_seed(alt_seed));\n";
  506. code += " params.initial_velocity_multiplier = mix(initial_linear_velocity_min, initial_linear_velocity_max, rand_from_seed(alt_seed));\n";
  507. code += " params.directional_velocity = mix(directional_velocity_min, directional_velocity_max, rand_from_seed(alt_seed));\n";
  508. code += " params.radial_velocity = mix(radial_velocity_min, radial_velocity_max, rand_from_seed(alt_seed));\n";
  509. code += " params.orbit_velocity = mix(orbit_velocity_min, orbit_velocity_max, rand_from_seed(alt_seed));\n";
  510. if (turbulence_enabled) {
  511. code += " params.turb_influence = mix(turbulence_influence_min, turbulence_influence_max, rand_from_seed(alt_seed));\n";
  512. }
  513. code += "}\n\n";
  514. code += "void calculate_initial_display_params(inout DisplayParameters params, inout uint alt_seed) {\n";
  515. code += " // -------------------- DO NOT REORDER OPERATIONS, IT BREAKS VISUAL COMPATIBILITY\n";
  516. code += " // -------------------- ADD NEW OPERATIONS AT THE BOTTOM\n";
  517. code += " float pi = 3.14159;\n";
  518. code += " params.scale = vec3(mix(scale_min, scale_max, rand_from_seed(alt_seed)));\n";
  519. code += " params.scale = sign(params.scale) * max(abs(params.scale), 0.001);\n";
  520. code += " params.hue_rotation = pi * 2.0 * mix(hue_variation_min, hue_variation_max, rand_from_seed(alt_seed));\n";
  521. code += " params.animation_speed = mix(anim_speed_min, anim_speed_max, rand_from_seed(alt_seed));\n";
  522. code += " params.animation_offset = mix(anim_offset_min, anim_offset_max, rand_from_seed(alt_seed));\n";
  523. code += " params.lifetime = (1.0 - lifetime_randomness * rand_from_seed(alt_seed));\n";
  524. code += " params.color = color_value;\n";
  525. if (color_initial_ramp.is_valid()) {
  526. code += " params.color *= texture(color_initial_ramp, vec2(rand_from_seed(alt_seed)));\n";
  527. }
  528. if (emission_shape == EMISSION_SHAPE_POINTS || emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  529. code += " params.emission_texture_position = rand_from_seed(alt_seed);\n";
  530. if (emission_color_texture.is_valid()) {
  531. code += " int point = min(emission_texture_point_count - 1, int(params.emission_texture_position * float(emission_texture_point_count)));\n";
  532. code += " ivec2 emission_tex_size = textureSize(emission_texture_points, 0);\n";
  533. code += " ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);\n";
  534. code += " params.color *= texelFetch(emission_texture_color, emission_tex_ofs, 0);\n";
  535. }
  536. }
  537. code += "}\n\n";
  538. // Process display parameters that are bound solely by lifetime.
  539. code += "void process_display_param(inout DisplayParameters parameters, float lifetime) {\n";
  540. code += " // Compile-time add textures.\n";
  541. if (tex_parameters[PARAM_SCALE].is_valid()) {
  542. code += " parameters.scale *= texture(scale_curve, vec2(lifetime)).rgb;\n";
  543. }
  544. if (tex_parameters[PARAM_HUE_VARIATION].is_valid()) {
  545. code += " parameters.hue_rotation *= texture(hue_rot_curve, vec2(lifetime)).r;\n";
  546. }
  547. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  548. code += " parameters.animation_offset += texture(animation_offset_curve, vec2(lifetime)).r;\n";
  549. }
  550. if (tex_parameters[PARAM_ANIM_SPEED].is_valid()) {
  551. code += " parameters.animation_speed *= texture(animation_speed_curve, vec2(lifetime)).r;\n";
  552. }
  553. if (color_ramp.is_valid()) {
  554. code += " parameters.color *= texture(color_ramp, vec2(lifetime));\n";
  555. }
  556. if (alpha_curve.is_valid()) {
  557. code += " parameters.color.a *= texture(alpha_curve, vec2(lifetime)).r;\n";
  558. }
  559. code += " parameters.color = rotate_hue(parameters.color, parameters.hue_rotation);\n";
  560. if (emission_curve.is_valid()) {
  561. code += " parameters.color.rgb *= 1.0 + texture(emission_curve, vec2(lifetime)).r;\n";
  562. }
  563. code += "}\n\n";
  564. code += "vec3 calculate_initial_position(inout DisplayParameters params, inout uint alt_seed) {\n";
  565. code += " float pi = 3.14159;\n";
  566. code += " vec3 pos = vec3(0.0);\n";
  567. code += " { // Emission shape.\n";
  568. if (emission_shape == EMISSION_SHAPE_POINT) {
  569. code += " pos = vec3(0.0);\n";
  570. }
  571. if (emission_shape == EMISSION_SHAPE_SPHERE) {
  572. code += " float s = rand_from_seed(alt_seed) * 2.0 - 1.0;\n";
  573. code += " float t = rand_from_seed(alt_seed) * 2.0 * pi;\n";
  574. code += " float p = rand_from_seed(alt_seed);\n";
  575. code += " float radius = emission_sphere_radius * sqrt(1.0 - s * s);\n";
  576. code += " pos = mix(vec3(0.0, 0.0, 0.0), vec3(radius * cos(t), radius * sin(t), emission_sphere_radius * s), p);\n";
  577. }
  578. if (emission_shape == EMISSION_SHAPE_SPHERE_SURFACE) {
  579. code += " float s = rand_from_seed(alt_seed) * 2.0 - 1.0;\n";
  580. code += " float t = rand_from_seed(alt_seed) * 2.0 * pi;\n";
  581. code += " float radius = emission_sphere_radius * sqrt(1.0 - s * s);\n";
  582. code += " pos = vec3(radius * cos(t), radius * sin(t), emission_sphere_radius * s);\n";
  583. }
  584. if (emission_shape == EMISSION_SHAPE_BOX) {
  585. code += " pos = 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";
  586. }
  587. if (emission_shape == EMISSION_SHAPE_POINTS || emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  588. code += " int point = min(emission_texture_point_count - 1, int(params.emission_texture_position * float(emission_texture_point_count)));\n";
  589. code += " ivec2 emission_tex_size = textureSize(emission_texture_points, 0);\n";
  590. code += " ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);\n";
  591. code += " pos = texelFetch(emission_texture_points, emission_tex_ofs, 0).xyz;\n";
  592. }
  593. if (emission_shape == EMISSION_SHAPE_RING) {
  594. code += " float radius_clamped = max(0.001, emission_ring_radius);\n";
  595. code += " float top_radius = max(radius_clamped - tan(radians(90.0 - emission_ring_cone_angle)) * emission_ring_height, 0.0);\n";
  596. code += " float y_pos = rand_from_seed(alt_seed);\n";
  597. code += " float skew = max(min(radius_clamped, top_radius) / max(radius_clamped, top_radius), 0.5);\n";
  598. code += " y_pos = radius_clamped < top_radius ? pow(y_pos, skew) : 1.0 - pow(y_pos, skew);\n";
  599. code += " float ring_spawn_angle = rand_from_seed(alt_seed) * 2.0 * pi;\n";
  600. code += " float ring_random_radius = sqrt(rand_from_seed(alt_seed) * (radius_clamped * radius_clamped - emission_ring_inner_radius * emission_ring_inner_radius) + emission_ring_inner_radius * emission_ring_inner_radius);\n";
  601. code += " ring_random_radius = mix(ring_random_radius, ring_random_radius * (top_radius / radius_clamped), y_pos);\n";
  602. code += " vec3 axis = emission_ring_axis == vec3(0.0) ? vec3(0.0, 0.0, 1.0) : normalize(emission_ring_axis);\n";
  603. code += " vec3 ortho_axis = vec3(0.0);\n";
  604. code += " if (abs(axis) == vec3(1.0, 0.0, 0.0)) {\n";
  605. code += " ortho_axis = cross(axis, vec3(0.0, 1.0, 0.0));\n";
  606. code += " } else {\n";
  607. code += " ortho_axis = cross(axis, vec3(1.0, 0.0, 0.0));\n";
  608. code += " }\n";
  609. code += " ortho_axis = normalize(ortho_axis);\n";
  610. code += " float s = sin(ring_spawn_angle);\n";
  611. code += " float c = cos(ring_spawn_angle);\n";
  612. code += " float oc = 1.0 - c;\n";
  613. code += " ortho_axis = mat3(\n";
  614. code += " vec3(c + axis.x * axis.x * oc, axis.x * axis.y * oc - axis.z * s, axis.x * axis.z *oc + axis.y * s),\n";
  615. code += " vec3(axis.x * axis.y * oc + s * axis.z, c + axis.y * axis.y * oc, axis.y * axis.z * oc - axis.x * s),\n";
  616. code += " vec3(axis.z * axis.x * oc - axis.y * s, axis.z * axis.y * oc + axis.x * s, c + axis.z * axis.z * oc)\n";
  617. code += " ) * ortho_axis;\n";
  618. code += " ortho_axis = normalize(ortho_axis);\n";
  619. code += " pos = ortho_axis * ring_random_radius + (y_pos * emission_ring_height - emission_ring_height / 2.0) * axis;\n";
  620. }
  621. code += " }\n";
  622. code += " return pos * emission_shape_scale + emission_shape_offset;\n";
  623. code += "}\n\n";
  624. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid() || particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  625. code += "vec3 process_orbit_displacement(DynamicsParameters param, float lifetime, inout uint alt_seed, mat4 transform, mat4 emission_transform, float delta, float total_lifetime) {\n";
  626. // No reason to run all these expensive calculation below if we have no orbit velocity
  627. // HOWEVER
  628. // May be a bad idea for fps consistency?
  629. code += " if (abs(param.orbit_velocity) < 0.01 || delta < 0.001) {\n";
  630. code += " return vec3(0.0);\n";
  631. code += " }\n";
  632. code += " vec3 displacement = vec3(0.0);\n";
  633. code += " float pi = 3.14159;\n";
  634. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  635. code += " float orbit_amount = param.orbit_velocity;\n";
  636. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  637. CurveTexture *texture = Object::cast_to<CurveTexture>(tex_parameters[PARAM_ORBIT_VELOCITY].ptr());
  638. if (texture) {
  639. code += " orbit_amount *= texture(orbit_velocity_curve, vec2(lifetime)).r;\n";
  640. } else {
  641. code += " orbit_amount *= texture(orbit_velocity_curve, vec2(lifetime)).b;\n";
  642. }
  643. }
  644. code += " if (orbit_amount != 0.0) {\n";
  645. code += " vec3 pos = transform[3].xyz;\n";
  646. code += " vec3 org = emission_transform[3].xyz;\n";
  647. code += " vec3 diff = pos - org;\n";
  648. code += " float ang = orbit_amount * pi * 2.0 * delta;\n";
  649. code += " mat2 rot = mat2(vec2(cos(ang), -sin(ang)), vec2(sin(ang), cos(ang)));\n";
  650. code += " displacement.xy -= diff.xy;\n";
  651. code += " displacement.xy += rot * diff.xy;\n";
  652. code += " }\n";
  653. } else {
  654. code += " vec3 orbit_velocities = vec3(param.orbit_velocity);\n";
  655. code += " orbit_velocities *= texture(orbit_velocity_curve, vec2(lifetime)).rgb;\n";
  656. code += " orbit_velocities *= pi * 2.0;\n";
  657. code += " orbit_velocities *= delta; // We wanna process those by the delta angle.\n\n";
  658. code += " // X axis.\n";
  659. code += " vec3 local_pos = (inverse(emission_transform) * transform[3]).xyz;\n";
  660. code += " local_pos -= velocity_pivot;\n";
  661. code += " local_pos.x = 0.0;\n";
  662. code += " mat3 x_rotation_mat = mat3(\n";
  663. code += " vec3(1.0, 0.0, 0.0),\n";
  664. code += " vec3(0.0, cos(orbit_velocities.x), sin(orbit_velocities.x)),\n";
  665. code += " vec3(0.0, -sin(orbit_velocities.x), cos(orbit_velocities.x))\n";
  666. code += " );\n";
  667. code += " vec3 new_pos = x_rotation_mat * local_pos;\n";
  668. code += " displacement = new_pos - local_pos;\n\n";
  669. code += " // Y axis.\n";
  670. code += " local_pos = (inverse(emission_transform) * transform[3]).xyz;\n";
  671. code += " local_pos -= velocity_pivot;\n";
  672. code += " local_pos.y = 0.0;\n";
  673. code += " mat3 y_rotation_mat = mat3(\n";
  674. code += " vec3(cos(orbit_velocities.y), 0.0, -sin(orbit_velocities.y)),\n";
  675. code += " vec3(0.0, 1.0, 0.0),\n";
  676. code += " vec3(sin(orbit_velocities.y), 0.0, cos(orbit_velocities.y))\n";
  677. code += " );\n";
  678. code += " new_pos = y_rotation_mat * local_pos;\n";
  679. code += " displacement += new_pos - local_pos;\n\n";
  680. code += " // Z axis.\n";
  681. code += " local_pos = (inverse(emission_transform) * transform[3]).xyz;\n";
  682. code += " local_pos -= velocity_pivot;\n";
  683. code += " local_pos.z = 0.0;\n";
  684. code += " mat3 z_rotation_mat = mat3(\n";
  685. code += " vec3(cos(orbit_velocities.z), sin(orbit_velocities.z), 0.0),\n";
  686. code += " vec3(-sin(orbit_velocities.z), cos(orbit_velocities.z), 0.0),\n";
  687. code += " vec3(0.0, 0.0, 1.0)\n";
  688. code += " );\n";
  689. code += " new_pos = z_rotation_mat * local_pos;\n";
  690. code += " displacement += new_pos - local_pos;\n\n";
  691. }
  692. code += " return (emission_transform * vec4(displacement / delta, 0.0)).xyz;\n";
  693. code += "}\n\n";
  694. }
  695. code += "vec3 get_random_direction_from_spread(inout uint alt_seed, float spread_angle) {\n";
  696. code += " float pi = 3.14159;\n";
  697. code += " float degree_to_rad = pi / 180.0;\n";
  698. code += " float spread_rad = spread_angle * degree_to_rad;\n";
  699. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  700. // Spread calculation for 2D.
  701. code += " float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;\n";
  702. code += " angle1_rad += direction.x != 0.0 ? atan(direction.y, direction.x) : sign(direction.y) * (pi / 2.0);\n";
  703. code += " vec3 spread_direction = vec3(cos(angle1_rad), sin(angle1_rad), 0.0);\n";
  704. code += " return spread_direction;\n";
  705. } else {
  706. // Spread calculation for 3D.
  707. code += " float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;\n";
  708. code += " float angle2_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad * (1.0 - flatness);\n";
  709. code += " vec3 direction_xz = vec3(sin(angle1_rad), 0.0, cos(angle1_rad));\n";
  710. code += " vec3 direction_yz = vec3(0.0, sin(angle2_rad), cos(angle2_rad));\n";
  711. code += " direction_yz.z = direction_yz.z / max(0.0001, sqrt(abs(direction_yz.z))); // Better uniform distribution.\n";
  712. code += " vec3 spread_direction = vec3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);\n";
  713. code += " vec3 direction_nrm = length(direction) > 0.0 ? normalize(direction) : vec3(0.0, 0.0, 1.0);\n";
  714. code += " // Rotate spread to direction.\n";
  715. code += " vec3 binormal = cross(vec3(0.0, 1.0, 0.0), direction_nrm);\n";
  716. code += " if (length(binormal) < 0.0001) {\n";
  717. code += " // Direction is parallel to Y. Choose Z as the binormal.\n";
  718. code += " binormal = vec3(0.0, 0.0, 1.0);\n";
  719. code += " }\n";
  720. code += " binormal = normalize(binormal);\n";
  721. code += " vec3 normal = cross(binormal, direction_nrm);\n";
  722. code += " spread_direction = binormal * spread_direction.x + normal * spread_direction.y + direction_nrm * spread_direction.z;\n";
  723. code += " return normalize(spread_direction);\n";
  724. }
  725. code += "}\n\n";
  726. code += "vec3 process_radial_displacement(DynamicsParameters param, float lifetime, inout uint alt_seed, mat4 transform, mat4 emission_transform, float delta) {\n";
  727. code += " vec3 radial_displacement = vec3(0.0);\n";
  728. code += " if (delta < 0.001) {\n";
  729. code += " return radial_displacement;\n";
  730. code += " }\n";
  731. code += " float radial_displacement_multiplier = 1.0;\n";
  732. if (tex_parameters[PARAM_RADIAL_VELOCITY].is_valid()) {
  733. code += " radial_displacement_multiplier = texture(radial_velocity_curve, vec2(lifetime)).r;\n";
  734. }
  735. code += " vec3 global_pivot = (emission_transform * vec4(velocity_pivot, 1.0)).xyz;\n";
  736. code += " if (length(transform[3].xyz - global_pivot) > 0.01) {\n";
  737. code += " radial_displacement = normalize(transform[3].xyz - global_pivot) * radial_displacement_multiplier * param.radial_velocity;\n";
  738. code += " } else {\n";
  739. code += " radial_displacement = get_random_direction_from_spread(alt_seed, 360.0) * param.radial_velocity;\n";
  740. code += " }\n";
  741. code += " if (radial_displacement_multiplier * param.radial_velocity < 0.0) {\n";
  742. code += " // Prevent inwards velocity to flicker once the point is reached.\n";
  743. code += " radial_displacement = normalize(radial_displacement) * min(abs(radial_displacement_multiplier * param.radial_velocity), length(transform[3].xyz - global_pivot) / delta);\n";
  744. code += " }\n";
  745. code += " return radial_displacement;\n";
  746. code += "}\n\n";
  747. if (tex_parameters[PARAM_DIRECTIONAL_VELOCITY].is_valid()) {
  748. code += "vec3 process_directional_displacement(DynamicsParameters param, float lifetime_percent, mat4 transform, mat4 emission_transform) {\n";
  749. code += " vec3 displacement = texture(directional_velocity_curve, vec2(lifetime_percent)).xyz * param.directional_velocity;\n";
  750. if (directional_velocity_global) {
  751. code += " displacement = (emission_transform * vec4(displacement, 0.0)).xyz;\n";
  752. }
  753. code += " return displacement;\n";
  754. code += "}\n\n";
  755. }
  756. code += "void process_physical_parameters(inout PhysicalParameters params, float lifetime_percent) {\n";
  757. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  758. code += " params.linear_accel *= texture(linear_accel_texture, vec2(lifetime_percent)).r;\n";
  759. }
  760. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  761. code += " params.radial_accel *= texture(radial_accel_texture, vec2(lifetime_percent)).r;\n";
  762. }
  763. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  764. code += " params.tangent_accel *= texture(tangent_accel_texture, vec2(lifetime_percent)).r;\n";
  765. }
  766. if (tex_parameters[PARAM_DAMPING].is_valid()) {
  767. code += " params.damping *= texture(damping_texture, vec2(lifetime_percent)).r;\n";
  768. }
  769. code += "}\n\n";
  770. code += "void start() {\n";
  771. code += " uint base_number = NUMBER;\n";
  772. code += " uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
  773. code += " DisplayParameters params;\n";
  774. code += " calculate_initial_display_params(params, alt_seed);\n";
  775. code += " // Reset alt seed?\n";
  776. code += " //alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
  777. code += " DynamicsParameters dynamic_params;\n";
  778. code += " calculate_initial_dynamics_params(dynamic_params, alt_seed);\n";
  779. code += " PhysicalParameters physics_params;\n";
  780. code += " calculate_initial_physical_params(physics_params, alt_seed);\n";
  781. code += " process_display_param(params, 0.0);\n";
  782. code += " if (rand_from_seed(alt_seed) > AMOUNT_RATIO) {\n";
  783. code += " ACTIVE = false;\n";
  784. code += " }\n\n";
  785. code += " if (RESTART_CUSTOM) {\n";
  786. code += " CUSTOM = vec4(0.0);\n";
  787. code += " CUSTOM.w = params.lifetime;\n";
  788. code += " CUSTOM.x = dynamic_params.angle;\n";
  789. code += " }\n";
  790. code += " if (RESTART_COLOR) {\n";
  791. code += " COLOR = params.color;\n";
  792. code += " }\n";
  793. code += " if (RESTART_ROT_SCALE) {\n";
  794. code += " TRANSFORM[0].xyz = vec3(1.0, 0.0, 0.0);\n";
  795. code += " TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);\n";
  796. code += " TRANSFORM[2].xyz = vec3(0.0, 0.0, 1.0);\n";
  797. code += " }\n";
  798. code += " if (RESTART_POSITION) {\n";
  799. code += " TRANSFORM[3].xyz = calculate_initial_position(params, alt_seed);\n";
  800. if (turbulence_enabled) {
  801. code += " float initial_turbulence_displacement = mix(turbulence_initial_displacement_min, turbulence_initial_displacement_max, rand_from_seed(alt_seed));\n";
  802. code += " vec3 noise_direction = get_noise_direction(TRANSFORM[3].xyz);\n";
  803. code += " TRANSFORM[3].xyz += noise_direction * initial_turbulence_displacement;\n";
  804. }
  805. code += " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  806. code += " }\n";
  807. code += " if (RESTART_VELOCITY) {\n";
  808. code += " VELOCITY = get_random_direction_from_spread(alt_seed, spread) * dynamic_params.initial_velocity_multiplier;\n";
  809. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  810. code += " int point = min(emission_texture_point_count - 1, int(params.emission_texture_position * float(emission_texture_point_count)));\n";
  811. code += " ivec2 emission_tex_size = textureSize(emission_texture_points, 0);\n";
  812. code += " ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);\n";
  813. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  814. code += " {\n";
  815. code += " mat2 rotm;\n";
  816. code += " rotm[0] = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xy;\n";
  817. code += " rotm[1] = rotm[0].yx * vec2(1.0, -1.0);\n";
  818. code += " VELOCITY.xy = rotm * VELOCITY.xy;\n";
  819. code += " }\n";
  820. } else {
  821. code += " {\n";
  822. code += " vec3 normal = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xyz;\n";
  823. code += " vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);\n";
  824. code += " vec3 tangent = normalize(cross(v0, normal));\n";
  825. code += " vec3 bitangent = normalize(cross(tangent, normal));\n";
  826. code += " VELOCITY = mat3(tangent, bitangent, normal) * VELOCITY;\n";
  827. code += " }\n";
  828. }
  829. }
  830. code += " }\n\n";
  831. code += " process_display_param(params, 0.0);\n\n";
  832. code += " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
  833. code += " VELOCITY += EMITTER_VELOCITY * inherit_emitter_velocity_ratio;\n";
  834. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  835. code += " VELOCITY.z = 0.0;\n";
  836. code += " TRANSFORM[3].z = 0.0;\n";
  837. }
  838. code += "}\n\n";
  839. code += "void process() {\n";
  840. code += " uint base_number = NUMBER;\n";
  841. // TODO: Add optional determinism here.
  842. code += " //if (repeatable) {\n";
  843. code += " // base_number = INDEX;\n";
  844. code += " //}\n";
  845. code += " uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
  846. code += " DisplayParameters params;\n";
  847. code += " calculate_initial_display_params(params, alt_seed);\n";
  848. code += " DynamicsParameters dynamic_params;\n";
  849. code += " calculate_initial_dynamics_params(dynamic_params, alt_seed);\n";
  850. code += " PhysicalParameters physics_params;\n";
  851. code += " calculate_initial_physical_params(physics_params, alt_seed);\n\n";
  852. code += " float pi = 3.14159;\n";
  853. code += " float degree_to_rad = pi / 180.0;\n\n";
  854. if (sub_emitter_mode == SUB_EMITTER_AT_START && !RenderingServer::get_singleton()->is_low_end()) {
  855. code += " bool just_spawned = CUSTOM.y == 0.0;\n";
  856. }
  857. code += " CUSTOM.y += DELTA / LIFETIME;\n";
  858. code += " CUSTOM.y = mix(CUSTOM.y, 1.0, INTERPOLATE_TO_END);\n";
  859. code += " float lifetime_percent = CUSTOM.y / params.lifetime;\n";
  860. code += " if (CUSTOM.y > CUSTOM.w) {\n";
  861. code += " ACTIVE = false;\n";
  862. code += " }\n\n";
  863. code += " // Calculate all velocity.\n";
  864. code += " vec3 controlled_displacement = vec3(0.0);\n";
  865. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid() || particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  866. code += " controlled_displacement += process_orbit_displacement(dynamic_params, lifetime_percent, alt_seed, TRANSFORM, EMISSION_TRANSFORM, DELTA, params.lifetime * LIFETIME);\n";
  867. }
  868. code += " controlled_displacement += process_radial_displacement(dynamic_params, lifetime_percent, alt_seed, TRANSFORM, EMISSION_TRANSFORM, DELTA);\n";
  869. if (tex_parameters[PARAM_DIRECTIONAL_VELOCITY].is_valid()) {
  870. code += " controlled_displacement += process_directional_displacement(dynamic_params, lifetime_percent, TRANSFORM, EMISSION_TRANSFORM);\n";
  871. }
  872. code += "\n";
  873. code += " process_physical_parameters(physics_params, lifetime_percent);\n";
  874. code += " vec3 force;\n";
  875. code += " {\n";
  876. code += " // Copied from previous version.\n";
  877. code += " vec3 pos = TRANSFORM[3].xyz;\n";
  878. code += " force = gravity;\n";
  879. code += " // Apply linear acceleration.\n";
  880. code += " force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * physics_params.linear_accel : vec3(0.0);\n";
  881. code += " // Apply radial acceleration.\n";
  882. code += " vec3 org = EMISSION_TRANSFORM[3].xyz;\n";
  883. code += " vec3 diff = pos - org;\n";
  884. code += " force += length(diff) > 0.0 ? normalize(diff) * physics_params.radial_accel : vec3(0.0);\n";
  885. code += " // Apply tangential acceleration.\n";
  886. code += " float tangent_accel_val = physics_params.tangent_accel;\n";
  887. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  888. code += " force += length(diff.yx) > 0.0 ? vec3(normalize(diff.yx * vec2(-1.0, 1.0)), 0.0) * tangent_accel_val : vec3(0.0);\n";
  889. } else {
  890. code += " vec3 crossDiff = cross(normalize(diff), normalize(gravity));\n";
  891. code += " force += length(crossDiff) > 0.0 ? normalize(crossDiff) * tangent_accel_val : vec3(0.0);\n";
  892. }
  893. if (attractor_interaction_enabled) {
  894. code += " force += ATTRACTOR_FORCE;\n";
  895. }
  896. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  897. code += " force.z = 0.0;\n";
  898. }
  899. code += " // Apply attractor forces.\n";
  900. code += " VELOCITY += force * DELTA;\n";
  901. code += " }\n";
  902. code += " {\n";
  903. code += " // Copied from previous version.\n";
  904. code += " if (physics_params.damping > 0.0) {\n";
  905. code += " float v = length(VELOCITY);\n";
  906. if (!particle_flags[PARTICLE_FLAG_DAMPING_AS_FRICTION]) {
  907. code += " v -= physics_params.damping * DELTA;\n";
  908. } else {
  909. code += " if (v > 0.001) {\n";
  910. code += " // Realistic friction formula. We assume the mass of a particle to be 0.05 kg.\n";
  911. code += " float damp = v * v * physics_params.damping * 0.05 * DELTA;\n";
  912. code += " v -= damp;\n";
  913. code += " }\n";
  914. }
  915. code += " if (v < 0.0) {\n";
  916. code += " VELOCITY = vec3(0.0);\n";
  917. code += " } else {\n";
  918. code += " VELOCITY = normalize(VELOCITY) * v;\n";
  919. code += " }\n";
  920. code += " }\n";
  921. code += " }\n\n";
  922. if (collision_mode == COLLISION_RIGID) {
  923. code += " if (COLLIDED) {\n";
  924. code += " float collision_response = dot(COLLISION_NORMAL, VELOCITY);\n";
  925. code += " float slide_to_bounce_trigger = step(2.0 / clamp(collision_bounce + 1.0, 1.0, 2.0), abs(collision_response));\n";
  926. code += " TRANSFORM[3].xyz += COLLISION_NORMAL * COLLISION_DEPTH;\n";
  927. code += " // Remove all components of VELOCITY that are not tangential to COLLISION_NORMAL.\n";
  928. code += " VELOCITY -= COLLISION_NORMAL * collision_response;\n";
  929. code += " // Apply friction only to VELOCITY across the surface (effectively decouples friction and bounce behavior).\n";
  930. code += " VELOCITY = mix(VELOCITY, vec3(0.0), clamp(collision_friction, 0.0, 1.0));\n";
  931. code += " // Add bounce velocity to VELOCITY.\n";
  932. code += " VELOCITY -= COLLISION_NORMAL * collision_response * (collision_bounce * slide_to_bounce_trigger);\n";
  933. code += " }\n\n";
  934. } else if (collision_mode == COLLISION_HIDE_ON_CONTACT) {
  935. code += " if (COLLIDED) {\n";
  936. code += " ACTIVE = false;\n";
  937. code += " }\n\n";
  938. }
  939. code += " // Turbulence before limiting.\n";
  940. if (turbulence_enabled) {
  941. if (tex_parameters[PARAM_TURB_INFLUENCE_OVER_LIFE].is_valid()) {
  942. code += " float turbulence_influence = textureLod(turbulence_influence_over_life, vec2(lifetime_percent, 0.0), 0.0).r;\n";
  943. } else {
  944. code += " float turbulence_influence = 1.0;\n";
  945. }
  946. code += "\n";
  947. code += " vec3 noise_direction = get_noise_direction(TRANSFORM[3].xyz);\n";
  948. // Godot detects when the COLLIDED keyword is used. If it's used anywhere in the shader then Godot will generate the screen space SDF for collisions.
  949. // We don't need it as long as collision is disabled. Refer to GH-83744 for more info.
  950. if (collision_mode == COLLISION_RIGID) {
  951. code += " if (!COLLIDED) {\n";
  952. } else {
  953. code += " {\n";
  954. }
  955. code += " float vel_mag = length(VELOCITY);\n";
  956. code += " float vel_infl = clamp(dynamic_params.turb_influence * turbulence_influence, 0.0, 1.0) * (DELTA <= 0.0 ? 0.0 : 1.0);\n";
  957. code += " VELOCITY = mix(VELOCITY, normalize(noise_direction) * vel_mag * (1.0 + (1.0 - vel_infl) * 0.2), vel_infl);\n";
  958. code += " vel_mag = length(controlled_displacement);\n";
  959. code += " controlled_displacement = mix(controlled_displacement, normalize(noise_direction) * vel_mag * (1.0 + (1.0 - vel_infl) * 0.2), vel_infl);\n";
  960. code += " }\n";
  961. }
  962. code += " vec3 final_velocity = controlled_displacement + VELOCITY;\n\n";
  963. if (velocity_limit_curve.is_valid()) {
  964. code += " // Limit velocity.\n";
  965. code += " if (length(final_velocity) > 0.001) {\n";
  966. code += " final_velocity = normalize(final_velocity) * min(abs(length(final_velocity)), abs(texture(velocity_limit_curve, vec2(lifetime_percent)).r));\n";
  967. code += " }\n\n";
  968. }
  969. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  970. code += " final_velocity.z = 0.0;\n\n";
  971. }
  972. code += " TRANSFORM[3].xyz += final_velocity * DELTA;\n\n";
  973. code += " process_display_param(params, lifetime_percent);\n\n";
  974. code += " float base_angle = dynamic_params.angle;\n";
  975. if (tex_parameters[PARAM_ANGLE].is_valid()) {
  976. code += " base_angle *= texture(angle_texture, vec2(lifetime_percent)).r;\n";
  977. }
  978. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  979. code += " base_angle += CUSTOM.y * LIFETIME * dynamic_params.angular_velocity * texture(angular_velocity_texture, vec2(lifetime_percent)).r;\n";
  980. } else {
  981. code += " base_angle += CUSTOM.y * LIFETIME * dynamic_params.angular_velocity;\n";
  982. }
  983. code += " CUSTOM.x = base_angle * degree_to_rad;\n";
  984. code += " COLOR = params.color;\n\n";
  985. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  986. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  987. code += " if (length(final_velocity) > 0.0) {\n";
  988. code += " TRANSFORM[1].xyz = normalize(final_velocity);\n";
  989. code += " } else {\n";
  990. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  991. code += " }\n";
  992. code += " TRANSFORM[0].xyz = normalize(cross(TRANSFORM[1].xyz, TRANSFORM[2].xyz));\n";
  993. code += " TRANSFORM[2] = vec4(0.0, 0.0, 1.0, 0.0);\n";
  994. } else {
  995. code += " TRANSFORM[0] = vec4(cos(CUSTOM.x), -sin(CUSTOM.x), 0.0, 0.0);\n";
  996. code += " TRANSFORM[1] = vec4(sin(CUSTOM.x), cos(CUSTOM.x), 0.0, 0.0);\n";
  997. code += " TRANSFORM[2] = vec4(0.0, 0.0, 1.0, 0.0);\n";
  998. }
  999. } else {
  1000. // Orient particle Y towards velocity.
  1001. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  1002. code += " if (length(final_velocity) > 0.0) {\n";
  1003. code += " TRANSFORM[1].xyz = normalize(final_velocity);\n";
  1004. code += " } else {\n";
  1005. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  1006. code += " }\n";
  1007. code += " if (TRANSFORM[1].xyz == normalize(TRANSFORM[0].xyz)) {\n";
  1008. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz), normalize(TRANSFORM[2].xyz)));\n";
  1009. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz), normalize(TRANSFORM[1].xyz)));\n";
  1010. code += " } else {\n";
  1011. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz), normalize(TRANSFORM[1].xyz)));\n";
  1012. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz), normalize(TRANSFORM[2].xyz)));\n";
  1013. code += " }\n";
  1014. } else {
  1015. code += " TRANSFORM[0].xyz = normalize(TRANSFORM[0].xyz);\n";
  1016. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  1017. code += " TRANSFORM[2].xyz = normalize(TRANSFORM[2].xyz);\n";
  1018. }
  1019. // Turn particle by rotation in Y.
  1020. if (particle_flags[PARTICLE_FLAG_ROTATE_Y]) {
  1021. code += " vec4 origin = TRANSFORM[3];\n";
  1022. code += " 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";
  1023. code += " TRANSFORM[3] = origin;\n";
  1024. }
  1025. }
  1026. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  1027. code += " TRANSFORM[3].z = 0.0;\n\n";
  1028. }
  1029. if (tex_parameters[PARAM_SCALE_OVER_VELOCITY].is_valid()) {
  1030. code += " if (length(final_velocity) > 0.001) {\n";
  1031. code += " params.scale *= texture(scale_over_velocity_curve, vec2(clamp(length(final_velocity) / (scale_over_velocity_max - scale_over_velocity_min), 0.0, 1.0), 0.0)).rgb;\n";
  1032. code += " } else {\n";
  1033. code += " params.scale *= texture(scale_over_velocity_curve, vec2(0.0)).rgb;\n";
  1034. code += " }\n";
  1035. }
  1036. // A scale of 0 results in no emission at some emission amounts (including 3 and 6).
  1037. // `sign(scale)` is unsuitable, because sign(0) returns 0, nullifying the minimum value.
  1038. // The following evaluates to 1 when scale is 0, falling back to a positive minimum value.
  1039. code += " float scale_sign_x = params.scale.x < 0.0 ? -1.0 : 1.0;\n";
  1040. code += " float scale_sign_y = params.scale.y < 0.0 ? -1.0 : 1.0;\n";
  1041. code += " float scale_sign_z = params.scale.z < 0.0 ? -1.0 : 1.0;\n";
  1042. code += " float scale_minimum = 0.001;\n";
  1043. code += " TRANSFORM[0].xyz *= scale_sign_x * max(abs(params.scale.x), scale_minimum);\n";
  1044. code += " TRANSFORM[1].xyz *= scale_sign_y * max(abs(params.scale.y), scale_minimum);\n";
  1045. code += " TRANSFORM[2].xyz *= scale_sign_z * max(abs(params.scale.z), scale_minimum);\n";
  1046. code += "\n";
  1047. code += " CUSTOM.z = params.animation_offset + lifetime_percent * params.animation_speed;\n\n";
  1048. if (sub_emitter_mode != SUB_EMITTER_DISABLED && !RenderingServer::get_singleton()->is_low_end()) {
  1049. code += " int emit_count = 0;\n";
  1050. switch (sub_emitter_mode) {
  1051. case SUB_EMITTER_CONSTANT: {
  1052. code += " float interval_from = CUSTOM.y * LIFETIME - DELTA;\n";
  1053. code += " float interval_rem = sub_emitter_frequency - mod(interval_from, sub_emitter_frequency);\n";
  1054. code += " if (DELTA >= interval_rem) {\n";
  1055. code += " emit_count = 1;\n";
  1056. code += " }\n";
  1057. } break;
  1058. case SUB_EMITTER_AT_COLLISION: {
  1059. code += " if (COLLIDED) {\n";
  1060. code += " emit_count = sub_emitter_amount_at_collision;\n";
  1061. code += " }\n";
  1062. } break;
  1063. case SUB_EMITTER_AT_END: {
  1064. code += " if ((CUSTOM.y / CUSTOM.w * LIFETIME) > (LIFETIME - DELTA)) {\n";
  1065. code += " emit_count = sub_emitter_amount_at_end;\n";
  1066. code += " }\n";
  1067. } break;
  1068. case SUB_EMITTER_AT_START: {
  1069. code += " if (just_spawned) {\n";
  1070. code += " emit_count = sub_emitter_amount_at_start;\n";
  1071. code += " }\n";
  1072. } break;
  1073. default: {
  1074. }
  1075. }
  1076. code += " for (int i = 0; i < emit_count; i++) {\n";
  1077. code += " uint flags = FLAG_EMIT_POSITION | FLAG_EMIT_ROT_SCALE;\n";
  1078. code += " if (sub_emitter_keep_velocity) {\n";
  1079. code += " flags |= FLAG_EMIT_VELOCITY;\n";
  1080. code += " }\n";
  1081. code += " emit_subparticle(TRANSFORM, VELOCITY, vec4(0.0), vec4(0.0), flags);\n";
  1082. code += " }\n\n";
  1083. }
  1084. code += " if (CUSTOM.y > CUSTOM.w) {\n";
  1085. code += " ACTIVE = false;\n";
  1086. code += " }\n";
  1087. code += "}\n";
  1088. // We must create the shader outside the shader_map_mutex to avoid potential deadlocks with
  1089. // other tasks in the WorkerThreadPool simultaneously creating materials, which
  1090. // may also hold the shared shader_map_mutex lock.
  1091. RID new_shader = RS::get_singleton()->shader_create_from_code(code);
  1092. MutexLock lock(shader_map_mutex);
  1093. ShaderData *v = shader_map.getptr(mk);
  1094. if (unlikely(v)) {
  1095. // We raced and managed to create the same key concurrently, so we'll free the shader we just created,
  1096. // given we know it isn't used, and use the winner.
  1097. RS::get_singleton()->free(new_shader);
  1098. } else {
  1099. ShaderData shader_data;
  1100. shader_data.shader = new_shader;
  1101. // ShaderData will be inserted with a users count of 0, but we
  1102. // increment unconditionally outside this if block, whilst still under lock.
  1103. v = &shader_map.insert(mk, shader_data)->value;
  1104. }
  1105. shader_rid = v->shader;
  1106. v->users++;
  1107. RS::get_singleton()->material_set_shader(_get_material(), shader_rid);
  1108. }
  1109. void ParticleProcessMaterial::flush_changes() {
  1110. MutexLock lock(dirty_materials_mutex);
  1111. while (dirty_materials.first()) {
  1112. dirty_materials.first()->self()->_update_shader();
  1113. dirty_materials.first()->remove_from_list();
  1114. }
  1115. }
  1116. void ParticleProcessMaterial::_queue_shader_change() {
  1117. if (!_is_initialized()) {
  1118. return;
  1119. }
  1120. MutexLock lock(dirty_materials_mutex);
  1121. if (!element.in_list()) {
  1122. dirty_materials.add(&element);
  1123. }
  1124. }
  1125. bool ParticleProcessMaterial::has_min_max_property(const String &p_name) {
  1126. return min_max_properties.has(p_name);
  1127. }
  1128. void ParticleProcessMaterial::set_direction(Vector3 p_direction) {
  1129. direction = p_direction;
  1130. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->direction, direction);
  1131. }
  1132. Vector3 ParticleProcessMaterial::get_direction() const {
  1133. return direction;
  1134. }
  1135. void ParticleProcessMaterial::set_spread(float p_spread) {
  1136. spread = p_spread;
  1137. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->spread, p_spread);
  1138. }
  1139. float ParticleProcessMaterial::get_spread() const {
  1140. return spread;
  1141. }
  1142. void ParticleProcessMaterial::set_flatness(float p_flatness) {
  1143. flatness = p_flatness;
  1144. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->flatness, p_flatness);
  1145. }
  1146. float ParticleProcessMaterial::get_flatness() const {
  1147. return flatness;
  1148. }
  1149. void ParticleProcessMaterial::set_velocity_pivot(const Vector3 &p_pivot) {
  1150. velocity_pivot = p_pivot;
  1151. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->velocity_pivot, p_pivot);
  1152. }
  1153. Vector3 ParticleProcessMaterial::get_velocity_pivot() {
  1154. return velocity_pivot;
  1155. }
  1156. void ParticleProcessMaterial::set_param(Parameter p_param, const Vector2 &p_value) {
  1157. set_param_min(p_param, p_value.x);
  1158. set_param_max(p_param, p_value.y);
  1159. }
  1160. Vector2 ParticleProcessMaterial::get_param(Parameter p_param) const {
  1161. return Vector2(get_param_min(p_param), get_param_max(p_param));
  1162. }
  1163. void ParticleProcessMaterial::set_param_min(Parameter p_param, float p_value) {
  1164. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  1165. params_min[p_param] = p_value;
  1166. if (params_min[p_param] > params_max[p_param]) {
  1167. set_param_max(p_param, p_value);
  1168. }
  1169. switch (p_param) {
  1170. case PARAM_INITIAL_LINEAR_VELOCITY: {
  1171. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity_min, p_value);
  1172. } break;
  1173. case PARAM_ANGULAR_VELOCITY: {
  1174. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_min, p_value);
  1175. } break;
  1176. case PARAM_ORBIT_VELOCITY: {
  1177. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_min, p_value);
  1178. } break;
  1179. case PARAM_LINEAR_ACCEL: {
  1180. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_min, p_value);
  1181. } break;
  1182. case PARAM_RADIAL_ACCEL: {
  1183. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_min, p_value);
  1184. } break;
  1185. case PARAM_TANGENTIAL_ACCEL: {
  1186. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_min, p_value);
  1187. } break;
  1188. case PARAM_DAMPING: {
  1189. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_min, p_value);
  1190. } break;
  1191. case PARAM_ANGLE: {
  1192. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle_min, p_value);
  1193. } break;
  1194. case PARAM_SCALE: {
  1195. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_min, p_value);
  1196. } break;
  1197. case PARAM_HUE_VARIATION: {
  1198. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_min, p_value);
  1199. } break;
  1200. case PARAM_ANIM_SPEED: {
  1201. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_min, p_value);
  1202. } break;
  1203. case PARAM_ANIM_OFFSET: {
  1204. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_min, p_value);
  1205. } break;
  1206. case PARAM_TURB_VEL_INFLUENCE: {
  1207. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_influence_min, p_value);
  1208. } break;
  1209. case PARAM_TURB_INIT_DISPLACEMENT: {
  1210. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_initial_displacement_min, p_value);
  1211. } break;
  1212. case PARAM_TURB_INFLUENCE_OVER_LIFE: {
  1213. // Can't happen, but silences warning
  1214. } break;
  1215. case PARAM_RADIAL_VELOCITY: {
  1216. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_velocity_min, p_value);
  1217. } break;
  1218. case PARAM_SCALE_OVER_VELOCITY: {
  1219. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_over_velocity_min, p_value);
  1220. } break;
  1221. case PARAM_DIRECTIONAL_VELOCITY: {
  1222. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->directional_velocity_min, p_value);
  1223. } break;
  1224. case PARAM_MAX:
  1225. break; // Can't happen, but silences warning
  1226. }
  1227. }
  1228. float ParticleProcessMaterial::get_param_min(Parameter p_param) const {
  1229. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  1230. return params_min[p_param];
  1231. }
  1232. void ParticleProcessMaterial::set_param_max(Parameter p_param, float p_value) {
  1233. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  1234. params_max[p_param] = p_value;
  1235. if (params_min[p_param] > params_max[p_param]) {
  1236. set_param_min(p_param, p_value);
  1237. }
  1238. switch (p_param) {
  1239. case PARAM_INITIAL_LINEAR_VELOCITY: {
  1240. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity_max, p_value);
  1241. } break;
  1242. case PARAM_ANGULAR_VELOCITY: {
  1243. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_max, p_value);
  1244. } break;
  1245. case PARAM_ORBIT_VELOCITY: {
  1246. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_max, p_value);
  1247. } break;
  1248. case PARAM_LINEAR_ACCEL: {
  1249. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_max, p_value);
  1250. } break;
  1251. case PARAM_RADIAL_ACCEL: {
  1252. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_max, p_value);
  1253. } break;
  1254. case PARAM_TANGENTIAL_ACCEL: {
  1255. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_max, p_value);
  1256. } break;
  1257. case PARAM_DAMPING: {
  1258. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_max, p_value);
  1259. } break;
  1260. case PARAM_ANGLE: {
  1261. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle_max, p_value);
  1262. } break;
  1263. case PARAM_SCALE: {
  1264. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_max, p_value);
  1265. } break;
  1266. case PARAM_HUE_VARIATION: {
  1267. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_max, p_value);
  1268. } break;
  1269. case PARAM_ANIM_SPEED: {
  1270. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_max, p_value);
  1271. } break;
  1272. case PARAM_ANIM_OFFSET: {
  1273. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_max, p_value);
  1274. } break;
  1275. case PARAM_TURB_VEL_INFLUENCE: {
  1276. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_influence_max, p_value);
  1277. } break;
  1278. case PARAM_TURB_INIT_DISPLACEMENT: {
  1279. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_initial_displacement_max, p_value);
  1280. } break;
  1281. case PARAM_TURB_INFLUENCE_OVER_LIFE: {
  1282. // Can't happen, but silences warning
  1283. } break;
  1284. case PARAM_RADIAL_VELOCITY: {
  1285. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_velocity_max, p_value);
  1286. } break;
  1287. case PARAM_SCALE_OVER_VELOCITY: {
  1288. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_over_velocity_max, p_value);
  1289. } break;
  1290. case PARAM_DIRECTIONAL_VELOCITY: {
  1291. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->directional_velocity_max, p_value);
  1292. } break;
  1293. case PARAM_MAX:
  1294. break; // Can't happen, but silences warning
  1295. }
  1296. }
  1297. float ParticleProcessMaterial::get_param_max(Parameter p_param) const {
  1298. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  1299. return params_max[p_param];
  1300. }
  1301. static void _adjust_curve_range(const Ref<Texture2D> &p_texture, float p_min, float p_max) {
  1302. Ref<CurveTexture> curve_tex = p_texture;
  1303. if (curve_tex.is_valid()) {
  1304. curve_tex->ensure_default_setup(p_min, p_max);
  1305. return;
  1306. }
  1307. Ref<CurveXYZTexture> curve_xyz_tex = p_texture;
  1308. if (curve_xyz_tex.is_valid()) {
  1309. curve_xyz_tex->ensure_default_setup(p_min, p_max);
  1310. return;
  1311. }
  1312. }
  1313. void ParticleProcessMaterial::set_param_texture(Parameter p_param, const Ref<Texture2D> &p_texture) {
  1314. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  1315. tex_parameters[p_param] = p_texture;
  1316. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1317. switch (p_param) {
  1318. case PARAM_INITIAL_LINEAR_VELOCITY: {
  1319. //do none for this one
  1320. } break;
  1321. case PARAM_ANGULAR_VELOCITY: {
  1322. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_texture, tex_rid);
  1323. _adjust_curve_range(p_texture, -360, 360);
  1324. } break;
  1325. case PARAM_ORBIT_VELOCITY: {
  1326. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_texture, tex_rid);
  1327. _adjust_curve_range(p_texture, -2, 2);
  1328. notify_property_list_changed();
  1329. } break;
  1330. case PARAM_LINEAR_ACCEL: {
  1331. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_texture, tex_rid);
  1332. _adjust_curve_range(p_texture, -200, 200);
  1333. } break;
  1334. case PARAM_RADIAL_ACCEL: {
  1335. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_texture, tex_rid);
  1336. _adjust_curve_range(p_texture, -200, 200);
  1337. } break;
  1338. case PARAM_TANGENTIAL_ACCEL: {
  1339. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_texture, tex_rid);
  1340. _adjust_curve_range(p_texture, -200, 200);
  1341. } break;
  1342. case PARAM_DAMPING: {
  1343. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_texture, tex_rid);
  1344. _adjust_curve_range(p_texture, 0, 100);
  1345. } break;
  1346. case PARAM_ANGLE: {
  1347. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->angle_texture, tex_rid);
  1348. _adjust_curve_range(p_texture, -360, 360);
  1349. } break;
  1350. case PARAM_SCALE: {
  1351. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_texture, tex_rid);
  1352. _adjust_curve_range(p_texture, 0, 1);
  1353. } break;
  1354. case PARAM_HUE_VARIATION: {
  1355. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_texture, tex_rid);
  1356. _adjust_curve_range(p_texture, -1, 1);
  1357. } break;
  1358. case PARAM_ANIM_SPEED: {
  1359. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_texture, tex_rid);
  1360. _adjust_curve_range(p_texture, 0, 200);
  1361. } break;
  1362. case PARAM_ANIM_OFFSET: {
  1363. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_texture, tex_rid);
  1364. } break;
  1365. case PARAM_TURB_INFLUENCE_OVER_LIFE: {
  1366. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_influence_over_life, tex_rid);
  1367. _adjust_curve_range(p_texture, 0, 1);
  1368. } break;
  1369. case PARAM_TURB_VEL_INFLUENCE: {
  1370. // Can't happen, but silences warning
  1371. } break;
  1372. case PARAM_TURB_INIT_DISPLACEMENT: {
  1373. // Can't happen, but silences warning
  1374. } break;
  1375. case PARAM_RADIAL_VELOCITY: {
  1376. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_velocity_texture, tex_rid);
  1377. } break;
  1378. case PARAM_SCALE_OVER_VELOCITY: {
  1379. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_over_velocity_texture, tex_rid);
  1380. _adjust_curve_range(p_texture, 0, 3);
  1381. notify_property_list_changed();
  1382. } break;
  1383. case PARAM_DIRECTIONAL_VELOCITY: {
  1384. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->directional_velocity_texture, tex_rid);
  1385. notify_property_list_changed();
  1386. } break;
  1387. case PARAM_MAX:
  1388. break; // Can't happen, but silences warning
  1389. }
  1390. _queue_shader_change();
  1391. }
  1392. Ref<Texture2D> ParticleProcessMaterial::get_param_texture(Parameter p_param) const {
  1393. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Texture2D>());
  1394. return tex_parameters[p_param];
  1395. }
  1396. void ParticleProcessMaterial::set_color(const Color &p_color) {
  1397. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->color, p_color);
  1398. color = p_color;
  1399. }
  1400. Color ParticleProcessMaterial::get_color() const {
  1401. return color;
  1402. }
  1403. void ParticleProcessMaterial::set_color_ramp(const Ref<Texture2D> &p_texture) {
  1404. color_ramp = p_texture;
  1405. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1406. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->color_ramp, tex_rid);
  1407. _queue_shader_change();
  1408. notify_property_list_changed();
  1409. }
  1410. Ref<Texture2D> ParticleProcessMaterial::get_color_ramp() const {
  1411. return color_ramp;
  1412. }
  1413. void ParticleProcessMaterial::set_color_initial_ramp(const Ref<Texture2D> &p_texture) {
  1414. color_initial_ramp = p_texture;
  1415. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1416. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->color_initial_ramp, tex_rid);
  1417. _queue_shader_change();
  1418. notify_property_list_changed();
  1419. }
  1420. Ref<Texture2D> ParticleProcessMaterial::get_color_initial_ramp() const {
  1421. return color_initial_ramp;
  1422. }
  1423. void ParticleProcessMaterial::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
  1424. ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
  1425. particle_flags[p_particle_flag] = p_enable;
  1426. _queue_shader_change();
  1427. if (p_particle_flag == PARTICLE_FLAG_DISABLE_Z) {
  1428. notify_property_list_changed();
  1429. }
  1430. }
  1431. void ParticleProcessMaterial::set_alpha_curve(const Ref<Texture2D> &p_texture) {
  1432. alpha_curve = p_texture;
  1433. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1434. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->alpha_ramp, tex_rid);
  1435. _queue_shader_change();
  1436. notify_property_list_changed();
  1437. }
  1438. Ref<Texture2D> ParticleProcessMaterial::get_alpha_curve() const {
  1439. return alpha_curve;
  1440. }
  1441. void ParticleProcessMaterial::set_emission_curve(const Ref<Texture2D> &p_texture) {
  1442. emission_curve = p_texture;
  1443. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1444. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ramp, tex_rid);
  1445. _queue_shader_change();
  1446. notify_property_list_changed();
  1447. }
  1448. Ref<Texture2D> ParticleProcessMaterial::get_emission_curve() const {
  1449. return emission_curve;
  1450. }
  1451. void ParticleProcessMaterial::set_velocity_limit_curve(const Ref<Texture2D> &p_texture) {
  1452. velocity_limit_curve = p_texture;
  1453. Variant tex_rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  1454. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->velocity_limit_curve, tex_rid);
  1455. _queue_shader_change();
  1456. notify_property_list_changed();
  1457. }
  1458. Ref<Texture2D> ParticleProcessMaterial::get_velocity_limit_curve() const {
  1459. return velocity_limit_curve;
  1460. }
  1461. bool ParticleProcessMaterial::get_particle_flag(ParticleFlags p_particle_flag) const {
  1462. ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
  1463. return particle_flags[p_particle_flag];
  1464. }
  1465. void ParticleProcessMaterial::set_emission_shape(EmissionShape p_shape) {
  1466. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  1467. emission_shape = p_shape;
  1468. notify_property_list_changed();
  1469. _queue_shader_change();
  1470. #ifdef TOOLS_ENABLED
  1471. emit_signal("emission_shape_changed");
  1472. #endif
  1473. }
  1474. void ParticleProcessMaterial::set_emission_sphere_radius(real_t p_radius) {
  1475. emission_sphere_radius = p_radius;
  1476. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_sphere_radius, p_radius);
  1477. #ifdef TOOLS_ENABLED
  1478. emit_signal("emission_shape_changed");
  1479. #endif
  1480. }
  1481. void ParticleProcessMaterial::set_emission_box_extents(Vector3 p_extents) {
  1482. emission_box_extents = p_extents;
  1483. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_box_extents, p_extents);
  1484. #ifdef TOOLS_ENABLED
  1485. emit_signal("emission_shape_changed");
  1486. #endif
  1487. }
  1488. void ParticleProcessMaterial::set_emission_point_texture(const Ref<Texture2D> &p_points) {
  1489. emission_point_texture = p_points;
  1490. Variant tex_rid = p_points.is_valid() ? Variant(p_points->get_rid()) : Variant();
  1491. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_points, tex_rid);
  1492. }
  1493. void ParticleProcessMaterial::set_emission_normal_texture(const Ref<Texture2D> &p_normals) {
  1494. emission_normal_texture = p_normals;
  1495. Variant tex_rid = p_normals.is_valid() ? Variant(p_normals->get_rid()) : Variant();
  1496. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_normal, tex_rid);
  1497. }
  1498. void ParticleProcessMaterial::set_emission_color_texture(const Ref<Texture2D> &p_colors) {
  1499. emission_color_texture = p_colors;
  1500. Variant tex_rid = p_colors.is_valid() ? Variant(p_colors->get_rid()) : Variant();
  1501. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_color, tex_rid);
  1502. _queue_shader_change();
  1503. }
  1504. void ParticleProcessMaterial::set_emission_point_count(int p_count) {
  1505. emission_point_count = p_count;
  1506. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_point_count, p_count);
  1507. }
  1508. void ParticleProcessMaterial::set_emission_ring_axis(Vector3 p_axis) {
  1509. emission_ring_axis = p_axis;
  1510. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ring_axis, p_axis);
  1511. #ifdef TOOLS_ENABLED
  1512. emit_signal("emission_shape_changed");
  1513. #endif
  1514. }
  1515. void ParticleProcessMaterial::set_emission_ring_height(real_t p_height) {
  1516. emission_ring_height = p_height;
  1517. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ring_height, p_height);
  1518. #ifdef TOOLS_ENABLED
  1519. emit_signal("emission_shape_changed");
  1520. #endif
  1521. }
  1522. void ParticleProcessMaterial::set_emission_ring_radius(real_t p_radius) {
  1523. emission_ring_radius = p_radius;
  1524. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ring_radius, p_radius);
  1525. #ifdef TOOLS_ENABLED
  1526. emit_signal("emission_shape_changed");
  1527. #endif
  1528. }
  1529. void ParticleProcessMaterial::set_emission_ring_inner_radius(real_t p_radius) {
  1530. emission_ring_inner_radius = p_radius;
  1531. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ring_inner_radius, p_radius);
  1532. #ifdef TOOLS_ENABLED
  1533. emit_signal("emission_shape_changed");
  1534. #endif
  1535. }
  1536. void ParticleProcessMaterial::set_emission_ring_cone_angle(real_t p_angle) {
  1537. emission_ring_cone_angle = p_angle;
  1538. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_ring_cone_angle, p_angle);
  1539. #ifdef TOOLS_ENABLED
  1540. emit_signal("emission_shape_changed");
  1541. #endif
  1542. }
  1543. void ParticleProcessMaterial::set_inherit_velocity_ratio(double p_ratio) {
  1544. inherit_emitter_velocity_ratio = p_ratio;
  1545. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->inherit_emitter_velocity_ratio, p_ratio);
  1546. }
  1547. ParticleProcessMaterial::EmissionShape ParticleProcessMaterial::get_emission_shape() const {
  1548. return emission_shape;
  1549. }
  1550. real_t ParticleProcessMaterial::get_emission_sphere_radius() const {
  1551. return emission_sphere_radius;
  1552. }
  1553. Vector3 ParticleProcessMaterial::get_emission_box_extents() const {
  1554. return emission_box_extents;
  1555. }
  1556. Ref<Texture2D> ParticleProcessMaterial::get_emission_point_texture() const {
  1557. return emission_point_texture;
  1558. }
  1559. Ref<Texture2D> ParticleProcessMaterial::get_emission_normal_texture() const {
  1560. return emission_normal_texture;
  1561. }
  1562. Ref<Texture2D> ParticleProcessMaterial::get_emission_color_texture() const {
  1563. return emission_color_texture;
  1564. }
  1565. int ParticleProcessMaterial::get_emission_point_count() const {
  1566. return emission_point_count;
  1567. }
  1568. Vector3 ParticleProcessMaterial::get_emission_ring_axis() const {
  1569. return emission_ring_axis;
  1570. }
  1571. real_t ParticleProcessMaterial::get_emission_ring_height() const {
  1572. return emission_ring_height;
  1573. }
  1574. real_t ParticleProcessMaterial::get_emission_ring_radius() const {
  1575. return emission_ring_radius;
  1576. }
  1577. real_t ParticleProcessMaterial::get_emission_ring_inner_radius() const {
  1578. return emission_ring_inner_radius;
  1579. }
  1580. real_t ParticleProcessMaterial::get_emission_ring_cone_angle() const {
  1581. return emission_ring_cone_angle;
  1582. }
  1583. void ParticleProcessMaterial::set_emission_shape_offset(const Vector3 &p_emission_shape_offset) {
  1584. emission_shape_offset = p_emission_shape_offset;
  1585. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_shape_offset, p_emission_shape_offset);
  1586. #ifdef TOOLS_ENABLED
  1587. emit_signal("emission_shape_changed");
  1588. #endif
  1589. }
  1590. Vector3 ParticleProcessMaterial::get_emission_shape_offset() const {
  1591. return emission_shape_offset;
  1592. }
  1593. void ParticleProcessMaterial::set_emission_shape_scale(const Vector3 &p_emission_shape_scale) {
  1594. emission_shape_scale = p_emission_shape_scale;
  1595. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_shape_scale, p_emission_shape_scale);
  1596. #ifdef TOOLS_ENABLED
  1597. emit_signal("emission_shape_changed");
  1598. #endif
  1599. }
  1600. Vector3 ParticleProcessMaterial::get_emission_shape_scale() const {
  1601. return emission_shape_scale;
  1602. }
  1603. double ParticleProcessMaterial::get_inherit_velocity_ratio() {
  1604. return inherit_emitter_velocity_ratio;
  1605. }
  1606. void ParticleProcessMaterial::set_turbulence_enabled(const bool p_turbulence_enabled) {
  1607. turbulence_enabled = p_turbulence_enabled;
  1608. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_enabled, turbulence_enabled);
  1609. _queue_shader_change();
  1610. notify_property_list_changed();
  1611. }
  1612. bool ParticleProcessMaterial::get_turbulence_enabled() const {
  1613. return turbulence_enabled;
  1614. }
  1615. void ParticleProcessMaterial::set_turbulence_noise_strength(float p_turbulence_noise_strength) {
  1616. turbulence_noise_strength = p_turbulence_noise_strength;
  1617. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_noise_strength, p_turbulence_noise_strength);
  1618. }
  1619. float ParticleProcessMaterial::get_turbulence_noise_strength() const {
  1620. return turbulence_noise_strength;
  1621. }
  1622. void ParticleProcessMaterial::set_turbulence_noise_scale(float p_turbulence_noise_scale) {
  1623. turbulence_noise_scale = p_turbulence_noise_scale;
  1624. const float noise_frequency_when_slider_is_zero = 4.0;
  1625. const float max_slider_value = 10.0;
  1626. const float curve_exponent = 0.25;
  1627. const float curve_rescale = noise_frequency_when_slider_is_zero / std::pow(max_slider_value, curve_exponent);
  1628. float shader_turbulence_noise_scale = std::pow(p_turbulence_noise_scale, curve_exponent) * curve_rescale - noise_frequency_when_slider_is_zero;
  1629. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_noise_scale, shader_turbulence_noise_scale);
  1630. }
  1631. float ParticleProcessMaterial::get_turbulence_noise_scale() const {
  1632. return turbulence_noise_scale;
  1633. }
  1634. void ParticleProcessMaterial::set_turbulence_noise_speed_random(float p_turbulence_noise_speed_random) {
  1635. turbulence_noise_speed_random = p_turbulence_noise_speed_random;
  1636. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_noise_speed_random, p_turbulence_noise_speed_random);
  1637. }
  1638. float ParticleProcessMaterial::get_turbulence_noise_speed_random() const {
  1639. return turbulence_noise_speed_random;
  1640. }
  1641. void ParticleProcessMaterial::set_turbulence_noise_speed(const Vector3 &p_turbulence_noise_speed) {
  1642. turbulence_noise_speed = p_turbulence_noise_speed;
  1643. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->turbulence_noise_speed, turbulence_noise_speed);
  1644. }
  1645. Vector3 ParticleProcessMaterial::get_turbulence_noise_speed() const {
  1646. return turbulence_noise_speed;
  1647. }
  1648. void ParticleProcessMaterial::set_gravity(const Vector3 &p_gravity) {
  1649. gravity = p_gravity;
  1650. Vector3 gset = gravity;
  1651. if (gset == Vector3()) {
  1652. gset = Vector3(0, -0.000001, 0); //as gravity is used as upvector in some calculations
  1653. }
  1654. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->gravity, gset);
  1655. }
  1656. Vector3 ParticleProcessMaterial::get_gravity() const {
  1657. return gravity;
  1658. }
  1659. void ParticleProcessMaterial::set_lifetime_randomness(double p_lifetime) {
  1660. lifetime_randomness = p_lifetime;
  1661. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->lifetime_randomness, lifetime_randomness);
  1662. }
  1663. double ParticleProcessMaterial::get_lifetime_randomness() const {
  1664. return lifetime_randomness;
  1665. }
  1666. RID ParticleProcessMaterial::get_rid() const {
  1667. const_cast<ParticleProcessMaterial *>(this)->_update_shader();
  1668. return Material::get_rid();
  1669. }
  1670. RID ParticleProcessMaterial::get_shader_rid() const {
  1671. const_cast<ParticleProcessMaterial *>(this)->_update_shader();
  1672. return shader_rid;
  1673. }
  1674. void ParticleProcessMaterial::_validate_property(PropertyInfo &p_property) const {
  1675. if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
  1676. p_property.usage = PROPERTY_USAGE_NONE;
  1677. }
  1678. if (p_property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
  1679. p_property.usage = PROPERTY_USAGE_NONE;
  1680. }
  1681. if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS)) {
  1682. p_property.usage = PROPERTY_USAGE_NONE;
  1683. }
  1684. if (p_property.name == "emission_normal_texture" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  1685. p_property.usage = PROPERTY_USAGE_NONE;
  1686. }
  1687. if (p_property.name == "emission_point_count" && (emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS)) {
  1688. p_property.usage = PROPERTY_USAGE_NONE;
  1689. }
  1690. if (p_property.name.begins_with("emission_ring_") && emission_shape != EMISSION_SHAPE_RING) {
  1691. p_property.usage = PROPERTY_USAGE_NONE;
  1692. }
  1693. if (p_property.name == "sub_emitter_frequency" && sub_emitter_mode != SUB_EMITTER_CONSTANT) {
  1694. p_property.usage = PROPERTY_USAGE_NONE;
  1695. }
  1696. if (p_property.name == "sub_emitter_amount_at_end" && sub_emitter_mode != SUB_EMITTER_AT_END) {
  1697. p_property.usage = PROPERTY_USAGE_NONE;
  1698. }
  1699. if (p_property.name == "sub_emitter_amount_at_collision" && sub_emitter_mode != SUB_EMITTER_AT_COLLISION) {
  1700. p_property.usage = PROPERTY_USAGE_NONE;
  1701. }
  1702. if (p_property.name == "sub_emitter_amount_at_start" && sub_emitter_mode != SUB_EMITTER_AT_START) {
  1703. p_property.usage = PROPERTY_USAGE_NONE;
  1704. }
  1705. if (p_property.name == "collision_friction" && collision_mode != COLLISION_RIGID) {
  1706. p_property.usage = PROPERTY_USAGE_NONE;
  1707. }
  1708. if (p_property.name == "collision_bounce" && collision_mode != COLLISION_RIGID) {
  1709. p_property.usage = PROPERTY_USAGE_NONE;
  1710. }
  1711. if ((p_property.name == "directional_velocity_min" || p_property.name == "directional_velocity_max") && !tex_parameters[PARAM_DIRECTIONAL_VELOCITY].is_valid()) {
  1712. p_property.usage = PROPERTY_USAGE_NONE;
  1713. }
  1714. if (Engine::get_singleton()->is_editor_hint()) {
  1715. if ((p_property.name == "scale_over_velocity_min" || p_property.name == "scale_over_velocity_max") && !tex_parameters[PARAM_SCALE_OVER_VELOCITY].is_valid()) {
  1716. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1717. }
  1718. if ((p_property.name == "orbit_velocity_min" || p_property.name == "orbit_velocity_max") && (!tex_parameters[PARAM_ORBIT_VELOCITY].is_valid() && !particle_flags[PARTICLE_FLAG_DISABLE_Z])) {
  1719. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1720. }
  1721. if (p_property.usage & PROPERTY_USAGE_EDITOR && (p_property.name.ends_with("_min") || p_property.name.ends_with("_max"))) {
  1722. p_property.usage &= ~PROPERTY_USAGE_EDITOR;
  1723. }
  1724. }
  1725. }
  1726. void ParticleProcessMaterial::set_sub_emitter_mode(SubEmitterMode p_sub_emitter_mode) {
  1727. sub_emitter_mode = p_sub_emitter_mode;
  1728. _queue_shader_change();
  1729. notify_property_list_changed();
  1730. if (sub_emitter_mode != SUB_EMITTER_DISABLED && RenderingServer::get_singleton()->is_low_end()) {
  1731. WARN_PRINT_ONCE_ED("Sub-emitter modes other than SUB_EMITTER_DISABLED are not supported in the Compatibility renderer.");
  1732. }
  1733. }
  1734. ParticleProcessMaterial::SubEmitterMode ParticleProcessMaterial::get_sub_emitter_mode() const {
  1735. return sub_emitter_mode;
  1736. }
  1737. void ParticleProcessMaterial::set_sub_emitter_frequency(double p_frequency) {
  1738. sub_emitter_frequency = p_frequency;
  1739. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->sub_emitter_frequency, 1.0 / p_frequency); //pass delta instead of frequency, since its easier to compute
  1740. }
  1741. double ParticleProcessMaterial::get_sub_emitter_frequency() const {
  1742. return sub_emitter_frequency;
  1743. }
  1744. void ParticleProcessMaterial::set_sub_emitter_amount_at_end(int p_amount) {
  1745. sub_emitter_amount_at_end = p_amount;
  1746. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->sub_emitter_amount_at_end, p_amount);
  1747. }
  1748. int ParticleProcessMaterial::get_sub_emitter_amount_at_end() const {
  1749. return sub_emitter_amount_at_end;
  1750. }
  1751. void ParticleProcessMaterial::set_sub_emitter_amount_at_collision(int p_amount) {
  1752. sub_emitter_amount_at_collision = p_amount;
  1753. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->sub_emitter_amount_at_collision, p_amount);
  1754. }
  1755. int ParticleProcessMaterial::get_sub_emitter_amount_at_collision() const {
  1756. return sub_emitter_amount_at_collision;
  1757. }
  1758. void ParticleProcessMaterial::set_sub_emitter_amount_at_start(int p_amount) {
  1759. sub_emitter_amount_at_start = p_amount;
  1760. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->sub_emitter_amount_at_start, p_amount);
  1761. }
  1762. int ParticleProcessMaterial::get_sub_emitter_amount_at_start() const {
  1763. return sub_emitter_amount_at_start;
  1764. }
  1765. void ParticleProcessMaterial::set_sub_emitter_keep_velocity(bool p_enable) {
  1766. sub_emitter_keep_velocity = p_enable;
  1767. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->sub_emitter_keep_velocity, p_enable);
  1768. }
  1769. bool ParticleProcessMaterial::get_sub_emitter_keep_velocity() const {
  1770. return sub_emitter_keep_velocity;
  1771. }
  1772. void ParticleProcessMaterial::set_attractor_interaction_enabled(bool p_enable) {
  1773. attractor_interaction_enabled = p_enable;
  1774. _queue_shader_change();
  1775. }
  1776. bool ParticleProcessMaterial::is_attractor_interaction_enabled() const {
  1777. return attractor_interaction_enabled;
  1778. }
  1779. void ParticleProcessMaterial::set_collision_mode(CollisionMode p_collision_mode) {
  1780. collision_mode = p_collision_mode;
  1781. _queue_shader_change();
  1782. notify_property_list_changed();
  1783. }
  1784. ParticleProcessMaterial::CollisionMode ParticleProcessMaterial::get_collision_mode() const {
  1785. return collision_mode;
  1786. }
  1787. void ParticleProcessMaterial::set_collision_use_scale(bool p_scale) {
  1788. collision_scale = p_scale;
  1789. _queue_shader_change();
  1790. }
  1791. bool ParticleProcessMaterial::is_collision_using_scale() const {
  1792. return collision_scale;
  1793. }
  1794. void ParticleProcessMaterial::set_collision_friction(float p_friction) {
  1795. collision_friction = p_friction;
  1796. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->collision_friction, p_friction);
  1797. }
  1798. float ParticleProcessMaterial::get_collision_friction() const {
  1799. return collision_friction;
  1800. }
  1801. void ParticleProcessMaterial::set_collision_bounce(float p_bounce) {
  1802. collision_bounce = p_bounce;
  1803. RenderingServer::get_singleton()->material_set_param(_get_material(), shader_names->collision_bounce, p_bounce);
  1804. }
  1805. float ParticleProcessMaterial::get_collision_bounce() const {
  1806. return collision_bounce;
  1807. }
  1808. Shader::Mode ParticleProcessMaterial::get_shader_mode() const {
  1809. return Shader::MODE_PARTICLES;
  1810. }
  1811. void ParticleProcessMaterial::_bind_methods() {
  1812. ClassDB::bind_method(D_METHOD("set_direction", "degrees"), &ParticleProcessMaterial::set_direction);
  1813. ClassDB::bind_method(D_METHOD("get_direction"), &ParticleProcessMaterial::get_direction);
  1814. ClassDB::bind_method(D_METHOD("set_inherit_velocity_ratio", "ratio"), &ParticleProcessMaterial::set_inherit_velocity_ratio);
  1815. ClassDB::bind_method(D_METHOD("get_inherit_velocity_ratio"), &ParticleProcessMaterial::get_inherit_velocity_ratio);
  1816. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &ParticleProcessMaterial::set_spread);
  1817. ClassDB::bind_method(D_METHOD("get_spread"), &ParticleProcessMaterial::get_spread);
  1818. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &ParticleProcessMaterial::set_flatness);
  1819. ClassDB::bind_method(D_METHOD("get_flatness"), &ParticleProcessMaterial::get_flatness);
  1820. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &ParticleProcessMaterial::set_param);
  1821. ClassDB::bind_method(D_METHOD("get_param", "param"), &ParticleProcessMaterial::get_param);
  1822. ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &ParticleProcessMaterial::set_param_min);
  1823. ClassDB::bind_method(D_METHOD("get_param_min", "param"), &ParticleProcessMaterial::get_param_min);
  1824. ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &ParticleProcessMaterial::set_param_max);
  1825. ClassDB::bind_method(D_METHOD("get_param_max", "param"), &ParticleProcessMaterial::get_param_max);
  1826. ClassDB::bind_method(D_METHOD("set_param_texture", "param", "texture"), &ParticleProcessMaterial::set_param_texture);
  1827. ClassDB::bind_method(D_METHOD("get_param_texture", "param"), &ParticleProcessMaterial::get_param_texture);
  1828. ClassDB::bind_method(D_METHOD("set_color", "color"), &ParticleProcessMaterial::set_color);
  1829. ClassDB::bind_method(D_METHOD("get_color"), &ParticleProcessMaterial::get_color);
  1830. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &ParticleProcessMaterial::set_color_ramp);
  1831. ClassDB::bind_method(D_METHOD("get_color_ramp"), &ParticleProcessMaterial::get_color_ramp);
  1832. ClassDB::bind_method(D_METHOD("set_alpha_curve", "curve"), &ParticleProcessMaterial::set_alpha_curve);
  1833. ClassDB::bind_method(D_METHOD("get_alpha_curve"), &ParticleProcessMaterial::get_alpha_curve);
  1834. ClassDB::bind_method(D_METHOD("set_emission_curve", "curve"), &ParticleProcessMaterial::set_emission_curve);
  1835. ClassDB::bind_method(D_METHOD("get_emission_curve"), &ParticleProcessMaterial::get_emission_curve);
  1836. ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &ParticleProcessMaterial::set_color_initial_ramp);
  1837. ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &ParticleProcessMaterial::get_color_initial_ramp);
  1838. ClassDB::bind_method(D_METHOD("set_velocity_limit_curve", "curve"), &ParticleProcessMaterial::set_velocity_limit_curve);
  1839. ClassDB::bind_method(D_METHOD("get_velocity_limit_curve"), &ParticleProcessMaterial::get_velocity_limit_curve);
  1840. ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &ParticleProcessMaterial::set_particle_flag);
  1841. ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &ParticleProcessMaterial::get_particle_flag);
  1842. ClassDB::bind_method(D_METHOD("set_velocity_pivot", "pivot"), &ParticleProcessMaterial::set_velocity_pivot);
  1843. ClassDB::bind_method(D_METHOD("get_velocity_pivot"), &ParticleProcessMaterial::get_velocity_pivot);
  1844. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &ParticleProcessMaterial::set_emission_shape);
  1845. ClassDB::bind_method(D_METHOD("get_emission_shape"), &ParticleProcessMaterial::get_emission_shape);
  1846. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &ParticleProcessMaterial::set_emission_sphere_radius);
  1847. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &ParticleProcessMaterial::get_emission_sphere_radius);
  1848. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &ParticleProcessMaterial::set_emission_box_extents);
  1849. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &ParticleProcessMaterial::get_emission_box_extents);
  1850. ClassDB::bind_method(D_METHOD("set_emission_point_texture", "texture"), &ParticleProcessMaterial::set_emission_point_texture);
  1851. ClassDB::bind_method(D_METHOD("get_emission_point_texture"), &ParticleProcessMaterial::get_emission_point_texture);
  1852. ClassDB::bind_method(D_METHOD("set_emission_normal_texture", "texture"), &ParticleProcessMaterial::set_emission_normal_texture);
  1853. ClassDB::bind_method(D_METHOD("get_emission_normal_texture"), &ParticleProcessMaterial::get_emission_normal_texture);
  1854. ClassDB::bind_method(D_METHOD("set_emission_color_texture", "texture"), &ParticleProcessMaterial::set_emission_color_texture);
  1855. ClassDB::bind_method(D_METHOD("get_emission_color_texture"), &ParticleProcessMaterial::get_emission_color_texture);
  1856. ClassDB::bind_method(D_METHOD("set_emission_point_count", "point_count"), &ParticleProcessMaterial::set_emission_point_count);
  1857. ClassDB::bind_method(D_METHOD("get_emission_point_count"), &ParticleProcessMaterial::get_emission_point_count);
  1858. ClassDB::bind_method(D_METHOD("set_emission_ring_axis", "axis"), &ParticleProcessMaterial::set_emission_ring_axis);
  1859. ClassDB::bind_method(D_METHOD("get_emission_ring_axis"), &ParticleProcessMaterial::get_emission_ring_axis);
  1860. ClassDB::bind_method(D_METHOD("set_emission_ring_height", "height"), &ParticleProcessMaterial::set_emission_ring_height);
  1861. ClassDB::bind_method(D_METHOD("get_emission_ring_height"), &ParticleProcessMaterial::get_emission_ring_height);
  1862. ClassDB::bind_method(D_METHOD("set_emission_ring_radius", "radius"), &ParticleProcessMaterial::set_emission_ring_radius);
  1863. ClassDB::bind_method(D_METHOD("get_emission_ring_radius"), &ParticleProcessMaterial::get_emission_ring_radius);
  1864. ClassDB::bind_method(D_METHOD("set_emission_ring_inner_radius", "inner_radius"), &ParticleProcessMaterial::set_emission_ring_inner_radius);
  1865. ClassDB::bind_method(D_METHOD("get_emission_ring_inner_radius"), &ParticleProcessMaterial::get_emission_ring_inner_radius);
  1866. ClassDB::bind_method(D_METHOD("set_emission_ring_cone_angle", "cone_angle"), &ParticleProcessMaterial::set_emission_ring_cone_angle);
  1867. ClassDB::bind_method(D_METHOD("get_emission_ring_cone_angle"), &ParticleProcessMaterial::get_emission_ring_cone_angle);
  1868. ClassDB::bind_method(D_METHOD("set_emission_shape_offset", "emission_shape_offset"), &ParticleProcessMaterial::set_emission_shape_offset);
  1869. ClassDB::bind_method(D_METHOD("get_emission_shape_offset"), &ParticleProcessMaterial::get_emission_shape_offset);
  1870. ClassDB::bind_method(D_METHOD("set_emission_shape_scale", "emission_shape_scale"), &ParticleProcessMaterial::set_emission_shape_scale);
  1871. ClassDB::bind_method(D_METHOD("get_emission_shape_scale"), &ParticleProcessMaterial::get_emission_shape_scale);
  1872. ClassDB::bind_method(D_METHOD("get_turbulence_enabled"), &ParticleProcessMaterial::get_turbulence_enabled);
  1873. ClassDB::bind_method(D_METHOD("set_turbulence_enabled", "turbulence_enabled"), &ParticleProcessMaterial::set_turbulence_enabled);
  1874. ClassDB::bind_method(D_METHOD("get_turbulence_noise_strength"), &ParticleProcessMaterial::get_turbulence_noise_strength);
  1875. ClassDB::bind_method(D_METHOD("set_turbulence_noise_strength", "turbulence_noise_strength"), &ParticleProcessMaterial::set_turbulence_noise_strength);
  1876. ClassDB::bind_method(D_METHOD("get_turbulence_noise_scale"), &ParticleProcessMaterial::get_turbulence_noise_scale);
  1877. ClassDB::bind_method(D_METHOD("set_turbulence_noise_scale", "turbulence_noise_scale"), &ParticleProcessMaterial::set_turbulence_noise_scale);
  1878. ClassDB::bind_method(D_METHOD("get_turbulence_noise_speed_random"), &ParticleProcessMaterial::get_turbulence_noise_speed_random);
  1879. ClassDB::bind_method(D_METHOD("set_turbulence_noise_speed_random", "turbulence_noise_speed_random"), &ParticleProcessMaterial::set_turbulence_noise_speed_random);
  1880. ClassDB::bind_method(D_METHOD("get_turbulence_noise_speed"), &ParticleProcessMaterial::get_turbulence_noise_speed);
  1881. ClassDB::bind_method(D_METHOD("set_turbulence_noise_speed", "turbulence_noise_speed"), &ParticleProcessMaterial::set_turbulence_noise_speed);
  1882. ClassDB::bind_method(D_METHOD("get_gravity"), &ParticleProcessMaterial::get_gravity);
  1883. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &ParticleProcessMaterial::set_gravity);
  1884. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "randomness"), &ParticleProcessMaterial::set_lifetime_randomness);
  1885. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &ParticleProcessMaterial::get_lifetime_randomness);
  1886. ClassDB::bind_method(D_METHOD("get_sub_emitter_mode"), &ParticleProcessMaterial::get_sub_emitter_mode);
  1887. ClassDB::bind_method(D_METHOD("set_sub_emitter_mode", "mode"), &ParticleProcessMaterial::set_sub_emitter_mode);
  1888. ClassDB::bind_method(D_METHOD("get_sub_emitter_frequency"), &ParticleProcessMaterial::get_sub_emitter_frequency);
  1889. ClassDB::bind_method(D_METHOD("set_sub_emitter_frequency", "hz"), &ParticleProcessMaterial::set_sub_emitter_frequency);
  1890. ClassDB::bind_method(D_METHOD("get_sub_emitter_amount_at_end"), &ParticleProcessMaterial::get_sub_emitter_amount_at_end);
  1891. ClassDB::bind_method(D_METHOD("set_sub_emitter_amount_at_end", "amount"), &ParticleProcessMaterial::set_sub_emitter_amount_at_end);
  1892. ClassDB::bind_method(D_METHOD("get_sub_emitter_amount_at_collision"), &ParticleProcessMaterial::get_sub_emitter_amount_at_collision);
  1893. ClassDB::bind_method(D_METHOD("set_sub_emitter_amount_at_collision", "amount"), &ParticleProcessMaterial::set_sub_emitter_amount_at_collision);
  1894. ClassDB::bind_method(D_METHOD("get_sub_emitter_amount_at_start"), &ParticleProcessMaterial::get_sub_emitter_amount_at_start);
  1895. ClassDB::bind_method(D_METHOD("set_sub_emitter_amount_at_start", "amount"), &ParticleProcessMaterial::set_sub_emitter_amount_at_start);
  1896. ClassDB::bind_method(D_METHOD("get_sub_emitter_keep_velocity"), &ParticleProcessMaterial::get_sub_emitter_keep_velocity);
  1897. ClassDB::bind_method(D_METHOD("set_sub_emitter_keep_velocity", "enable"), &ParticleProcessMaterial::set_sub_emitter_keep_velocity);
  1898. ClassDB::bind_method(D_METHOD("set_attractor_interaction_enabled", "enabled"), &ParticleProcessMaterial::set_attractor_interaction_enabled);
  1899. ClassDB::bind_method(D_METHOD("is_attractor_interaction_enabled"), &ParticleProcessMaterial::is_attractor_interaction_enabled);
  1900. ClassDB::bind_method(D_METHOD("set_collision_mode", "mode"), &ParticleProcessMaterial::set_collision_mode);
  1901. ClassDB::bind_method(D_METHOD("get_collision_mode"), &ParticleProcessMaterial::get_collision_mode);
  1902. ClassDB::bind_method(D_METHOD("set_collision_use_scale", "radius"), &ParticleProcessMaterial::set_collision_use_scale);
  1903. ClassDB::bind_method(D_METHOD("is_collision_using_scale"), &ParticleProcessMaterial::is_collision_using_scale);
  1904. ClassDB::bind_method(D_METHOD("set_collision_friction", "friction"), &ParticleProcessMaterial::set_collision_friction);
  1905. ClassDB::bind_method(D_METHOD("get_collision_friction"), &ParticleProcessMaterial::get_collision_friction);
  1906. ClassDB::bind_method(D_METHOD("set_collision_bounce", "bounce"), &ParticleProcessMaterial::set_collision_bounce);
  1907. ClassDB::bind_method(D_METHOD("get_collision_bounce"), &ParticleProcessMaterial::get_collision_bounce);
  1908. #define ADD_MIN_MAX_PROPERTY(m_property, m_range, m_parameter_name) \
  1909. ADD_PROPERTYI(PropertyInfo(Variant::VECTOR2, m_property, PROPERTY_HINT_RANGE, m_range, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "set_param", "get_param", m_parameter_name); \
  1910. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, m_property "_min", PROPERTY_HINT_RANGE, m_range), "set_param_min", "get_param_min", m_parameter_name); \
  1911. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, m_property "_max", PROPERTY_HINT_RANGE, m_range), "set_param_max", "get_param_max", m_parameter_name); \
  1912. min_max_properties.insert(m_property);
  1913. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1914. ADD_GROUP("Particle Flags", "particle_flag_");
  1915. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1916. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_rotate_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ROTATE_Y);
  1917. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_disable_z"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DISABLE_Z);
  1918. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_damping_as_friction"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DAMPING_AS_FRICTION);
  1919. ADD_GROUP("Spawn", "");
  1920. ADD_SUBGROUP("Position", "");
  1921. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_shape_offset"), "set_emission_shape_offset", "get_emission_shape_offset");
  1922. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_shape_scale"), "set_emission_shape_scale", "get_emission_shape_scale");
  1923. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Box,Points,Directed Points,Ring"), "set_emission_shape", "get_emission_shape");
  1924. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,or_greater"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1925. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  1926. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_point_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_emission_point_texture", "get_emission_point_texture");
  1927. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_emission_normal_texture", "get_emission_normal_texture");
  1928. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_color_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_emission_color_texture", "get_emission_color_texture");
  1929. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_point_count", PROPERTY_HINT_RANGE, "0,1000000,1"), "set_emission_point_count", "get_emission_point_count");
  1930. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_ring_axis"), "set_emission_ring_axis", "get_emission_ring_axis");
  1931. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_height", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_height", "get_emission_ring_height");
  1932. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_radius", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_radius", "get_emission_ring_radius");
  1933. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_inner_radius", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_inner_radius", "get_emission_ring_inner_radius");
  1934. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_cone_angle", PROPERTY_HINT_RANGE, "0,90,0.01,degrees"), "set_emission_ring_cone_angle", "get_emission_ring_cone_angle");
  1935. ADD_SUBGROUP("Angle", "");
  1936. ADD_MIN_MAX_PROPERTY("angle", "-720,720,0.1,or_less,or_greater,degrees", PARAM_ANGLE);
  1937. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGLE);
  1938. ADD_SUBGROUP("Velocity", "");
  1939. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inherit_velocity_ratio", PROPERTY_HINT_RANGE, "0.0,1.0,0.001,or_less,or_greater"), "set_inherit_velocity_ratio", "get_inherit_velocity_ratio");
  1940. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity_pivot"), "set_velocity_pivot", "get_velocity_pivot");
  1941. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction");
  1942. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.001"), "set_spread", "get_spread");
  1943. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "flatness", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_flatness", "get_flatness");
  1944. ADD_MIN_MAX_PROPERTY("initial_velocity", "0,1000,0.01,or_less,or_greater", PARAM_INITIAL_LINEAR_VELOCITY);
  1945. ADD_GROUP("Animated Velocity", "");
  1946. ADD_SUBGROUP("Velocity Limit", "");
  1947. ADD_SUBGROUP("Angular Velocity", "angular_");
  1948. ADD_MIN_MAX_PROPERTY("angular_velocity", "-720,720,0.01,or_less,or_greater", PARAM_ANGULAR_VELOCITY);
  1949. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGULAR_VELOCITY);
  1950. ADD_SUBGROUP("Directional Velocity", "directional_");
  1951. ADD_MIN_MAX_PROPERTY("directional_velocity", "-720,720,0.01,or_less,or_greater", PARAM_DIRECTIONAL_VELOCITY);
  1952. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "directional_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveXYZTexture"), "set_param_texture", "get_param_texture", PARAM_DIRECTIONAL_VELOCITY);
  1953. ADD_SUBGROUP("Orbit Velocity", "orbit_");
  1954. ADD_MIN_MAX_PROPERTY("orbit_velocity", "-2,2,0.001,or_less,or_greater", PARAM_ORBIT_VELOCITY);
  1955. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture,CurveXYZTexture"), "set_param_texture", "get_param_texture", PARAM_ORBIT_VELOCITY);
  1956. ADD_SUBGROUP("Radial Velocity", "radial_");
  1957. ADD_MIN_MAX_PROPERTY("radial_velocity", "-1000,1000,0.01,or_less,or_greater", PARAM_RADIAL_VELOCITY);
  1958. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_RADIAL_VELOCITY);
  1959. ADD_SUBGROUP("Velocity Limit", "");
  1960. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "velocity_limit_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_velocity_limit_curve", "get_velocity_limit_curve");
  1961. ADD_GROUP("Accelerations", "");
  1962. ADD_SUBGROUP("Gravity", "");
  1963. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  1964. ADD_SUBGROUP("Linear Accel", "linear_");
  1965. ADD_MIN_MAX_PROPERTY("linear_accel", "-100,100,0.01,or_less,or_greater", PARAM_LINEAR_ACCEL);
  1966. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_LINEAR_ACCEL);
  1967. ADD_SUBGROUP("Radial Accel", "radial_");
  1968. ADD_MIN_MAX_PROPERTY("radial_accel", "-100,100,0.01,or_less,or_greater", PARAM_RADIAL_ACCEL);
  1969. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_RADIAL_ACCEL);
  1970. ADD_SUBGROUP("Tangential Accel", "tangential_");
  1971. ADD_MIN_MAX_PROPERTY("tangential_accel", "-100,100,0.01,or_less,or_greater", PARAM_TANGENTIAL_ACCEL);
  1972. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_TANGENTIAL_ACCEL);
  1973. ADD_SUBGROUP("Damping", "");
  1974. ADD_MIN_MAX_PROPERTY("damping", "0,100,0.001,or_greater", PARAM_DAMPING);
  1975. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_DAMPING);
  1976. ADD_SUBGROUP("Attractor Interaction", "attractor_interaction_");
  1977. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "attractor_interaction_enabled"), "set_attractor_interaction_enabled", "is_attractor_interaction_enabled");
  1978. ADD_GROUP("Display", "");
  1979. ADD_SUBGROUP("Scale", "");
  1980. ADD_MIN_MAX_PROPERTY("scale", "0,1000,0.01,or_greater", PARAM_SCALE);
  1981. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture,CurveXYZTexture"), "set_param_texture", "get_param_texture", PARAM_SCALE);
  1982. ADD_SUBGROUP("Scale Over Velocity", "");
  1983. ADD_MIN_MAX_PROPERTY("scale_over_velocity", "0,1000,0.01,or_greater", PARAM_SCALE_OVER_VELOCITY);
  1984. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_over_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture,CurveXYZTexture"), "set_param_texture", "get_param_texture", PARAM_SCALE_OVER_VELOCITY);
  1985. ADD_SUBGROUP("Color Curves", "");
  1986. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1987. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture1D"), "set_color_ramp", "get_color_ramp");
  1988. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture1D"), "set_color_initial_ramp", "get_color_initial_ramp");
  1989. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "alpha_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_alpha_curve", "get_alpha_curve");
  1990. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_emission_curve", "get_emission_curve");
  1991. ADD_SUBGROUP("Hue Variation", "hue_");
  1992. ADD_MIN_MAX_PROPERTY("hue_variation", "-1,1,0.01", PARAM_HUE_VARIATION);
  1993. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_HUE_VARIATION);
  1994. ADD_SUBGROUP("Animation", "anim_");
  1995. ADD_MIN_MAX_PROPERTY("anim_speed", "0,16,0.01,or_less,or_greater", PARAM_ANIM_SPEED);
  1996. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_SPEED);
  1997. ADD_MIN_MAX_PROPERTY("anim_offset", "0,1,0.0001", PARAM_ANIM_OFFSET);
  1998. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_OFFSET);
  1999. ADD_GROUP("Turbulence", "turbulence_");
  2000. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "turbulence_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_turbulence_enabled", "get_turbulence_enabled");
  2001. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "turbulence_noise_strength", PROPERTY_HINT_RANGE, "0,20,0.01"), "set_turbulence_noise_strength", "get_turbulence_noise_strength");
  2002. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "turbulence_noise_scale", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater"), "set_turbulence_noise_scale", "get_turbulence_noise_scale");
  2003. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "turbulence_noise_speed"), "set_turbulence_noise_speed", "get_turbulence_noise_speed");
  2004. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "turbulence_noise_speed_random", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_turbulence_noise_speed_random", "get_turbulence_noise_speed_random");
  2005. ADD_MIN_MAX_PROPERTY("turbulence_influence", "0,1,0.001", PARAM_TURB_VEL_INFLUENCE);
  2006. ADD_MIN_MAX_PROPERTY("turbulence_initial_displacement", "-100,100,0.1", PARAM_TURB_INIT_DISPLACEMENT);
  2007. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "turbulence_influence_over_life", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_TURB_INFLUENCE_OVER_LIFE);
  2008. ADD_GROUP("Collision", "collision_");
  2009. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mode", PROPERTY_HINT_ENUM, "Disabled,Rigid,Hide On Contact"), "set_collision_mode", "get_collision_mode");
  2010. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_friction", "get_collision_friction");
  2011. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_collision_bounce", "get_collision_bounce");
  2012. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_use_scale"), "set_collision_use_scale", "is_collision_using_scale");
  2013. ADD_GROUP("Sub Emitter", "sub_emitter_");
  2014. ADD_PROPERTY(PropertyInfo(Variant::INT, "sub_emitter_mode", PROPERTY_HINT_ENUM, "Disabled:0,Constant:1,At Start:4,At End:2,At Collision:3"), "set_sub_emitter_mode", "get_sub_emitter_mode");
  2015. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sub_emitter_frequency", PROPERTY_HINT_RANGE, "0.01,100,0.01,suffix:Hz"), "set_sub_emitter_frequency", "get_sub_emitter_frequency");
  2016. ADD_PROPERTY(PropertyInfo(Variant::INT, "sub_emitter_amount_at_end", PROPERTY_HINT_RANGE, "1,32,1"), "set_sub_emitter_amount_at_end", "get_sub_emitter_amount_at_end");
  2017. ADD_PROPERTY(PropertyInfo(Variant::INT, "sub_emitter_amount_at_collision", PROPERTY_HINT_RANGE, "1,32,1"), "set_sub_emitter_amount_at_collision", "get_sub_emitter_amount_at_collision");
  2018. ADD_PROPERTY(PropertyInfo(Variant::INT, "sub_emitter_amount_at_start", PROPERTY_HINT_RANGE, "1,32,1"), "set_sub_emitter_amount_at_start", "get_sub_emitter_amount_at_start");
  2019. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sub_emitter_keep_velocity"), "set_sub_emitter_keep_velocity", "get_sub_emitter_keep_velocity");
  2020. ADD_SIGNAL(MethodInfo("emission_shape_changed"));
  2021. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  2022. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  2023. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  2024. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  2025. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  2026. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  2027. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  2028. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  2029. BIND_ENUM_CONSTANT(PARAM_SCALE);
  2030. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  2031. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  2032. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  2033. BIND_ENUM_CONSTANT(PARAM_RADIAL_VELOCITY);
  2034. BIND_ENUM_CONSTANT(PARAM_DIRECTIONAL_VELOCITY);
  2035. BIND_ENUM_CONSTANT(PARAM_SCALE_OVER_VELOCITY);
  2036. BIND_ENUM_CONSTANT(PARAM_MAX);
  2037. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  2038. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y);
  2039. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z);
  2040. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DAMPING_AS_FRICTION);
  2041. BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
  2042. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  2043. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  2044. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
  2045. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  2046. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  2047. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  2048. BIND_ENUM_CONSTANT(EMISSION_SHAPE_RING);
  2049. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  2050. BIND_ENUM_CONSTANT(PARAM_TURB_VEL_INFLUENCE);
  2051. BIND_ENUM_CONSTANT(PARAM_TURB_INIT_DISPLACEMENT);
  2052. BIND_ENUM_CONSTANT(PARAM_TURB_INFLUENCE_OVER_LIFE);
  2053. BIND_ENUM_CONSTANT(SUB_EMITTER_DISABLED);
  2054. BIND_ENUM_CONSTANT(SUB_EMITTER_CONSTANT);
  2055. BIND_ENUM_CONSTANT(SUB_EMITTER_AT_END);
  2056. BIND_ENUM_CONSTANT(SUB_EMITTER_AT_COLLISION);
  2057. BIND_ENUM_CONSTANT(SUB_EMITTER_AT_START);
  2058. BIND_ENUM_CONSTANT(SUB_EMITTER_MAX);
  2059. BIND_ENUM_CONSTANT(COLLISION_DISABLED);
  2060. BIND_ENUM_CONSTANT(COLLISION_RIGID);
  2061. BIND_ENUM_CONSTANT(COLLISION_HIDE_ON_CONTACT);
  2062. BIND_ENUM_CONSTANT(COLLISION_MAX);
  2063. #undef ADD_MIN_MAX_PROPERTY
  2064. }
  2065. ParticleProcessMaterial::ParticleProcessMaterial() :
  2066. element(this) {
  2067. _set_material(RS::get_singleton()->material_create());
  2068. set_direction(Vector3(1, 0, 0));
  2069. set_spread(45);
  2070. set_flatness(0);
  2071. set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  2072. set_param_min(PARAM_ANGULAR_VELOCITY, 0);
  2073. set_param_min(PARAM_ORBIT_VELOCITY, 0);
  2074. set_param_min(PARAM_LINEAR_ACCEL, 0);
  2075. set_param_min(PARAM_RADIAL_ACCEL, 0);
  2076. set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
  2077. set_param_min(PARAM_DAMPING, 0);
  2078. set_param_min(PARAM_ANGLE, 0);
  2079. set_param_min(PARAM_SCALE, 1);
  2080. set_param_min(PARAM_HUE_VARIATION, 0);
  2081. set_param_min(PARAM_ANIM_SPEED, 0);
  2082. set_param_min(PARAM_ANIM_OFFSET, 0);
  2083. set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  2084. set_param_max(PARAM_ANGULAR_VELOCITY, 0);
  2085. set_param_max(PARAM_ORBIT_VELOCITY, 0);
  2086. set_param_max(PARAM_LINEAR_ACCEL, 0);
  2087. set_param_max(PARAM_RADIAL_ACCEL, 0);
  2088. set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
  2089. set_param_max(PARAM_DAMPING, 0);
  2090. set_param_max(PARAM_ANGLE, 0);
  2091. set_param_max(PARAM_SCALE, 1);
  2092. set_param_max(PARAM_HUE_VARIATION, 0);
  2093. set_param_max(PARAM_ANIM_SPEED, 0);
  2094. set_param_max(PARAM_ANIM_OFFSET, 0);
  2095. set_param_min(PARAM_DIRECTIONAL_VELOCITY, 1.0);
  2096. set_param_max(PARAM_DIRECTIONAL_VELOCITY, 1.0);
  2097. set_emission_shape(EMISSION_SHAPE_POINT);
  2098. set_emission_sphere_radius(1);
  2099. set_emission_box_extents(Vector3(1, 1, 1));
  2100. set_emission_ring_axis(Vector3(0, 0, 1.0));
  2101. set_emission_ring_height(1);
  2102. set_emission_ring_radius(1);
  2103. set_emission_ring_inner_radius(0);
  2104. set_emission_ring_cone_angle(90);
  2105. set_emission_shape_offset(Vector3(0.0, 0.0, 0.0));
  2106. set_emission_shape_scale(Vector3(1.0, 1.0, 1.0));
  2107. set_turbulence_enabled(false);
  2108. set_turbulence_noise_speed(Vector3(0.0, 0.0, 0.0));
  2109. set_turbulence_noise_strength(1);
  2110. set_turbulence_noise_scale(9);
  2111. set_turbulence_noise_speed_random(0.2);
  2112. set_param_min(PARAM_TURB_VEL_INFLUENCE, 0.1);
  2113. set_param_max(PARAM_TURB_VEL_INFLUENCE, 0.1);
  2114. set_param_min(PARAM_TURB_INIT_DISPLACEMENT, 0.0);
  2115. set_param_max(PARAM_TURB_INIT_DISPLACEMENT, 0.0);
  2116. set_gravity(Vector3(0, -9.8, 0));
  2117. set_lifetime_randomness(0);
  2118. set_sub_emitter_mode(SUB_EMITTER_DISABLED);
  2119. set_sub_emitter_frequency(4);
  2120. set_sub_emitter_amount_at_end(1);
  2121. set_sub_emitter_amount_at_collision(1);
  2122. set_sub_emitter_amount_at_start(1);
  2123. set_sub_emitter_keep_velocity(false);
  2124. set_attractor_interaction_enabled(true);
  2125. set_collision_mode(COLLISION_DISABLED);
  2126. set_collision_bounce(0.0);
  2127. set_collision_friction(0.0);
  2128. set_collision_use_scale(false);
  2129. for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
  2130. particle_flags[i] = false;
  2131. }
  2132. set_color(Color(1, 1, 1, 1));
  2133. current_key.invalid_key = 1;
  2134. }
  2135. ParticleProcessMaterial::~ParticleProcessMaterial() {
  2136. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2137. MutexLock lock(shader_map_mutex);
  2138. if (shader_map.has(current_key)) {
  2139. shader_map[current_key].users--;
  2140. if (shader_map[current_key].users == 0) {
  2141. //deallocate shader, as it's no longer in use
  2142. RS::get_singleton()->free(shader_map[current_key].shader);
  2143. shader_map.erase(current_key);
  2144. }
  2145. RS::get_singleton()->material_set_shader(_get_material(), RID());
  2146. }
  2147. }