canvas_item_material.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* canvas_item_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. #pragma once
  31. #include "scene/resources/material.h"
  32. class CanvasItemMaterial : public Material {
  33. GDCLASS(CanvasItemMaterial, Material);
  34. public:
  35. enum BlendMode {
  36. BLEND_MODE_MIX,
  37. BLEND_MODE_ADD,
  38. BLEND_MODE_SUB,
  39. BLEND_MODE_MUL,
  40. BLEND_MODE_PREMULT_ALPHA,
  41. BLEND_MODE_DISABLED
  42. };
  43. enum LightMode {
  44. LIGHT_MODE_NORMAL,
  45. LIGHT_MODE_UNSHADED,
  46. LIGHT_MODE_LIGHT_ONLY
  47. };
  48. private:
  49. union MaterialKey {
  50. struct {
  51. uint32_t blend_mode : 4;
  52. uint32_t light_mode : 4;
  53. uint32_t particles_animation : 1;
  54. uint32_t invalid_key : 1;
  55. };
  56. uint32_t key = 0;
  57. static uint32_t hash(const MaterialKey &p_key) {
  58. return hash_murmur3_one_32(p_key.key);
  59. }
  60. bool operator==(const MaterialKey &p_key) const {
  61. return key == p_key.key;
  62. }
  63. };
  64. struct ShaderNames {
  65. StringName particles_anim_h_frames;
  66. StringName particles_anim_v_frames;
  67. StringName particles_anim_loop;
  68. };
  69. static ShaderNames *shader_names;
  70. struct ShaderData {
  71. RID shader;
  72. int users = 0;
  73. };
  74. static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;
  75. MaterialKey current_key;
  76. _FORCE_INLINE_ MaterialKey _compute_key() const {
  77. MaterialKey mk;
  78. mk.key = 0;
  79. mk.blend_mode = blend_mode;
  80. mk.light_mode = light_mode;
  81. mk.particles_animation = particles_animation;
  82. return mk;
  83. }
  84. static Mutex material_mutex;
  85. static SelfList<CanvasItemMaterial>::List dirty_materials;
  86. SelfList<CanvasItemMaterial> element;
  87. void _update_shader();
  88. _FORCE_INLINE_ void _queue_shader_change();
  89. BlendMode blend_mode = BLEND_MODE_MIX;
  90. LightMode light_mode = LIGHT_MODE_NORMAL;
  91. bool particles_animation = false;
  92. // Proper values set in constructor.
  93. int particles_anim_h_frames = 0;
  94. int particles_anim_v_frames = 0;
  95. bool particles_anim_loop = false;
  96. protected:
  97. static void _bind_methods();
  98. void _validate_property(PropertyInfo &p_property) const;
  99. public:
  100. void set_blend_mode(BlendMode p_blend_mode);
  101. BlendMode get_blend_mode() const;
  102. void set_light_mode(LightMode p_light_mode);
  103. LightMode get_light_mode() const;
  104. void set_particles_animation(bool p_particles_anim);
  105. bool get_particles_animation() const;
  106. void set_particles_anim_h_frames(int p_frames);
  107. int get_particles_anim_h_frames() const;
  108. void set_particles_anim_v_frames(int p_frames);
  109. int get_particles_anim_v_frames() const;
  110. void set_particles_anim_loop(bool p_loop);
  111. bool get_particles_anim_loop() const;
  112. static void init_shaders();
  113. static void finish_shaders();
  114. static void flush_changes();
  115. virtual RID get_shader_rid() const override;
  116. virtual Shader::Mode get_shader_mode() const override;
  117. CanvasItemMaterial();
  118. virtual ~CanvasItemMaterial();
  119. };
  120. VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)
  121. VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)