particle_process_material.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**************************************************************************/
  2. /* particle_process_material.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 PARTICLE_PROCESS_MATERIAL_H
  31. #define PARTICLE_PROCESS_MATERIAL_H
  32. #include "core/templates/rid.h"
  33. #include "core/templates/self_list.h"
  34. #include "scene/resources/curve_texture.h"
  35. #include "scene/resources/material.h"
  36. /*
  37. TODO:
  38. -Path following
  39. -Emitter positions deformable by bones
  40. -Proper trails
  41. */
  42. class ParticleProcessMaterial : public Material {
  43. GDCLASS(ParticleProcessMaterial, Material);
  44. public:
  45. enum Parameter {
  46. PARAM_INITIAL_LINEAR_VELOCITY,
  47. PARAM_ANGULAR_VELOCITY,
  48. PARAM_ORBIT_VELOCITY,
  49. PARAM_LINEAR_ACCEL,
  50. PARAM_RADIAL_ACCEL,
  51. PARAM_TANGENTIAL_ACCEL,
  52. PARAM_DAMPING,
  53. PARAM_ANGLE,
  54. PARAM_SCALE,
  55. PARAM_HUE_VARIATION,
  56. PARAM_ANIM_SPEED,
  57. PARAM_ANIM_OFFSET,
  58. PARAM_TURB_INFLUENCE_OVER_LIFE,
  59. PARAM_TURB_VEL_INFLUENCE,
  60. PARAM_TURB_INIT_DISPLACEMENT,
  61. PARAM_RADIAL_VELOCITY,
  62. PARAM_DIRECTIONAL_VELOCITY,
  63. PARAM_SCALE_OVER_VELOCITY,
  64. PARAM_MAX
  65. };
  66. // When extending, make sure not to overflow the size of the MaterialKey below.
  67. enum ParticleFlags {
  68. PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
  69. PARTICLE_FLAG_ROTATE_Y,
  70. PARTICLE_FLAG_DISABLE_Z,
  71. PARTICLE_FLAG_DAMPING_AS_FRICTION,
  72. PARTICLE_FLAG_MAX
  73. };
  74. // When extending, make sure not to overflow the size of the MaterialKey below.
  75. enum EmissionShape {
  76. EMISSION_SHAPE_POINT,
  77. EMISSION_SHAPE_SPHERE,
  78. EMISSION_SHAPE_SPHERE_SURFACE,
  79. EMISSION_SHAPE_BOX,
  80. EMISSION_SHAPE_POINTS,
  81. EMISSION_SHAPE_DIRECTED_POINTS,
  82. EMISSION_SHAPE_RING,
  83. EMISSION_SHAPE_MAX
  84. };
  85. // When extending, make sure not to overflow the size of the MaterialKey below.
  86. enum SubEmitterMode {
  87. SUB_EMITTER_DISABLED,
  88. SUB_EMITTER_CONSTANT,
  89. SUB_EMITTER_AT_END,
  90. SUB_EMITTER_AT_COLLISION,
  91. SUB_EMITTER_AT_START,
  92. SUB_EMITTER_MAX
  93. };
  94. // When extending, make sure not to overflow the size of the MaterialKey below.
  95. enum CollisionMode {
  96. COLLISION_DISABLED,
  97. COLLISION_RIGID,
  98. COLLISION_HIDE_ON_CONTACT,
  99. COLLISION_MAX
  100. };
  101. private:
  102. struct MaterialKey {
  103. // The bit size of the struct must be kept below or equal to 64 bits.
  104. // Consider this when extending ParticleFlags, EmissionShape, or SubEmitterMode.
  105. uint64_t texture_mask : PARAM_MAX;
  106. uint64_t texture_color : 1;
  107. uint64_t particle_flags : PARTICLE_FLAG_MAX;
  108. uint64_t emission_shape : 3;
  109. uint64_t invalid_key : 1;
  110. uint64_t has_emission_color : 1;
  111. uint64_t sub_emitter : 3;
  112. uint64_t attractor_enabled : 1;
  113. uint64_t collision_mode : 2;
  114. uint64_t collision_scale : 1;
  115. uint64_t turbulence_enabled : 1;
  116. uint64_t limiter_curve : 1;
  117. uint64_t alpha_curve : 1;
  118. uint64_t emission_curve : 1;
  119. uint64_t has_initial_ramp : 1;
  120. uint64_t orbit_uses_curve_xyz : 1;
  121. MaterialKey() {
  122. memset(this, 0, sizeof(MaterialKey));
  123. }
  124. static uint32_t hash(const MaterialKey &p_key) {
  125. return hash_djb2_buffer((const uint8_t *)&p_key, sizeof(MaterialKey));
  126. }
  127. bool operator==(const MaterialKey &p_key) const {
  128. return memcmp(this, &p_key, sizeof(MaterialKey)) == 0;
  129. }
  130. bool operator<(const MaterialKey &p_key) const {
  131. return memcmp(this, &p_key, sizeof(MaterialKey)) < 0;
  132. }
  133. };
  134. struct ShaderData {
  135. RID shader;
  136. int users = 0;
  137. };
  138. static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;
  139. static RBSet<String> min_max_properties;
  140. MaterialKey current_key;
  141. _FORCE_INLINE_ MaterialKey _compute_key() const {
  142. MaterialKey mk;
  143. mk.texture_color = color_ramp.is_valid() ? 1 : 0;
  144. mk.emission_shape = emission_shape;
  145. mk.has_emission_color = emission_shape >= EMISSION_SHAPE_POINTS && emission_color_texture.is_valid();
  146. mk.sub_emitter = sub_emitter_mode;
  147. mk.collision_mode = collision_mode;
  148. mk.attractor_enabled = attractor_interaction_enabled;
  149. mk.collision_scale = collision_scale;
  150. mk.turbulence_enabled = turbulence_enabled;
  151. mk.limiter_curve = velocity_limit_curve.is_valid() ? 1 : 0;
  152. mk.alpha_curve = alpha_curve.is_valid() ? 1 : 0;
  153. mk.emission_curve = emission_curve.is_valid() ? 1 : 0;
  154. mk.has_initial_ramp = color_initial_ramp.is_valid() ? 1 : 0;
  155. CurveXYZTexture *texture = Object::cast_to<CurveXYZTexture>(tex_parameters[PARAM_ORBIT_VELOCITY].ptr());
  156. mk.orbit_uses_curve_xyz = texture ? 1 : 0;
  157. for (int i = 0; i < PARAM_MAX; i++) {
  158. if (tex_parameters[i].is_valid()) {
  159. mk.texture_mask |= ((uint64_t)1 << i);
  160. }
  161. }
  162. for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
  163. if (particle_flags[i]) {
  164. mk.particle_flags |= ((uint64_t)1 << i);
  165. }
  166. }
  167. return mk;
  168. }
  169. static Mutex material_mutex;
  170. static SelfList<ParticleProcessMaterial>::List dirty_materials;
  171. struct ShaderNames {
  172. StringName direction;
  173. StringName spread;
  174. StringName flatness;
  175. StringName initial_linear_velocity_min;
  176. StringName initial_angle_min;
  177. StringName angular_velocity_min;
  178. StringName orbit_velocity_min;
  179. StringName radial_velocity_min;
  180. StringName linear_accel_min;
  181. StringName radial_accel_min;
  182. StringName tangent_accel_min;
  183. StringName damping_min;
  184. StringName scale_min;
  185. StringName scale_over_velocity_min;
  186. StringName hue_variation_min;
  187. StringName anim_speed_min;
  188. StringName anim_offset_min;
  189. StringName directional_velocity_min;
  190. StringName initial_linear_velocity_max;
  191. StringName initial_angle_max;
  192. StringName angular_velocity_max;
  193. StringName orbit_velocity_max;
  194. StringName radial_velocity_max;
  195. StringName linear_accel_max;
  196. StringName radial_accel_max;
  197. StringName tangent_accel_max;
  198. StringName damping_max;
  199. StringName scale_max;
  200. StringName scale_over_velocity_max;
  201. StringName hue_variation_max;
  202. StringName anim_speed_max;
  203. StringName anim_offset_max;
  204. StringName directional_velocity_max;
  205. StringName angle_texture;
  206. StringName angular_velocity_texture;
  207. StringName orbit_velocity_texture;
  208. StringName radial_velocity_texture;
  209. StringName linear_accel_texture;
  210. StringName radial_accel_texture;
  211. StringName tangent_accel_texture;
  212. StringName damping_texture;
  213. StringName scale_texture;
  214. StringName scale_over_velocity_texture;
  215. StringName hue_variation_texture;
  216. StringName anim_speed_texture;
  217. StringName anim_offset_texture;
  218. StringName velocity_limiter_texture;
  219. StringName directional_velocity_texture;
  220. StringName color;
  221. StringName color_ramp;
  222. StringName alpha_ramp;
  223. StringName emission_ramp;
  224. StringName color_initial_ramp;
  225. StringName velocity_limit_curve;
  226. StringName velocity_pivot;
  227. StringName emission_sphere_radius;
  228. StringName emission_box_extents;
  229. StringName emission_texture_point_count;
  230. StringName emission_texture_points;
  231. StringName emission_texture_normal;
  232. StringName emission_texture_color;
  233. StringName emission_ring_axis;
  234. StringName emission_ring_height;
  235. StringName emission_ring_radius;
  236. StringName emission_ring_inner_radius;
  237. StringName emission_ring_cone_angle;
  238. StringName emission_shape_offset;
  239. StringName emission_shape_scale;
  240. StringName turbulence_enabled;
  241. StringName turbulence_noise_strength;
  242. StringName turbulence_noise_scale;
  243. StringName turbulence_noise_speed;
  244. StringName turbulence_noise_speed_random;
  245. StringName turbulence_influence_over_life;
  246. StringName turbulence_influence_min;
  247. StringName turbulence_influence_max;
  248. StringName turbulence_initial_displacement_min;
  249. StringName turbulence_initial_displacement_max;
  250. StringName gravity;
  251. StringName inherit_emitter_velocity_ratio;
  252. StringName lifetime_randomness;
  253. StringName sub_emitter_frequency;
  254. StringName sub_emitter_amount_at_end;
  255. StringName sub_emitter_amount_at_collision;
  256. StringName sub_emitter_amount_at_start;
  257. StringName sub_emitter_keep_velocity;
  258. StringName collision_friction;
  259. StringName collision_bounce;
  260. };
  261. static ShaderNames *shader_names;
  262. SelfList<ParticleProcessMaterial> element;
  263. void _update_shader();
  264. _FORCE_INLINE_ void _queue_shader_change();
  265. Vector3 direction;
  266. float spread = 0.0f;
  267. float flatness = 0.0f;
  268. float params_min[PARAM_MAX] = {};
  269. float params_max[PARAM_MAX] = {};
  270. float params[PARAM_MAX] = {};
  271. Ref<Texture2D> tex_parameters[PARAM_MAX];
  272. Color color;
  273. Ref<Texture2D> color_ramp;
  274. Ref<Texture2D> alpha_curve;
  275. Ref<Texture2D> emission_curve;
  276. Ref<Texture2D> color_initial_ramp;
  277. Ref<Texture2D> velocity_limit_curve;
  278. bool directional_velocity_global = false;
  279. Vector3 velocity_pivot;
  280. bool particle_flags[PARTICLE_FLAG_MAX];
  281. EmissionShape emission_shape;
  282. float emission_sphere_radius = 0.0f;
  283. Vector3 emission_box_extents;
  284. Ref<Texture2D> emission_point_texture;
  285. Ref<Texture2D> emission_normal_texture;
  286. Ref<Texture2D> emission_color_texture;
  287. Vector3 emission_ring_axis;
  288. real_t emission_ring_height = 0.0f;
  289. real_t emission_ring_radius = 0.0f;
  290. real_t emission_ring_inner_radius = 0.0f;
  291. real_t emission_ring_cone_angle = 0.0f;
  292. int emission_point_count = 1;
  293. Vector3 emission_shape_offset;
  294. Vector3 emission_shape_scale;
  295. bool anim_loop = false;
  296. bool turbulence_enabled;
  297. Vector3 turbulence_noise_speed;
  298. Ref<Texture2D> turbulence_color_ramp;
  299. float turbulence_noise_strength = 0.0f;
  300. float turbulence_noise_scale = 0.0f;
  301. float turbulence_noise_speed_random = 0.0f;
  302. Vector3 gravity;
  303. double lifetime_randomness = 0.0;
  304. double inherit_emitter_velocity_ratio = 0.0;
  305. SubEmitterMode sub_emitter_mode;
  306. double sub_emitter_frequency = 0.0;
  307. int sub_emitter_amount_at_end = 0;
  308. int sub_emitter_amount_at_collision = 0;
  309. int sub_emitter_amount_at_start = 0;
  310. bool sub_emitter_keep_velocity = false;
  311. //do not save emission points here
  312. bool attractor_interaction_enabled = false;
  313. CollisionMode collision_mode;
  314. bool collision_scale = false;
  315. float collision_friction = 0.0f;
  316. float collision_bounce = 0.0f;
  317. protected:
  318. static void _bind_methods();
  319. void _validate_property(PropertyInfo &p_property) const;
  320. public:
  321. static bool has_min_max_property(const String &p_name);
  322. void set_direction(Vector3 p_direction);
  323. Vector3 get_direction() const;
  324. void set_spread(float p_spread);
  325. float get_spread() const;
  326. void set_flatness(float p_flatness);
  327. float get_flatness() const;
  328. void set_velocity_pivot(const Vector3 &p_pivot);
  329. Vector3 get_velocity_pivot();
  330. void set_param(Parameter p_param, const Vector2 &p_value);
  331. Vector2 get_param(Parameter p_param) const;
  332. void set_param_min(Parameter p_param, float p_value);
  333. float get_param_min(Parameter p_param) const;
  334. void set_param_max(Parameter p_param, float p_value);
  335. float get_param_max(Parameter p_param) const;
  336. void set_param_texture(Parameter p_param, const Ref<Texture2D> &p_texture);
  337. Ref<Texture2D> get_param_texture(Parameter p_param) const;
  338. void set_velocity_limit_curve(const Ref<Texture2D> &p_texture);
  339. Ref<Texture2D> get_velocity_limit_curve() const;
  340. void set_alpha_curve(const Ref<Texture2D> &p_texture);
  341. Ref<Texture2D> get_alpha_curve() const;
  342. void set_color(const Color &p_color);
  343. Color get_color() const;
  344. void set_color_ramp(const Ref<Texture2D> &p_texture);
  345. Ref<Texture2D> get_color_ramp() const;
  346. void set_color_initial_ramp(const Ref<Texture2D> &p_texture);
  347. Ref<Texture2D> get_color_initial_ramp() const;
  348. void set_emission_curve(const Ref<Texture2D> &p_texture);
  349. Ref<Texture2D> get_emission_curve() const;
  350. void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
  351. bool get_particle_flag(ParticleFlags p_particle_flag) const;
  352. void set_emission_shape(EmissionShape p_shape);
  353. void set_emission_sphere_radius(real_t p_radius);
  354. void set_emission_box_extents(Vector3 p_extents);
  355. void set_emission_point_texture(const Ref<Texture2D> &p_points);
  356. void set_emission_normal_texture(const Ref<Texture2D> &p_normals);
  357. void set_emission_color_texture(const Ref<Texture2D> &p_colors);
  358. void set_emission_ring_axis(Vector3 p_axis);
  359. void set_emission_ring_height(real_t p_height);
  360. void set_emission_ring_radius(real_t p_radius);
  361. void set_emission_ring_inner_radius(real_t p_radius);
  362. void set_emission_ring_cone_angle(real_t p_angle);
  363. void set_emission_point_count(int p_count);
  364. EmissionShape get_emission_shape() const;
  365. real_t get_emission_sphere_radius() const;
  366. Vector3 get_emission_box_extents() const;
  367. Ref<Texture2D> get_emission_point_texture() const;
  368. Ref<Texture2D> get_emission_normal_texture() const;
  369. Ref<Texture2D> get_emission_color_texture() const;
  370. Vector3 get_emission_ring_axis() const;
  371. real_t get_emission_ring_height() const;
  372. real_t get_emission_ring_radius() const;
  373. real_t get_emission_ring_inner_radius() const;
  374. real_t get_emission_ring_cone_angle() const;
  375. int get_emission_point_count() const;
  376. void set_turbulence_enabled(bool p_turbulence_enabled);
  377. void set_turbulence_noise_strength(float p_turbulence_noise_strength);
  378. void set_turbulence_noise_scale(float p_turbulence_noise_scale);
  379. void set_turbulence_noise_speed_random(float p_turbulence_noise_speed_random);
  380. void set_turbulence_noise_speed(const Vector3 &p_turbulence_noise_speed);
  381. bool get_turbulence_enabled() const;
  382. float get_turbulence_noise_strength() const;
  383. float get_turbulence_noise_scale() const;
  384. float get_turbulence_noise_speed_random() const;
  385. Vector3 get_turbulence_noise_speed() const;
  386. void set_gravity(const Vector3 &p_gravity);
  387. Vector3 get_gravity() const;
  388. void set_lifetime_randomness(double p_lifetime);
  389. double get_lifetime_randomness() const;
  390. void set_inherit_velocity_ratio(double p_ratio);
  391. double get_inherit_velocity_ratio();
  392. void set_attractor_interaction_enabled(bool p_enable);
  393. bool is_attractor_interaction_enabled() const;
  394. void set_collision_mode(CollisionMode p_collision_mode);
  395. CollisionMode get_collision_mode() const;
  396. void set_collision_use_scale(bool p_scale);
  397. bool is_collision_using_scale() const;
  398. void set_collision_friction(float p_friction);
  399. float get_collision_friction() const;
  400. void set_collision_bounce(float p_bounce);
  401. float get_collision_bounce() const;
  402. static void init_shaders();
  403. static void finish_shaders();
  404. static void flush_changes();
  405. void set_sub_emitter_mode(SubEmitterMode p_sub_emitter_mode);
  406. SubEmitterMode get_sub_emitter_mode() const;
  407. void set_sub_emitter_frequency(double p_frequency);
  408. double get_sub_emitter_frequency() const;
  409. void set_sub_emitter_amount_at_end(int p_amount);
  410. int get_sub_emitter_amount_at_end() const;
  411. void set_sub_emitter_amount_at_collision(int p_amount);
  412. int get_sub_emitter_amount_at_collision() const;
  413. void set_sub_emitter_amount_at_start(int p_amount);
  414. int get_sub_emitter_amount_at_start() const;
  415. void set_sub_emitter_keep_velocity(bool p_enable);
  416. bool get_sub_emitter_keep_velocity() const;
  417. void set_emission_shape_offset(const Vector3 &p_emission_shape_offset);
  418. Vector3 get_emission_shape_offset() const;
  419. void set_emission_shape_scale(const Vector3 &p_emission_shape_scale);
  420. Vector3 get_emission_shape_scale() const;
  421. virtual RID get_shader_rid() const override;
  422. virtual Shader::Mode get_shader_mode() const override;
  423. ParticleProcessMaterial();
  424. ~ParticleProcessMaterial();
  425. };
  426. VARIANT_ENUM_CAST(ParticleProcessMaterial::Parameter)
  427. VARIANT_ENUM_CAST(ParticleProcessMaterial::ParticleFlags)
  428. VARIANT_ENUM_CAST(ParticleProcessMaterial::EmissionShape)
  429. VARIANT_ENUM_CAST(ParticleProcessMaterial::SubEmitterMode)
  430. VARIANT_ENUM_CAST(ParticleProcessMaterial::CollisionMode)
  431. #endif // PARTICLE_PROCESS_MATERIAL_H