cpu_particles.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* cpu_particles.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_H
  31. #define CPU_PARTICLES_H
  32. #include "core/rid.h"
  33. #include "scene/3d/visual_instance.h"
  34. class CPUParticles : public GeometryInstance {
  35. private:
  36. GDCLASS(CPUParticles, GeometryInstance);
  37. public:
  38. enum DrawOrder {
  39. DRAW_ORDER_INDEX,
  40. DRAW_ORDER_LIFETIME,
  41. DRAW_ORDER_VIEW_DEPTH,
  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,
  61. FLAG_DISABLE_Z,
  62. FLAG_MAX
  63. };
  64. enum EmissionShape {
  65. EMISSION_SHAPE_POINT,
  66. EMISSION_SHAPE_SPHERE,
  67. EMISSION_SHAPE_BOX,
  68. EMISSION_SHAPE_POINTS,
  69. EMISSION_SHAPE_DIRECTED_POINTS,
  70. EMISSION_SHAPE_MAX
  71. };
  72. private:
  73. bool emitting;
  74. struct Particle {
  75. Transform transform;
  76. Color color;
  77. float custom[4];
  78. Vector3 velocity;
  79. bool active;
  80. float angle_rand;
  81. float scale_rand;
  82. float hue_rot_rand;
  83. float anim_offset_rand;
  84. float time;
  85. float lifetime;
  86. Color base_color;
  87. uint32_t seed;
  88. };
  89. float time;
  90. float inactive_time;
  91. float frame_remainder;
  92. int cycle;
  93. bool redraw;
  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. Vector3 axis;
  107. bool operator()(int p_a, int p_b) const {
  108. return axis.dot(particles[p_a].transform.origin) < axis.dot(particles[p_b].transform.origin);
  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 lifetime_randomness;
  118. float speed_scale;
  119. bool local_coords;
  120. int fixed_fps;
  121. bool fractional_delta;
  122. Transform inv_emission_transform;
  123. volatile bool can_update;
  124. DrawOrder draw_order;
  125. Ref<Mesh> mesh;
  126. ////////
  127. Vector3 direction;
  128. float spread;
  129. float flatness;
  130. float parameters[PARAM_MAX];
  131. float randomness[PARAM_MAX];
  132. Ref<Curve> curve_parameters[PARAM_MAX];
  133. Color color;
  134. Ref<Gradient> color_ramp;
  135. bool flags[FLAG_MAX];
  136. EmissionShape emission_shape;
  137. float emission_sphere_radius;
  138. Vector3 emission_box_extents;
  139. PoolVector<Vector3> emission_points;
  140. PoolVector<Vector3> emission_normals;
  141. PoolVector<Color> emission_colors;
  142. int emission_point_count;
  143. Vector3 gravity;
  144. void _update_internal();
  145. void _particles_process(float p_delta);
  146. void _update_particle_data_buffer();
  147. Mutex *update_mutex;
  148. void _update_render_thread();
  149. void _set_redraw(bool p_redraw);
  150. protected:
  151. static void _bind_methods();
  152. void _notification(int p_what);
  153. virtual void _validate_property(PropertyInfo &property) const;
  154. public:
  155. AABB get_aabb() const;
  156. PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  157. void set_emitting(bool p_emitting);
  158. void set_amount(int p_amount);
  159. void set_lifetime(float p_lifetime);
  160. void set_one_shot(bool p_one_shot);
  161. void set_pre_process_time(float p_time);
  162. void set_explosiveness_ratio(float p_ratio);
  163. void set_randomness_ratio(float p_ratio);
  164. void set_lifetime_randomness(float p_random);
  165. void set_visibility_aabb(const AABB &p_aabb);
  166. void set_use_local_coordinates(bool p_enable);
  167. void set_speed_scale(float p_scale);
  168. bool is_emitting() const;
  169. int get_amount() const;
  170. float get_lifetime() const;
  171. bool get_one_shot() const;
  172. float get_pre_process_time() const;
  173. float get_explosiveness_ratio() const;
  174. float get_randomness_ratio() const;
  175. float get_lifetime_randomness() const;
  176. AABB get_visibility_aabb() const;
  177. bool get_use_local_coordinates() const;
  178. float get_speed_scale() const;
  179. void set_fixed_fps(int p_count);
  180. int get_fixed_fps() const;
  181. void set_fractional_delta(bool p_enable);
  182. bool get_fractional_delta() const;
  183. void set_draw_order(DrawOrder p_order);
  184. DrawOrder get_draw_order() const;
  185. void set_draw_passes(int p_count);
  186. int get_draw_passes() const;
  187. void set_mesh(const Ref<Mesh> &p_mesh);
  188. Ref<Mesh> get_mesh() const;
  189. ///////////////////
  190. void set_direction(Vector3 p_direction);
  191. Vector3 get_direction() const;
  192. void set_spread(float p_spread);
  193. float get_spread() const;
  194. void set_flatness(float p_flatness);
  195. float get_flatness() const;
  196. void set_param(Parameter p_param, float p_value);
  197. float get_param(Parameter p_param) const;
  198. void set_param_randomness(Parameter p_param, float p_value);
  199. float get_param_randomness(Parameter p_param) const;
  200. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  201. Ref<Curve> get_param_curve(Parameter p_param) const;
  202. void set_color(const Color &p_color);
  203. Color get_color() const;
  204. void set_color_ramp(const Ref<Gradient> &p_ramp);
  205. Ref<Gradient> get_color_ramp() const;
  206. void set_particle_flag(Flags p_flag, bool p_enable);
  207. bool get_particle_flag(Flags p_flag) const;
  208. void set_emission_shape(EmissionShape p_shape);
  209. void set_emission_sphere_radius(float p_radius);
  210. void set_emission_box_extents(Vector3 p_extents);
  211. void set_emission_points(const PoolVector<Vector3> &p_points);
  212. void set_emission_normals(const PoolVector<Vector3> &p_normals);
  213. void set_emission_colors(const PoolVector<Color> &p_colors);
  214. void set_emission_point_count(int p_count);
  215. EmissionShape get_emission_shape() const;
  216. float get_emission_sphere_radius() const;
  217. Vector3 get_emission_box_extents() const;
  218. PoolVector<Vector3> get_emission_points() const;
  219. PoolVector<Vector3> get_emission_normals() const;
  220. PoolVector<Color> get_emission_colors() const;
  221. int get_emission_point_count() const;
  222. void set_gravity(const Vector3 &p_gravity);
  223. Vector3 get_gravity() const;
  224. virtual String get_configuration_warning() const;
  225. void restart();
  226. void convert_from_particles(Node *p_particles);
  227. CPUParticles();
  228. ~CPUParticles();
  229. };
  230. VARIANT_ENUM_CAST(CPUParticles::DrawOrder)
  231. VARIANT_ENUM_CAST(CPUParticles::Parameter)
  232. VARIANT_ENUM_CAST(CPUParticles::Flags)
  233. VARIANT_ENUM_CAST(CPUParticles::EmissionShape)
  234. #endif // CPU_PARTICLES_H