particles_material.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**************************************************************************/
  2. /* particles_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. #include "core/rid.h"
  31. #include "scene/resources/material.h"
  32. #ifndef PARTICLES_MATERIAL_H
  33. #define PARTICLES_MATERIAL_H
  34. class ParticlesMaterial : public Material {
  35. GDCLASS(ParticlesMaterial, Material);
  36. public:
  37. enum Parameter {
  38. PARAM_INITIAL_LINEAR_VELOCITY,
  39. PARAM_ANGULAR_VELOCITY,
  40. PARAM_ORBIT_VELOCITY,
  41. PARAM_LINEAR_ACCEL,
  42. PARAM_RADIAL_ACCEL,
  43. PARAM_TANGENTIAL_ACCEL,
  44. PARAM_DAMPING,
  45. PARAM_ANGLE,
  46. PARAM_SCALE,
  47. PARAM_HUE_VARIATION,
  48. PARAM_ANIM_SPEED,
  49. PARAM_ANIM_OFFSET,
  50. PARAM_MAX
  51. };
  52. // When extending, make sure not to overflow the size of the MaterialKey below.
  53. enum Flags {
  54. FLAG_ALIGN_Y_TO_VELOCITY,
  55. FLAG_ROTATE_Y,
  56. FLAG_DISABLE_Z,
  57. FLAG_MAX
  58. };
  59. // When extending, make sure not to overflow the size of the MaterialKey below.
  60. enum EmissionShape {
  61. EMISSION_SHAPE_POINT,
  62. EMISSION_SHAPE_SPHERE,
  63. EMISSION_SHAPE_BOX,
  64. EMISSION_SHAPE_POINTS,
  65. EMISSION_SHAPE_DIRECTED_POINTS,
  66. EMISSION_SHAPE_RING,
  67. EMISSION_SHAPE_MAX
  68. };
  69. private:
  70. union MaterialKey {
  71. // The bit size of the struct must be kept below or equal to 32 bits.
  72. // Consider this when extending Flags, or EmissionShape.
  73. struct {
  74. uint32_t texture_mask : 16;
  75. uint32_t texture_color : 1;
  76. uint32_t texture_initial_color : 1;
  77. uint32_t flags : 4;
  78. uint32_t emission_shape : 3;
  79. uint32_t trail_size_texture : 1;
  80. uint32_t trail_color_texture : 1;
  81. uint32_t invalid_key : 1;
  82. uint32_t has_emission_color : 1;
  83. };
  84. uint32_t key;
  85. bool operator<(const MaterialKey &p_key) const {
  86. return key < p_key.key;
  87. }
  88. };
  89. struct ShaderData {
  90. RID shader;
  91. int users;
  92. };
  93. static Map<MaterialKey, ShaderData> shader_map;
  94. MaterialKey current_key;
  95. _FORCE_INLINE_ MaterialKey _compute_key() const {
  96. MaterialKey mk;
  97. mk.key = 0;
  98. for (int i = 0; i < PARAM_MAX; i++) {
  99. if (tex_parameters[i].is_valid()) {
  100. mk.texture_mask |= (1 << i);
  101. }
  102. }
  103. for (int i = 0; i < FLAG_MAX; i++) {
  104. if (flags[i]) {
  105. mk.flags |= (1 << i);
  106. }
  107. }
  108. mk.texture_color = color_ramp.is_valid() ? 1 : 0;
  109. mk.texture_initial_color = color_initial_ramp.is_valid() ? 1 : 0;
  110. mk.emission_shape = emission_shape;
  111. mk.trail_color_texture = trail_color_modifier.is_valid() ? 1 : 0;
  112. mk.trail_size_texture = trail_size_modifier.is_valid() ? 1 : 0;
  113. mk.has_emission_color = emission_shape >= EMISSION_SHAPE_POINTS && emission_color_texture.is_valid();
  114. return mk;
  115. }
  116. static Mutex material_mutex;
  117. static SelfList<ParticlesMaterial>::List *dirty_materials;
  118. struct ShaderNames {
  119. StringName direction;
  120. StringName spread;
  121. StringName flatness;
  122. StringName initial_linear_velocity;
  123. StringName initial_angle;
  124. StringName angular_velocity;
  125. StringName orbit_velocity;
  126. StringName linear_accel;
  127. StringName radial_accel;
  128. StringName tangent_accel;
  129. StringName damping;
  130. StringName scale;
  131. StringName hue_variation;
  132. StringName anim_speed;
  133. StringName anim_offset;
  134. StringName initial_linear_velocity_random;
  135. StringName initial_angle_random;
  136. StringName angular_velocity_random;
  137. StringName orbit_velocity_random;
  138. StringName linear_accel_random;
  139. StringName radial_accel_random;
  140. StringName tangent_accel_random;
  141. StringName damping_random;
  142. StringName scale_random;
  143. StringName hue_variation_random;
  144. StringName anim_speed_random;
  145. StringName anim_offset_random;
  146. StringName angle_texture;
  147. StringName angular_velocity_texture;
  148. StringName orbit_velocity_texture;
  149. StringName linear_accel_texture;
  150. StringName radial_accel_texture;
  151. StringName tangent_accel_texture;
  152. StringName damping_texture;
  153. StringName scale_texture;
  154. StringName hue_variation_texture;
  155. StringName anim_speed_texture;
  156. StringName anim_offset_texture;
  157. StringName color;
  158. StringName color_ramp;
  159. StringName color_initial_ramp;
  160. StringName emission_sphere_radius;
  161. StringName emission_box_extents;
  162. StringName emission_texture_point_count;
  163. StringName emission_texture_points;
  164. StringName emission_texture_normal;
  165. StringName emission_texture_color;
  166. StringName emission_ring_radius;
  167. StringName emission_ring_inner_radius;
  168. StringName emission_ring_height;
  169. StringName emission_ring_axis;
  170. StringName trail_divisor;
  171. StringName trail_size_modifier;
  172. StringName trail_color_modifier;
  173. StringName gravity;
  174. StringName lifetime_randomness;
  175. };
  176. static ShaderNames *shader_names;
  177. SelfList<ParticlesMaterial> element;
  178. void _update_shader();
  179. _FORCE_INLINE_ void _queue_shader_change();
  180. _FORCE_INLINE_ bool _is_shader_dirty() const;
  181. bool is_initialized = false;
  182. Vector3 direction;
  183. float spread;
  184. float flatness;
  185. float parameters[PARAM_MAX];
  186. float randomness[PARAM_MAX];
  187. Ref<Texture> tex_parameters[PARAM_MAX];
  188. Color color;
  189. Ref<Texture> color_ramp;
  190. Ref<Texture> color_initial_ramp;
  191. bool flags[FLAG_MAX];
  192. EmissionShape emission_shape;
  193. float emission_sphere_radius;
  194. Vector3 emission_box_extents;
  195. Ref<Texture> emission_point_texture;
  196. Ref<Texture> emission_normal_texture;
  197. Ref<Texture> emission_color_texture;
  198. int emission_point_count;
  199. float emission_ring_height;
  200. float emission_ring_radius;
  201. float emission_ring_inner_radius;
  202. Vector3 emission_ring_axis;
  203. bool anim_loop;
  204. int trail_divisor;
  205. Ref<CurveTexture> trail_size_modifier;
  206. Ref<GradientTexture> trail_color_modifier;
  207. Vector3 gravity;
  208. float lifetime_randomness;
  209. //do not save emission points here
  210. protected:
  211. static void _bind_methods();
  212. virtual void _validate_property(PropertyInfo &property) const;
  213. public:
  214. void set_direction(Vector3 p_direction);
  215. Vector3 get_direction() const;
  216. void set_spread(float p_spread);
  217. float get_spread() const;
  218. void set_flatness(float p_flatness);
  219. float get_flatness() const;
  220. void set_param(Parameter p_param, float p_value);
  221. float get_param(Parameter p_param) const;
  222. void set_param_randomness(Parameter p_param, float p_value);
  223. float get_param_randomness(Parameter p_param) const;
  224. void set_param_texture(Parameter p_param, const Ref<Texture> &p_texture);
  225. Ref<Texture> get_param_texture(Parameter p_param) const;
  226. void set_color(const Color &p_color);
  227. Color get_color() const;
  228. void set_color_ramp(const Ref<Texture> &p_texture);
  229. Ref<Texture> get_color_ramp() const;
  230. void set_color_initial_ramp(const Ref<Texture> &p_texture);
  231. Ref<Texture> get_color_initial_ramp() const;
  232. void set_flag(Flags p_flag, bool p_enable);
  233. bool get_flag(Flags p_flag) const;
  234. void set_emission_shape(EmissionShape p_shape);
  235. void set_emission_sphere_radius(float p_radius);
  236. void set_emission_box_extents(Vector3 p_extents);
  237. void set_emission_point_texture(const Ref<Texture> &p_points);
  238. void set_emission_normal_texture(const Ref<Texture> &p_normals);
  239. void set_emission_color_texture(const Ref<Texture> &p_colors);
  240. void set_emission_point_count(int p_count);
  241. void set_emission_ring_radius(float p_radius);
  242. void set_emission_ring_inner_radius(float p_offset);
  243. void set_emission_ring_height(float p_height);
  244. void set_emission_ring_axis(Vector3 p_axis);
  245. EmissionShape get_emission_shape() const;
  246. float get_emission_sphere_radius() const;
  247. Vector3 get_emission_box_extents() const;
  248. Ref<Texture> get_emission_point_texture() const;
  249. Ref<Texture> get_emission_normal_texture() const;
  250. Ref<Texture> get_emission_color_texture() const;
  251. int get_emission_point_count() const;
  252. float get_emission_ring_radius() const;
  253. float get_emission_ring_inner_radius() const;
  254. float get_emission_ring_height() const;
  255. Vector3 get_emission_ring_axis() const;
  256. void set_trail_divisor(int p_divisor);
  257. int get_trail_divisor() const;
  258. void set_trail_size_modifier(const Ref<CurveTexture> &p_trail_size_modifier);
  259. Ref<CurveTexture> get_trail_size_modifier() const;
  260. void set_trail_color_modifier(const Ref<GradientTexture> &p_trail_color_modifier);
  261. Ref<GradientTexture> get_trail_color_modifier() const;
  262. void set_gravity(const Vector3 &p_gravity);
  263. Vector3 get_gravity() const;
  264. void set_lifetime_randomness(float p_lifetime);
  265. float get_lifetime_randomness() const;
  266. static void init_shaders();
  267. static void finish_shaders();
  268. static void flush_changes();
  269. RID get_shader_rid() const;
  270. virtual Shader::Mode get_shader_mode() const;
  271. ParticlesMaterial();
  272. ~ParticlesMaterial();
  273. };
  274. VARIANT_ENUM_CAST(ParticlesMaterial::Parameter)
  275. VARIANT_ENUM_CAST(ParticlesMaterial::Flags)
  276. VARIANT_ENUM_CAST(ParticlesMaterial::EmissionShape)
  277. #endif // PARTICLES_MATERIAL_H