gltf_light.cpp 9.4 KB

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