particles.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*************************************************************************/
  2. /* particles.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 VISUALINSTANCEPARTICLES_H
  31. #define VISUALINSTANCEPARTICLES_H
  32. #include "rid.h"
  33. #include "scene/3d/visual_instance.h"
  34. #include "scene/main/timer.h"
  35. #include "scene/resources/material.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class Particles : public GeometryInstance {
  40. private:
  41. GDCLASS(Particles, GeometryInstance);
  42. public:
  43. enum DrawOrder {
  44. DRAW_ORDER_INDEX,
  45. DRAW_ORDER_LIFETIME,
  46. DRAW_ORDER_VIEW_DEPTH,
  47. };
  48. enum {
  49. MAX_DRAW_PASSES = 4
  50. };
  51. private:
  52. RID particles;
  53. bool emitting;
  54. bool one_shot;
  55. int amount;
  56. float lifetime;
  57. float pre_process_time;
  58. float explosiveness_ratio;
  59. float randomness_ratio;
  60. float speed_scale;
  61. Rect3 visibility_aabb;
  62. bool local_coords;
  63. int fixed_fps;
  64. bool fractional_delta;
  65. Ref<Material> process_material;
  66. DrawOrder draw_order;
  67. Vector<Ref<Mesh> > draw_passes;
  68. protected:
  69. static void _bind_methods();
  70. void _notification(int p_what);
  71. virtual void _validate_property(PropertyInfo &property) const;
  72. public:
  73. Rect3 get_aabb() const;
  74. PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  75. void set_emitting(bool p_emitting);
  76. void set_amount(int p_amount);
  77. void set_lifetime(float p_lifetime);
  78. void set_one_shot(bool p_one_shot);
  79. void set_pre_process_time(float p_time);
  80. void set_explosiveness_ratio(float p_ratio);
  81. void set_randomness_ratio(float p_ratio);
  82. void set_visibility_aabb(const Rect3 &p_aabb);
  83. void set_use_local_coordinates(bool p_enable);
  84. void set_process_material(const Ref<Material> &p_material);
  85. void set_speed_scale(float p_scale);
  86. bool is_emitting() const;
  87. int get_amount() const;
  88. float get_lifetime() const;
  89. bool get_one_shot() const;
  90. float get_pre_process_time() const;
  91. float get_explosiveness_ratio() const;
  92. float get_randomness_ratio() const;
  93. Rect3 get_visibility_aabb() const;
  94. bool get_use_local_coordinates() const;
  95. Ref<Material> get_process_material() const;
  96. float get_speed_scale() const;
  97. void set_fixed_fps(int p_count);
  98. int get_fixed_fps() const;
  99. void set_fractional_delta(bool p_enable);
  100. bool get_fractional_delta() const;
  101. void set_draw_order(DrawOrder p_order);
  102. DrawOrder get_draw_order() const;
  103. void set_draw_passes(int p_count);
  104. int get_draw_passes() const;
  105. void set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh);
  106. Ref<Mesh> get_draw_pass_mesh(int p_pass) const;
  107. virtual String get_configuration_warning() const;
  108. void restart();
  109. Rect3 capture_aabb() const;
  110. Particles();
  111. ~Particles();
  112. };
  113. VARIANT_ENUM_CAST(Particles::DrawOrder)
  114. class ParticlesMaterial : public Material {
  115. GDCLASS(ParticlesMaterial, Material)
  116. public:
  117. enum Parameter {
  118. PARAM_INITIAL_LINEAR_VELOCITY,
  119. PARAM_ANGULAR_VELOCITY,
  120. PARAM_ORBIT_VELOCITY,
  121. PARAM_LINEAR_ACCEL,
  122. PARAM_RADIAL_ACCEL,
  123. PARAM_TANGENTIAL_ACCEL,
  124. PARAM_DAMPING,
  125. PARAM_ANGLE,
  126. PARAM_SCALE,
  127. PARAM_HUE_VARIATION,
  128. PARAM_ANIM_SPEED,
  129. PARAM_ANIM_OFFSET,
  130. PARAM_MAX
  131. };
  132. enum Flags {
  133. FLAG_ALIGN_Y_TO_VELOCITY,
  134. FLAG_ROTATE_Y,
  135. FLAG_DISABLE_Z,
  136. FLAG_ANIM_LOOP,
  137. FLAG_MAX
  138. };
  139. enum EmissionShape {
  140. EMISSION_SHAPE_POINT,
  141. EMISSION_SHAPE_SPHERE,
  142. EMISSION_SHAPE_BOX,
  143. EMISSION_SHAPE_POINTS,
  144. EMISSION_SHAPE_DIRECTED_POINTS,
  145. };
  146. private:
  147. union MaterialKey {
  148. struct {
  149. uint32_t texture_mask : 16;
  150. uint32_t texture_color : 1;
  151. uint32_t flags : 4;
  152. uint32_t emission_shape : 2;
  153. uint32_t trail_size_texture : 1;
  154. uint32_t trail_color_texture : 1;
  155. uint32_t invalid_key : 1;
  156. uint32_t has_emission_color : 1;
  157. };
  158. uint32_t key;
  159. bool operator<(const MaterialKey &p_key) const {
  160. return key < p_key.key;
  161. }
  162. };
  163. struct ShaderData {
  164. RID shader;
  165. int users;
  166. };
  167. static Map<MaterialKey, ShaderData> shader_map;
  168. MaterialKey current_key;
  169. _FORCE_INLINE_ MaterialKey _compute_key() const {
  170. MaterialKey mk;
  171. mk.key = 0;
  172. for (int i = 0; i < PARAM_MAX; i++) {
  173. if (tex_parameters[i].is_valid()) {
  174. mk.texture_mask |= (1 << i);
  175. }
  176. }
  177. for (int i = 0; i < FLAG_MAX; i++) {
  178. if (flags[i]) {
  179. mk.flags |= (1 << i);
  180. }
  181. }
  182. mk.texture_color = color_ramp.is_valid() ? 1 : 0;
  183. mk.emission_shape = emission_shape;
  184. mk.trail_color_texture = trail_color_modifier.is_valid() ? 1 : 0;
  185. mk.trail_size_texture = trail_size_modifier.is_valid() ? 1 : 0;
  186. mk.has_emission_color = emission_shape >= EMISSION_SHAPE_POINTS && emission_color_texture.is_valid();
  187. return mk;
  188. }
  189. static Mutex *material_mutex;
  190. static SelfList<ParticlesMaterial>::List dirty_materials;
  191. struct ShaderNames {
  192. StringName spread;
  193. StringName flatness;
  194. StringName initial_linear_velocity;
  195. StringName initial_angle;
  196. StringName angular_velocity;
  197. StringName orbit_velocity;
  198. StringName linear_accel;
  199. StringName radial_accel;
  200. StringName tangent_accel;
  201. StringName damping;
  202. StringName scale;
  203. StringName hue_variation;
  204. StringName anim_speed;
  205. StringName anim_offset;
  206. StringName initial_linear_velocity_random;
  207. StringName initial_angle_random;
  208. StringName angular_velocity_random;
  209. StringName orbit_velocity_random;
  210. StringName linear_accel_random;
  211. StringName radial_accel_random;
  212. StringName tangent_accel_random;
  213. StringName damping_random;
  214. StringName scale_random;
  215. StringName hue_variation_random;
  216. StringName anim_speed_random;
  217. StringName anim_offset_random;
  218. StringName angle_texture;
  219. StringName angular_velocity_texture;
  220. StringName orbit_velocity_texture;
  221. StringName linear_accel_texture;
  222. StringName radial_accel_texture;
  223. StringName tangent_accel_texture;
  224. StringName damping_texture;
  225. StringName scale_texture;
  226. StringName hue_variation_texture;
  227. StringName anim_speed_texture;
  228. StringName anim_offset_texture;
  229. StringName color;
  230. StringName color_ramp;
  231. StringName emission_sphere_radius;
  232. StringName emission_box_extents;
  233. StringName emission_texture_point_count;
  234. StringName emission_texture_points;
  235. StringName emission_texture_normal;
  236. StringName emission_texture_color;
  237. StringName trail_divisor;
  238. StringName trail_size_modifier;
  239. StringName trail_color_modifier;
  240. StringName gravity;
  241. };
  242. static ShaderNames *shader_names;
  243. SelfList<ParticlesMaterial> element;
  244. void _update_shader();
  245. _FORCE_INLINE_ void _queue_shader_change();
  246. _FORCE_INLINE_ bool _is_shader_dirty() const;
  247. float spread;
  248. float flatness;
  249. float parameters[PARAM_MAX];
  250. float randomness[PARAM_MAX];
  251. Ref<Texture> tex_parameters[PARAM_MAX];
  252. Color color;
  253. Ref<Texture> color_ramp;
  254. bool flags[FLAG_MAX];
  255. EmissionShape emission_shape;
  256. float emission_sphere_radius;
  257. Vector3 emission_box_extents;
  258. Ref<Texture> emission_point_texture;
  259. Ref<Texture> emission_normal_texture;
  260. Ref<Texture> emission_color_texture;
  261. int emission_point_count;
  262. bool anim_loop;
  263. int trail_divisor;
  264. Ref<CurveTexture> trail_size_modifier;
  265. Ref<GradientTexture> trail_color_modifier;
  266. Vector3 gravity;
  267. //do not save emission points here
  268. protected:
  269. static void _bind_methods();
  270. virtual void _validate_property(PropertyInfo &property) const;
  271. public:
  272. void set_spread(float p_spread);
  273. float get_spread() const;
  274. void set_flatness(float p_flatness);
  275. float get_flatness() const;
  276. void set_param(Parameter p_param, float p_value);
  277. float get_param(Parameter p_param) const;
  278. void set_param_randomness(Parameter p_param, float p_value);
  279. float get_param_randomness(Parameter p_param) const;
  280. void set_param_texture(Parameter p_param, const Ref<Texture> &p_texture);
  281. Ref<Texture> get_param_texture(Parameter p_param) const;
  282. void set_color(const Color &p_color);
  283. Color get_color() const;
  284. void set_color_ramp(const Ref<Texture> &p_texture);
  285. Ref<Texture> get_color_ramp() const;
  286. void set_flag(Flags p_flag, bool p_enable);
  287. bool get_flag(Flags p_flag) const;
  288. void set_emission_shape(EmissionShape p_shape);
  289. void set_emission_sphere_radius(float p_radius);
  290. void set_emission_box_extents(Vector3 p_extents);
  291. void set_emission_point_texture(const Ref<Texture> &p_points);
  292. void set_emission_normal_texture(const Ref<Texture> &p_normals);
  293. void set_emission_color_texture(const Ref<Texture> &p_colors);
  294. void set_emission_point_count(int p_count);
  295. EmissionShape get_emission_shape() const;
  296. float get_emission_sphere_radius() const;
  297. Vector3 get_emission_box_extents() const;
  298. Ref<Texture> get_emission_point_texture() const;
  299. Ref<Texture> get_emission_normal_texture() const;
  300. Ref<Texture> get_emission_color_texture() const;
  301. int get_emission_point_count() const;
  302. void set_trail_divisor(int p_divisor);
  303. int get_trail_divisor() const;
  304. void set_trail_size_modifier(const Ref<CurveTexture> &p_trail_size_modifier);
  305. Ref<CurveTexture> get_trail_size_modifier() const;
  306. void set_trail_color_modifier(const Ref<GradientTexture> &p_trail_color_modifier);
  307. Ref<GradientTexture> get_trail_color_modifier() const;
  308. void set_gravity(const Vector3 &p_gravity);
  309. Vector3 get_gravity() const;
  310. static void init_shaders();
  311. static void finish_shaders();
  312. static void flush_changes();
  313. RID get_shader_rid() const;
  314. ParticlesMaterial();
  315. ~ParticlesMaterial();
  316. };
  317. VARIANT_ENUM_CAST(ParticlesMaterial::Parameter)
  318. VARIANT_ENUM_CAST(ParticlesMaterial::Flags)
  319. VARIANT_ENUM_CAST(ParticlesMaterial::EmissionShape)
  320. #endif