light.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*************************************************************************/
  2. /* light.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "light.h"
  31. #include "engine.h"
  32. #include "project_settings.h"
  33. #include "scene/resources/surface_tool.h"
  34. bool Light::_can_gizmo_scale() const {
  35. return false;
  36. }
  37. void Light::set_param(Param p_param, float p_value) {
  38. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  39. param[p_param] = p_value;
  40. VS::get_singleton()->light_set_param(light, VS::LightParam(p_param), p_value);
  41. if (p_param == PARAM_SPOT_ANGLE || p_param == PARAM_RANGE) {
  42. update_gizmo();
  43. }
  44. }
  45. float Light::get_param(Param p_param) const {
  46. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  47. return param[p_param];
  48. }
  49. void Light::set_shadow(bool p_enable) {
  50. shadow = p_enable;
  51. VS::get_singleton()->light_set_shadow(light, p_enable);
  52. }
  53. bool Light::has_shadow() const {
  54. return shadow;
  55. }
  56. void Light::set_negative(bool p_enable) {
  57. negative = p_enable;
  58. VS::get_singleton()->light_set_negative(light, p_enable);
  59. }
  60. bool Light::is_negative() const {
  61. return negative;
  62. }
  63. void Light::set_cull_mask(uint32_t p_cull_mask) {
  64. cull_mask = p_cull_mask;
  65. VS::get_singleton()->light_set_cull_mask(light, p_cull_mask);
  66. }
  67. uint32_t Light::get_cull_mask() const {
  68. return cull_mask;
  69. }
  70. void Light::set_color(const Color &p_color) {
  71. color = p_color;
  72. VS::get_singleton()->light_set_color(light, p_color);
  73. }
  74. Color Light::get_color() const {
  75. return color;
  76. }
  77. void Light::set_shadow_color(const Color &p_shadow_color) {
  78. shadow_color = p_shadow_color;
  79. VS::get_singleton()->light_set_shadow_color(light, p_shadow_color);
  80. }
  81. Color Light::get_shadow_color() const {
  82. return shadow_color;
  83. }
  84. void Light::set_shadow_reverse_cull_face(bool p_enable) {
  85. reverse_cull = p_enable;
  86. VS::get_singleton()->light_set_reverse_cull_face_mode(light, reverse_cull);
  87. }
  88. bool Light::get_shadow_reverse_cull_face() const {
  89. return reverse_cull;
  90. }
  91. Rect3 Light::get_aabb() const {
  92. if (type == VisualServer::LIGHT_DIRECTIONAL) {
  93. return Rect3(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  94. } else if (type == VisualServer::LIGHT_OMNI) {
  95. return Rect3(Vector3(-1, -1, -1) * param[PARAM_RANGE], Vector3(2, 2, 2) * param[PARAM_RANGE]);
  96. } else if (type == VisualServer::LIGHT_SPOT) {
  97. float len = param[PARAM_RANGE];
  98. float size = Math::tan(Math::deg2rad(param[PARAM_SPOT_ANGLE])) * len;
  99. return Rect3(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  100. }
  101. return Rect3();
  102. }
  103. PoolVector<Face3> Light::get_faces(uint32_t p_usage_flags) const {
  104. return PoolVector<Face3>();
  105. }
  106. void Light::_update_visibility() {
  107. if (!is_inside_tree())
  108. return;
  109. bool editor_ok = true;
  110. #ifdef TOOLS_ENABLED
  111. if (editor_only) {
  112. if (!Engine::get_singleton()->is_editor_hint()) {
  113. editor_ok = false;
  114. } else {
  115. editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
  116. }
  117. }
  118. #else
  119. if (editor_only) {
  120. editor_ok = false;
  121. }
  122. #endif
  123. //VS::get_singleton()->instance_light_set_enabled(get_instance(),is_visible_in_tree() && editor_ok);
  124. _change_notify("geometry/visible");
  125. }
  126. void Light::_notification(int p_what) {
  127. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  128. _update_visibility();
  129. }
  130. if (p_what == NOTIFICATION_ENTER_TREE) {
  131. _update_visibility();
  132. }
  133. if (p_what == NOTIFICATION_EXIT_TREE) {
  134. }
  135. }
  136. void Light::set_editor_only(bool p_editor_only) {
  137. editor_only = p_editor_only;
  138. _update_visibility();
  139. }
  140. bool Light::is_editor_only() const {
  141. return editor_only;
  142. }
  143. void Light::_bind_methods() {
  144. ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light::set_editor_only);
  145. ClassDB::bind_method(D_METHOD("is_editor_only"), &Light::is_editor_only);
  146. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &Light::set_param);
  147. ClassDB::bind_method(D_METHOD("get_param", "param"), &Light::get_param);
  148. ClassDB::bind_method(D_METHOD("set_shadow", "enabled"), &Light::set_shadow);
  149. ClassDB::bind_method(D_METHOD("has_shadow"), &Light::has_shadow);
  150. ClassDB::bind_method(D_METHOD("set_negative", "enabled"), &Light::set_negative);
  151. ClassDB::bind_method(D_METHOD("is_negative"), &Light::is_negative);
  152. ClassDB::bind_method(D_METHOD("set_cull_mask", "cull_mask"), &Light::set_cull_mask);
  153. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Light::get_cull_mask);
  154. ClassDB::bind_method(D_METHOD("set_color", "color"), &Light::set_color);
  155. ClassDB::bind_method(D_METHOD("get_color"), &Light::get_color);
  156. ClassDB::bind_method(D_METHOD("set_shadow_reverse_cull_face", "enable"), &Light::set_shadow_reverse_cull_face);
  157. ClassDB::bind_method(D_METHOD("get_shadow_reverse_cull_face"), &Light::get_shadow_reverse_cull_face);
  158. ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light::set_shadow_color);
  159. ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light::get_shadow_color);
  160. ADD_GROUP("Light", "light_");
  161. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_color", "get_color");
  162. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "light_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_ENERGY);
  163. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "light_negative"), "set_negative", "is_negative");
  164. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "light_specular", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_SPECULAR);
  165. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  166. ADD_GROUP("Shadow", "shadow_");
  167. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow", "has_shadow");
  168. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_shadow_color", "get_shadow_color");
  169. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "shadow_bias", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_param", "get_param", PARAM_SHADOW_BIAS);
  170. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "shadow_contact", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_CONTACT_SHADOW_SIZE);
  171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_reverse_cull_face"), "set_shadow_reverse_cull_face", "get_shadow_reverse_cull_face");
  172. ADD_GROUP("Editor", "");
  173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
  174. ADD_GROUP("", "");
  175. BIND_ENUM_CONSTANT(PARAM_ENERGY);
  176. BIND_ENUM_CONSTANT(PARAM_SPECULAR);
  177. BIND_ENUM_CONSTANT(PARAM_RANGE);
  178. BIND_ENUM_CONSTANT(PARAM_ATTENUATION);
  179. BIND_ENUM_CONSTANT(PARAM_SPOT_ANGLE);
  180. BIND_ENUM_CONSTANT(PARAM_SPOT_ATTENUATION);
  181. BIND_ENUM_CONSTANT(PARAM_CONTACT_SHADOW_SIZE);
  182. BIND_ENUM_CONSTANT(PARAM_SHADOW_MAX_DISTANCE);
  183. BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_1_OFFSET);
  184. BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_2_OFFSET);
  185. BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_3_OFFSET);
  186. BIND_ENUM_CONSTANT(PARAM_SHADOW_NORMAL_BIAS);
  187. BIND_ENUM_CONSTANT(PARAM_SHADOW_BIAS);
  188. BIND_ENUM_CONSTANT(PARAM_SHADOW_BIAS_SPLIT_SCALE);
  189. BIND_ENUM_CONSTANT(PARAM_MAX);
  190. }
  191. Light::Light(VisualServer::LightType p_type) {
  192. type = p_type;
  193. light = VisualServer::get_singleton()->light_create(p_type);
  194. VS::get_singleton()->instance_set_base(get_instance(), light);
  195. reverse_cull = false;
  196. editor_only = false;
  197. set_color(Color(1, 1, 1, 1));
  198. set_shadow(false);
  199. set_negative(false);
  200. set_cull_mask(0xFFFFFFFF);
  201. set_param(PARAM_ENERGY, 1);
  202. set_param(PARAM_SPECULAR, 0.5);
  203. set_param(PARAM_RANGE, 5);
  204. set_param(PARAM_ATTENUATION, 1);
  205. set_param(PARAM_SPOT_ANGLE, 45);
  206. set_param(PARAM_SPOT_ATTENUATION, 1);
  207. set_param(PARAM_CONTACT_SHADOW_SIZE, 0);
  208. set_param(PARAM_SHADOW_MAX_DISTANCE, 0);
  209. set_param(PARAM_SHADOW_SPLIT_1_OFFSET, 0.1);
  210. set_param(PARAM_SHADOW_SPLIT_2_OFFSET, 0.2);
  211. set_param(PARAM_SHADOW_SPLIT_3_OFFSET, 0.5);
  212. set_param(PARAM_SHADOW_NORMAL_BIAS, 0.0);
  213. set_param(PARAM_SHADOW_BIAS, 0.15);
  214. }
  215. Light::Light() {
  216. type = VisualServer::LIGHT_DIRECTIONAL;
  217. ERR_PRINT("Light shouldn't be instanced dircetly, use the subtypes.");
  218. }
  219. Light::~Light() {
  220. VS::get_singleton()->instance_set_base(get_instance(), RID());
  221. if (light.is_valid())
  222. VisualServer::get_singleton()->free(light);
  223. }
  224. /////////////////////////////////////////
  225. void DirectionalLight::set_shadow_mode(ShadowMode p_mode) {
  226. shadow_mode = p_mode;
  227. VS::get_singleton()->light_directional_set_shadow_mode(light, VS::LightDirectionalShadowMode(p_mode));
  228. }
  229. DirectionalLight::ShadowMode DirectionalLight::get_shadow_mode() const {
  230. return shadow_mode;
  231. }
  232. void DirectionalLight::set_shadow_depth_range(ShadowDepthRange p_range) {
  233. shadow_depth_range = p_range;
  234. VS::get_singleton()->light_directional_set_shadow_depth_range_mode(light, VS::LightDirectionalShadowDepthRangeMode(p_range));
  235. }
  236. DirectionalLight::ShadowDepthRange DirectionalLight::get_shadow_depth_range() const {
  237. return shadow_depth_range;
  238. }
  239. void DirectionalLight::set_blend_splits(bool p_enable) {
  240. blend_splits = p_enable;
  241. VS::get_singleton()->light_directional_set_blend_splits(light, p_enable);
  242. }
  243. bool DirectionalLight::is_blend_splits_enabled() const {
  244. return blend_splits;
  245. }
  246. void DirectionalLight::_bind_methods() {
  247. ClassDB::bind_method(D_METHOD("set_shadow_mode", "mode"), &DirectionalLight::set_shadow_mode);
  248. ClassDB::bind_method(D_METHOD("get_shadow_mode"), &DirectionalLight::get_shadow_mode);
  249. ClassDB::bind_method(D_METHOD("set_shadow_depth_range", "mode"), &DirectionalLight::set_shadow_depth_range);
  250. ClassDB::bind_method(D_METHOD("get_shadow_depth_range"), &DirectionalLight::get_shadow_depth_range);
  251. ClassDB::bind_method(D_METHOD("set_blend_splits", "enabled"), &DirectionalLight::set_blend_splits);
  252. ClassDB::bind_method(D_METHOD("is_blend_splits_enabled"), &DirectionalLight::is_blend_splits_enabled);
  253. ADD_GROUP("Directional Shadow", "directional_shadow_");
  254. ADD_PROPERTY(PropertyInfo(Variant::INT, "directional_shadow_mode", PROPERTY_HINT_ENUM, "Orthogonal,PSSM 2 Splits,PSSM 4 Splits"), "set_shadow_mode", "get_shadow_mode");
  255. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_1", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_1_OFFSET);
  256. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_2", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_2_OFFSET);
  257. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_3", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_3_OFFSET);
  258. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional_shadow_blend_splits"), "set_blend_splits", "is_blend_splits_enabled");
  259. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_normal_bias", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_SHADOW_NORMAL_BIAS);
  260. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_bias_split_scale", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_SHADOW_BIAS_SPLIT_SCALE);
  261. ADD_PROPERTY(PropertyInfo(Variant::INT, "directional_shadow_depth_range", PROPERTY_HINT_ENUM, "Stable,Optimized"), "set_shadow_depth_range", "get_shadow_depth_range");
  262. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_max_distance", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_SHADOW_MAX_DISTANCE);
  263. BIND_ENUM_CONSTANT(SHADOW_ORTHOGONAL);
  264. BIND_ENUM_CONSTANT(SHADOW_PARALLEL_2_SPLITS);
  265. BIND_ENUM_CONSTANT(SHADOW_PARALLEL_4_SPLITS);
  266. BIND_ENUM_CONSTANT(SHADOW_DEPTH_RANGE_STABLE);
  267. BIND_ENUM_CONSTANT(SHADOW_DEPTH_RANGE_OPTIMIZED);
  268. }
  269. DirectionalLight::DirectionalLight()
  270. : Light(VisualServer::LIGHT_DIRECTIONAL) {
  271. set_param(PARAM_SHADOW_NORMAL_BIAS, 0.8);
  272. set_param(PARAM_SHADOW_BIAS, 0.1);
  273. set_param(PARAM_SHADOW_MAX_DISTANCE, 200);
  274. set_param(PARAM_SHADOW_BIAS_SPLIT_SCALE, 0.25);
  275. set_shadow_mode(SHADOW_PARALLEL_4_SPLITS);
  276. set_shadow_depth_range(SHADOW_DEPTH_RANGE_STABLE);
  277. blend_splits = false;
  278. }
  279. void OmniLight::set_shadow_mode(ShadowMode p_mode) {
  280. shadow_mode = p_mode;
  281. VS::get_singleton()->light_omni_set_shadow_mode(light, VS::LightOmniShadowMode(p_mode));
  282. }
  283. OmniLight::ShadowMode OmniLight::get_shadow_mode() const {
  284. return shadow_mode;
  285. }
  286. void OmniLight::set_shadow_detail(ShadowDetail p_detail) {
  287. shadow_detail = p_detail;
  288. VS::get_singleton()->light_omni_set_shadow_detail(light, VS::LightOmniShadowDetail(p_detail));
  289. }
  290. OmniLight::ShadowDetail OmniLight::get_shadow_detail() const {
  291. return shadow_detail;
  292. }
  293. void OmniLight::_bind_methods() {
  294. ClassDB::bind_method(D_METHOD("set_shadow_mode", "mode"), &OmniLight::set_shadow_mode);
  295. ClassDB::bind_method(D_METHOD("get_shadow_mode"), &OmniLight::get_shadow_mode);
  296. ClassDB::bind_method(D_METHOD("set_shadow_detail", "detail"), &OmniLight::set_shadow_detail);
  297. ClassDB::bind_method(D_METHOD("get_shadow_detail"), &OmniLight::get_shadow_detail);
  298. ADD_GROUP("Omni", "omni_");
  299. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "omni_range", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_RANGE);
  300. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "omni_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_ATTENUATION);
  301. ADD_PROPERTY(PropertyInfo(Variant::INT, "omni_shadow_mode", PROPERTY_HINT_ENUM, "Dual Paraboloid,Cube"), "set_shadow_mode", "get_shadow_mode");
  302. ADD_PROPERTY(PropertyInfo(Variant::INT, "omni_shadow_detail", PROPERTY_HINT_ENUM, "Vertical,Horizontal"), "set_shadow_detail", "get_shadow_detail");
  303. BIND_ENUM_CONSTANT(SHADOW_DUAL_PARABOLOID);
  304. BIND_ENUM_CONSTANT(SHADOW_CUBE);
  305. BIND_ENUM_CONSTANT(SHADOW_DETAIL_VERTICAL);
  306. BIND_ENUM_CONSTANT(SHADOW_DETAIL_HORIZONTAL);
  307. }
  308. OmniLight::OmniLight()
  309. : Light(VisualServer::LIGHT_OMNI) {
  310. set_shadow_mode(SHADOW_CUBE);
  311. set_shadow_detail(SHADOW_DETAIL_HORIZONTAL);
  312. }
  313. void SpotLight::_bind_methods() {
  314. ADD_GROUP("Spot", "spot_");
  315. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_range", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_RANGE);
  316. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_ATTENUATION);
  317. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_angle", PROPERTY_HINT_RANGE, "0,180,0.1"), "set_param", "get_param", PARAM_SPOT_ANGLE);
  318. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_angle_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_SPOT_ATTENUATION);
  319. }