particles_storage.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /**************************************************************************/
  2. /* particles_storage.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 "particles_storage.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/rendering_server_globals.h"
  33. #include "texture_storage.h"
  34. using namespace RendererRD;
  35. ParticlesStorage *ParticlesStorage::singleton = nullptr;
  36. ParticlesStorage *ParticlesStorage::get_singleton() {
  37. return singleton;
  38. }
  39. ParticlesStorage::ParticlesStorage() {
  40. singleton = this;
  41. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  42. /* Effects */
  43. sort_effects = memnew(SortEffects);
  44. /* Particles */
  45. {
  46. String defines = "#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n";
  47. // Initialize particles
  48. Vector<String> particles_modes;
  49. particles_modes.push_back("");
  50. particles_shader.shader.initialize(particles_modes, defines);
  51. }
  52. MaterialStorage::get_singleton()->shader_set_data_request_function(MaterialStorage::SHADER_TYPE_PARTICLES, _create_particles_shader_funcs);
  53. MaterialStorage::get_singleton()->material_set_data_request_function(MaterialStorage::SHADER_TYPE_PARTICLES, _create_particles_material_funcs);
  54. {
  55. ShaderCompiler::DefaultIdentifierActions actions;
  56. actions.renames["COLOR"] = "PARTICLE.color";
  57. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  58. //actions.renames["MASS"] = "mass"; ?
  59. actions.renames["ACTIVE"] = "particle_active";
  60. actions.renames["RESTART"] = "restart";
  61. actions.renames["CUSTOM"] = "PARTICLE.custom";
  62. actions.renames["AMOUNT_RATIO"] = "FRAME.amount_ratio";
  63. for (int i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  64. String udname = "USERDATA" + itos(i + 1);
  65. actions.renames[udname] = "PARTICLE.userdata" + itos(i + 1);
  66. actions.usage_defines[udname] = "#define USERDATA" + itos(i + 1) + "_USED\n";
  67. }
  68. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  69. actions.renames["TIME"] = "frame_history.data[0].time";
  70. actions.renames["PI"] = _MKSTR(Math_PI);
  71. actions.renames["TAU"] = _MKSTR(Math_TAU);
  72. actions.renames["E"] = _MKSTR(Math_E);
  73. actions.renames["LIFETIME"] = "params.lifetime";
  74. actions.renames["DELTA"] = "local_delta";
  75. actions.renames["NUMBER"] = "particle_number";
  76. actions.renames["INDEX"] = "index";
  77. //actions.renames["GRAVITY"] = "current_gravity";
  78. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  79. actions.renames["EMITTER_VELOCITY"] = "FRAME.emitter_velocity";
  80. actions.renames["INTERPOLATE_TO_END"] = "FRAME.interp_to_end";
  81. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  82. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  83. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  84. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  85. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  86. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  87. actions.renames["RESTART_POSITION"] = "restart_position";
  88. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  89. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  90. actions.renames["RESTART_COLOR"] = "restart_color";
  91. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  92. actions.renames["emit_subparticle"] = "emit_subparticle";
  93. actions.renames["COLLIDED"] = "collided";
  94. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  95. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  96. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  97. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  98. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  99. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  100. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISION_SCALE\n";
  101. actions.base_texture_binding_index = 1;
  102. actions.texture_layout_set = 3;
  103. actions.base_uniform_string = "material.";
  104. actions.base_varying_index = 10;
  105. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  106. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  107. actions.global_buffer_array_variable = "global_shader_uniforms.data";
  108. particles_shader.compiler.initialize(actions);
  109. }
  110. {
  111. // default material and shader for particles shader
  112. particles_shader.default_shader = material_storage->shader_allocate();
  113. material_storage->shader_initialize(particles_shader.default_shader);
  114. material_storage->shader_set_code(particles_shader.default_shader, R"(
  115. // Default particles shader.
  116. shader_type particles;
  117. void process() {
  118. COLOR = vec4(1.0);
  119. }
  120. )");
  121. particles_shader.default_material = material_storage->material_allocate();
  122. material_storage->material_initialize(particles_shader.default_material);
  123. material_storage->material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  124. ParticleProcessMaterialData *md = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, MaterialStorage::SHADER_TYPE_PARTICLES));
  125. particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0);
  126. Vector<RD::Uniform> uniforms;
  127. {
  128. RD::Uniform u;
  129. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  130. u.binding = 2;
  131. u.append_id(material_storage->global_shader_uniforms_get_storage_buffer());
  132. uniforms.push_back(u);
  133. }
  134. uniforms.append_array(material_storage->samplers_rd_get_default().get_uniforms(SAMPLERS_BINDING_FIRST_INDEX));
  135. particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, BASE_UNIFORM_SET);
  136. }
  137. {
  138. Vector<String> copy_modes;
  139. for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) {
  140. if (i == 0) {
  141. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n");
  142. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n");
  143. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n");
  144. } else {
  145. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USERDATA_COUNT " + itos(i) + "\n");
  146. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n");
  147. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n#define USERDATA_COUNT " + itos(i) + "\n");
  148. }
  149. }
  150. particles_shader.copy_shader.initialize(copy_modes);
  151. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  152. for (int i = 0; i <= ParticlesShader::MAX_USERDATAS; i++) {
  153. for (int j = 0; j < ParticlesShader::COPY_MODE_MAX; j++) {
  154. particles_shader.copy_pipelines[i * ParticlesShader::COPY_MODE_MAX + j] = RD::get_singleton()->compute_pipeline_create(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i * ParticlesShader::COPY_MODE_MAX + j));
  155. }
  156. }
  157. }
  158. }
  159. ParticlesStorage::~ParticlesStorage() {
  160. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  161. particles_shader.copy_shader.version_free(particles_shader.copy_shader_version);
  162. material_storage->material_free(particles_shader.default_material);
  163. material_storage->shader_free(particles_shader.default_shader);
  164. if (sort_effects) {
  165. memdelete(sort_effects);
  166. sort_effects = nullptr;
  167. }
  168. singleton = nullptr;
  169. }
  170. bool ParticlesStorage::free(RID p_rid) {
  171. if (owns_particles(p_rid)) {
  172. particles_free(p_rid);
  173. return true;
  174. } else if (owns_particles_collision(p_rid)) {
  175. particles_collision_free(p_rid);
  176. return true;
  177. } else if (owns_particles_collision_instance(p_rid)) {
  178. particles_collision_instance_free(p_rid);
  179. return true;
  180. }
  181. return false;
  182. }
  183. /* PARTICLES */
  184. RID ParticlesStorage::particles_allocate() {
  185. return particles_owner.allocate_rid();
  186. }
  187. void ParticlesStorage::particles_initialize(RID p_rid) {
  188. particles_owner.initialize_rid(p_rid);
  189. }
  190. void ParticlesStorage::particles_free(RID p_rid) {
  191. Particles *particles = particles_owner.get_or_null(p_rid);
  192. particles->dependency.deleted_notify(p_rid);
  193. particles->update_list.remove_from_list();
  194. _particles_free_data(particles);
  195. particles_owner.free(p_rid);
  196. }
  197. void ParticlesStorage::particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) {
  198. Particles *particles = particles_owner.get_or_null(p_particles);
  199. ERR_FAIL_NULL(particles);
  200. if (particles->mode == p_mode) {
  201. return;
  202. }
  203. _particles_free_data(particles);
  204. particles->mode = p_mode;
  205. }
  206. void ParticlesStorage::particles_set_emitting(RID p_particles, bool p_emitting) {
  207. Particles *particles = particles_owner.get_or_null(p_particles);
  208. ERR_FAIL_NULL(particles);
  209. particles->emitting = p_emitting;
  210. }
  211. bool ParticlesStorage::particles_get_emitting(RID p_particles) {
  212. Particles *particles = particles_owner.get_or_null(p_particles);
  213. ERR_FAIL_NULL_V(particles, false);
  214. return particles->emitting;
  215. }
  216. void ParticlesStorage::_particles_free_data(Particles *particles) {
  217. if (particles->particle_buffer.is_valid()) {
  218. RD::get_singleton()->free(particles->particle_buffer);
  219. particles->particle_buffer = RID();
  220. RD::get_singleton()->free(particles->particle_instance_buffer);
  221. particles->particle_instance_buffer = RID();
  222. }
  223. particles->userdata_count = 0;
  224. if (particles->frame_params_buffer.is_valid()) {
  225. RD::get_singleton()->free(particles->frame_params_buffer);
  226. particles->frame_params_buffer = RID();
  227. }
  228. particles->particles_transforms_buffer_uniform_set = RID();
  229. if (RD::get_singleton()->uniform_set_is_valid(particles->trail_bind_pose_uniform_set)) {
  230. RD::get_singleton()->free(particles->trail_bind_pose_uniform_set);
  231. }
  232. particles->trail_bind_pose_uniform_set = RID();
  233. if (particles->trail_bind_pose_buffer.is_valid()) {
  234. RD::get_singleton()->free(particles->trail_bind_pose_buffer);
  235. particles->trail_bind_pose_buffer = RID();
  236. }
  237. if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) {
  238. RD::get_singleton()->free(particles->collision_textures_uniform_set);
  239. }
  240. particles->collision_textures_uniform_set = RID();
  241. if (particles->particles_sort_buffer.is_valid()) {
  242. RD::get_singleton()->free(particles->particles_sort_buffer);
  243. particles->particles_sort_buffer = RID();
  244. particles->particles_sort_uniform_set = RID();
  245. }
  246. if (particles->emission_buffer != nullptr) {
  247. particles->emission_buffer = nullptr;
  248. particles->emission_buffer_data.clear();
  249. RD::get_singleton()->free(particles->emission_storage_buffer);
  250. particles->emission_storage_buffer = RID();
  251. }
  252. if (particles->unused_emission_storage_buffer.is_valid()) {
  253. RD::get_singleton()->free(particles->unused_emission_storage_buffer);
  254. particles->unused_emission_storage_buffer = RID();
  255. }
  256. if (particles->unused_trail_storage_buffer.is_valid()) {
  257. RD::get_singleton()->free(particles->unused_trail_storage_buffer);
  258. particles->unused_trail_storage_buffer = RID();
  259. }
  260. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  261. //will need to be re-created
  262. RD::get_singleton()->free(particles->particles_material_uniform_set);
  263. }
  264. particles->particles_material_uniform_set = RID();
  265. }
  266. void ParticlesStorage::particles_set_amount(RID p_particles, int p_amount) {
  267. Particles *particles = particles_owner.get_or_null(p_particles);
  268. ERR_FAIL_NULL(particles);
  269. if (particles->amount == p_amount) {
  270. return;
  271. }
  272. _particles_free_data(particles);
  273. particles->amount = p_amount;
  274. particles->prev_ticks = 0;
  275. particles->phase = 0;
  276. particles->prev_phase = 0;
  277. particles->clear = true;
  278. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  279. }
  280. void ParticlesStorage::particles_set_amount_ratio(RID p_particles, float p_amount_ratio) {
  281. Particles *particles = particles_owner.get_or_null(p_particles);
  282. ERR_FAIL_NULL(particles);
  283. particles->amount_ratio = p_amount_ratio;
  284. }
  285. void ParticlesStorage::particles_set_lifetime(RID p_particles, double p_lifetime) {
  286. Particles *particles = particles_owner.get_or_null(p_particles);
  287. ERR_FAIL_NULL(particles);
  288. particles->lifetime = p_lifetime;
  289. }
  290. void ParticlesStorage::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  291. Particles *particles = particles_owner.get_or_null(p_particles);
  292. ERR_FAIL_NULL(particles);
  293. particles->one_shot = p_one_shot;
  294. }
  295. void ParticlesStorage::particles_set_pre_process_time(RID p_particles, double p_time) {
  296. Particles *particles = particles_owner.get_or_null(p_particles);
  297. ERR_FAIL_NULL(particles);
  298. particles->pre_process_time = p_time;
  299. }
  300. void ParticlesStorage::particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) {
  301. Particles *particles = particles_owner.get_or_null(p_particles);
  302. ERR_FAIL_NULL(particles);
  303. particles->explosiveness = p_ratio;
  304. }
  305. void ParticlesStorage::particles_set_randomness_ratio(RID p_particles, real_t p_ratio) {
  306. Particles *particles = particles_owner.get_or_null(p_particles);
  307. ERR_FAIL_NULL(particles);
  308. particles->randomness = p_ratio;
  309. }
  310. void ParticlesStorage::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  311. Particles *particles = particles_owner.get_or_null(p_particles);
  312. ERR_FAIL_NULL(particles);
  313. particles->custom_aabb = p_aabb;
  314. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  315. }
  316. void ParticlesStorage::particles_set_speed_scale(RID p_particles, double p_scale) {
  317. Particles *particles = particles_owner.get_or_null(p_particles);
  318. ERR_FAIL_NULL(particles);
  319. particles->speed_scale = p_scale;
  320. }
  321. void ParticlesStorage::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  322. Particles *particles = particles_owner.get_or_null(p_particles);
  323. ERR_FAIL_NULL(particles);
  324. particles->use_local_coords = p_enable;
  325. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  326. }
  327. void ParticlesStorage::particles_set_fixed_fps(RID p_particles, int p_fps) {
  328. Particles *particles = particles_owner.get_or_null(p_particles);
  329. ERR_FAIL_NULL(particles);
  330. particles->fixed_fps = p_fps;
  331. _particles_free_data(particles);
  332. particles->prev_ticks = 0;
  333. particles->phase = 0;
  334. particles->prev_phase = 0;
  335. particles->clear = true;
  336. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  337. }
  338. void ParticlesStorage::particles_set_interpolate(RID p_particles, bool p_enable) {
  339. Particles *particles = particles_owner.get_or_null(p_particles);
  340. ERR_FAIL_NULL(particles);
  341. particles->interpolate = p_enable;
  342. }
  343. void ParticlesStorage::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  344. Particles *particles = particles_owner.get_or_null(p_particles);
  345. ERR_FAIL_NULL(particles);
  346. particles->fractional_delta = p_enable;
  347. }
  348. void ParticlesStorage::particles_set_trails(RID p_particles, bool p_enable, double p_length) {
  349. Particles *particles = particles_owner.get_or_null(p_particles);
  350. ERR_FAIL_NULL(particles);
  351. ERR_FAIL_COND(p_length < 0.01);
  352. p_length = MIN(10.0, p_length);
  353. particles->trails_enabled = p_enable;
  354. particles->trail_lifetime = p_length;
  355. _particles_free_data(particles);
  356. particles->prev_ticks = 0;
  357. particles->phase = 0;
  358. particles->prev_phase = 0;
  359. particles->clear = true;
  360. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  361. }
  362. void ParticlesStorage::particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) {
  363. Particles *particles = particles_owner.get_or_null(p_particles);
  364. ERR_FAIL_NULL(particles);
  365. if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses.size() != p_bind_poses.size()) {
  366. _particles_free_data(particles);
  367. particles->prev_ticks = 0;
  368. particles->phase = 0;
  369. particles->prev_phase = 0;
  370. particles->clear = true;
  371. }
  372. particles->trail_bind_poses = p_bind_poses;
  373. particles->trail_bind_poses_dirty = true;
  374. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES);
  375. }
  376. void ParticlesStorage::particles_set_collision_base_size(RID p_particles, real_t p_size) {
  377. Particles *particles = particles_owner.get_or_null(p_particles);
  378. ERR_FAIL_NULL(particles);
  379. particles->collision_base_size = p_size;
  380. }
  381. void ParticlesStorage::particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) {
  382. Particles *particles = particles_owner.get_or_null(p_particles);
  383. ERR_FAIL_NULL(particles);
  384. particles->transform_align = p_transform_align;
  385. }
  386. void ParticlesStorage::particles_set_process_material(RID p_particles, RID p_material) {
  387. Particles *particles = particles_owner.get_or_null(p_particles);
  388. ERR_FAIL_NULL(particles);
  389. particles->process_material = p_material;
  390. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); //the instance buffer may have changed
  391. }
  392. RID ParticlesStorage::particles_get_process_material(RID p_particles) const {
  393. Particles *particles = particles_owner.get_or_null(p_particles);
  394. ERR_FAIL_NULL_V(particles, RID());
  395. return particles->process_material;
  396. }
  397. void ParticlesStorage::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  398. Particles *particles = particles_owner.get_or_null(p_particles);
  399. ERR_FAIL_NULL(particles);
  400. particles->draw_order = p_order;
  401. }
  402. void ParticlesStorage::particles_set_draw_passes(RID p_particles, int p_passes) {
  403. Particles *particles = particles_owner.get_or_null(p_particles);
  404. ERR_FAIL_NULL(particles);
  405. particles->draw_passes.resize(p_passes);
  406. }
  407. void ParticlesStorage::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  408. Particles *particles = particles_owner.get_or_null(p_particles);
  409. ERR_FAIL_NULL(particles);
  410. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  411. particles->draw_passes.write[p_pass] = p_mesh;
  412. }
  413. void ParticlesStorage::particles_restart(RID p_particles) {
  414. Particles *particles = particles_owner.get_or_null(p_particles);
  415. ERR_FAIL_NULL(particles);
  416. particles->restart_request = true;
  417. }
  418. void ParticlesStorage::_particles_allocate_emission_buffer(Particles *particles) {
  419. ERR_FAIL_COND(particles->emission_buffer != nullptr);
  420. particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4);
  421. memset(particles->emission_buffer_data.ptrw(), 0, particles->emission_buffer_data.size());
  422. particles->emission_buffer = reinterpret_cast<ParticleEmissionBuffer *>(particles->emission_buffer_data.ptrw());
  423. particles->emission_buffer->particle_max = particles->amount;
  424. particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data);
  425. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  426. //will need to be re-created
  427. RD::get_singleton()->free(particles->particles_material_uniform_set);
  428. particles->particles_material_uniform_set = RID();
  429. }
  430. }
  431. void ParticlesStorage::_particles_ensure_unused_emission_buffer(Particles *particles) {
  432. if (particles->unused_emission_storage_buffer.is_null()) {
  433. particles->unused_emission_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4);
  434. }
  435. }
  436. void ParticlesStorage::_particles_ensure_unused_trail_buffer(Particles *particles) {
  437. if (particles->unused_trail_storage_buffer.is_null()) {
  438. particles->unused_trail_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4);
  439. }
  440. }
  441. void ParticlesStorage::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  442. Particles *particles = particles_owner.get_or_null(p_particles);
  443. ERR_FAIL_NULL(particles);
  444. ERR_FAIL_COND(p_particles == p_subemitter_particles);
  445. particles->sub_emitter = p_subemitter_particles;
  446. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  447. RD::get_singleton()->free(particles->particles_material_uniform_set);
  448. particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting
  449. }
  450. }
  451. void ParticlesStorage::particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
  452. Particles *particles = particles_owner.get_or_null(p_particles);
  453. ERR_FAIL_NULL(particles);
  454. ERR_FAIL_COND(particles->amount == 0);
  455. if (particles->emitting) {
  456. particles->clear = true;
  457. particles->emitting = false;
  458. }
  459. if (particles->emission_buffer == nullptr) {
  460. _particles_allocate_emission_buffer(particles);
  461. }
  462. particles->inactive = false;
  463. particles->inactive_time = 0;
  464. int32_t idx = particles->emission_buffer->particle_count;
  465. if (idx < particles->emission_buffer->particle_max) {
  466. RendererRD::MaterialStorage::store_transform(p_transform, particles->emission_buffer->data[idx].xform);
  467. particles->emission_buffer->data[idx].velocity[0] = p_velocity.x;
  468. particles->emission_buffer->data[idx].velocity[1] = p_velocity.y;
  469. particles->emission_buffer->data[idx].velocity[2] = p_velocity.z;
  470. particles->emission_buffer->data[idx].custom[0] = p_custom.r;
  471. particles->emission_buffer->data[idx].custom[1] = p_custom.g;
  472. particles->emission_buffer->data[idx].custom[2] = p_custom.b;
  473. particles->emission_buffer->data[idx].custom[3] = p_custom.a;
  474. particles->emission_buffer->data[idx].color[0] = p_color.r;
  475. particles->emission_buffer->data[idx].color[1] = p_color.g;
  476. particles->emission_buffer->data[idx].color[2] = p_color.b;
  477. particles->emission_buffer->data[idx].color[3] = p_color.a;
  478. particles->emission_buffer->data[idx].flags = p_emit_flags;
  479. particles->emission_buffer->particle_count++;
  480. }
  481. }
  482. void ParticlesStorage::particles_request_process(RID p_particles) {
  483. Particles *particles = particles_owner.get_or_null(p_particles);
  484. ERR_FAIL_NULL(particles);
  485. if (!particles->dirty) {
  486. particles->dirty = true;
  487. if (!particles->update_list.in_list()) {
  488. particle_update_list.add(&particles->update_list);
  489. }
  490. }
  491. }
  492. AABB ParticlesStorage::particles_get_current_aabb(RID p_particles) {
  493. const Particles *particles = particles_owner.get_or_null(p_particles);
  494. ERR_FAIL_NULL_V(particles, AABB());
  495. int total_amount = particles->amount;
  496. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  497. total_amount *= particles->trail_bind_poses.size();
  498. }
  499. uint32_t particle_data_size = sizeof(ParticleData) + sizeof(float) * 4 * particles->userdata_count;
  500. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer);
  501. ERR_FAIL_COND_V(buffer.size() != (int)(total_amount * particle_data_size), AABB());
  502. Transform3D inv = particles->emission_transform.affine_inverse();
  503. AABB aabb;
  504. if (buffer.size()) {
  505. bool first = true;
  506. const uint8_t *data_ptr = (const uint8_t *)buffer.ptr();
  507. for (int i = 0; i < total_amount; i++) {
  508. const ParticleData &particle_data = *(const ParticleData *)&data_ptr[particle_data_size * i];
  509. if (particle_data.active) {
  510. Vector3 pos = Vector3(particle_data.xform[12], particle_data.xform[13], particle_data.xform[14]);
  511. if (!particles->use_local_coords) {
  512. pos = inv.xform(pos);
  513. }
  514. if (first) {
  515. aabb.position = pos;
  516. first = false;
  517. } else {
  518. aabb.expand_to(pos);
  519. }
  520. }
  521. }
  522. }
  523. float longest_axis_size = 0;
  524. for (int i = 0; i < particles->draw_passes.size(); i++) {
  525. if (particles->draw_passes[i].is_valid()) {
  526. AABB maabb = MeshStorage::get_singleton()->mesh_get_aabb(particles->draw_passes[i], RID());
  527. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  528. }
  529. }
  530. aabb.grow_by(longest_axis_size);
  531. return aabb;
  532. }
  533. AABB ParticlesStorage::particles_get_aabb(RID p_particles) const {
  534. const Particles *particles = particles_owner.get_or_null(p_particles);
  535. ERR_FAIL_NULL_V(particles, AABB());
  536. return particles->custom_aabb;
  537. }
  538. void ParticlesStorage::particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) {
  539. Particles *particles = particles_owner.get_or_null(p_particles);
  540. ERR_FAIL_NULL(particles);
  541. particles->emission_transform = p_transform;
  542. }
  543. void ParticlesStorage::particles_set_emitter_velocity(RID p_particles, const Vector3 &p_velocity) {
  544. Particles *particles = particles_owner.get_or_null(p_particles);
  545. ERR_FAIL_NULL(particles);
  546. particles->emitter_velocity = p_velocity;
  547. }
  548. void ParticlesStorage::particles_set_interp_to_end(RID p_particles, float p_interp) {
  549. Particles *particles = particles_owner.get_or_null(p_particles);
  550. ERR_FAIL_NULL(particles);
  551. particles->interp_to_end = p_interp;
  552. }
  553. int ParticlesStorage::particles_get_draw_passes(RID p_particles) const {
  554. const Particles *particles = particles_owner.get_or_null(p_particles);
  555. ERR_FAIL_NULL_V(particles, 0);
  556. return particles->draw_passes.size();
  557. }
  558. RID ParticlesStorage::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  559. const Particles *particles = particles_owner.get_or_null(p_particles);
  560. ERR_FAIL_NULL_V(particles, RID());
  561. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  562. return particles->draw_passes[p_pass];
  563. }
  564. void ParticlesStorage::particles_update_dependency(RID p_particles, DependencyTracker *p_instance) {
  565. Particles *particles = particles_owner.get_or_null(p_particles);
  566. ERR_FAIL_NULL(particles);
  567. p_instance->update_dependency(&particles->dependency);
  568. }
  569. void ParticlesStorage::particles_get_instance_buffer_motion_vectors_offsets(RID p_particles, uint32_t &r_current_offset, uint32_t &r_prev_offset) {
  570. Particles *particles = particles_owner.get_or_null(p_particles);
  571. ERR_FAIL_NULL(particles);
  572. r_current_offset = particles->instance_motion_vectors_current_offset;
  573. r_prev_offset = particles->instance_motion_vectors_previous_offset;
  574. }
  575. void ParticlesStorage::particles_add_collision(RID p_particles, RID p_particles_collision_instance) {
  576. Particles *particles = particles_owner.get_or_null(p_particles);
  577. ERR_FAIL_NULL(particles);
  578. particles->collisions.insert(p_particles_collision_instance);
  579. }
  580. void ParticlesStorage::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) {
  581. Particles *particles = particles_owner.get_or_null(p_particles);
  582. ERR_FAIL_NULL(particles);
  583. particles->collisions.erase(p_particles_collision_instance);
  584. }
  585. void ParticlesStorage::particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, RID p_texture) {
  586. Particles *particles = particles_owner.get_or_null(p_particles);
  587. ERR_FAIL_NULL(particles);
  588. particles->has_sdf_collision = p_enable;
  589. particles->sdf_collision_transform = p_xform;
  590. particles->sdf_collision_to_screen = p_to_screen;
  591. particles->sdf_collision_texture = p_texture;
  592. }
  593. void ParticlesStorage::_particles_process(Particles *p_particles, double p_delta) {
  594. TextureStorage *texture_storage = TextureStorage::get_singleton();
  595. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  596. if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) {
  597. Vector<RD::Uniform> uniforms;
  598. {
  599. RD::Uniform u;
  600. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  601. u.binding = 0;
  602. u.append_id(p_particles->frame_params_buffer);
  603. uniforms.push_back(u);
  604. }
  605. {
  606. RD::Uniform u;
  607. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  608. u.binding = 1;
  609. u.append_id(p_particles->particle_buffer);
  610. uniforms.push_back(u);
  611. }
  612. {
  613. RD::Uniform u;
  614. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  615. u.binding = 2;
  616. if (p_particles->emission_storage_buffer.is_valid()) {
  617. u.append_id(p_particles->emission_storage_buffer);
  618. } else {
  619. _particles_ensure_unused_emission_buffer(p_particles);
  620. u.append_id(p_particles->unused_emission_storage_buffer);
  621. }
  622. uniforms.push_back(u);
  623. }
  624. {
  625. RD::Uniform u;
  626. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  627. u.binding = 3;
  628. Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter);
  629. if (sub_emitter) {
  630. if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer
  631. _particles_allocate_emission_buffer(sub_emitter);
  632. }
  633. u.append_id(sub_emitter->emission_storage_buffer);
  634. } else {
  635. _particles_ensure_unused_emission_buffer(p_particles);
  636. u.append_id(p_particles->unused_emission_storage_buffer);
  637. }
  638. uniforms.push_back(u);
  639. }
  640. p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1);
  641. }
  642. double new_phase = Math::fmod((double)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, 1.0);
  643. //move back history (if there is any)
  644. for (uint32_t i = p_particles->frame_history.size() - 1; i > 0; i--) {
  645. p_particles->frame_history[i] = p_particles->frame_history[i - 1];
  646. }
  647. //update current frame
  648. ParticlesFrameParams &frame_params = p_particles->frame_history[0];
  649. if (p_particles->clear) {
  650. p_particles->cycle_number = 0;
  651. p_particles->random_seed = Math::rand();
  652. } else if (new_phase < p_particles->phase) {
  653. if (p_particles->one_shot) {
  654. p_particles->emitting = false;
  655. }
  656. p_particles->cycle_number++;
  657. }
  658. frame_params.emitting = p_particles->emitting;
  659. frame_params.system_phase = new_phase;
  660. frame_params.prev_system_phase = p_particles->phase;
  661. p_particles->phase = new_phase;
  662. frame_params.time = RendererCompositorRD::get_singleton()->get_total_time();
  663. frame_params.delta = p_delta * p_particles->speed_scale;
  664. frame_params.random_seed = p_particles->random_seed;
  665. frame_params.explosiveness = p_particles->explosiveness;
  666. frame_params.randomness = p_particles->randomness;
  667. if (p_particles->use_local_coords) {
  668. RendererRD::MaterialStorage::store_transform(Transform3D(), frame_params.emission_transform);
  669. } else {
  670. RendererRD::MaterialStorage::store_transform(p_particles->emission_transform, frame_params.emission_transform);
  671. }
  672. frame_params.cycle = p_particles->cycle_number;
  673. frame_params.frame = p_particles->frame_counter++;
  674. frame_params.amount_ratio = p_particles->amount_ratio;
  675. frame_params.pad1 = 0;
  676. frame_params.pad2 = 0;
  677. frame_params.emitter_velocity[0] = p_particles->emitter_velocity.x;
  678. frame_params.emitter_velocity[1] = p_particles->emitter_velocity.y;
  679. frame_params.emitter_velocity[2] = p_particles->emitter_velocity.z;
  680. frame_params.interp_to_end = p_particles->interp_to_end;
  681. { //collision and attractors
  682. frame_params.collider_count = 0;
  683. frame_params.attractor_count = 0;
  684. frame_params.particle_size = p_particles->collision_base_size;
  685. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  686. RID collision_heightmap_texture;
  687. Transform3D to_particles;
  688. if (p_particles->use_local_coords) {
  689. to_particles = p_particles->emission_transform.affine_inverse();
  690. }
  691. if (p_particles->has_sdf_collision && RD::get_singleton()->texture_is_valid(p_particles->sdf_collision_texture)) {
  692. //2D collision
  693. Transform2D xform = p_particles->sdf_collision_transform; //will use dotproduct manually so invert beforehand
  694. if (!p_particles->use_local_coords) {
  695. Transform2D emission;
  696. emission.columns[0] = Vector2(p_particles->emission_transform.basis.get_column(0).x, p_particles->emission_transform.basis.get_column(0).y);
  697. emission.columns[1] = Vector2(p_particles->emission_transform.basis.get_column(1).x, p_particles->emission_transform.basis.get_column(1).y);
  698. emission.set_origin(Vector2(p_particles->emission_transform.origin.x, p_particles->emission_transform.origin.y));
  699. xform = xform * emission.affine_inverse();
  700. }
  701. Transform2D revert = xform.affine_inverse();
  702. frame_params.collider_count = 1;
  703. frame_params.colliders[0].transform[0] = xform.columns[0][0];
  704. frame_params.colliders[0].transform[1] = xform.columns[0][1];
  705. frame_params.colliders[0].transform[2] = 0;
  706. frame_params.colliders[0].transform[3] = xform.columns[2][0];
  707. frame_params.colliders[0].transform[4] = xform.columns[1][0];
  708. frame_params.colliders[0].transform[5] = xform.columns[1][1];
  709. frame_params.colliders[0].transform[6] = 0;
  710. frame_params.colliders[0].transform[7] = xform.columns[2][1];
  711. frame_params.colliders[0].transform[8] = revert.columns[0][0];
  712. frame_params.colliders[0].transform[9] = revert.columns[0][1];
  713. frame_params.colliders[0].transform[10] = 0;
  714. frame_params.colliders[0].transform[11] = revert.columns[2][0];
  715. frame_params.colliders[0].transform[12] = revert.columns[1][0];
  716. frame_params.colliders[0].transform[13] = revert.columns[1][1];
  717. frame_params.colliders[0].transform[14] = 0;
  718. frame_params.colliders[0].transform[15] = revert.columns[2][1];
  719. frame_params.colliders[0].extents[0] = p_particles->sdf_collision_to_screen.size.x;
  720. frame_params.colliders[0].extents[1] = p_particles->sdf_collision_to_screen.size.y;
  721. frame_params.colliders[0].extents[2] = p_particles->sdf_collision_to_screen.position.x;
  722. frame_params.colliders[0].scale = p_particles->sdf_collision_to_screen.position.y;
  723. frame_params.colliders[0].texture_index = 0;
  724. frame_params.colliders[0].type = ParticlesFrameParams::COLLISION_TYPE_2D_SDF;
  725. collision_heightmap_texture = p_particles->sdf_collision_texture;
  726. //replace in all other history frames where used because parameters are no longer valid if screen moves
  727. for (ParticlesFrameParams &params : p_particles->frame_history) {
  728. if (params.collider_count > 0 && params.colliders[0].type == ParticlesFrameParams::COLLISION_TYPE_2D_SDF) {
  729. params.colliders[0] = frame_params.colliders[0];
  730. }
  731. }
  732. }
  733. uint32_t collision_3d_textures_used = 0;
  734. for (const RID &E : p_particles->collisions) {
  735. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(E);
  736. if (!pci || !pci->active) {
  737. continue;
  738. }
  739. ParticlesCollision *pc = particles_collision_owner.get_or_null(pci->collision);
  740. ERR_CONTINUE(!pc);
  741. Transform3D to_collider = pci->transform;
  742. if (p_particles->use_local_coords) {
  743. to_collider = to_particles * to_collider;
  744. }
  745. Vector3 scale = to_collider.basis.get_scale();
  746. to_collider.basis.orthonormalize();
  747. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  748. //attractor
  749. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  750. continue;
  751. }
  752. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  753. RendererRD::MaterialStorage::store_transform(to_collider, attr.transform);
  754. attr.strength = pc->attractor_strength;
  755. attr.attenuation = pc->attractor_attenuation;
  756. attr.directionality = pc->attractor_directionality;
  757. switch (pc->type) {
  758. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  759. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  760. float radius = pc->radius;
  761. radius *= (scale.x + scale.y + scale.z) / 3.0;
  762. attr.extents[0] = radius;
  763. attr.extents[1] = radius;
  764. attr.extents[2] = radius;
  765. } break;
  766. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  767. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  768. Vector3 extents = pc->extents * scale;
  769. attr.extents[0] = extents.x;
  770. attr.extents[1] = extents.y;
  771. attr.extents[2] = extents.z;
  772. } break;
  773. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  774. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  775. continue;
  776. }
  777. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD;
  778. Vector3 extents = pc->extents * scale;
  779. attr.extents[0] = extents.x;
  780. attr.extents[1] = extents.y;
  781. attr.extents[2] = extents.z;
  782. attr.texture_index = collision_3d_textures_used;
  783. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  784. collision_3d_textures_used++;
  785. } break;
  786. default: {
  787. }
  788. }
  789. frame_params.attractor_count++;
  790. } else {
  791. //collider
  792. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  793. continue;
  794. }
  795. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  796. RendererRD::MaterialStorage::store_transform(to_collider, col.transform);
  797. switch (pc->type) {
  798. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  799. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  800. float radius = pc->radius;
  801. radius *= (scale.x + scale.y + scale.z) / 3.0;
  802. col.extents[0] = radius;
  803. col.extents[1] = radius;
  804. col.extents[2] = radius;
  805. } break;
  806. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  807. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  808. Vector3 extents = pc->extents * scale;
  809. col.extents[0] = extents.x;
  810. col.extents[1] = extents.y;
  811. col.extents[2] = extents.z;
  812. } break;
  813. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  814. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  815. continue;
  816. }
  817. col.type = ParticlesFrameParams::COLLISION_TYPE_SDF;
  818. Vector3 extents = pc->extents * scale;
  819. col.extents[0] = extents.x;
  820. col.extents[1] = extents.y;
  821. col.extents[2] = extents.z;
  822. col.texture_index = collision_3d_textures_used;
  823. col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported
  824. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  825. collision_3d_textures_used++;
  826. } break;
  827. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  828. if (collision_heightmap_texture != RID()) { //already taken
  829. continue;
  830. }
  831. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  832. Vector3 extents = pc->extents * scale;
  833. col.extents[0] = extents.x;
  834. col.extents[1] = extents.y;
  835. col.extents[2] = extents.z;
  836. collision_heightmap_texture = pc->heightfield_texture;
  837. } break;
  838. default: {
  839. }
  840. }
  841. frame_params.collider_count++;
  842. }
  843. }
  844. bool different = false;
  845. if (collision_3d_textures_used == p_particles->collision_3d_textures_used) {
  846. for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  847. if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) {
  848. different = true;
  849. break;
  850. }
  851. }
  852. }
  853. if (collision_heightmap_texture != p_particles->collision_heightmap_texture) {
  854. different = true;
  855. }
  856. bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set);
  857. if (different || !uniform_set_valid) {
  858. if (uniform_set_valid) {
  859. RD::get_singleton()->free(p_particles->collision_textures_uniform_set);
  860. }
  861. Vector<RD::Uniform> uniforms;
  862. {
  863. RD::Uniform u;
  864. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  865. u.binding = 0;
  866. for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  867. RID rd_tex;
  868. if (i < collision_3d_textures_used) {
  869. if (TextureStorage::get_singleton()->texture_get_type(collision_3d_textures[i]) == TextureStorage::TYPE_3D) {
  870. rd_tex = TextureStorage::get_singleton()->texture_get_rd_texture(collision_3d_textures[i]);
  871. }
  872. }
  873. if (rd_tex == RID()) {
  874. rd_tex = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
  875. }
  876. u.append_id(rd_tex);
  877. }
  878. uniforms.push_back(u);
  879. }
  880. {
  881. RD::Uniform u;
  882. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  883. u.binding = 1;
  884. if (collision_heightmap_texture.is_valid()) {
  885. u.append_id(collision_heightmap_texture);
  886. } else {
  887. u.append_id(texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_BLACK));
  888. }
  889. uniforms.push_back(u);
  890. }
  891. p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2);
  892. p_particles->collision_heightmap_texture = collision_heightmap_texture;
  893. }
  894. }
  895. ParticlesShader::PushConstant push_constant;
  896. int process_amount = p_particles->amount;
  897. if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) {
  898. process_amount *= p_particles->trail_bind_poses.size();
  899. }
  900. push_constant.clear = p_particles->clear;
  901. push_constant.total_particles = p_particles->amount;
  902. push_constant.lifetime = p_particles->lifetime;
  903. push_constant.trail_size = p_particles->trail_params.size();
  904. push_constant.use_fractional_delta = p_particles->fractional_delta;
  905. push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit);
  906. push_constant.trail_pass = false;
  907. p_particles->force_sub_emit = false; //reset
  908. Particles *sub_emitter = particles_owner.get_or_null(p_particles->sub_emitter);
  909. if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) {
  910. // print_line("updating subemitter buffer");
  911. int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 };
  912. RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero);
  913. push_constant.can_emit = true;
  914. if (sub_emitter->emitting) {
  915. sub_emitter->emitting = false;
  916. sub_emitter->clear = true; //will need to clear if it was emitting, sorry
  917. }
  918. //make sure the sub emitter processes particles too
  919. sub_emitter->inactive = false;
  920. sub_emitter->inactive_time = 0;
  921. sub_emitter->force_sub_emit = true;
  922. } else {
  923. push_constant.can_emit = false;
  924. }
  925. if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) {
  926. RD::get_singleton()->buffer_update(p_particles->emission_storage_buffer, 0, sizeof(uint32_t) * 4 + sizeof(ParticleEmissionBuffer::Data) * p_particles->emission_buffer->particle_count, p_particles->emission_buffer);
  927. p_particles->emission_buffer->particle_count = 0;
  928. }
  929. p_particles->clear = false;
  930. if (p_particles->trail_params.size() > 1) {
  931. //fill the trail params
  932. for (uint32_t i = 0; i < p_particles->trail_params.size(); i++) {
  933. uint32_t src_idx = i * p_particles->frame_history.size() / p_particles->trail_params.size();
  934. p_particles->trail_params[i] = p_particles->frame_history[src_idx];
  935. }
  936. } else {
  937. p_particles->trail_params[0] = p_particles->frame_history[0];
  938. }
  939. RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams) * p_particles->trail_params.size(), p_particles->trail_params.ptr());
  940. ParticleProcessMaterialData *m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(p_particles->process_material, MaterialStorage::SHADER_TYPE_PARTICLES));
  941. if (!m) {
  942. m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, MaterialStorage::SHADER_TYPE_PARTICLES));
  943. }
  944. ERR_FAIL_NULL(m);
  945. p_particles->has_collision_cache = m->shader_data->uses_collision;
  946. //todo should maybe compute all particle systems together?
  947. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  948. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline);
  949. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, BASE_UNIFORM_SET);
  950. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, MATERIAL_UNIFORM_SET);
  951. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, COLLISION_TEXTURTES_UNIFORM_SET);
  952. if (m->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(m->uniform_set)) {
  953. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3);
  954. m->set_as_used();
  955. }
  956. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  957. if (p_particles->trails_enabled && p_particles->trail_bind_poses.size() > 1) {
  958. //trails requires two passes in order to catch particle starts
  959. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount / p_particles->trail_bind_poses.size(), 1, 1);
  960. RD::get_singleton()->compute_list_add_barrier(compute_list);
  961. push_constant.trail_pass = true;
  962. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  963. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount - p_particles->amount, 1, 1);
  964. } else {
  965. RD::get_singleton()->compute_list_dispatch_threads(compute_list, process_amount, 1, 1);
  966. }
  967. RD::get_singleton()->compute_list_end();
  968. }
  969. void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) {
  970. Particles *particles = particles_owner.get_or_null(p_particles);
  971. ERR_FAIL_NULL(particles);
  972. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) {
  973. return;
  974. }
  975. if (particles->particle_buffer.is_null() || particles->trail_bind_pose_uniform_set.is_null()) {
  976. return; //particles have not processed yet
  977. }
  978. bool do_sort = particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH;
  979. //copy to sort buffer
  980. if (do_sort && particles->particles_sort_buffer == RID()) {
  981. uint32_t size = particles->amount;
  982. if (size & 1) {
  983. size++; //make multiple of 16
  984. }
  985. size *= sizeof(float) * 2;
  986. particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size);
  987. {
  988. Vector<RD::Uniform> uniforms;
  989. {
  990. RD::Uniform u;
  991. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  992. u.binding = 0;
  993. u.append_id(particles->particles_sort_buffer);
  994. uniforms.push_back(u);
  995. }
  996. particles->particles_sort_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, ParticlesShader::COPY_MODE_FILL_SORT_BUFFER), 1);
  997. }
  998. }
  999. ParticlesShader::CopyPushConstant copy_push_constant;
  1000. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1001. int fixed_fps = 60.0;
  1002. if (particles->fixed_fps > 0) {
  1003. fixed_fps = particles->fixed_fps;
  1004. }
  1005. copy_push_constant.trail_size = particles->trail_bind_poses.size();
  1006. copy_push_constant.trail_total = particles->frame_history.size();
  1007. copy_push_constant.frame_delta = 1.0 / fixed_fps;
  1008. } else {
  1009. copy_push_constant.trail_size = 1;
  1010. copy_push_constant.trail_total = 1;
  1011. copy_push_constant.frame_delta = 0.0;
  1012. }
  1013. copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME);
  1014. copy_push_constant.lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount;
  1015. copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME;
  1016. copy_push_constant.motion_vectors_current_offset = particles->instance_motion_vectors_current_offset;
  1017. copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0;
  1018. copy_push_constant.total_particles = particles->amount;
  1019. copy_push_constant.copy_mode_2d = false;
  1020. Vector3 axis = -p_axis; // cameras look to z negative
  1021. if (particles->use_local_coords) {
  1022. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  1023. }
  1024. copy_push_constant.sort_direction[0] = axis.x;
  1025. copy_push_constant.sort_direction[1] = axis.y;
  1026. copy_push_constant.sort_direction[2] = axis.z;
  1027. copy_push_constant.align_up[0] = p_up_axis.x;
  1028. copy_push_constant.align_up[1] = p_up_axis.y;
  1029. copy_push_constant.align_up[2] = p_up_axis.z;
  1030. copy_push_constant.align_mode = particles->transform_align;
  1031. if (do_sort) {
  1032. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1033. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_SORT_BUFFER + particles->userdata_count * ParticlesShader::COPY_MODE_MAX]);
  1034. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  1035. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  1036. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  1037. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  1038. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1);
  1039. RD::get_singleton()->compute_list_end();
  1040. sort_effects->sort_buffer(particles->particles_sort_uniform_set, particles->amount);
  1041. }
  1042. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1043. copy_push_constant.total_particles *= particles->trail_bind_poses.size();
  1044. }
  1045. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1046. uint32_t copy_pipeline = do_sort ? ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER : ParticlesShader::COPY_MODE_FILL_INSTANCES;
  1047. copy_pipeline += particles->userdata_count * ParticlesShader::COPY_MODE_MAX;
  1048. copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0;
  1049. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[copy_pipeline]);
  1050. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  1051. if (do_sort) {
  1052. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  1053. }
  1054. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  1055. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  1056. RD::get_singleton()->compute_list_dispatch_threads(compute_list, copy_push_constant.total_particles, 1, 1);
  1057. RD::get_singleton()->compute_list_end();
  1058. }
  1059. void ParticlesStorage::_particles_update_buffers(Particles *particles) {
  1060. uint32_t userdata_count = 0;
  1061. MaterialStorage::ShaderData *shader_data = MaterialStorage::get_singleton()->material_get_shader_data(particles->process_material);
  1062. if (shader_data) {
  1063. const ParticlesShaderData *particle_shader_data = static_cast<const ParticlesShaderData *>(shader_data);
  1064. userdata_count = particle_shader_data->userdata_count;
  1065. }
  1066. bool uses_motion_vectors = RSG::viewport->get_num_viewports_with_motion_vectors() > 0 || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0);
  1067. bool index_draw_order = particles->draw_order == RS::ParticlesDrawOrder::PARTICLES_DRAW_ORDER_INDEX;
  1068. bool enable_motion_vectors = uses_motion_vectors && index_draw_order && !particles->instance_motion_vectors_enabled;
  1069. bool only_instances_changed = false;
  1070. if (userdata_count != particles->userdata_count) {
  1071. // Mismatch userdata, re-create all buffers.
  1072. _particles_free_data(particles);
  1073. } else if (enable_motion_vectors) {
  1074. // Only motion vectors are required, release the transforms buffer and uniform set.
  1075. if (particles->particle_instance_buffer.is_valid()) {
  1076. RD::get_singleton()->free(particles->particle_instance_buffer);
  1077. particles->particle_instance_buffer = RID();
  1078. }
  1079. particles->particles_transforms_buffer_uniform_set = RID();
  1080. only_instances_changed = true;
  1081. } else if (!particles->particle_buffer.is_null()) {
  1082. // No operation is required because a buffer already exists, return early.
  1083. return;
  1084. }
  1085. if (particles->amount > 0) {
  1086. int total_amount = particles->amount;
  1087. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1088. total_amount *= particles->trail_bind_poses.size();
  1089. }
  1090. uint32_t xform_size = particles->mode == RS::PARTICLES_MODE_2D ? 2 : 3;
  1091. if (particles->particle_buffer.is_null()) {
  1092. particles->particle_buffer = RD::get_singleton()->storage_buffer_create((sizeof(ParticleData) + userdata_count * sizeof(float) * 4) * total_amount);
  1093. particles->userdata_count = userdata_count;
  1094. }
  1095. PackedByteArray data;
  1096. uint32_t particle_instance_buffer_size = total_amount * (xform_size + 1 + 1) * sizeof(float) * 4;
  1097. if (uses_motion_vectors) {
  1098. particle_instance_buffer_size *= 2;
  1099. particles->instance_motion_vectors_enabled = true;
  1100. }
  1101. data.resize_zeroed(particle_instance_buffer_size);
  1102. particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(particle_instance_buffer_size, data);
  1103. {
  1104. Vector<RD::Uniform> uniforms;
  1105. {
  1106. RD::Uniform u;
  1107. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1108. u.binding = 1;
  1109. u.append_id(particles->particle_buffer);
  1110. uniforms.push_back(u);
  1111. }
  1112. {
  1113. RD::Uniform u;
  1114. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1115. u.binding = 2;
  1116. u.append_id(particles->particle_instance_buffer);
  1117. uniforms.push_back(u);
  1118. }
  1119. particles->particles_copy_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 0);
  1120. }
  1121. particles->instance_motion_vectors_current_offset = 0;
  1122. particles->instance_motion_vectors_previous_offset = 0;
  1123. particles->instance_motion_vectors_last_change = -1;
  1124. if (only_instances_changed) {
  1125. // Notify the renderer the instances uniform must be retrieved again, as it's the only element that has been changed because motion vectors were enabled.
  1126. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES_INSTANCES);
  1127. }
  1128. }
  1129. }
  1130. void ParticlesStorage::update_particles() {
  1131. if (!particle_update_list.first()) {
  1132. return;
  1133. }
  1134. RENDER_TIMESTAMP("Update GPUParticles");
  1135. uint32_t frame = RSG::rasterizer->get_frame_number();
  1136. bool uses_motion_vectors = RSG::viewport->get_num_viewports_with_motion_vectors() > 0 || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0);
  1137. while (particle_update_list.first()) {
  1138. //use transform feedback to process particles
  1139. Particles *particles = particle_update_list.first()->self();
  1140. particles->update_list.remove_from_list();
  1141. particles->dirty = false;
  1142. _particles_update_buffers(particles);
  1143. if (particles->restart_request) {
  1144. particles->prev_ticks = 0;
  1145. particles->phase = 0;
  1146. particles->prev_phase = 0;
  1147. particles->clear = true;
  1148. particles->restart_request = false;
  1149. }
  1150. if (particles->inactive && !particles->emitting) {
  1151. //go next
  1152. continue;
  1153. }
  1154. if (particles->emitting) {
  1155. if (particles->inactive) {
  1156. //restart system from scratch
  1157. particles->prev_ticks = 0;
  1158. particles->phase = 0;
  1159. particles->prev_phase = 0;
  1160. particles->clear = true;
  1161. }
  1162. particles->inactive = false;
  1163. particles->inactive_time = 0;
  1164. } else {
  1165. particles->inactive_time += particles->speed_scale * RendererCompositorRD::get_singleton()->get_frame_delta_time();
  1166. if (particles->inactive_time > particles->lifetime * 1.2) {
  1167. particles->inactive = true;
  1168. continue;
  1169. }
  1170. }
  1171. // TODO: Should use display refresh rate for all this.
  1172. float screen_hz = 60;
  1173. int fixed_fps = 0;
  1174. if (particles->fixed_fps > 0) {
  1175. fixed_fps = particles->fixed_fps;
  1176. } else if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1177. fixed_fps = screen_hz;
  1178. }
  1179. {
  1180. //update trails
  1181. int history_size = 1;
  1182. int trail_steps = 1;
  1183. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1184. history_size = MAX(1, int(particles->trail_lifetime * fixed_fps));
  1185. trail_steps = particles->trail_bind_poses.size();
  1186. }
  1187. if (uint32_t(history_size) != particles->frame_history.size()) {
  1188. particles->frame_history.resize(history_size);
  1189. memset(particles->frame_history.ptr(), 0, sizeof(ParticlesFrameParams) * history_size);
  1190. // Set the frame number so that we are able to distinguish an uninitialized
  1191. // frame from the true frame number zero. See issue #88712 for details.
  1192. for (int i = 0; i < history_size; i++) {
  1193. particles->frame_history[i].frame = UINT32_MAX;
  1194. }
  1195. }
  1196. if (uint32_t(trail_steps) != particles->trail_params.size() || particles->frame_params_buffer.is_null()) {
  1197. particles->trail_params.resize(trail_steps);
  1198. if (particles->frame_params_buffer.is_valid()) {
  1199. RD::get_singleton()->free(particles->frame_params_buffer);
  1200. }
  1201. particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * trail_steps);
  1202. }
  1203. if (particles->trail_bind_poses.size() > 1 && particles->trail_bind_pose_buffer.is_null()) {
  1204. particles->trail_bind_pose_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 16 * particles->trail_bind_poses.size());
  1205. particles->trail_bind_poses_dirty = true;
  1206. }
  1207. if (particles->trail_bind_pose_uniform_set.is_null()) {
  1208. Vector<RD::Uniform> uniforms;
  1209. {
  1210. RD::Uniform u;
  1211. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1212. u.binding = 0;
  1213. if (particles->trail_bind_pose_buffer.is_valid()) {
  1214. u.append_id(particles->trail_bind_pose_buffer);
  1215. } else {
  1216. _particles_ensure_unused_trail_buffer(particles);
  1217. u.append_id(particles->unused_trail_storage_buffer);
  1218. }
  1219. uniforms.push_back(u);
  1220. }
  1221. particles->trail_bind_pose_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 2);
  1222. }
  1223. if (particles->trail_bind_pose_buffer.is_valid() && particles->trail_bind_poses_dirty) {
  1224. if (particles_shader.pose_update_buffer.size() < uint32_t(particles->trail_bind_poses.size()) * 16) {
  1225. particles_shader.pose_update_buffer.resize(particles->trail_bind_poses.size() * 16);
  1226. }
  1227. for (int i = 0; i < particles->trail_bind_poses.size(); i++) {
  1228. RendererRD::MaterialStorage::store_transform(particles->trail_bind_poses[i], &particles_shader.pose_update_buffer[i * 16]);
  1229. }
  1230. RD::get_singleton()->buffer_update(particles->trail_bind_pose_buffer, 0, particles->trail_bind_poses.size() * 16 * sizeof(float), particles_shader.pose_update_buffer.ptr());
  1231. }
  1232. }
  1233. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  1234. if (particles->clear && particles->pre_process_time > 0.0) {
  1235. double frame_time;
  1236. if (fixed_fps > 0) {
  1237. frame_time = 1.0 / fixed_fps;
  1238. } else {
  1239. frame_time = 1.0 / 30.0;
  1240. }
  1241. double todo = particles->pre_process_time;
  1242. while (todo >= 0) {
  1243. _particles_process(particles, frame_time);
  1244. todo -= frame_time;
  1245. }
  1246. }
  1247. if (fixed_fps > 0) {
  1248. double frame_time;
  1249. double decr;
  1250. if (zero_time_scale) {
  1251. frame_time = 0.0;
  1252. decr = 1.0 / fixed_fps;
  1253. } else {
  1254. frame_time = 1.0 / fixed_fps;
  1255. decr = frame_time;
  1256. }
  1257. double delta = RendererCompositorRD::get_singleton()->get_frame_delta_time();
  1258. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  1259. delta = 0.1;
  1260. } else if (delta <= 0.0) { //unlikely but..
  1261. delta = 0.001;
  1262. }
  1263. double todo = particles->frame_remainder + delta;
  1264. while (todo >= frame_time || particles->clear) {
  1265. _particles_process(particles, frame_time);
  1266. todo -= decr;
  1267. }
  1268. particles->frame_remainder = todo;
  1269. } else {
  1270. if (zero_time_scale) {
  1271. _particles_process(particles, 0.0);
  1272. } else {
  1273. _particles_process(particles, RendererCompositorRD::get_singleton()->get_frame_delta_time());
  1274. }
  1275. }
  1276. // Ensure that memory is initialized (the code above should ensure that _particles_process is always called at least once upon clearing).
  1277. DEV_ASSERT(!particles->clear);
  1278. int total_amount = particles->amount;
  1279. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1280. total_amount *= particles->trail_bind_poses.size();
  1281. }
  1282. // Swap offsets for motion vectors. Motion vectors can only be used when the draw order keeps the indices consistent across frames.
  1283. bool index_draw_order = particles->draw_order == RS::ParticlesDrawOrder::PARTICLES_DRAW_ORDER_INDEX;
  1284. particles->instance_motion_vectors_previous_offset = particles->instance_motion_vectors_current_offset;
  1285. if (uses_motion_vectors && index_draw_order && particles->instance_motion_vectors_enabled && (frame - particles->instance_motion_vectors_last_change) == 1) {
  1286. particles->instance_motion_vectors_current_offset = total_amount - particles->instance_motion_vectors_current_offset;
  1287. }
  1288. particles->instance_motion_vectors_last_change = frame;
  1289. // Copy particles to instance buffer.
  1290. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) {
  1291. //does not need view dependent operation, do copy here
  1292. ParticlesShader::CopyPushConstant copy_push_constant;
  1293. // Affect 2D only.
  1294. if (particles->use_local_coords) {
  1295. // In local mode, particle positions are calculated locally (relative to the node position)
  1296. // and they're also drawn locally.
  1297. // It works as expected, so we just pass an identity transform.
  1298. RendererRD::MaterialStorage::store_transform(Transform3D(), copy_push_constant.inv_emission_transform);
  1299. } else {
  1300. // In global mode, particle positions are calculated globally (relative to the canvas origin)
  1301. // but they're drawn locally.
  1302. // So, we need to pass the inverse of the emission transform to bring the
  1303. // particles to local coordinates before drawing.
  1304. Transform3D inv = particles->emission_transform.affine_inverse();
  1305. RendererRD::MaterialStorage::store_transform(inv, copy_push_constant.inv_emission_transform);
  1306. }
  1307. copy_push_constant.total_particles = total_amount;
  1308. copy_push_constant.frame_remainder = particles->interpolate ? particles->frame_remainder : 0.0;
  1309. copy_push_constant.align_mode = particles->transform_align;
  1310. copy_push_constant.align_up[0] = 0;
  1311. copy_push_constant.align_up[1] = 0;
  1312. copy_push_constant.align_up[2] = 0;
  1313. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1314. copy_push_constant.trail_size = particles->trail_bind_poses.size();
  1315. copy_push_constant.trail_total = particles->frame_history.size();
  1316. copy_push_constant.frame_delta = 1.0 / fixed_fps;
  1317. } else {
  1318. copy_push_constant.trail_size = 1;
  1319. copy_push_constant.trail_total = 1;
  1320. copy_push_constant.frame_delta = 0.0;
  1321. }
  1322. copy_push_constant.order_by_lifetime = (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME);
  1323. copy_push_constant.lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount;
  1324. copy_push_constant.lifetime_reverse = particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME;
  1325. copy_push_constant.motion_vectors_current_offset = particles->instance_motion_vectors_current_offset;
  1326. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1327. copy_push_constant.copy_mode_2d = particles->mode == RS::PARTICLES_MODE_2D ? 1 : 0;
  1328. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES + particles->userdata_count * ParticlesShader::COPY_MODE_MAX]);
  1329. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  1330. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->trail_bind_pose_uniform_set, 2);
  1331. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  1332. RD::get_singleton()->compute_list_dispatch_threads(compute_list, total_amount, 1, 1);
  1333. RD::get_singleton()->compute_list_end();
  1334. }
  1335. particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1336. }
  1337. }
  1338. Dependency *ParticlesStorage::particles_get_dependency(RID p_particles) const {
  1339. Particles *particles = particles_owner.get_or_null(p_particles);
  1340. ERR_FAIL_NULL_V(particles, nullptr);
  1341. return &particles->dependency;
  1342. }
  1343. bool ParticlesStorage::particles_is_inactive(RID p_particles) const {
  1344. const Particles *particles = particles_owner.get_or_null(p_particles);
  1345. ERR_FAIL_NULL_V(particles, false);
  1346. return !particles->emitting && particles->inactive;
  1347. }
  1348. /* Particles SHADER */
  1349. void ParticlesStorage::ParticlesShaderData::set_code(const String &p_code) {
  1350. ParticlesStorage *particles_storage = ParticlesStorage::get_singleton();
  1351. //compile
  1352. code = p_code;
  1353. valid = false;
  1354. ubo_size = 0;
  1355. uniforms.clear();
  1356. uses_collision = false;
  1357. if (code.is_empty()) {
  1358. return; //just invalid, but no error
  1359. }
  1360. ShaderCompiler::GeneratedCode gen_code;
  1361. ShaderCompiler::IdentifierActions actions;
  1362. actions.entry_point_stages["start"] = ShaderCompiler::STAGE_COMPUTE;
  1363. actions.entry_point_stages["process"] = ShaderCompiler::STAGE_COMPUTE;
  1364. /*
  1365. uses_time = false;
  1366. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  1367. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  1368. actions.usage_flag_pointers["TIME"] = &uses_time;
  1369. */
  1370. actions.usage_flag_pointers["COLLIDED"] = &uses_collision;
  1371. userdata_count = 0;
  1372. for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  1373. userdatas_used[i] = false;
  1374. actions.usage_flag_pointers["USERDATA" + itos(i + 1)] = &userdatas_used[i];
  1375. }
  1376. actions.uniforms = &uniforms;
  1377. Error err = particles_storage->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code);
  1378. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  1379. if (version.is_null()) {
  1380. version = particles_storage->particles_shader.shader.version_create();
  1381. }
  1382. for (uint32_t i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  1383. if (userdatas_used[i]) {
  1384. userdata_count++;
  1385. }
  1386. }
  1387. particles_storage->particles_shader.shader.version_set_compute_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_COMPUTE], gen_code.defines);
  1388. ERR_FAIL_COND(!particles_storage->particles_shader.shader.version_is_valid(version));
  1389. ubo_size = gen_code.uniform_total_size;
  1390. ubo_offsets = gen_code.uniform_offsets;
  1391. texture_uniforms = gen_code.texture_uniforms;
  1392. //update pipelines
  1393. pipeline = RD::get_singleton()->compute_pipeline_create(particles_storage->particles_shader.shader.version_get_shader(version, 0));
  1394. valid = true;
  1395. }
  1396. bool ParticlesStorage::ParticlesShaderData::is_animated() const {
  1397. return false;
  1398. }
  1399. bool ParticlesStorage::ParticlesShaderData::casts_shadows() const {
  1400. return false;
  1401. }
  1402. RS::ShaderNativeSourceCode ParticlesStorage::ParticlesShaderData::get_native_source_code() const {
  1403. return ParticlesStorage::get_singleton()->particles_shader.shader.version_get_native_source_code(version);
  1404. }
  1405. ParticlesStorage::ParticlesShaderData::~ParticlesShaderData() {
  1406. //pipeline variants will clear themselves if shader is gone
  1407. if (version.is_valid()) {
  1408. ParticlesStorage::get_singleton()->particles_shader.shader.version_free(version);
  1409. }
  1410. }
  1411. MaterialStorage::ShaderData *ParticlesStorage::_create_particles_shader_func() {
  1412. ParticlesShaderData *shader_data = memnew(ParticlesShaderData);
  1413. return shader_data;
  1414. }
  1415. bool ParticlesStorage::ParticleProcessMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  1416. return update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, ParticlesStorage::get_singleton()->particles_shader.shader.version_get_shader(shader_data->version, 0), 3, true, false);
  1417. }
  1418. ParticlesStorage::ParticleProcessMaterialData::~ParticleProcessMaterialData() {
  1419. free_parameters_uniform_set(uniform_set);
  1420. }
  1421. MaterialStorage::MaterialData *ParticlesStorage::_create_particles_material_func(ParticlesShaderData *p_shader) {
  1422. ParticleProcessMaterialData *material_data = memnew(ParticleProcessMaterialData);
  1423. material_data->shader_data = p_shader;
  1424. //update will happen later anyway so do nothing.
  1425. return material_data;
  1426. }
  1427. ////////
  1428. /* PARTICLES COLLISION API */
  1429. RID ParticlesStorage::particles_collision_allocate() {
  1430. return particles_collision_owner.allocate_rid();
  1431. }
  1432. void ParticlesStorage::particles_collision_initialize(RID p_rid) {
  1433. particles_collision_owner.initialize_rid(p_rid, ParticlesCollision());
  1434. }
  1435. void ParticlesStorage::particles_collision_free(RID p_rid) {
  1436. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_rid);
  1437. if (particles_collision->heightfield_texture.is_valid()) {
  1438. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1439. }
  1440. particles_collision->dependency.deleted_notify(p_rid);
  1441. particles_collision_owner.free(p_rid);
  1442. }
  1443. RID ParticlesStorage::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  1444. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1445. ERR_FAIL_NULL_V(particles_collision, RID());
  1446. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID());
  1447. if (particles_collision->heightfield_texture == RID()) {
  1448. //create
  1449. const int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  1450. Size2i size;
  1451. if (particles_collision->extents.x > particles_collision->extents.z) {
  1452. size.x = resolutions[particles_collision->heightfield_resolution];
  1453. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  1454. } else {
  1455. size.y = resolutions[particles_collision->heightfield_resolution];
  1456. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  1457. }
  1458. RD::TextureFormat tf;
  1459. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  1460. tf.width = size.x;
  1461. tf.height = size.y;
  1462. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1463. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1464. particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1465. Vector<RID> fb_tex;
  1466. fb_tex.push_back(particles_collision->heightfield_texture);
  1467. particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  1468. particles_collision->heightfield_fb_size = size;
  1469. }
  1470. return particles_collision->heightfield_fb;
  1471. }
  1472. void ParticlesStorage::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  1473. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1474. ERR_FAIL_NULL(particles_collision);
  1475. if (p_type == particles_collision->type) {
  1476. return;
  1477. }
  1478. if (particles_collision->heightfield_texture.is_valid()) {
  1479. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1480. particles_collision->heightfield_texture = RID();
  1481. }
  1482. particles_collision->type = p_type;
  1483. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1484. }
  1485. void ParticlesStorage::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  1486. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1487. ERR_FAIL_NULL(particles_collision);
  1488. particles_collision->cull_mask = p_cull_mask;
  1489. }
  1490. void ParticlesStorage::particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) {
  1491. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1492. ERR_FAIL_NULL(particles_collision);
  1493. particles_collision->radius = p_radius;
  1494. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1495. }
  1496. void ParticlesStorage::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  1497. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1498. ERR_FAIL_NULL(particles_collision);
  1499. particles_collision->extents = p_extents;
  1500. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1501. }
  1502. void ParticlesStorage::particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) {
  1503. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1504. ERR_FAIL_NULL(particles_collision);
  1505. particles_collision->attractor_strength = p_strength;
  1506. }
  1507. void ParticlesStorage::particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) {
  1508. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1509. ERR_FAIL_NULL(particles_collision);
  1510. particles_collision->attractor_directionality = p_directionality;
  1511. }
  1512. void ParticlesStorage::particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) {
  1513. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1514. ERR_FAIL_NULL(particles_collision);
  1515. particles_collision->attractor_attenuation = p_curve;
  1516. }
  1517. void ParticlesStorage::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  1518. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1519. ERR_FAIL_NULL(particles_collision);
  1520. particles_collision->field_texture = p_texture;
  1521. }
  1522. void ParticlesStorage::particles_collision_height_field_update(RID p_particles_collision) {
  1523. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1524. ERR_FAIL_NULL(particles_collision);
  1525. particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1526. }
  1527. void ParticlesStorage::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  1528. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1529. ERR_FAIL_NULL(particles_collision);
  1530. ERR_FAIL_INDEX(p_resolution, RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX);
  1531. if (particles_collision->heightfield_resolution == p_resolution) {
  1532. return;
  1533. }
  1534. particles_collision->heightfield_resolution = p_resolution;
  1535. if (particles_collision->heightfield_texture.is_valid()) {
  1536. RD::get_singleton()->free(particles_collision->heightfield_texture);
  1537. particles_collision->heightfield_texture = RID();
  1538. }
  1539. }
  1540. AABB ParticlesStorage::particles_collision_get_aabb(RID p_particles_collision) const {
  1541. ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1542. ERR_FAIL_NULL_V(particles_collision, AABB());
  1543. switch (particles_collision->type) {
  1544. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  1545. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  1546. AABB aabb;
  1547. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  1548. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  1549. return aabb;
  1550. }
  1551. default: {
  1552. AABB aabb;
  1553. aabb.position = -particles_collision->extents;
  1554. aabb.size = particles_collision->extents * 2;
  1555. return aabb;
  1556. }
  1557. }
  1558. }
  1559. Vector3 ParticlesStorage::particles_collision_get_extents(RID p_particles_collision) const {
  1560. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1561. ERR_FAIL_NULL_V(particles_collision, Vector3());
  1562. return particles_collision->extents;
  1563. }
  1564. bool ParticlesStorage::particles_collision_is_heightfield(RID p_particles_collision) const {
  1565. const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision);
  1566. ERR_FAIL_NULL_V(particles_collision, false);
  1567. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  1568. }
  1569. Dependency *ParticlesStorage::particles_collision_get_dependency(RID p_particles_collision) const {
  1570. ParticlesCollision *pc = particles_collision_owner.get_or_null(p_particles_collision);
  1571. ERR_FAIL_NULL_V(pc, nullptr);
  1572. return &pc->dependency;
  1573. }
  1574. /* Particles collision instance */
  1575. RID ParticlesStorage::particles_collision_instance_create(RID p_collision) {
  1576. ParticlesCollisionInstance pci;
  1577. pci.collision = p_collision;
  1578. return particles_collision_instance_owner.make_rid(pci);
  1579. }
  1580. void ParticlesStorage::particles_collision_instance_free(RID p_rid) {
  1581. particles_collision_instance_owner.free(p_rid);
  1582. }
  1583. void ParticlesStorage::particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) {
  1584. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1585. ERR_FAIL_NULL(pci);
  1586. pci->transform = p_transform;
  1587. }
  1588. void ParticlesStorage::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) {
  1589. ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance);
  1590. ERR_FAIL_NULL(pci);
  1591. pci->active = p_active;
  1592. }