light_2d.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. virtual void _physics_interpolated_changed() override;
  70. protected:
  71. _FORCE_INLINE_ RID _get_light() const { return canvas_light; }
  72. void _notification(int p_what);
  73. static void _bind_methods();
  74. void _validate_property(PropertyInfo &p_property) const;
  75. public:
  76. void set_enabled(bool p_enabled);
  77. bool is_enabled() const;
  78. void set_editor_only(bool p_editor_only);
  79. bool is_editor_only() const;
  80. void set_color(const Color &p_color);
  81. Color get_color() const;
  82. void set_height(real_t p_height);
  83. real_t get_height() const;
  84. void set_energy(real_t p_energy);
  85. real_t get_energy() const;
  86. void set_z_range_min(int p_min_z);
  87. int get_z_range_min() const;
  88. void set_z_range_max(int p_max_z);
  89. int get_z_range_max() const;
  90. void set_layer_range_min(int p_min_layer);
  91. int get_layer_range_min() const;
  92. void set_layer_range_max(int p_max_layer);
  93. int get_layer_range_max() const;
  94. void set_item_cull_mask(int p_mask);
  95. int get_item_cull_mask() const;
  96. void set_item_shadow_cull_mask(int p_mask);
  97. int get_item_shadow_cull_mask() const;
  98. void set_shadow_enabled(bool p_enabled);
  99. bool is_shadow_enabled() const;
  100. void set_shadow_filter(ShadowFilter p_filter);
  101. ShadowFilter get_shadow_filter() const;
  102. void set_shadow_color(const Color &p_shadow_color);
  103. Color get_shadow_color() const;
  104. void set_shadow_smooth(real_t p_amount);
  105. real_t get_shadow_smooth() const;
  106. void set_blend_mode(BlendMode p_mode);
  107. BlendMode get_blend_mode() const;
  108. Light2D();
  109. ~Light2D();
  110. };
  111. VARIANT_ENUM_CAST(Light2D::ShadowFilter);
  112. VARIANT_ENUM_CAST(Light2D::BlendMode);
  113. class PointLight2D : public Light2D {
  114. GDCLASS(PointLight2D, Light2D);
  115. private:
  116. real_t _scale = 1.0;
  117. Ref<Texture2D> texture;
  118. Vector2 texture_offset;
  119. protected:
  120. #ifndef DISABLE_DEPRECATED
  121. bool _set(const StringName &p_name, const Variant &p_value);
  122. #endif // DISABLE_DEPRECATED
  123. static void _bind_methods();
  124. public:
  125. #ifdef TOOLS_ENABLED
  126. virtual Dictionary _edit_get_state() const override;
  127. virtual void _edit_set_state(const Dictionary &p_state) override;
  128. virtual void _edit_set_pivot(const Point2 &p_pivot) override;
  129. virtual Point2 _edit_get_pivot() const override;
  130. virtual bool _edit_use_pivot() const override;
  131. #endif // TOOLS_ENABLED
  132. #ifdef DEBUG_ENABLED
  133. virtual Rect2 _edit_get_rect() const override;
  134. virtual bool _edit_use_rect() const override;
  135. #endif // DEBUG_ENABLED
  136. virtual Rect2 get_anchorable_rect() const override;
  137. void set_texture(const Ref<Texture2D> &p_texture);
  138. Ref<Texture2D> get_texture() const;
  139. void set_texture_offset(const Vector2 &p_offset);
  140. Vector2 get_texture_offset() const;
  141. void set_texture_scale(real_t p_scale);
  142. real_t get_texture_scale() const;
  143. PackedStringArray get_configuration_warnings() const override;
  144. PointLight2D();
  145. };
  146. class DirectionalLight2D : public Light2D {
  147. GDCLASS(DirectionalLight2D, Light2D);
  148. real_t max_distance = 10000.0;
  149. protected:
  150. static void _bind_methods();
  151. public:
  152. void set_max_distance(real_t p_distance);
  153. real_t get_max_distance() const;
  154. DirectionalLight2D();
  155. };
  156. #endif // LIGHT_2D_H