gltf_light.cpp 11 KB

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