gltf_light.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*************************************************************************/
  2. /* gltf_light.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "gltf_light.h"
  31. void GLTFLight::_bind_methods() {
  32. ClassDB::bind_static_method("GLTFLight", D_METHOD("from_node", "light_node"), &GLTFLight::from_node);
  33. ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node);
  34. ClassDB::bind_static_method("GLTFLight", D_METHOD("from_dictionary", "dictionary"), &GLTFLight::from_dictionary);
  35. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary);
  36. ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color);
  37. ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);
  38. ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity);
  39. ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &GLTFLight::set_intensity);
  40. ClassDB::bind_method(D_METHOD("get_light_type"), &GLTFLight::get_light_type);
  41. ClassDB::bind_method(D_METHOD("set_light_type", "light_type"), &GLTFLight::set_light_type);
  42. ClassDB::bind_method(D_METHOD("get_range"), &GLTFLight::get_range);
  43. ClassDB::bind_method(D_METHOD("set_range", "range"), &GLTFLight::set_range);
  44. ClassDB::bind_method(D_METHOD("get_inner_cone_angle"), &GLTFLight::get_inner_cone_angle);
  45. ClassDB::bind_method(D_METHOD("set_inner_cone_angle", "inner_cone_angle"), &GLTFLight::set_inner_cone_angle);
  46. ClassDB::bind_method(D_METHOD("get_outer_cone_angle"), &GLTFLight::get_outer_cone_angle);
  47. ClassDB::bind_method(D_METHOD("set_outer_cone_angle", "outer_cone_angle"), &GLTFLight::set_outer_cone_angle);
  48. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); // Color
  49. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity"), "set_intensity", "get_intensity"); // float
  50. ADD_PROPERTY(PropertyInfo(Variant::STRING, "light_type"), "set_light_type", "get_light_type"); // String
  51. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range"), "set_range", "get_range"); // float
  52. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_cone_angle"), "set_inner_cone_angle", "get_inner_cone_angle"); // float
  53. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_cone_angle"), "set_outer_cone_angle", "get_outer_cone_angle"); // float
  54. }
  55. Color GLTFLight::get_color() {
  56. return color;
  57. }
  58. void GLTFLight::set_color(Color p_color) {
  59. color = p_color;
  60. }
  61. float GLTFLight::get_intensity() {
  62. return intensity;
  63. }
  64. void GLTFLight::set_intensity(float p_intensity) {
  65. intensity = p_intensity;
  66. }
  67. String GLTFLight::get_light_type() {
  68. return light_type;
  69. }
  70. void GLTFLight::set_light_type(String p_light_type) {
  71. light_type = p_light_type;
  72. }
  73. float GLTFLight::get_range() {
  74. return range;
  75. }
  76. void GLTFLight::set_range(float p_range) {
  77. range = p_range;
  78. }
  79. float GLTFLight::get_inner_cone_angle() {
  80. return inner_cone_angle;
  81. }
  82. void GLTFLight::set_inner_cone_angle(float p_inner_cone_angle) {
  83. inner_cone_angle = p_inner_cone_angle;
  84. }
  85. float GLTFLight::get_outer_cone_angle() {
  86. return outer_cone_angle;
  87. }
  88. void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) {
  89. outer_cone_angle = p_outer_cone_angle;
  90. }
  91. Ref<GLTFLight> GLTFLight::from_node(const Light3D *p_light) {
  92. Ref<GLTFLight> l;
  93. l.instantiate();
  94. l->color = p_light->get_color();
  95. if (cast_to<DirectionalLight3D>(p_light)) {
  96. l->light_type = "directional";
  97. const DirectionalLight3D *light = cast_to<const DirectionalLight3D>(p_light);
  98. l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY);
  99. l->range = FLT_MAX; // Range for directional lights is infinite in Godot.
  100. } else if (cast_to<const OmniLight3D>(p_light)) {
  101. l->light_type = "point";
  102. const OmniLight3D *light = cast_to<const OmniLight3D>(p_light);
  103. l->range = light->get_param(OmniLight3D::PARAM_RANGE);
  104. l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY);
  105. } else if (cast_to<const SpotLight3D>(p_light)) {
  106. l->light_type = "spot";
  107. const SpotLight3D *light = cast_to<const SpotLight3D>(p_light);
  108. l->range = light->get_param(SpotLight3D::PARAM_RANGE);
  109. l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY);
  110. l->outer_cone_angle = Math::deg_to_rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE));
  111. // This equation is the inverse of the import equation (which has a desmos link).
  112. float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION)));
  113. angle_ratio = MAX(0, angle_ratio);
  114. l->inner_cone_angle = l->outer_cone_angle * angle_ratio;
  115. }
  116. return l;
  117. }
  118. Light3D *GLTFLight::to_node() const {
  119. if (light_type == "directional") {
  120. DirectionalLight3D *light = memnew(DirectionalLight3D);
  121. light->set_param(Light3D::PARAM_ENERGY, intensity);
  122. light->set_color(color);
  123. return light;
  124. }
  125. const float range = CLAMP(this->range, 0, 4096);
  126. if (light_type == "point") {
  127. OmniLight3D *light = memnew(OmniLight3D);
  128. light->set_param(OmniLight3D::PARAM_ENERGY, intensity);
  129. light->set_param(OmniLight3D::PARAM_RANGE, range);
  130. light->set_color(color);
  131. return light;
  132. }
  133. if (light_type == "spot") {
  134. SpotLight3D *light = memnew(SpotLight3D);
  135. light->set_param(SpotLight3D::PARAM_ENERGY, intensity);
  136. light->set_param(SpotLight3D::PARAM_RANGE, range);
  137. light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad_to_deg(outer_cone_angle));
  138. light->set_color(color);
  139. // Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
  140. // The points in desmos are not exact, except for (1, infinity).
  141. float angle_ratio = inner_cone_angle / outer_cone_angle;
  142. float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
  143. light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation);
  144. return light;
  145. }
  146. return memnew(Light3D);
  147. }
  148. Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
  149. ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse GLTF light, missing required field 'type'.");
  150. Ref<GLTFLight> light;
  151. light.instantiate();
  152. const String &type = p_dictionary["type"];
  153. light->light_type = type;
  154. if (p_dictionary.has("color")) {
  155. const Array &arr = p_dictionary["color"];
  156. if (arr.size() == 3) {
  157. light->color = Color(arr[0], arr[1], arr[2]).linear_to_srgb();
  158. } else {
  159. ERR_PRINT("Error parsing GLTF light: The color must have exactly 3 numbers.");
  160. }
  161. }
  162. if (p_dictionary.has("intensity")) {
  163. light->intensity = p_dictionary["intensity"];
  164. }
  165. if (p_dictionary.has("range")) {
  166. light->range = p_dictionary["range"];
  167. }
  168. if (type == "spot") {
  169. const Dictionary &spot = p_dictionary["spot"];
  170. light->inner_cone_angle = spot["innerConeAngle"];
  171. light->outer_cone_angle = spot["outerConeAngle"];
  172. if (light->inner_cone_angle >= light->outer_cone_angle) {
  173. ERR_PRINT("Error parsing GLTF light: The inner angle must be smaller than the outer angle.");
  174. }
  175. } else if (type != "point" && type != "directional") {
  176. ERR_PRINT("Error parsing GLTF light: Light type '" + type + "' is unknown.");
  177. }
  178. return light;
  179. }
  180. Dictionary GLTFLight::to_dictionary() const {
  181. Dictionary d;
  182. Array color_array;
  183. color_array.resize(3);
  184. color_array[0] = color.r;
  185. color_array[1] = color.g;
  186. color_array[2] = color.b;
  187. d["color"] = color_array;
  188. d["type"] = light_type;
  189. if (light_type == "spot") {
  190. Dictionary spot_dict;
  191. spot_dict["innerConeAngle"] = inner_cone_angle;
  192. spot_dict["outerConeAngle"] = outer_cone_angle;
  193. d["spot"] = spot_dict;
  194. }
  195. d["intensity"] = intensity;
  196. d["range"] = range;
  197. return d;
  198. }