light_2d.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* light_2d.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 LIGHT_2D_H
  31. #define LIGHT_2D_H
  32. #include "scene/2d/node_2d.h"
  33. class Light2D : public Node2D {
  34. GDCLASS(Light2D, Node2D);
  35. public:
  36. enum ShadowFilter {
  37. SHADOW_FILTER_NONE,
  38. SHADOW_FILTER_PCF5,
  39. SHADOW_FILTER_PCF13,
  40. SHADOW_FILTER_MAX
  41. };
  42. enum BlendMode {
  43. BLEND_MODE_ADD,
  44. BLEND_MODE_SUB,
  45. BLEND_MODE_MIX,
  46. };
  47. private:
  48. RID canvas_light;
  49. bool enabled = true;
  50. bool editor_only = false;
  51. bool shadow = false;
  52. Color color = Color(1, 1, 1);
  53. Color shadow_color = Color(0, 0, 0, 0);
  54. real_t height = 0.0;
  55. real_t energy = 1.0;
  56. int z_min = -1024;
  57. int z_max = 1024;
  58. int layer_min = 0;
  59. int layer_max = 0;
  60. int item_mask = 1;
  61. int item_shadow_mask = 1;
  62. real_t shadow_smooth = 0.0;
  63. Ref<Texture2D> texture;
  64. Vector2 texture_offset;
  65. ShadowFilter shadow_filter = SHADOW_FILTER_NONE;
  66. BlendMode blend_mode = BLEND_MODE_ADD;
  67. void _update_light_visibility();
  68. virtual void owner_changed_notify() override;
  69. protected:
  70. _FORCE_INLINE_ RID _get_light() const { return canvas_light; }
  71. void _notification(int p_what);
  72. static void _bind_methods();
  73. void _validate_property(PropertyInfo &p_property) const;
  74. public:
  75. void set_enabled(bool p_enabled);
  76. bool is_enabled() const;
  77. void set_editor_only(bool p_editor_only);
  78. bool is_editor_only() const;
  79. void set_color(const Color &p_color);
  80. Color get_color() const;
  81. void set_height(real_t p_height);
  82. real_t get_height() const;
  83. void set_energy(real_t p_energy);
  84. real_t get_energy() const;
  85. void set_z_range_min(int p_min_z);
  86. int get_z_range_min() const;
  87. void set_z_range_max(int p_max_z);
  88. int get_z_range_max() const;
  89. void set_layer_range_min(int p_min_layer);
  90. int get_layer_range_min() const;
  91. void set_layer_range_max(int p_max_layer);
  92. int get_layer_range_max() const;
  93. void set_item_cull_mask(int p_mask);
  94. int get_item_cull_mask() const;
  95. void set_item_shadow_cull_mask(int p_mask);
  96. int get_item_shadow_cull_mask() const;
  97. void set_shadow_enabled(bool p_enabled);
  98. bool is_shadow_enabled() const;
  99. void set_shadow_filter(ShadowFilter p_filter);
  100. ShadowFilter get_shadow_filter() const;
  101. void set_shadow_color(const Color &p_shadow_color);
  102. Color get_shadow_color() const;
  103. void set_shadow_smooth(real_t p_amount);
  104. real_t get_shadow_smooth() const;
  105. void set_blend_mode(BlendMode p_mode);
  106. BlendMode get_blend_mode() const;
  107. Light2D();
  108. ~Light2D();
  109. };
  110. VARIANT_ENUM_CAST(Light2D::ShadowFilter);
  111. VARIANT_ENUM_CAST(Light2D::BlendMode);
  112. class PointLight2D : public Light2D {
  113. GDCLASS(PointLight2D, Light2D);
  114. private:
  115. real_t _scale = 1.0;
  116. Ref<Texture2D> texture;
  117. Vector2 texture_offset;
  118. protected:
  119. static void _bind_methods();
  120. public:
  121. #ifdef TOOLS_ENABLED
  122. virtual Dictionary _edit_get_state() const override;
  123. virtual void _edit_set_state(const Dictionary &p_state) override;
  124. virtual void _edit_set_pivot(const Point2 &p_pivot) override;
  125. virtual Point2 _edit_get_pivot() const override;
  126. virtual bool _edit_use_pivot() const override;
  127. virtual Rect2 _edit_get_rect() const override;
  128. virtual bool _edit_use_rect() const override;
  129. #endif
  130. virtual Rect2 get_anchorable_rect() const override;
  131. void set_texture(const Ref<Texture2D> &p_texture);
  132. Ref<Texture2D> get_texture() const;
  133. void set_texture_offset(const Vector2 &p_offset);
  134. Vector2 get_texture_offset() const;
  135. void set_texture_scale(real_t p_scale);
  136. real_t get_texture_scale() const;
  137. PackedStringArray get_configuration_warnings() const override;
  138. PointLight2D();
  139. };
  140. class DirectionalLight2D : public Light2D {
  141. GDCLASS(DirectionalLight2D, Light2D);
  142. real_t max_distance = 10000.0;
  143. protected:
  144. static void _bind_methods();
  145. public:
  146. void set_max_distance(real_t p_distance);
  147. real_t get_max_distance() const;
  148. DirectionalLight2D();
  149. };
  150. #endif // LIGHT_2D_H