light.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* light.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef LIGHT_H
  31. #define LIGHT_H
  32. #include "scene/3d/visual_instance.h"
  33. #include "scene/resources/texture.h"
  34. #include "servers/visual_server.h"
  35. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class Light : public VisualInstance {
  39. OBJ_TYPE(Light, VisualInstance);
  40. OBJ_CATEGORY("3D Light Nodes");
  41. public:
  42. enum Parameter {
  43. PARAM_RADIUS = VisualServer::LIGHT_PARAM_RADIUS,
  44. PARAM_ENERGY = VisualServer::LIGHT_PARAM_ENERGY,
  45. PARAM_ATTENUATION = VisualServer::LIGHT_PARAM_ATTENUATION,
  46. PARAM_SPOT_ANGLE = VisualServer::LIGHT_PARAM_SPOT_ANGLE,
  47. PARAM_SPOT_ATTENUATION = VisualServer::LIGHT_PARAM_SPOT_ATTENUATION,
  48. PARAM_SHADOW_DARKENING = VisualServer::LIGHT_PARAM_SHADOW_DARKENING,
  49. PARAM_SHADOW_Z_OFFSET = VisualServer::LIGHT_PARAM_SHADOW_Z_OFFSET,
  50. PARAM_SHADOW_Z_SLOPE_SCALE = VisualServer::LIGHT_PARAM_SHADOW_Z_SLOPE_SCALE,
  51. PARAM_SHADOW_ESM_MULTIPLIER = VisualServer::LIGHT_PARAM_SHADOW_ESM_MULTIPLIER,
  52. PARAM_SHADOW_BLUR_PASSES = VisualServer::LIGHT_PARAM_SHADOW_BLUR_PASSES,
  53. PARAM_MAX = VisualServer::LIGHT_PARAM_MAX
  54. };
  55. enum LightColor {
  56. COLOR_DIFFUSE = VisualServer::LIGHT_COLOR_DIFFUSE,
  57. COLOR_SPECULAR = VisualServer::LIGHT_COLOR_SPECULAR
  58. };
  59. enum BakeMode {
  60. BAKE_MODE_DISABLED,
  61. BAKE_MODE_INDIRECT,
  62. BAKE_MODE_INDIRECT_AND_SHADOWS,
  63. BAKE_MODE_FULL
  64. };
  65. enum Operator {
  66. OPERATOR_ADD,
  67. OPERATOR_SUB
  68. };
  69. private:
  70. Ref<Texture> projector;
  71. float vars[PARAM_MAX];
  72. Color colors[3];
  73. BakeMode bake_mode;
  74. VisualServer::LightType type;
  75. bool shadows;
  76. bool enabled;
  77. bool editor_only;
  78. Operator op;
  79. void _update_visibility();
  80. // bind helpers
  81. protected:
  82. RID light;
  83. virtual bool _can_gizmo_scale() const;
  84. virtual RES _get_gizmo_geometry() const;
  85. static void _bind_methods();
  86. void _notification(int p_what);
  87. Light(VisualServer::LightType p_type);
  88. public:
  89. VS::LightType get_light_type() const { return type; }
  90. void set_parameter(Parameter p_var, float p_value);
  91. float get_parameter(Parameter p_var) const;
  92. void set_color(LightColor p_color, const Color &p_value);
  93. Color get_color(LightColor p_color) const;
  94. void set_project_shadows(bool p_enabled);
  95. bool has_project_shadows() const;
  96. void set_projector(const Ref<Texture> &p_projector);
  97. Ref<Texture> get_projector() const;
  98. void set_operator(Operator p_op);
  99. Operator get_operator() const;
  100. void set_bake_mode(BakeMode p_bake_mode);
  101. BakeMode get_bake_mode() const;
  102. void set_enabled(bool p_enabled);
  103. bool is_enabled() const;
  104. void set_editor_only(bool p_editor_only);
  105. bool is_editor_only() const;
  106. virtual AABB get_aabb() const;
  107. virtual DVector<Face3> get_faces(uint32_t p_usage_flags) const;
  108. void approximate_opengl_attenuation(float p_constant, float p_linear, float p_quadratic, float p_radius_treshold = 0.5);
  109. Light();
  110. ~Light();
  111. };
  112. VARIANT_ENUM_CAST(Light::Parameter);
  113. VARIANT_ENUM_CAST(Light::LightColor);
  114. VARIANT_ENUM_CAST(Light::Operator);
  115. VARIANT_ENUM_CAST(Light::BakeMode);
  116. class DirectionalLight : public Light {
  117. OBJ_TYPE(DirectionalLight, Light);
  118. public:
  119. enum ShadowMode {
  120. SHADOW_ORTHOGONAL,
  121. SHADOW_PERSPECTIVE,
  122. SHADOW_PARALLEL_2_SPLITS,
  123. SHADOW_PARALLEL_4_SPLITS
  124. };
  125. enum ShadowParam {
  126. SHADOW_PARAM_MAX_DISTANCE,
  127. SHADOW_PARAM_PSSM_SPLIT_WEIGHT,
  128. SHADOW_PARAM_PSSM_ZOFFSET_SCALE
  129. };
  130. private:
  131. ShadowMode shadow_mode;
  132. float shadow_param[3];
  133. protected:
  134. static void _bind_methods();
  135. public:
  136. void set_shadow_mode(ShadowMode p_mode);
  137. ShadowMode get_shadow_mode() const;
  138. void set_shadow_max_distance(float p_distance);
  139. float get_shadow_max_distance() const;
  140. void set_shadow_param(ShadowParam p_param, float p_value);
  141. float get_shadow_param(ShadowParam p_param) const;
  142. DirectionalLight();
  143. };
  144. VARIANT_ENUM_CAST(DirectionalLight::ShadowMode);
  145. VARIANT_ENUM_CAST(DirectionalLight::ShadowParam);
  146. class OmniLight : public Light {
  147. OBJ_TYPE(OmniLight, Light);
  148. protected:
  149. static void _bind_methods();
  150. public:
  151. OmniLight() :
  152. Light(VisualServer::LIGHT_OMNI) { set_parameter(PARAM_SHADOW_Z_OFFSET, 0.001); }
  153. };
  154. class SpotLight : public Light {
  155. OBJ_TYPE(SpotLight, Light);
  156. protected:
  157. static void _bind_methods();
  158. public:
  159. SpotLight() :
  160. Light(VisualServer::LIGHT_SPOT) {}
  161. };
  162. #endif