cpu_particles_2d.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef CPU_PARTICLES_2D_H
  31. #define CPU_PARTICLES_2D_H
  32. #include "core/rid.h"
  33. #include "scene/2d/node_2d.h"
  34. #include "scene/resources/texture.h"
  35. class CPUParticles2D : public Node2D {
  36. private:
  37. GDCLASS(CPUParticles2D, Node2D);
  38. public:
  39. enum DrawOrder {
  40. DRAW_ORDER_INDEX,
  41. DRAW_ORDER_LIFETIME,
  42. };
  43. enum Parameter {
  44. PARAM_INITIAL_LINEAR_VELOCITY,
  45. PARAM_ANGULAR_VELOCITY,
  46. PARAM_ORBIT_VELOCITY,
  47. PARAM_LINEAR_ACCEL,
  48. PARAM_RADIAL_ACCEL,
  49. PARAM_TANGENTIAL_ACCEL,
  50. PARAM_DAMPING,
  51. PARAM_ANGLE,
  52. PARAM_SCALE,
  53. PARAM_HUE_VARIATION,
  54. PARAM_ANIM_SPEED,
  55. PARAM_ANIM_OFFSET,
  56. PARAM_MAX
  57. };
  58. enum Flags {
  59. FLAG_ALIGN_Y_TO_VELOCITY,
  60. FLAG_ROTATE_Y, // Unused, but exposed for consistency with 3D.
  61. FLAG_DISABLE_Z, // Unused, but exposed for consistency with 3D.
  62. FLAG_MAX
  63. };
  64. enum EmissionShape {
  65. EMISSION_SHAPE_POINT,
  66. EMISSION_SHAPE_SPHERE,
  67. EMISSION_SHAPE_RECTANGLE,
  68. EMISSION_SHAPE_POINTS,
  69. EMISSION_SHAPE_DIRECTED_POINTS,
  70. EMISSION_SHAPE_MAX
  71. };
  72. private:
  73. bool emitting;
  74. // warning - beware of adding non-trivial types
  75. // to this structure as it is zeroed to initialize in set_amount()
  76. struct Particle {
  77. Transform2D transform;
  78. Color color;
  79. float custom[4];
  80. float rotation;
  81. Vector2 velocity;
  82. bool active;
  83. float angle_rand;
  84. float scale_rand;
  85. float hue_rot_rand;
  86. float anim_offset_rand;
  87. Color start_color_rand;
  88. float time;
  89. float lifetime;
  90. Color base_color;
  91. uint32_t seed;
  92. };
  93. float time;
  94. float inactive_time;
  95. float frame_remainder;
  96. int cycle;
  97. bool redraw;
  98. RID mesh;
  99. RID multimesh;
  100. PoolVector<Particle> particles;
  101. PoolVector<float> particle_data;
  102. PoolVector<int> particle_order;
  103. struct SortLifetime {
  104. const Particle *particles;
  105. bool operator()(int p_a, int p_b) const {
  106. return particles[p_a].time > particles[p_b].time;
  107. }
  108. };
  109. struct SortAxis {
  110. const Particle *particles;
  111. Vector2 axis;
  112. bool operator()(int p_a, int p_b) const {
  113. return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
  114. }
  115. };
  116. //
  117. bool one_shot;
  118. float lifetime;
  119. float pre_process_time;
  120. float explosiveness_ratio;
  121. float randomness_ratio;
  122. float lifetime_randomness;
  123. float speed_scale;
  124. bool local_coords;
  125. int fixed_fps;
  126. bool fractional_delta;
  127. Transform2D inv_emission_transform;
  128. DrawOrder draw_order;
  129. Ref<Texture> texture;
  130. Ref<Texture> normalmap;
  131. ////////
  132. Vector2 direction;
  133. float spread;
  134. float parameters[PARAM_MAX];
  135. float randomness[PARAM_MAX];
  136. Ref<Curve> curve_parameters[PARAM_MAX];
  137. Color color;
  138. Ref<Gradient> color_ramp;
  139. Ref<Gradient> color_initial_ramp;
  140. bool flags[FLAG_MAX];
  141. EmissionShape emission_shape;
  142. float emission_sphere_radius;
  143. Vector2 emission_rect_extents;
  144. PoolVector<Vector2> emission_points;
  145. PoolVector<Vector2> emission_normals;
  146. PoolVector<Color> emission_colors;
  147. int emission_point_count;
  148. Vector2 gravity;
  149. void _update_internal();
  150. void _particles_process(float p_delta);
  151. void _update_particle_data_buffer();
  152. Mutex update_mutex;
  153. void _update_render_thread();
  154. void _update_mesh_texture();
  155. void _set_redraw(bool p_redraw);
  156. void _texture_changed();
  157. protected:
  158. static void _bind_methods();
  159. void _notification(int p_what);
  160. virtual void _validate_property(PropertyInfo &property) const;
  161. public:
  162. void set_emitting(bool p_emitting);
  163. void set_amount(int p_amount);
  164. void set_lifetime(float p_lifetime);
  165. void set_one_shot(bool p_one_shot);
  166. void set_pre_process_time(float p_time);
  167. void set_explosiveness_ratio(float p_ratio);
  168. void set_randomness_ratio(float p_ratio);
  169. void set_lifetime_randomness(float p_random);
  170. void set_use_local_coordinates(bool p_enable);
  171. void set_speed_scale(float p_scale);
  172. bool is_emitting() const;
  173. int get_amount() const;
  174. float get_lifetime() const;
  175. bool get_one_shot() const;
  176. float get_pre_process_time() const;
  177. float get_explosiveness_ratio() const;
  178. float get_randomness_ratio() const;
  179. float get_lifetime_randomness() const;
  180. bool get_use_local_coordinates() const;
  181. float get_speed_scale() const;
  182. void set_fixed_fps(int p_count);
  183. int get_fixed_fps() const;
  184. void set_fractional_delta(bool p_enable);
  185. bool get_fractional_delta() const;
  186. void set_draw_order(DrawOrder p_order);
  187. DrawOrder get_draw_order() const;
  188. void set_texture(const Ref<Texture> &p_texture);
  189. Ref<Texture> get_texture() const;
  190. void set_normalmap(const Ref<Texture> &p_normalmap);
  191. Ref<Texture> get_normalmap() const;
  192. ///////////////////
  193. void set_direction(Vector2 p_direction);
  194. Vector2 get_direction() const;
  195. void set_spread(float p_spread);
  196. float get_spread() const;
  197. void set_param(Parameter p_param, float p_value);
  198. float get_param(Parameter p_param) const;
  199. void set_param_randomness(Parameter p_param, float p_value);
  200. float get_param_randomness(Parameter p_param) const;
  201. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  202. Ref<Curve> get_param_curve(Parameter p_param) const;
  203. void set_color(const Color &p_color);
  204. Color get_color() const;
  205. void set_color_ramp(const Ref<Gradient> &p_ramp);
  206. Ref<Gradient> get_color_ramp() const;
  207. void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
  208. Ref<Gradient> get_color_initial_ramp() const;
  209. void set_particle_flag(Flags p_flag, bool p_enable);
  210. bool get_particle_flag(Flags p_flag) const;
  211. void set_emission_shape(EmissionShape p_shape);
  212. void set_emission_sphere_radius(float p_radius);
  213. void set_emission_rect_extents(Vector2 p_extents);
  214. void set_emission_points(const PoolVector<Vector2> &p_points);
  215. void set_emission_normals(const PoolVector<Vector2> &p_normals);
  216. void set_emission_colors(const PoolVector<Color> &p_colors);
  217. EmissionShape get_emission_shape() const;
  218. float get_emission_sphere_radius() const;
  219. Vector2 get_emission_rect_extents() const;
  220. PoolVector<Vector2> get_emission_points() const;
  221. PoolVector<Vector2> get_emission_normals() const;
  222. PoolVector<Color> get_emission_colors() const;
  223. void set_gravity(const Vector2 &p_gravity);
  224. Vector2 get_gravity() const;
  225. virtual String get_configuration_warning() const;
  226. void restart();
  227. void convert_from_particles(Node *p_particles);
  228. CPUParticles2D();
  229. ~CPUParticles2D();
  230. };
  231. VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
  232. VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
  233. VARIANT_ENUM_CAST(CPUParticles2D::Flags)
  234. VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)
  235. #endif // CPU_PARTICLES_2D_H