cpu_particles_3d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**************************************************************************/
  2. /* cpu_particles_3d.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_3D_H
  31. #define CPU_PARTICLES_3D_H
  32. #include "scene/3d/visual_instance_3d.h"
  33. class RandomNumberGenerator;
  34. class CPUParticles3D : public GeometryInstance3D {
  35. private:
  36. GDCLASS(CPUParticles3D, GeometryInstance3D);
  37. public:
  38. enum DrawOrder {
  39. DRAW_ORDER_INDEX,
  40. DRAW_ORDER_LIFETIME,
  41. DRAW_ORDER_VIEW_DEPTH,
  42. DRAW_ORDER_MAX
  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 ParticleFlags {
  60. PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
  61. PARTICLE_FLAG_ROTATE_Y,
  62. PARTICLE_FLAG_DISABLE_Z,
  63. PARTICLE_FLAG_MAX
  64. };
  65. enum EmissionShape {
  66. EMISSION_SHAPE_POINT,
  67. EMISSION_SHAPE_SPHERE,
  68. EMISSION_SHAPE_SPHERE_SURFACE,
  69. EMISSION_SHAPE_BOX,
  70. EMISSION_SHAPE_POINTS,
  71. EMISSION_SHAPE_DIRECTED_POINTS,
  72. EMISSION_SHAPE_RING,
  73. EMISSION_SHAPE_MAX
  74. };
  75. private:
  76. bool emitting = false;
  77. bool active = false;
  78. struct Particle {
  79. Transform3D transform;
  80. Color color;
  81. real_t custom[4] = {};
  82. Vector3 velocity;
  83. bool active = false;
  84. real_t angle_rand = 0.0;
  85. real_t scale_rand = 0.0;
  86. real_t hue_rot_rand = 0.0;
  87. real_t anim_offset_rand = 0.0;
  88. Color start_color_rand;
  89. double time = 0.0;
  90. double lifetime = 0.0;
  91. Color base_color;
  92. uint32_t seed = 0;
  93. };
  94. double time = 0.0;
  95. double frame_remainder = 0.0;
  96. int cycle = 0;
  97. bool redraw = false;
  98. RID multimesh;
  99. Vector<Particle> particles;
  100. Vector<float> particle_data;
  101. Vector<int> particle_order;
  102. struct SortLifetime {
  103. const Particle *particles = nullptr;
  104. bool operator()(int p_a, int p_b) const {
  105. return particles[p_a].time > particles[p_b].time;
  106. }
  107. };
  108. struct SortAxis {
  109. const Particle *particles = nullptr;
  110. Vector3 axis;
  111. bool operator()(int p_a, int p_b) const {
  112. return axis.dot(particles[p_a].transform.origin) < axis.dot(particles[p_b].transform.origin);
  113. }
  114. };
  115. //
  116. bool one_shot = false;
  117. double lifetime = 1.0;
  118. double pre_process_time = 0.0;
  119. double _requested_process_time = 0.0;
  120. real_t explosiveness_ratio = 0.0;
  121. real_t randomness_ratio = 0.0;
  122. double lifetime_randomness = 0.0;
  123. double speed_scale = 1.0;
  124. AABB visibility_aabb;
  125. bool local_coords = false;
  126. int fixed_fps = 0;
  127. bool fractional_delta = true;
  128. uint32_t seed = 0;
  129. bool use_fixed_seed = false;
  130. Transform3D inv_emission_transform;
  131. SafeFlag can_update;
  132. DrawOrder draw_order = DRAW_ORDER_INDEX;
  133. Ref<Mesh> mesh;
  134. ////////
  135. Vector3 direction = Vector3(1, 0, 0);
  136. real_t spread = 45.0;
  137. real_t flatness = 0.0;
  138. real_t parameters_min[PARAM_MAX] = {};
  139. real_t parameters_max[PARAM_MAX] = {};
  140. Ref<Curve> curve_parameters[PARAM_MAX];
  141. Color color = Color(1, 1, 1, 1);
  142. Ref<Gradient> color_ramp;
  143. Ref<Gradient> color_initial_ramp;
  144. bool particle_flags[PARTICLE_FLAG_MAX] = {};
  145. EmissionShape emission_shape = EMISSION_SHAPE_POINT;
  146. real_t emission_sphere_radius = 1.0;
  147. Vector3 emission_box_extents = Vector3(1, 1, 1);
  148. Vector<Vector3> emission_points;
  149. Vector<Vector3> emission_normals;
  150. Vector<Color> emission_colors;
  151. int emission_point_count = 0;
  152. Vector3 emission_ring_axis;
  153. real_t emission_ring_height = 0.0;
  154. real_t emission_ring_radius = 0.0;
  155. real_t emission_ring_inner_radius = 0.0;
  156. real_t emission_ring_cone_angle = 0.0;
  157. Ref<Curve> scale_curve_x;
  158. Ref<Curve> scale_curve_y;
  159. Ref<Curve> scale_curve_z;
  160. bool split_scale = false;
  161. Vector3 gravity = Vector3(0, -9.8, 0);
  162. Ref<RandomNumberGenerator> rng;
  163. void _update_internal();
  164. void _particles_process(double p_delta);
  165. void _update_particle_data_buffer();
  166. Mutex update_mutex;
  167. void _update_render_thread();
  168. void _set_redraw(bool p_redraw);
  169. protected:
  170. static void _bind_methods();
  171. void _notification(int p_what);
  172. void _validate_property(PropertyInfo &p_property) const;
  173. #ifndef DISABLE_DEPRECATED
  174. void _restart_bind_compat_92089();
  175. static void _bind_compatibility_methods();
  176. #endif
  177. public:
  178. AABB get_aabb() const override;
  179. void set_emitting(bool p_emitting);
  180. void set_amount(int p_amount);
  181. void set_lifetime(double p_lifetime);
  182. void set_one_shot(bool p_one_shot);
  183. void set_pre_process_time(double p_time);
  184. void set_explosiveness_ratio(real_t p_ratio);
  185. void set_randomness_ratio(real_t p_ratio);
  186. void set_visibility_aabb(const AABB &p_aabb);
  187. void set_lifetime_randomness(double p_random);
  188. void set_use_local_coordinates(bool p_enable);
  189. void set_speed_scale(double p_scale);
  190. bool is_emitting() const;
  191. int get_amount() const;
  192. double get_lifetime() const;
  193. bool get_one_shot() const;
  194. double get_pre_process_time() const;
  195. real_t get_explosiveness_ratio() const;
  196. real_t get_randomness_ratio() const;
  197. AABB get_visibility_aabb() const;
  198. double get_lifetime_randomness() const;
  199. bool get_use_local_coordinates() const;
  200. double get_speed_scale() const;
  201. void set_fixed_fps(int p_count);
  202. int get_fixed_fps() const;
  203. void set_fractional_delta(bool p_enable);
  204. bool get_fractional_delta() const;
  205. void set_draw_order(DrawOrder p_order);
  206. DrawOrder get_draw_order() const;
  207. void set_mesh(const Ref<Mesh> &p_mesh);
  208. Ref<Mesh> get_mesh() const;
  209. void set_use_fixed_seed(bool p_use_fixed_seed = false);
  210. bool get_use_fixed_seed() const;
  211. void set_seed(uint32_t p_seed);
  212. uint32_t get_seed() const;
  213. void request_particles_process(real_t p_requested_process_time);
  214. ///////////////////
  215. void set_direction(Vector3 p_direction);
  216. Vector3 get_direction() const;
  217. void set_spread(real_t p_spread);
  218. real_t get_spread() const;
  219. void set_flatness(real_t p_flatness);
  220. real_t get_flatness() const;
  221. void set_param_min(Parameter p_param, real_t p_value);
  222. real_t get_param_min(Parameter p_param) const;
  223. void set_param_max(Parameter p_param, real_t p_value);
  224. real_t get_param_max(Parameter p_param) const;
  225. void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
  226. Ref<Curve> get_param_curve(Parameter p_param) const;
  227. void set_color(const Color &p_color);
  228. Color get_color() const;
  229. void set_color_ramp(const Ref<Gradient> &p_ramp);
  230. Ref<Gradient> get_color_ramp() const;
  231. void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
  232. Ref<Gradient> get_color_initial_ramp() const;
  233. void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
  234. bool get_particle_flag(ParticleFlags p_particle_flag) const;
  235. void set_emission_shape(EmissionShape p_shape);
  236. void set_emission_sphere_radius(real_t p_radius);
  237. void set_emission_box_extents(Vector3 p_extents);
  238. void set_emission_points(const Vector<Vector3> &p_points);
  239. void set_emission_normals(const Vector<Vector3> &p_normals);
  240. void set_emission_colors(const Vector<Color> &p_colors);
  241. void set_emission_ring_axis(Vector3 p_axis);
  242. void set_emission_ring_height(real_t p_height);
  243. void set_emission_ring_radius(real_t p_radius);
  244. void set_emission_ring_inner_radius(real_t p_radius);
  245. void set_emission_ring_cone_angle(real_t p_angle);
  246. void set_scale_curve_x(Ref<Curve> p_scale_curve);
  247. void set_scale_curve_y(Ref<Curve> p_scale_curve);
  248. void set_scale_curve_z(Ref<Curve> p_scale_curve);
  249. void set_split_scale(bool p_split_scale);
  250. EmissionShape get_emission_shape() const;
  251. real_t get_emission_sphere_radius() const;
  252. Vector3 get_emission_box_extents() const;
  253. Vector<Vector3> get_emission_points() const;
  254. Vector<Vector3> get_emission_normals() const;
  255. Vector<Color> get_emission_colors() const;
  256. Vector3 get_emission_ring_axis() const;
  257. real_t get_emission_ring_height() const;
  258. real_t get_emission_ring_radius() const;
  259. real_t get_emission_ring_inner_radius() const;
  260. real_t get_emission_ring_cone_angle() const;
  261. Ref<Curve> get_scale_curve_x() const;
  262. Ref<Curve> get_scale_curve_y() const;
  263. Ref<Curve> get_scale_curve_z() const;
  264. bool get_split_scale();
  265. void set_gravity(const Vector3 &p_gravity);
  266. Vector3 get_gravity() const;
  267. PackedStringArray get_configuration_warnings() const override;
  268. void restart(bool p_keep_seed = false);
  269. void convert_from_particles(Node *p_particles);
  270. AABB capture_aabb() const;
  271. CPUParticles3D();
  272. ~CPUParticles3D();
  273. };
  274. VARIANT_ENUM_CAST(CPUParticles3D::DrawOrder)
  275. VARIANT_ENUM_CAST(CPUParticles3D::Parameter)
  276. VARIANT_ENUM_CAST(CPUParticles3D::ParticleFlags)
  277. VARIANT_ENUM_CAST(CPUParticles3D::EmissionShape)
  278. #endif // CPU_PARTICLES_3D_H