particles_material.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************************/
  2. /* particles_material.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #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. enum Flags {
  53. FLAG_ALIGN_Y_TO_VELOCITY,
  54. FLAG_ROTATE_Y,
  55. FLAG_DISABLE_Z,
  56. FLAG_MAX
  57. };
  58. enum EmissionShape {
  59. EMISSION_SHAPE_POINT,
  60. EMISSION_SHAPE_SPHERE,
  61. EMISSION_SHAPE_BOX,
  62. EMISSION_SHAPE_POINTS,
  63. EMISSION_SHAPE_DIRECTED_POINTS,
  64. };
  65. private:
  66. union MaterialKey {
  67. struct {
  68. uint32_t texture_mask : 16;
  69. uint32_t texture_color : 1;
  70. uint32_t flags : 4;
  71. uint32_t emission_shape : 2;
  72. uint32_t trail_size_texture : 1;
  73. uint32_t trail_color_texture : 1;
  74. uint32_t invalid_key : 1;
  75. uint32_t has_emission_color : 1;
  76. };
  77. uint32_t key;
  78. bool operator<(const MaterialKey &p_key) const {
  79. return key < p_key.key;
  80. }
  81. };
  82. struct ShaderData {
  83. RID shader;
  84. int users;
  85. };
  86. static Map<MaterialKey, ShaderData> shader_map;
  87. MaterialKey current_key;
  88. _FORCE_INLINE_ MaterialKey _compute_key() const {
  89. MaterialKey mk;
  90. mk.key = 0;
  91. for (int i = 0; i < PARAM_MAX; i++) {
  92. if (tex_parameters[i].is_valid()) {
  93. mk.texture_mask |= (1 << i);
  94. }
  95. }
  96. for (int i = 0; i < FLAG_MAX; i++) {
  97. if (flags[i]) {
  98. mk.flags |= (1 << i);
  99. }
  100. }
  101. mk.texture_color = color_ramp.is_valid() ? 1 : 0;
  102. mk.emission_shape = emission_shape;
  103. mk.trail_color_texture = trail_color_modifier.is_valid() ? 1 : 0;
  104. mk.trail_size_texture = trail_size_modifier.is_valid() ? 1 : 0;
  105. mk.has_emission_color = emission_shape >= EMISSION_SHAPE_POINTS && emission_color_texture.is_valid();
  106. return mk;
  107. }
  108. static Mutex *material_mutex;
  109. static SelfList<ParticlesMaterial>::List *dirty_materials;
  110. struct ShaderNames {
  111. StringName spread;
  112. StringName flatness;
  113. StringName initial_linear_velocity;
  114. StringName initial_angle;
  115. StringName angular_velocity;
  116. StringName orbit_velocity;
  117. StringName linear_accel;
  118. StringName radial_accel;
  119. StringName tangent_accel;
  120. StringName damping;
  121. StringName scale;
  122. StringName hue_variation;
  123. StringName anim_speed;
  124. StringName anim_offset;
  125. StringName initial_linear_velocity_random;
  126. StringName initial_angle_random;
  127. StringName angular_velocity_random;
  128. StringName orbit_velocity_random;
  129. StringName linear_accel_random;
  130. StringName radial_accel_random;
  131. StringName tangent_accel_random;
  132. StringName damping_random;
  133. StringName scale_random;
  134. StringName hue_variation_random;
  135. StringName anim_speed_random;
  136. StringName anim_offset_random;
  137. StringName angle_texture;
  138. StringName angular_velocity_texture;
  139. StringName orbit_velocity_texture;
  140. StringName linear_accel_texture;
  141. StringName radial_accel_texture;
  142. StringName tangent_accel_texture;
  143. StringName damping_texture;
  144. StringName scale_texture;
  145. StringName hue_variation_texture;
  146. StringName anim_speed_texture;
  147. StringName anim_offset_texture;
  148. StringName color;
  149. StringName color_ramp;
  150. StringName emission_sphere_radius;
  151. StringName emission_box_extents;
  152. StringName emission_texture_point_count;
  153. StringName emission_texture_points;
  154. StringName emission_texture_normal;
  155. StringName emission_texture_color;
  156. StringName trail_divisor;
  157. StringName trail_size_modifier;
  158. StringName trail_color_modifier;
  159. StringName gravity;
  160. };
  161. static ShaderNames *shader_names;
  162. SelfList<ParticlesMaterial> element;
  163. void _update_shader();
  164. _FORCE_INLINE_ void _queue_shader_change();
  165. _FORCE_INLINE_ bool _is_shader_dirty() const;
  166. float spread;
  167. float flatness;
  168. float parameters[PARAM_MAX];
  169. float randomness[PARAM_MAX];
  170. Ref<Texture> tex_parameters[PARAM_MAX];
  171. Color color;
  172. Ref<Texture> color_ramp;
  173. bool flags[FLAG_MAX];
  174. EmissionShape emission_shape;
  175. float emission_sphere_radius;
  176. Vector3 emission_box_extents;
  177. Ref<Texture> emission_point_texture;
  178. Ref<Texture> emission_normal_texture;
  179. Ref<Texture> emission_color_texture;
  180. int emission_point_count;
  181. bool anim_loop;
  182. int trail_divisor;
  183. Ref<CurveTexture> trail_size_modifier;
  184. Ref<GradientTexture> trail_color_modifier;
  185. Vector3 gravity;
  186. //do not save emission points here
  187. protected:
  188. static void _bind_methods();
  189. virtual void _validate_property(PropertyInfo &property) const;
  190. public:
  191. void set_spread(float p_spread);
  192. float get_spread() const;
  193. void set_flatness(float p_flatness);
  194. float get_flatness() const;
  195. void set_param(Parameter p_param, float p_value);
  196. float get_param(Parameter p_param) const;
  197. void set_param_randomness(Parameter p_param, float p_value);
  198. float get_param_randomness(Parameter p_param) const;
  199. void set_param_texture(Parameter p_param, const Ref<Texture> &p_texture);
  200. Ref<Texture> get_param_texture(Parameter p_param) const;
  201. void set_color(const Color &p_color);
  202. Color get_color() const;
  203. void set_color_ramp(const Ref<Texture> &p_texture);
  204. Ref<Texture> get_color_ramp() const;
  205. void set_flag(Flags p_flag, bool p_enable);
  206. bool get_flag(Flags p_flag) const;
  207. void set_emission_shape(EmissionShape p_shape);
  208. void set_emission_sphere_radius(float p_radius);
  209. void set_emission_box_extents(Vector3 p_extents);
  210. void set_emission_point_texture(const Ref<Texture> &p_points);
  211. void set_emission_normal_texture(const Ref<Texture> &p_normals);
  212. void set_emission_color_texture(const Ref<Texture> &p_colors);
  213. void set_emission_point_count(int p_count);
  214. EmissionShape get_emission_shape() const;
  215. float get_emission_sphere_radius() const;
  216. Vector3 get_emission_box_extents() const;
  217. Ref<Texture> get_emission_point_texture() const;
  218. Ref<Texture> get_emission_normal_texture() const;
  219. Ref<Texture> get_emission_color_texture() const;
  220. int get_emission_point_count() const;
  221. void set_trail_divisor(int p_divisor);
  222. int get_trail_divisor() const;
  223. void set_trail_size_modifier(const Ref<CurveTexture> &p_trail_size_modifier);
  224. Ref<CurveTexture> get_trail_size_modifier() const;
  225. void set_trail_color_modifier(const Ref<GradientTexture> &p_trail_color_modifier);
  226. Ref<GradientTexture> get_trail_color_modifier() const;
  227. void set_gravity(const Vector3 &p_gravity);
  228. Vector3 get_gravity() const;
  229. static void init_shaders();
  230. static void finish_shaders();
  231. static void flush_changes();
  232. RID get_shader_rid() const;
  233. virtual Shader::Mode get_shader_mode() const;
  234. ParticlesMaterial();
  235. ~ParticlesMaterial();
  236. };
  237. VARIANT_ENUM_CAST(ParticlesMaterial::Parameter)
  238. VARIANT_ENUM_CAST(ParticlesMaterial::Flags)
  239. VARIANT_ENUM_CAST(ParticlesMaterial::EmissionShape)
  240. #endif // PARTICLES_MATERIAL_H