cpu_particles_2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. // #define GODOT_CPU_PARTICLES_2D_LEGACY_COMPATIBILITY
  36. class CPUParticles2D : public Node2D {
  37. private:
  38. GDCLASS(CPUParticles2D, Node2D);
  39. public:
  40. enum DrawOrder {
  41. DRAW_ORDER_INDEX,
  42. DRAW_ORDER_LIFETIME,
  43. };
  44. enum Parameter {
  45. PARAM_INITIAL_LINEAR_VELOCITY,
  46. PARAM_ANGULAR_VELOCITY,
  47. PARAM_ORBIT_VELOCITY,
  48. PARAM_LINEAR_ACCEL,
  49. PARAM_RADIAL_ACCEL,
  50. PARAM_TANGENTIAL_ACCEL,
  51. PARAM_DAMPING,
  52. PARAM_ANGLE,
  53. PARAM_SCALE,
  54. PARAM_HUE_VARIATION,
  55. PARAM_ANIM_SPEED,
  56. PARAM_ANIM_OFFSET,
  57. PARAM_MAX
  58. };
  59. enum Flags {
  60. FLAG_ALIGN_Y_TO_VELOCITY,
  61. FLAG_ROTATE_Y, // Unused, but exposed for consistency with 3D.
  62. FLAG_DISABLE_Z, // Unused, but exposed for consistency with 3D.
  63. FLAG_MAX
  64. };
  65. enum EmissionShape {
  66. EMISSION_SHAPE_POINT,
  67. EMISSION_SHAPE_SPHERE,
  68. EMISSION_SHAPE_RECTANGLE,
  69. EMISSION_SHAPE_POINTS,
  70. EMISSION_SHAPE_DIRECTED_POINTS,
  71. EMISSION_SHAPE_MAX
  72. };
  73. private:
  74. bool emitting;
  75. struct ParticleBase {
  76. void blank() {
  77. for (int n = 0; n < 4; n++) {
  78. custom[n] = 0.0f;
  79. }
  80. }
  81. Transform2D transform;
  82. Color color;
  83. float custom[4];
  84. };
  85. // Warning - beware of adding non-trivial types
  86. // to this structure as it is zeroed to initialize in set_amount().
  87. struct Particle : public ParticleBase {
  88. void copy_to(ParticleBase &r_o) {
  89. r_o.transform = transform;
  90. r_o.color = color;
  91. memcpy(r_o.custom, custom, sizeof(custom));
  92. }
  93. float rotation;
  94. Vector2 velocity;
  95. bool active;
  96. float angle_rand;
  97. float scale_rand;
  98. float hue_rot_rand;
  99. float anim_offset_rand;
  100. Color start_color_rand;
  101. float time;
  102. float lifetime;
  103. Color base_color;
  104. uint32_t seed;
  105. };
  106. float time;
  107. float inactive_time;
  108. float frame_remainder;
  109. int cycle;
  110. bool redraw;
  111. RID mesh;
  112. RID multimesh;
  113. PoolVector<Particle> particles;
  114. LocalVector<ParticleBase> particles_prev;
  115. PoolVector<float> particle_data;
  116. PoolVector<float> particle_data_prev;
  117. PoolVector<int> particle_order;
  118. struct SortLifetime {
  119. const Particle *particles;
  120. bool operator()(int p_a, int p_b) const {
  121. return particles[p_a].time > particles[p_b].time;
  122. }
  123. };
  124. struct SortAxis {
  125. const Particle *particles;
  126. Vector2 axis;
  127. bool operator()(int p_a, int p_b) const {
  128. return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
  129. }
  130. };
  131. //
  132. bool one_shot;
  133. float lifetime;
  134. float pre_process_time;
  135. float explosiveness_ratio;
  136. float randomness_ratio;
  137. float lifetime_randomness;
  138. float speed_scale;
  139. bool local_coords;
  140. int fixed_fps;
  141. bool fractional_delta;
  142. Transform2D inv_emission_transform;
  143. DrawOrder draw_order;
  144. Ref<Texture> texture;
  145. Ref<Texture> normalmap;
  146. ////////
  147. Vector2 direction;
  148. float spread;
  149. float parameters[PARAM_MAX];
  150. float randomness[PARAM_MAX];
  151. Ref<Curve> curve_parameters[PARAM_MAX];
  152. Color color;
  153. Ref<Gradient> color_ramp;
  154. Ref<Gradient> color_initial_ramp;
  155. bool flags[FLAG_MAX];
  156. EmissionShape emission_shape;
  157. float emission_sphere_radius;
  158. Vector2 emission_rect_extents;
  159. PoolVector<Vector2> emission_points;
  160. PoolVector<Vector2> emission_normals;
  161. PoolVector<Color> emission_colors;
  162. int emission_point_count;
  163. Vector2 gravity;
  164. void _update_internal(bool p_on_physics_tick);
  165. void _particles_process(float p_delta);
  166. void _particle_process(Particle &r_p, const Transform2D &p_emission_xform, float p_local_delta, float &r_tv);
  167. void _update_particle_data_buffer();
  168. Mutex update_mutex;
  169. // Whether this particle system is interpolated.
  170. bool _interpolated = false;
  171. struct InterpolationData {
  172. // Whether this particle is non-interpolated, but following an interpolated parent.
  173. bool interpolated_follow = false;
  174. // If doing interpolated follow, we need to keep these updated per tick.
  175. Transform2D global_xform_curr;
  176. Transform2D global_xform_prev;
  177. } _interpolation_data;
  178. void _update_render_thread();
  179. void _update_mesh_texture();
  180. void _set_redraw(bool p_redraw);
  181. void _texture_changed();
  182. void _refresh_interpolation_state();
  183. template <bool TRANSFORM_PARTICLE>
  184. void _fill_particle_data(const ParticleBase &p_source, float *r_dest, bool p_active) const {
  185. if (p_active) {
  186. #ifdef GODOT_CPU_PARTICLES_2D_LEGACY_COMPATIBILITY
  187. Transform2D t = p_source.transform;
  188. if (TRANSFORM_PARTICLE) {
  189. t = inv_emission_transform * t;
  190. }
  191. #else
  192. const Transform2D &t = p_source.transform;
  193. #endif
  194. r_dest[0] = t.elements[0][0];
  195. r_dest[1] = t.elements[1][0];
  196. r_dest[2] = 0;
  197. r_dest[3] = t.elements[2][0];
  198. r_dest[4] = t.elements[0][1];
  199. r_dest[5] = t.elements[1][1];
  200. r_dest[6] = 0;
  201. r_dest[7] = t.elements[2][1];
  202. Color c = p_source.color;
  203. uint8_t *data8 = (uint8_t *)&r_dest[8];
  204. data8[0] = CLAMP(c.r * 255.0, 0, 255);
  205. data8[1] = CLAMP(c.g * 255.0, 0, 255);
  206. data8[2] = CLAMP(c.b * 255.0, 0, 255);
  207. data8[3] = CLAMP(c.a * 255.0, 0, 255);
  208. r_dest[9] = p_source.custom[0];
  209. r_dest[10] = p_source.custom[1];
  210. r_dest[11] = p_source.custom[2];
  211. r_dest[12] = p_source.custom[3];
  212. } else {
  213. memset(r_dest, 0, sizeof(float) * 13);
  214. }
  215. }
  216. protected:
  217. static void _bind_methods();
  218. void _notification(int p_what);
  219. virtual void _validate_property(PropertyInfo &property) const;
  220. public:
  221. void set_emitting(bool p_emitting);
  222. void set_amount(int p_amount);
  223. void set_lifetime(float p_lifetime);
  224. void set_one_shot(bool p_one_shot);
  225. void set_pre_process_time(float p_time);
  226. void set_explosiveness_ratio(float p_ratio);
  227. void set_randomness_ratio(float p_ratio);
  228. void set_lifetime_randomness(float p_random);
  229. void set_use_local_coordinates(bool p_enable);
  230. void set_speed_scale(float p_scale);
  231. bool is_emitting() const;
  232. int get_amount() const;
  233. float get_lifetime() const;
  234. bool get_one_shot() const;
  235. float get_pre_process_time() const;
  236. float get_explosiveness_ratio() const;
  237. float get_randomness_ratio() const;
  238. float get_lifetime_randomness() const;
  239. bool get_use_local_coordinates() const;
  240. float get_speed_scale() const;
  241. void set_fixed_fps(int p_count);
  242. int get_fixed_fps() const;
  243. void set_fractional_delta(bool p_enable);
  244. bool get_fractional_delta() const;
  245. void set_draw_order(DrawOrder p_order);
  246. DrawOrder get_draw_order() const;
  247. void set_texture(const Ref<Texture> &p_texture);
  248. Ref<Texture> get_texture() const;
  249. void set_normalmap(const Ref<Texture> &p_normalmap);
  250. Ref<Texture> get_normalmap() const;
  251. ///////////////////
  252. void set_direction(Vector2 p_direction);
  253. Vector2 get_direction() const;
  254. void set_spread(float p_spread);
  255. float get_spread() const;
  256. void set_param(Parameter p_param, float p_value);
  257. float get_param(Parameter p_param) const;
  258. void set_param_randomness(Parameter p_param, float p_value);
  259. float get_param_randomness(Parameter p_param) const;
  260. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  261. Ref<Curve> get_param_curve(Parameter p_param) const;
  262. void set_color(const Color &p_color);
  263. Color get_color() const;
  264. void set_color_ramp(const Ref<Gradient> &p_ramp);
  265. Ref<Gradient> get_color_ramp() const;
  266. void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
  267. Ref<Gradient> get_color_initial_ramp() const;
  268. void set_particle_flag(Flags p_flag, bool p_enable);
  269. bool get_particle_flag(Flags p_flag) const;
  270. void set_emission_shape(EmissionShape p_shape);
  271. void set_emission_sphere_radius(float p_radius);
  272. void set_emission_rect_extents(Vector2 p_extents);
  273. void set_emission_points(const PoolVector<Vector2> &p_points);
  274. void set_emission_normals(const PoolVector<Vector2> &p_normals);
  275. void set_emission_colors(const PoolVector<Color> &p_colors);
  276. EmissionShape get_emission_shape() const;
  277. float get_emission_sphere_radius() const;
  278. Vector2 get_emission_rect_extents() const;
  279. PoolVector<Vector2> get_emission_points() const;
  280. PoolVector<Vector2> get_emission_normals() const;
  281. PoolVector<Color> get_emission_colors() const;
  282. void set_gravity(const Vector2 &p_gravity);
  283. Vector2 get_gravity() const;
  284. virtual String get_configuration_warning() const;
  285. void restart();
  286. void convert_from_particles(Node *p_particles);
  287. CPUParticles2D();
  288. ~CPUParticles2D();
  289. };
  290. VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
  291. VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
  292. VARIANT_ENUM_CAST(CPUParticles2D::Flags)
  293. VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)
  294. #endif // CPU_PARTICLES_2D_H