cpu_particles_2d.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**************************************************************************/
  2. /* cpu_particles_2d.h */
  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. #pragma once
  31. #include "scene/2d/node_2d.h"
  32. class RandomNumberGenerator;
  33. class CPUParticles2D : public Node2D {
  34. private:
  35. GDCLASS(CPUParticles2D, Node2D);
  36. public:
  37. enum DrawOrder {
  38. DRAW_ORDER_INDEX,
  39. DRAW_ORDER_LIFETIME,
  40. };
  41. enum Parameter {
  42. PARAM_INITIAL_LINEAR_VELOCITY,
  43. PARAM_ANGULAR_VELOCITY,
  44. PARAM_ORBIT_VELOCITY,
  45. PARAM_LINEAR_ACCEL,
  46. PARAM_RADIAL_ACCEL,
  47. PARAM_TANGENTIAL_ACCEL,
  48. PARAM_DAMPING,
  49. PARAM_ANGLE,
  50. PARAM_SCALE,
  51. PARAM_HUE_VARIATION,
  52. PARAM_ANIM_SPEED,
  53. PARAM_ANIM_OFFSET,
  54. PARAM_MAX
  55. };
  56. enum ParticleFlags {
  57. PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
  58. PARTICLE_FLAG_ROTATE_Y, // Unused, but exposed for consistency with 3D.
  59. PARTICLE_FLAG_DISABLE_Z, // Unused, but exposed for consistency with 3D.
  60. PARTICLE_FLAG_MAX
  61. };
  62. enum EmissionShape {
  63. EMISSION_SHAPE_POINT,
  64. EMISSION_SHAPE_SPHERE,
  65. EMISSION_SHAPE_SPHERE_SURFACE,
  66. EMISSION_SHAPE_RECTANGLE,
  67. EMISSION_SHAPE_POINTS,
  68. EMISSION_SHAPE_DIRECTED_POINTS,
  69. EMISSION_SHAPE_MAX
  70. };
  71. private:
  72. bool emitting = false;
  73. bool active = false;
  74. struct Particle {
  75. Transform2D transform;
  76. Color color;
  77. real_t custom[4] = {};
  78. real_t rotation = 0.0;
  79. Vector2 velocity;
  80. bool active = false;
  81. real_t angle_rand = 0.0;
  82. real_t scale_rand = 0.0;
  83. real_t hue_rot_rand = 0.0;
  84. real_t anim_offset_rand = 0.0;
  85. Color start_color_rand;
  86. double time = 0.0;
  87. double lifetime = 0.0;
  88. Color base_color;
  89. uint32_t seed = 0;
  90. };
  91. double time = 0.0;
  92. double frame_remainder = 0.0;
  93. int cycle = 0;
  94. bool do_redraw = false;
  95. RID mesh;
  96. RID multimesh;
  97. Vector<Particle> particles;
  98. Vector<float> particle_data;
  99. Vector<int> particle_order;
  100. struct SortLifetime {
  101. const Particle *particles = nullptr;
  102. bool operator()(int p_a, int p_b) const {
  103. return particles[p_a].time > particles[p_b].time;
  104. }
  105. };
  106. struct SortAxis {
  107. const Particle *particles = nullptr;
  108. Vector2 axis;
  109. bool operator()(int p_a, int p_b) const {
  110. return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
  111. }
  112. };
  113. //
  114. bool one_shot = false;
  115. double lifetime = 1.0;
  116. double pre_process_time = 0.0;
  117. double _requested_process_time = 0.0;
  118. real_t explosiveness_ratio = 0.0;
  119. real_t randomness_ratio = 0.0;
  120. double lifetime_randomness = 0.0;
  121. double speed_scale = 1.0;
  122. bool local_coords = false;
  123. int fixed_fps = 0;
  124. bool fractional_delta = true;
  125. uint32_t seed = 0;
  126. bool use_fixed_seed = false;
  127. Transform2D inv_emission_transform;
  128. #ifdef TOOLS_ENABLED
  129. bool show_gizmos = false;
  130. #endif
  131. DrawOrder draw_order = DRAW_ORDER_INDEX;
  132. Ref<Texture2D> texture;
  133. ////////
  134. Vector2 direction = Vector2(1, 0);
  135. real_t spread = 45.0;
  136. real_t parameters_min[PARAM_MAX] = {};
  137. real_t parameters_max[PARAM_MAX] = {};
  138. Ref<Curve> curve_parameters[PARAM_MAX];
  139. Color color;
  140. Ref<Gradient> color_ramp;
  141. Ref<Gradient> color_initial_ramp;
  142. bool particle_flags[PARTICLE_FLAG_MAX];
  143. EmissionShape emission_shape = EMISSION_SHAPE_POINT;
  144. real_t emission_sphere_radius = 1.0;
  145. Vector2 emission_rect_extents = Vector2(1, 1);
  146. Vector<Vector2> emission_points;
  147. Vector<Vector2> emission_normals;
  148. Vector<Color> emission_colors;
  149. int emission_point_count = 0;
  150. Ref<Curve> scale_curve_x;
  151. Ref<Curve> scale_curve_y;
  152. bool split_scale = false;
  153. Vector2 gravity = Vector2(0, 980);
  154. Ref<RandomNumberGenerator> rng;
  155. void _update_internal();
  156. void _particles_process(double p_delta);
  157. void _update_particle_data_buffer();
  158. void _set_emitting();
  159. Mutex update_mutex;
  160. struct InterpolationData {
  161. // Whether this particle is non-interpolated, but following an interpolated parent.
  162. bool interpolated_follow = false;
  163. // If doing interpolated follow, we need to keep these updated per tick.
  164. Transform2D global_xform_curr;
  165. Transform2D global_xform_prev;
  166. } _interpolation_data;
  167. void _update_render_thread();
  168. void _update_mesh_texture();
  169. void _set_do_redraw(bool p_do_redraw);
  170. void _texture_changed();
  171. void _refresh_interpolation_state();
  172. protected:
  173. static void _bind_methods();
  174. void _notification(int p_what);
  175. #ifdef TOOLS_ENABLED
  176. void _draw_emission_gizmo();
  177. #endif
  178. void _validate_property(PropertyInfo &p_property) const;
  179. #ifndef DISABLE_DEPRECATED
  180. void _restart_bind_compat_92089();
  181. static void _bind_compatibility_methods();
  182. #endif
  183. public:
  184. void set_emitting(bool p_emitting);
  185. void set_amount(int p_amount);
  186. void set_lifetime(double p_lifetime);
  187. void set_one_shot(bool p_one_shot);
  188. void set_pre_process_time(double p_time);
  189. void set_explosiveness_ratio(real_t p_ratio);
  190. void set_randomness_ratio(real_t p_ratio);
  191. void set_lifetime_randomness(double p_random);
  192. void set_use_local_coordinates(bool p_enable);
  193. void set_speed_scale(double p_scale);
  194. bool is_emitting() const;
  195. int get_amount() const;
  196. double get_lifetime() const;
  197. bool get_one_shot() const;
  198. double get_pre_process_time() const;
  199. real_t get_explosiveness_ratio() const;
  200. real_t get_randomness_ratio() const;
  201. double get_lifetime_randomness() const;
  202. bool get_use_local_coordinates() const;
  203. double get_speed_scale() const;
  204. void set_fixed_fps(int p_count);
  205. int get_fixed_fps() const;
  206. void set_fractional_delta(bool p_enable);
  207. bool get_fractional_delta() const;
  208. void set_draw_order(DrawOrder p_order);
  209. DrawOrder get_draw_order() const;
  210. void set_texture(const Ref<Texture2D> &p_texture);
  211. Ref<Texture2D> get_texture() const;
  212. void set_use_fixed_seed(bool p_use_fixed_seed);
  213. bool get_use_fixed_seed() const;
  214. void set_seed(uint32_t p_seed);
  215. #ifdef TOOLS_ENABLED
  216. void set_show_gizmos(bool p_show_gizmos);
  217. #endif
  218. uint32_t get_seed() const;
  219. void request_particles_process(real_t p_requested_process_time);
  220. ///////////////////
  221. void set_direction(Vector2 p_direction);
  222. Vector2 get_direction() const;
  223. void set_spread(real_t p_spread);
  224. real_t get_spread() const;
  225. void set_param_min(Parameter p_param, real_t p_value);
  226. real_t get_param_min(Parameter p_param) const;
  227. void set_param_max(Parameter p_param, real_t p_value);
  228. real_t get_param_max(Parameter p_param) const;
  229. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  230. Ref<Curve> get_param_curve(Parameter p_param) const;
  231. void set_color(const Color &p_color);
  232. Color get_color() const;
  233. void set_color_ramp(const Ref<Gradient> &p_ramp);
  234. Ref<Gradient> get_color_ramp() const;
  235. void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
  236. Ref<Gradient> get_color_initial_ramp() const;
  237. void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
  238. bool get_particle_flag(ParticleFlags p_particle_flag) const;
  239. void set_emission_shape(EmissionShape p_shape);
  240. void set_emission_sphere_radius(real_t p_radius);
  241. void set_emission_rect_extents(Vector2 p_extents);
  242. void set_emission_points(const Vector<Vector2> &p_points);
  243. void set_emission_normals(const Vector<Vector2> &p_normals);
  244. void set_emission_colors(const Vector<Color> &p_colors);
  245. void set_scale_curve_x(Ref<Curve> p_scale_curve);
  246. void set_scale_curve_y(Ref<Curve> p_scale_curve);
  247. void set_split_scale(bool p_split_scale);
  248. EmissionShape get_emission_shape() const;
  249. real_t get_emission_sphere_radius() const;
  250. Vector2 get_emission_rect_extents() const;
  251. Vector<Vector2> get_emission_points() const;
  252. Vector<Vector2> get_emission_normals() const;
  253. Vector<Color> get_emission_colors() const;
  254. Ref<Curve> get_scale_curve_x() const;
  255. Ref<Curve> get_scale_curve_y() const;
  256. bool get_split_scale();
  257. void set_gravity(const Vector2 &p_gravity);
  258. Vector2 get_gravity() const;
  259. PackedStringArray get_configuration_warnings() const override;
  260. void restart(bool p_keep_seed = false);
  261. void convert_from_particles(Node *p_particles);
  262. CPUParticles2D();
  263. ~CPUParticles2D();
  264. };
  265. VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
  266. VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
  267. VARIANT_ENUM_CAST(CPUParticles2D::ParticleFlags)
  268. VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)