Debian Codegraph @

modules/gltf/extensions/gltf_light.cpp

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