light.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*************************************************************************/
  2. /* light.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef LIGHT_H
  30. #define LIGHT_H
  31. #include "scene/3d/visual_instance.h"
  32. #include "scene/resources/texture.h"
  33. #include "servers/visual_server.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class Light : public VisualInstance {
  38. OBJ_TYPE( Light, VisualInstance );
  39. OBJ_CATEGORY("3D Light Nodes");
  40. public:
  41. enum Parameter {
  42. PARAM_RADIUS=VisualServer::LIGHT_PARAM_RADIUS,
  43. PARAM_ENERGY=VisualServer::LIGHT_PARAM_ENERGY,
  44. PARAM_ATTENUATION=VisualServer::LIGHT_PARAM_ATTENUATION,
  45. PARAM_SPOT_ANGLE=VisualServer::LIGHT_PARAM_SPOT_ANGLE,
  46. PARAM_SPOT_ATTENUATION=VisualServer::LIGHT_PARAM_SPOT_ATTENUATION,
  47. PARAM_SHADOW_DARKENING=VisualServer::LIGHT_PARAM_SHADOW_DARKENING,
  48. PARAM_SHADOW_Z_OFFSET=VisualServer::LIGHT_PARAM_SHADOW_Z_OFFSET,
  49. PARAM_SHADOW_Z_SLOPE_SCALE=VisualServer::LIGHT_PARAM_SHADOW_Z_SLOPE_SCALE,
  50. PARAM_SHADOW_ESM_MULTIPLIER=VisualServer::LIGHT_PARAM_SHADOW_ESM_MULTIPLIER,
  51. PARAM_SHADOW_BLUR_PASSES=VisualServer::LIGHT_PARAM_SHADOW_BLUR_PASSES,
  52. PARAM_MAX=VisualServer::LIGHT_PARAM_MAX
  53. };
  54. enum LightColor {
  55. COLOR_DIFFUSE=VisualServer::LIGHT_COLOR_DIFFUSE,
  56. COLOR_SPECULAR=VisualServer::LIGHT_COLOR_SPECULAR
  57. };
  58. enum BakeMode {
  59. BAKE_MODE_DISABLED,
  60. BAKE_MODE_INDIRECT,
  61. BAKE_MODE_INDIRECT_AND_SHADOWS,
  62. BAKE_MODE_FULL
  63. };
  64. enum Operator {
  65. OPERATOR_ADD,
  66. OPERATOR_SUB
  67. };
  68. private:
  69. Ref<Texture> projector;
  70. float vars[PARAM_MAX];
  71. Color colors[3];
  72. BakeMode bake_mode;
  73. VisualServer::LightType type;
  74. bool shadows;
  75. bool enabled;
  76. bool editor_only;
  77. Operator op;
  78. void _update_visibility();
  79. // bind helpers
  80. protected:
  81. RID light;
  82. virtual bool _can_gizmo_scale() const;
  83. virtual RES _get_gizmo_geometry() const;
  84. static void _bind_methods();
  85. void _notification(int p_what);
  86. Light(VisualServer::LightType p_type);
  87. public:
  88. VS::LightType get_light_type() const { return type; }
  89. void set_parameter(Parameter p_var, float p_value);
  90. float get_parameter(Parameter p_var) const;
  91. void set_color(LightColor p_color,const Color& p_value);
  92. Color get_color(LightColor p_color) const;
  93. void set_project_shadows(bool p_enabled);
  94. bool has_project_shadows() const;
  95. void set_projector(const Ref<Texture>& p_projector);
  96. Ref<Texture> get_projector() const;
  97. void set_operator(Operator p_op);
  98. Operator get_operator() const;
  99. void set_bake_mode(BakeMode p_bake_mode);
  100. BakeMode get_bake_mode() const;
  101. void set_enabled(bool p_enabled);
  102. bool is_enabled() const;
  103. void set_editor_only(bool p_editor_only);
  104. bool is_editor_only() const;
  105. virtual AABB get_aabb() const;
  106. virtual DVector<Face3> get_faces(uint32_t p_usage_flags) const;
  107. void approximate_opengl_attenuation(float p_constant, float p_linear, float p_quadratic, float p_radius_treshold=0.5);
  108. Light();
  109. ~Light();
  110. };
  111. VARIANT_ENUM_CAST( Light::Parameter );
  112. VARIANT_ENUM_CAST( Light::LightColor );
  113. VARIANT_ENUM_CAST( Light::Operator );
  114. VARIANT_ENUM_CAST( Light::BakeMode);
  115. class DirectionalLight : public Light {
  116. OBJ_TYPE( DirectionalLight, Light );
  117. public:
  118. enum ShadowMode {
  119. SHADOW_ORTHOGONAL,
  120. SHADOW_PERSPECTIVE,
  121. SHADOW_PARALLEL_2_SPLITS,
  122. SHADOW_PARALLEL_4_SPLITS
  123. };
  124. enum ShadowParam {
  125. SHADOW_PARAM_MAX_DISTANCE,
  126. SHADOW_PARAM_PSSM_SPLIT_WEIGHT,
  127. SHADOW_PARAM_PSSM_ZOFFSET_SCALE
  128. };
  129. private:
  130. ShadowMode shadow_mode;
  131. float shadow_param[3];
  132. protected:
  133. static void _bind_methods();
  134. public:
  135. void set_shadow_mode(ShadowMode p_mode);
  136. ShadowMode get_shadow_mode() const;
  137. void set_shadow_max_distance(float p_distance);
  138. float get_shadow_max_distance() const;
  139. void set_shadow_param(ShadowParam p_param, float p_value);
  140. float get_shadow_param(ShadowParam p_param) const;
  141. DirectionalLight();
  142. };
  143. VARIANT_ENUM_CAST( DirectionalLight::ShadowMode );
  144. VARIANT_ENUM_CAST( DirectionalLight::ShadowParam );
  145. class OmniLight : public Light {
  146. OBJ_TYPE( OmniLight, Light );
  147. protected:
  148. static void _bind_methods();
  149. public:
  150. OmniLight() : Light( VisualServer::LIGHT_OMNI ) { set_parameter(PARAM_SHADOW_Z_OFFSET,0.001);}
  151. };
  152. class SpotLight : public Light {
  153. OBJ_TYPE( SpotLight, Light );
  154. protected:
  155. static void _bind_methods();
  156. public:
  157. SpotLight() : Light( VisualServer::LIGHT_SPOT ) {}
  158. };
  159. #endif