Path: blob/master/modules/gltf/extensions/gltf_light.cpp
10278 views
/**************************************************************************/1/* gltf_light.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gltf_light.h"3132#include "../structures/gltf_object_model_property.h"33#include "scene/3d/light_3d.h"3435void GLTFLight::_bind_methods() {36ClassDB::bind_static_method("GLTFLight", D_METHOD("from_node", "light_node"), &GLTFLight::from_node);37ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node);3839ClassDB::bind_static_method("GLTFLight", D_METHOD("from_dictionary", "dictionary"), &GLTFLight::from_dictionary);40ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary);4142ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color);43ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);44ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity);45ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &GLTFLight::set_intensity);46ClassDB::bind_method(D_METHOD("get_light_type"), &GLTFLight::get_light_type);47ClassDB::bind_method(D_METHOD("set_light_type", "light_type"), &GLTFLight::set_light_type);48ClassDB::bind_method(D_METHOD("get_range"), &GLTFLight::get_range);49ClassDB::bind_method(D_METHOD("set_range", "range"), &GLTFLight::set_range);50ClassDB::bind_method(D_METHOD("get_inner_cone_angle"), &GLTFLight::get_inner_cone_angle);51ClassDB::bind_method(D_METHOD("set_inner_cone_angle", "inner_cone_angle"), &GLTFLight::set_inner_cone_angle);52ClassDB::bind_method(D_METHOD("get_outer_cone_angle"), &GLTFLight::get_outer_cone_angle);53ClassDB::bind_method(D_METHOD("set_outer_cone_angle", "outer_cone_angle"), &GLTFLight::set_outer_cone_angle);54ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFLight::get_additional_data);55ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFLight::set_additional_data);5657ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); // Color58ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity"), "set_intensity", "get_intensity"); // float59ADD_PROPERTY(PropertyInfo(Variant::STRING, "light_type"), "set_light_type", "get_light_type"); // String60ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range"), "set_range", "get_range"); // float61ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_cone_angle"), "set_inner_cone_angle", "get_inner_cone_angle"); // float62ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_cone_angle"), "set_outer_cone_angle", "get_outer_cone_angle"); // float63}6465void GLTFLight::set_cone_inner_attenuation_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {66// Expression to convert glTF innerConeAngle to Godot spot_angle_attenuation.67Ref<Expression> gltf_to_godot_expr;68gltf_to_godot_expr.instantiate();69PackedStringArray gltf_to_godot_args = { "inner_cone_angle" };70gltf_to_godot_expr->parse("0.2 / (1.0 - inner_cone_angle / spot_angle) - 0.1", gltf_to_godot_args);71r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);72// Expression to convert Godot spot_angle_attenuation to glTF innerConeAngle.73Ref<Expression> godot_to_gltf_expr;74godot_to_gltf_expr.instantiate();75PackedStringArray godot_to_gltf_args = { "godot_spot_angle_att" };76godot_to_gltf_expr->parse("spot_angle * maxf(0.0, 1.0 - (0.2 / (0.1 + godot_spot_angle_att)))", godot_to_gltf_args);77r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);78}7980Color GLTFLight::get_color() {81return color;82}8384void GLTFLight::set_color(Color p_color) {85color = p_color;86}8788float GLTFLight::get_intensity() {89return intensity;90}9192void GLTFLight::set_intensity(float p_intensity) {93intensity = p_intensity;94}9596String GLTFLight::get_light_type() {97return light_type;98}99100void GLTFLight::set_light_type(String p_light_type) {101light_type = p_light_type;102}103104float GLTFLight::get_range() {105return range;106}107108void GLTFLight::set_range(float p_range) {109range = p_range;110}111112float GLTFLight::get_inner_cone_angle() {113return inner_cone_angle;114}115116void GLTFLight::set_inner_cone_angle(float p_inner_cone_angle) {117inner_cone_angle = p_inner_cone_angle;118}119120float GLTFLight::get_outer_cone_angle() {121return outer_cone_angle;122}123124void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) {125outer_cone_angle = p_outer_cone_angle;126}127128Ref<GLTFLight> GLTFLight::from_node(const Light3D *p_light) {129Ref<GLTFLight> l;130l.instantiate();131ERR_FAIL_NULL_V_MSG(p_light, l, "Tried to create a GLTFLight from a Light3D node, but the given node was null.");132l->color = p_light->get_color().srgb_to_linear();133if (cast_to<DirectionalLight3D>(p_light)) {134l->light_type = "directional";135const DirectionalLight3D *light = cast_to<const DirectionalLight3D>(p_light);136l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY);137l->range = FLT_MAX; // Range for directional lights is infinite in Godot.138} else if (cast_to<const OmniLight3D>(p_light)) {139l->light_type = "point";140const OmniLight3D *light = cast_to<const OmniLight3D>(p_light);141l->range = light->get_param(OmniLight3D::PARAM_RANGE);142l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY);143} else if (cast_to<const SpotLight3D>(p_light)) {144l->light_type = "spot";145const SpotLight3D *light = cast_to<const SpotLight3D>(p_light);146l->range = light->get_param(SpotLight3D::PARAM_RANGE);147l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY);148l->outer_cone_angle = Math::deg_to_rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE));149// This equation is the inverse of the import equation (which has a desmos link).150float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION)));151angle_ratio = MAX(0, angle_ratio);152l->inner_cone_angle = l->outer_cone_angle * angle_ratio;153}154return l;155}156157Light3D *GLTFLight::to_node() const {158Light3D *light = nullptr;159if (light_type == "directional") {160DirectionalLight3D *dir_light = memnew(DirectionalLight3D);161dir_light->set_param(Light3D::PARAM_ENERGY, intensity);162light = dir_light;163} else if (light_type == "point") {164OmniLight3D *omni_light = memnew(OmniLight3D);165omni_light->set_param(OmniLight3D::PARAM_ENERGY, intensity);166omni_light->set_param(OmniLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));167light = omni_light;168} else if (light_type == "spot") {169SpotLight3D *spot_light = memnew(SpotLight3D);170spot_light->set_param(SpotLight3D::PARAM_ENERGY, intensity);171spot_light->set_param(SpotLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));172spot_light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad_to_deg(outer_cone_angle));173// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b174// The points in desmos are not exact, except for (1, infinity).175float angle_ratio = inner_cone_angle / outer_cone_angle;176float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;177spot_light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation);178light = spot_light;179} else {180ERR_PRINT("Failed to create a Light3D node from GLTFLight, unknown light type '" + light_type + "'.");181return nullptr;182}183light->set_color(color.linear_to_srgb());184light->set_param(Light3D::PARAM_ATTENUATION, 2.0);185return light;186}187188Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {189ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse glTF light, missing required field 'type'.");190Ref<GLTFLight> light;191light.instantiate();192const String &type = p_dictionary["type"];193light->light_type = type;194195if (p_dictionary.has("color")) {196const Array &arr = p_dictionary["color"];197if (arr.size() == 3) {198light->color = Color(arr[0], arr[1], arr[2]);199} else {200ERR_PRINT("Error parsing glTF light: The color must have exactly 3 numbers.");201}202}203if (p_dictionary.has("intensity")) {204light->intensity = p_dictionary["intensity"];205}206if (p_dictionary.has("range")) {207light->range = p_dictionary["range"];208}209if (type == "spot") {210const Dictionary &spot = p_dictionary["spot"];211light->inner_cone_angle = spot["innerConeAngle"];212light->outer_cone_angle = spot["outerConeAngle"];213if (light->inner_cone_angle >= light->outer_cone_angle) {214ERR_PRINT("Error parsing glTF light: The inner angle must be smaller than the outer angle.");215}216} else if (type != "point" && type != "directional") {217ERR_PRINT("Error parsing glTF light: Light type '" + type + "' is unknown.");218}219return light;220}221222Dictionary GLTFLight::to_dictionary() const {223Dictionary d;224if (color != Color(1.0f, 1.0f, 1.0f)) {225Array color_array;226color_array.resize(3);227color_array[0] = color.r;228color_array[1] = color.g;229color_array[2] = color.b;230d["color"] = color_array;231}232if (intensity != 1.0f) {233d["intensity"] = intensity;234}235if (light_type != "directional" && range != Math::INF) {236d["range"] = range;237}238if (light_type == "spot") {239Dictionary spot_dict;240spot_dict["innerConeAngle"] = inner_cone_angle;241spot_dict["outerConeAngle"] = outer_cone_angle;242d["spot"] = spot_dict;243}244d["type"] = light_type;245return d;246}247248Variant GLTFLight::get_additional_data(const StringName &p_extension_name) {249return additional_data[p_extension_name];250}251252void GLTFLight::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {253additional_data[p_extension_name] = p_additional_data;254}255256257