particle_process_material.h 17 KB

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