cpu_particles.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*************************************************************************/
  2. /* cpu_particles.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_H
  31. #define CPU_PARTICLES_H
  32. #include "core/rid.h"
  33. #include "scene/3d/visual_instance.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class CPUParticles : public GeometryInstance {
  38. private:
  39. GDCLASS(CPUParticles, GeometryInstance);
  40. public:
  41. enum DrawOrder {
  42. DRAW_ORDER_INDEX,
  43. DRAW_ORDER_LIFETIME,
  44. DRAW_ORDER_VIEW_DEPTH,
  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_ROTATE_Y,
  64. FLAG_DISABLE_Z,
  65. FLAG_MAX
  66. };
  67. enum EmissionShape {
  68. EMISSION_SHAPE_POINT,
  69. EMISSION_SHAPE_SPHERE,
  70. EMISSION_SHAPE_BOX,
  71. EMISSION_SHAPE_POINTS,
  72. EMISSION_SHAPE_DIRECTED_POINTS,
  73. };
  74. private:
  75. bool emitting;
  76. struct Particle {
  77. Transform transform;
  78. Color color;
  79. float custom[4];
  80. Vector3 velocity;
  81. bool active;
  82. float angle_rand;
  83. float scale_rand;
  84. float hue_rot_rand;
  85. float anim_offset_rand;
  86. float time;
  87. Color base_color;
  88. uint32_t seed;
  89. };
  90. float time;
  91. float inactive_time;
  92. float frame_remainder;
  93. int cycle;
  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 speed_scale;
  118. bool local_coords;
  119. int fixed_fps;
  120. bool fractional_delta;
  121. volatile bool can_update;
  122. DrawOrder draw_order;
  123. Ref<Mesh> mesh;
  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. Vector3 emission_box_extents;
  136. PoolVector<Vector3> emission_points;
  137. PoolVector<Vector3> emission_normals;
  138. PoolVector<Color> emission_colors;
  139. int emission_point_count;
  140. Vector3 gravity;
  141. void _particles_process(float p_delta);
  142. void _update_particle_data_buffer();
  143. Mutex *update_mutex;
  144. void _update_render_thread();
  145. protected:
  146. static void _bind_methods();
  147. void _notification(int p_what);
  148. virtual void _validate_property(PropertyInfo &property) const;
  149. public:
  150. AABB get_aabb() const;
  151. PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  152. void set_emitting(bool p_emitting);
  153. void set_amount(int p_amount);
  154. void set_lifetime(float p_lifetime);
  155. void set_one_shot(bool p_one_shot);
  156. void set_pre_process_time(float p_time);
  157. void set_explosiveness_ratio(float p_ratio);
  158. void set_randomness_ratio(float p_ratio);
  159. void set_visibility_aabb(const AABB &p_aabb);
  160. void set_use_local_coordinates(bool p_enable);
  161. void set_speed_scale(float p_scale);
  162. bool is_emitting() const;
  163. int get_amount() const;
  164. float get_lifetime() const;
  165. bool get_one_shot() const;
  166. float get_pre_process_time() const;
  167. float get_explosiveness_ratio() const;
  168. float get_randomness_ratio() const;
  169. AABB get_visibility_aabb() const;
  170. bool get_use_local_coordinates() const;
  171. float get_speed_scale() const;
  172. void set_fixed_fps(int p_count);
  173. int get_fixed_fps() const;
  174. void set_fractional_delta(bool p_enable);
  175. bool get_fractional_delta() const;
  176. void set_draw_order(DrawOrder p_order);
  177. DrawOrder get_draw_order() const;
  178. void set_draw_passes(int p_count);
  179. int get_draw_passes() const;
  180. void set_mesh(const Ref<Mesh> &p_mesh);
  181. Ref<Mesh> get_mesh() const;
  182. ///////////////////
  183. void set_spread(float p_spread);
  184. float get_spread() const;
  185. void set_flatness(float p_flatness);
  186. float get_flatness() const;
  187. void set_param(Parameter p_param, float p_value);
  188. float get_param(Parameter p_param) const;
  189. void set_param_randomness(Parameter p_param, float p_value);
  190. float get_param_randomness(Parameter p_param) const;
  191. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  192. Ref<Curve> get_param_curve(Parameter p_param) const;
  193. void set_color(const Color &p_color);
  194. Color get_color() const;
  195. void set_color_ramp(const Ref<Gradient> &p_texture);
  196. Ref<Gradient> get_color_ramp() const;
  197. void set_particle_flag(Flags p_flag, bool p_enable);
  198. bool get_particle_flag(Flags p_flag) const;
  199. void set_emission_shape(EmissionShape p_shape);
  200. void set_emission_sphere_radius(float p_radius);
  201. void set_emission_box_extents(Vector3 p_extents);
  202. void set_emission_points(const PoolVector<Vector3> &p_points);
  203. void set_emission_normals(const PoolVector<Vector3> &p_normals);
  204. void set_emission_colors(const PoolVector<Color> &p_colors);
  205. void set_emission_point_count(int p_count);
  206. EmissionShape get_emission_shape() const;
  207. float get_emission_sphere_radius() const;
  208. Vector3 get_emission_box_extents() const;
  209. PoolVector<Vector3> get_emission_points() const;
  210. PoolVector<Vector3> get_emission_normals() const;
  211. PoolVector<Color> get_emission_colors() const;
  212. int get_emission_point_count() const;
  213. void set_gravity(const Vector3 &p_gravity);
  214. Vector3 get_gravity() const;
  215. virtual String get_configuration_warning() const;
  216. void restart();
  217. void convert_from_particles(Node *p_particles);
  218. CPUParticles();
  219. ~CPUParticles();
  220. };
  221. VARIANT_ENUM_CAST(CPUParticles::DrawOrder)
  222. VARIANT_ENUM_CAST(CPUParticles::Parameter)
  223. VARIANT_ENUM_CAST(CPUParticles::Flags)
  224. VARIANT_ENUM_CAST(CPUParticles::EmissionShape)
  225. #endif // CPU_PARTICLES_H