cpu_particles_2d.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*************************************************************************/
  2. /* cpu_particles_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #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. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class CPUParticles2D : public Node2D {
  39. private:
  40. GDCLASS(CPUParticles2D, Node2D);
  41. public:
  42. enum DrawOrder {
  43. DRAW_ORDER_INDEX,
  44. DRAW_ORDER_LIFETIME,
  45. };
  46. enum Parameter {
  47. PARAM_INITIAL_LINEAR_VELOCITY,
  48. PARAM_ANGULAR_VELOCITY,
  49. PARAM_ORBIT_VELOCITY,
  50. PARAM_LINEAR_ACCEL,
  51. PARAM_RADIAL_ACCEL,
  52. PARAM_TANGENTIAL_ACCEL,
  53. PARAM_DAMPING,
  54. PARAM_ANGLE,
  55. PARAM_SCALE,
  56. PARAM_HUE_VARIATION,
  57. PARAM_ANIM_SPEED,
  58. PARAM_ANIM_OFFSET,
  59. PARAM_MAX
  60. };
  61. enum Flags {
  62. FLAG_ALIGN_Y_TO_VELOCITY,
  63. FLAG_MAX
  64. };
  65. enum EmissionShape {
  66. EMISSION_SHAPE_POINT,
  67. EMISSION_SHAPE_CIRCLE,
  68. EMISSION_SHAPE_RECTANGLE,
  69. EMISSION_SHAPE_POINTS,
  70. EMISSION_SHAPE_DIRECTED_POINTS,
  71. };
  72. private:
  73. bool emitting;
  74. struct Particle {
  75. Transform2D transform;
  76. Color color;
  77. float custom[4];
  78. float rotation;
  79. Vector2 velocity;
  80. bool active;
  81. float angle_rand;
  82. float scale_rand;
  83. float hue_rot_rand;
  84. float anim_offset_rand;
  85. float time;
  86. Color base_color;
  87. uint32_t seed;
  88. };
  89. float time;
  90. float inactive_time;
  91. float frame_remainder;
  92. int cycle;
  93. RID mesh;
  94. RID multimesh;
  95. PoolVector<Particle> particles;
  96. PoolVector<float> particle_data;
  97. PoolVector<int> particle_order;
  98. struct SortLifetime {
  99. const Particle *particles;
  100. bool operator()(int p_a, int p_b) const {
  101. return particles[p_a].time < particles[p_b].time;
  102. }
  103. };
  104. struct SortAxis {
  105. const Particle *particles;
  106. Vector2 axis;
  107. bool operator()(int p_a, int p_b) const {
  108. return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
  109. }
  110. };
  111. //
  112. bool one_shot;
  113. float lifetime;
  114. float pre_process_time;
  115. float explosiveness_ratio;
  116. float randomness_ratio;
  117. float speed_scale;
  118. bool local_coords;
  119. int fixed_fps;
  120. bool fractional_delta;
  121. DrawOrder draw_order;
  122. Ref<Texture> texture;
  123. Ref<Texture> normalmap;
  124. ////////
  125. float spread;
  126. float flatness;
  127. float parameters[PARAM_MAX];
  128. float randomness[PARAM_MAX];
  129. Ref<Curve> curve_parameters[PARAM_MAX];
  130. Color color;
  131. Ref<Gradient> color_ramp;
  132. bool flags[FLAG_MAX];
  133. EmissionShape emission_shape;
  134. float emission_sphere_radius;
  135. Vector2 emission_rect_extents;
  136. PoolVector<Vector2> emission_points;
  137. PoolVector<Vector2> emission_normals;
  138. PoolVector<Color> emission_colors;
  139. int emission_point_count;
  140. Vector2 gravity;
  141. void _particles_process(float p_delta);
  142. void _update_particle_data_buffer();
  143. Mutex *update_mutex;
  144. void _update_render_thread();
  145. void _update_mesh_texture();
  146. protected:
  147. static void _bind_methods();
  148. void _notification(int p_what);
  149. virtual void _validate_property(PropertyInfo &property) const;
  150. public:
  151. void set_emitting(bool p_emitting);
  152. void set_amount(int p_amount);
  153. void set_lifetime(float p_lifetime);
  154. void set_one_shot(bool p_one_shot);
  155. void set_pre_process_time(float p_time);
  156. void set_explosiveness_ratio(float p_ratio);
  157. void set_randomness_ratio(float p_ratio);
  158. void set_visibility_aabb(const Rect2 &p_aabb);
  159. void set_use_local_coordinates(bool p_enable);
  160. void set_speed_scale(float p_scale);
  161. bool is_emitting() const;
  162. int get_amount() const;
  163. float get_lifetime() const;
  164. bool get_one_shot() const;
  165. float get_pre_process_time() const;
  166. float get_explosiveness_ratio() const;
  167. float get_randomness_ratio() const;
  168. Rect2 get_visibility_aabb() const;
  169. bool get_use_local_coordinates() const;
  170. float get_speed_scale() const;
  171. void set_fixed_fps(int p_count);
  172. int get_fixed_fps() const;
  173. void set_fractional_delta(bool p_enable);
  174. bool get_fractional_delta() const;
  175. void set_draw_order(DrawOrder p_order);
  176. DrawOrder get_draw_order() const;
  177. void set_draw_passes(int p_count);
  178. int get_draw_passes() const;
  179. void set_texture(const Ref<Texture> &p_texture);
  180. Ref<Texture> get_texture() const;
  181. void set_normalmap(const Ref<Texture> &p_normalmap);
  182. Ref<Texture> get_normalmap() const;
  183. ///////////////////
  184. void set_spread(float p_spread);
  185. float get_spread() const;
  186. void set_flatness(float p_flatness);
  187. float get_flatness() const;
  188. void set_param(Parameter p_param, float p_value);
  189. float get_param(Parameter p_param) const;
  190. void set_param_randomness(Parameter p_param, float p_value);
  191. float get_param_randomness(Parameter p_param) const;
  192. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  193. Ref<Curve> get_param_curve(Parameter p_param) const;
  194. void set_color(const Color &p_color);
  195. Color get_color() const;
  196. void set_color_ramp(const Ref<Gradient> &p_texture);
  197. Ref<Gradient> get_color_ramp() const;
  198. void set_particle_flag(Flags p_flag, bool p_enable);
  199. bool get_particle_flag(Flags p_flag) const;
  200. void set_emission_shape(EmissionShape p_shape);
  201. void set_emission_sphere_radius(float p_radius);
  202. void set_emission_rect_extents(Vector2 p_extents);
  203. void set_emission_points(const PoolVector<Vector2> &p_points);
  204. void set_emission_normals(const PoolVector<Vector2> &p_normals);
  205. void set_emission_colors(const PoolVector<Color> &p_colors);
  206. void set_emission_point_count(int p_count);
  207. EmissionShape get_emission_shape() const;
  208. float get_emission_sphere_radius() const;
  209. Vector2 get_emission_rect_extents() const;
  210. PoolVector<Vector2> get_emission_points() const;
  211. PoolVector<Vector2> get_emission_normals() const;
  212. PoolVector<Color> get_emission_colors() const;
  213. int get_emission_point_count() const;
  214. void set_gravity(const Vector2 &p_gravity);
  215. Vector2 get_gravity() const;
  216. virtual String get_configuration_warning() const;
  217. void restart();
  218. void convert_from_particles(Node *p_particles);
  219. CPUParticles2D();
  220. ~CPUParticles2D();
  221. };
  222. VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
  223. VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
  224. VARIANT_ENUM_CAST(CPUParticles2D::Flags)
  225. VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)
  226. #endif // CPU_PARTICLES_2D_H