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