diff options
Diffstat (limited to 'modules/gltf')
23 files changed, 469 insertions, 344 deletions
diff --git a/modules/gltf/doc_classes/GLTFCamera.xml b/modules/gltf/doc_classes/GLTFCamera.xml index 9b9eff6141..49efaa1564 100644 --- a/modules/gltf/doc_classes/GLTFCamera.xml +++ b/modules/gltf/doc_classes/GLTFCamera.xml @@ -1,19 +1,58 @@ <?xml version="1.0" encoding="UTF-8" ?> <class name="GLTFCamera" inherits="Resource" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> <brief_description> + Represents a GLTF camera. </brief_description> <description> + Represents a camera as defined by the base GLTF spec. </description> <tutorials> + <link title="GLTF camera detailed specification">https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-camera</link> + <link title="GLTF camera spec and example file">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md</link> </tutorials> + <methods> + <method name="from_dictionary" qualifiers="static"> + <return type="GLTFCamera" /> + <param index="0" name="dictionary" type="Dictionary" /> + <description> + Creates a new GLTFCamera instance by parsing the given [Dictionary]. + </description> + </method> + <method name="from_node" qualifiers="static"> + <return type="GLTFCamera" /> + <param index="0" name="camera_node" type="Camera3D" /> + <description> + Create a new GLTFCamera instance from the given Godot [Camera3D] node. + </description> + </method> + <method name="to_dictionary" qualifiers="const"> + <return type="Dictionary" /> + <description> + Serializes this GLTFCamera instance into a [Dictionary]. + </description> + </method> + <method name="to_node" qualifiers="const"> + <return type="Camera3D" /> + <description> + Converts this GLTFCamera instance into a Godot [Camera3D] node. + </description> + </method> + </methods> <members> <member name="depth_far" type="float" setter="set_depth_far" getter="get_depth_far" default="4000.0"> + The distance to the far culling boundary for this camera relative to its local Z axis, in meters. This maps to GLTF's [code]zfar[/code] property. </member> <member name="depth_near" type="float" setter="set_depth_near" getter="get_depth_near" default="0.05"> + The distance to the near culling boundary for this camera relative to its local Z axis, in meters. This maps to GLTF's [code]znear[/code] property. </member> - <member name="fov_size" type="float" setter="set_fov_size" getter="get_fov_size" default="75.0"> + <member name="fov" type="float" setter="set_fov" getter="get_fov" default="1.309"> + The FOV of the camera. This class and GLTF define the camera FOV in radians, while Godot uses degrees. This maps to GLTF's [code]yfov[/code] property. This value is only used for perspective cameras, when [member perspective] is true. </member> <member name="perspective" type="bool" setter="set_perspective" getter="get_perspective" default="true"> + Whether or not the camera is in perspective mode. If false, the camera is in orthographic/orthogonal mode. This maps to GLTF's camera [code]type[/code] property. See [member Camera3D.projection] and the GLTF spec for more information. + </member> + <member name="size_mag" type="float" setter="set_size_mag" getter="get_size_mag" default="0.5"> + The size of the camera. This class and GLTF define the camera size magnitude as a radius in meters, while Godot defines it as a diameter in meters. This maps to GLTF's [code]ymag[/code] property. This value is only used for orthographic/orthogonal cameras, when [member perspective] is false. </member> </members> </class> diff --git a/modules/gltf/doc_classes/GLTFLight.xml b/modules/gltf/doc_classes/GLTFLight.xml index db2dfb487a..7fd59e14bc 100644 --- a/modules/gltf/doc_classes/GLTFLight.xml +++ b/modules/gltf/doc_classes/GLTFLight.xml @@ -9,6 +9,34 @@ <tutorials> <link title="KHR_lights_punctual GLTF extension spec">https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual</link> </tutorials> + <methods> + <method name="from_dictionary" qualifiers="static"> + <return type="GLTFLight" /> + <param index="0" name="dictionary" type="Dictionary" /> + <description> + Creates a new GLTFLight instance by parsing the given [Dictionary]. + </description> + </method> + <method name="from_node" qualifiers="static"> + <return type="GLTFLight" /> + <param index="0" name="light_node" type="Light3D" /> + <description> + Create a new GLTFLight instance from the given Godot [Light3D] node. + </description> + </method> + <method name="to_dictionary" qualifiers="const"> + <return type="Dictionary" /> + <description> + Serializes this GLTFLight instance into a [Dictionary]. + </description> + </method> + <method name="to_node" qualifiers="const"> + <return type="Light3D" /> + <description> + Converts this GLTFLight instance into a Godot [Light3D] node. + </description> + </method> + </methods> <members> <member name="color" type="Color" setter="set_color" getter="get_color" default="Color(1, 1, 1, 1)"> The [Color] of the light. Defaults to white. A black color causes the light to have no effect. diff --git a/modules/gltf/doc_classes/GLTFMesh.xml b/modules/gltf/doc_classes/GLTFMesh.xml index bac351cc20..4d2df872ea 100644 --- a/modules/gltf/doc_classes/GLTFMesh.xml +++ b/modules/gltf/doc_classes/GLTFMesh.xml @@ -9,7 +9,7 @@ <members> <member name="blend_weights" type="PackedFloat32Array" setter="set_blend_weights" getter="get_blend_weights" default="PackedFloat32Array()"> </member> - <member name="instance_materials" type="Array" setter="set_instance_materials" getter="get_instance_materials" default="[]"> + <member name="instance_materials" type="Material[]" setter="set_instance_materials" getter="get_instance_materials" default="[]"> </member> <member name="mesh" type="ImporterMesh" setter="set_mesh" getter="get_mesh"> </member> diff --git a/modules/gltf/doc_classes/GLTFSkeleton.xml b/modules/gltf/doc_classes/GLTFSkeleton.xml index e1276d0e21..d6ec09f113 100644 --- a/modules/gltf/doc_classes/GLTFSkeleton.xml +++ b/modules/gltf/doc_classes/GLTFSkeleton.xml @@ -29,7 +29,7 @@ </description> </method> <method name="get_unique_names"> - <return type="Array" /> + <return type="String[]" /> <description> </description> </method> @@ -41,7 +41,7 @@ </method> <method name="set_unique_names"> <return type="void" /> - <param index="0" name="unique_names" type="Array" /> + <param index="0" name="unique_names" type="String[]" /> <description> </description> </method> diff --git a/modules/gltf/doc_classes/GLTFSkin.xml b/modules/gltf/doc_classes/GLTFSkin.xml index 5abdf33360..4de32857b5 100644 --- a/modules/gltf/doc_classes/GLTFSkin.xml +++ b/modules/gltf/doc_classes/GLTFSkin.xml @@ -8,7 +8,7 @@ </tutorials> <methods> <method name="get_inverse_binds"> - <return type="Array" /> + <return type="Transform3D[]" /> <description> </description> </method> @@ -24,7 +24,7 @@ </method> <method name="set_inverse_binds"> <return type="void" /> - <param index="0" name="inverse_binds" type="Array" /> + <param index="0" name="inverse_binds" type="Transform3D[]" /> <description> </description> </method> diff --git a/modules/gltf/doc_classes/GLTFState.xml b/modules/gltf/doc_classes/GLTFState.xml index adf51ab59e..1dbd89aed8 100644 --- a/modules/gltf/doc_classes/GLTFState.xml +++ b/modules/gltf/doc_classes/GLTFState.xml @@ -8,7 +8,7 @@ </tutorials> <methods> <method name="get_accessors"> - <return type="Array" /> + <return type="GLTFAccessor[]" /> <description> </description> </method> @@ -25,42 +25,42 @@ </description> </method> <method name="get_animations"> - <return type="Array" /> + <return type="GLTFAnimation[]" /> <description> </description> </method> <method name="get_buffer_views"> - <return type="Array" /> + <return type="GLTFBufferView[]" /> <description> </description> </method> <method name="get_cameras"> - <return type="Array" /> + <return type="GLTFCamera[]" /> <description> </description> </method> <method name="get_images"> - <return type="Array" /> + <return type="Texture2D[]" /> <description> </description> </method> <method name="get_lights"> - <return type="Array" /> + <return type="GLTFLight[]" /> <description> </description> </method> <method name="get_materials"> - <return type="Array" /> + <return type="BaseMaterial3D[]" /> <description> </description> </method> <method name="get_meshes"> - <return type="Array" /> + <return type="GLTFMesh[]" /> <description> </description> </method> <method name="get_nodes"> - <return type="Array" /> + <return type="GLTFNode[]" /> <description> </description> </method> @@ -76,81 +76,81 @@ </description> </method> <method name="get_skeletons"> - <return type="Array" /> + <return type="GLTFSkeleton[]" /> <description> </description> </method> <method name="get_skins"> - <return type="Array" /> + <return type="GLTFSkin[]" /> <description> </description> </method> <method name="get_textures"> - <return type="Array" /> + <return type="GLTFTexture[]" /> <description> </description> </method> <method name="get_unique_animation_names"> - <return type="Array" /> + <return type="String[]" /> <description> </description> </method> <method name="get_unique_names"> - <return type="Array" /> + <return type="String[]" /> <description> </description> </method> <method name="set_accessors"> <return type="void" /> - <param index="0" name="accessors" type="Array" /> + <param index="0" name="accessors" type="GLTFAccessor[]" /> <description> </description> </method> <method name="set_animations"> <return type="void" /> - <param index="0" name="animations" type="Array" /> + <param index="0" name="animations" type="GLTFAnimation[]" /> <description> </description> </method> <method name="set_buffer_views"> <return type="void" /> - <param index="0" name="buffer_views" type="Array" /> + <param index="0" name="buffer_views" type="GLTFBufferView[]" /> <description> </description> </method> <method name="set_cameras"> <return type="void" /> - <param index="0" name="cameras" type="Array" /> + <param index="0" name="cameras" type="GLTFCamera[]" /> <description> </description> </method> <method name="set_images"> <return type="void" /> - <param index="0" name="images" type="Array" /> + <param index="0" name="images" type="Texture2D[]" /> <description> </description> </method> <method name="set_lights"> <return type="void" /> - <param index="0" name="lights" type="Array" /> + <param index="0" name="lights" type="GLTFLight[]" /> <description> </description> </method> <method name="set_materials"> <return type="void" /> - <param index="0" name="materials" type="Array" /> + <param index="0" name="materials" type="BaseMaterial3D[]" /> <description> </description> </method> <method name="set_meshes"> <return type="void" /> - <param index="0" name="meshes" type="Array" /> + <param index="0" name="meshes" type="GLTFMesh[]" /> <description> </description> </method> <method name="set_nodes"> <return type="void" /> - <param index="0" name="nodes" type="Array" /> + <param index="0" name="nodes" type="GLTFNode[]" /> <description> </description> </method> @@ -162,31 +162,31 @@ </method> <method name="set_skeletons"> <return type="void" /> - <param index="0" name="skeletons" type="Array" /> + <param index="0" name="skeletons" type="GLTFSkeleton[]" /> <description> </description> </method> <method name="set_skins"> <return type="void" /> - <param index="0" name="skins" type="Array" /> + <param index="0" name="skins" type="GLTFSkin[]" /> <description> </description> </method> <method name="set_textures"> <return type="void" /> - <param index="0" name="textures" type="Array" /> + <param index="0" name="textures" type="GLTFTexture[]" /> <description> </description> </method> <method name="set_unique_animation_names"> <return type="void" /> - <param index="0" name="unique_animation_names" type="Array" /> + <param index="0" name="unique_animation_names" type="String[]" /> <description> </description> </method> <method name="set_unique_names"> <return type="void" /> - <param index="0" name="unique_names" type="Array" /> + <param index="0" name="unique_names" type="String[]" /> <description> </description> </method> @@ -194,7 +194,7 @@ <members> <member name="base_path" type="String" setter="set_base_path" getter="get_base_path" default=""""> </member> - <member name="buffers" type="Array" setter="set_buffers" getter="get_buffers" default="[]"> + <member name="buffers" type="PackedByteArray[]" setter="set_buffers" getter="get_buffers" default="[]"> </member> <member name="create_animations" type="bool" setter="set_create_animations" getter="get_create_animations" default="true"> </member> @@ -206,7 +206,7 @@ </member> <member name="minor_version" type="int" setter="set_minor_version" getter="get_minor_version" default="0"> </member> - <member name="root_nodes" type="Array" setter="set_root_nodes" getter="get_root_nodes" default="[]"> + <member name="root_nodes" type="PackedInt32Array" setter="set_root_nodes" getter="get_root_nodes" default="PackedInt32Array()"> </member> <member name="scene_name" type="String" setter="set_scene_name" getter="get_scene_name" default=""""> </member> diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp index 8002c185c7..ab52761e17 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.cpp +++ b/modules/gltf/editor/editor_scene_importer_blend.cpp @@ -64,7 +64,7 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ // Escape paths to be valid Python strings to embed in the script. const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape(); - const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file( + const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join( vformat("%s-%s.gltf", p_path.get_file().get_basename(), p_path.md5_text())); const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape(); @@ -193,9 +193,9 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ String blender_path = EDITOR_GET("filesystem/import/blender/blender3_path"); #ifdef WINDOWS_ENABLED - blender_path = blender_path.plus_file("blender.exe"); + blender_path = blender_path.path_join("blender.exe"); #else - blender_path = blender_path.plus_file("blender"); + blender_path = blender_path.path_join("blender"); #endif List<String> args; @@ -287,14 +287,14 @@ void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, Li static bool _test_blender_path(const String &p_path, String *r_err = nullptr) { String path = p_path; #ifdef WINDOWS_ENABLED - path = path.plus_file("blender.exe"); + path = path.path_join("blender.exe"); #else - path = path.plus_file("blender"); + path = path.path_join("blender"); #endif #if defined(MACOS_ENABLED) if (!FileAccess::exists(path)) { - path = path.plus_file("Blender"); + path = path.path_join("Blender"); } #endif @@ -451,7 +451,7 @@ bool EditorFileSystemImportFormatSupportQueryBlend::query() { configure_blender_dialog->set_ok_button_text(TTR("Confirm Path")); configure_blender_dialog->set_cancel_button_text(TTR("Disable '.blend' Import")); - configure_blender_dialog->get_cancel_button()->set_tooltip(TTR("Disables Blender '.blend' files import for this project. Can be re-enabled in Project Settings.")); + configure_blender_dialog->get_cancel_button()->set_tooltip_text(TTR("Disables Blender '.blend' files import for this project. Can be re-enabled in Project Settings.")); configure_blender_dialog->connect("confirmed", callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_path_confirmed)); browse_dialog = memnew(EditorFileDialog); @@ -485,7 +485,7 @@ bool EditorFileSystemImportFormatSupportQueryBlend::query() { bool found = false; for (const String &path : mdfind_paths) { - found = _autodetect_path(path.plus_file("Contents/MacOS")); + found = _autodetect_path(path.path_join("Contents/MacOS")); if (found) { break; } diff --git a/modules/gltf/editor/editor_scene_importer_fbx.cpp b/modules/gltf/editor/editor_scene_importer_fbx.cpp index faad2d315d..017a44cccf 100644 --- a/modules/gltf/editor/editor_scene_importer_fbx.cpp +++ b/modules/gltf/editor/editor_scene_importer_fbx.cpp @@ -57,7 +57,7 @@ Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t // enclosed in double quotes by OS::execute(), so we only need to escape those. // `c_escape_multiline()` seems to do this (escapes `\` and `"` only). const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape_multiline(); - const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file( + const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join( vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text())); const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape_multiline(); diff --git a/modules/gltf/extensions/gltf_light.cpp b/modules/gltf/extensions/gltf_light.cpp index af21a4e804..ab5a15c671 100644 --- a/modules/gltf/extensions/gltf_light.cpp +++ b/modules/gltf/extensions/gltf_light.cpp @@ -31,6 +31,12 @@ #include "gltf_light.h" void GLTFLight::_bind_methods() { + ClassDB::bind_static_method("GLTFLight", D_METHOD("from_node", "light_node"), &GLTFLight::from_node); + ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node); + + ClassDB::bind_static_method("GLTFLight", D_METHOD("from_dictionary", "dictionary"), &GLTFLight::from_dictionary); + ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary); + ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color); ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color); ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity); @@ -99,3 +105,116 @@ float GLTFLight::get_outer_cone_angle() { void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) { outer_cone_angle = p_outer_cone_angle; } + +Ref<GLTFLight> GLTFLight::from_node(const Light3D *p_light) { + Ref<GLTFLight> l; + l.instantiate(); + l->color = p_light->get_color(); + if (cast_to<DirectionalLight3D>(p_light)) { + l->light_type = "directional"; + const DirectionalLight3D *light = cast_to<const DirectionalLight3D>(p_light); + l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY); + l->range = FLT_MAX; // Range for directional lights is infinite in Godot. + } else if (cast_to<const OmniLight3D>(p_light)) { + l->light_type = "point"; + const OmniLight3D *light = cast_to<const OmniLight3D>(p_light); + l->range = light->get_param(OmniLight3D::PARAM_RANGE); + l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY); + } else if (cast_to<const SpotLight3D>(p_light)) { + l->light_type = "spot"; + const SpotLight3D *light = cast_to<const SpotLight3D>(p_light); + l->range = light->get_param(SpotLight3D::PARAM_RANGE); + l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY); + l->outer_cone_angle = Math::deg_to_rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE)); + // This equation is the inverse of the import equation (which has a desmos link). + float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION))); + angle_ratio = MAX(0, angle_ratio); + l->inner_cone_angle = l->outer_cone_angle * angle_ratio; + } + return l; +} + +Light3D *GLTFLight::to_node() const { + if (light_type == "directional") { + DirectionalLight3D *light = memnew(DirectionalLight3D); + light->set_param(Light3D::PARAM_ENERGY, intensity); + light->set_color(color); + return light; + } + const float range = CLAMP(this->range, 0, 4096); + if (light_type == "point") { + OmniLight3D *light = memnew(OmniLight3D); + light->set_param(OmniLight3D::PARAM_ENERGY, intensity); + light->set_param(OmniLight3D::PARAM_RANGE, range); + light->set_color(color); + return light; + } + if (light_type == "spot") { + SpotLight3D *light = memnew(SpotLight3D); + light->set_param(SpotLight3D::PARAM_ENERGY, intensity); + light->set_param(SpotLight3D::PARAM_RANGE, range); + light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad_to_deg(outer_cone_angle)); + light->set_color(color); + // Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b + // The points in desmos are not exact, except for (1, infinity). + float angle_ratio = inner_cone_angle / outer_cone_angle; + float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1; + light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation); + return light; + } + return memnew(Light3D); +} + +Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) { + ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse GLTF light, missing required field 'type'."); + Ref<GLTFLight> light; + light.instantiate(); + const String &type = p_dictionary["type"]; + light->light_type = type; + + if (p_dictionary.has("color")) { + const Array &arr = p_dictionary["color"]; + if (arr.size() == 3) { + light->color = Color(arr[0], arr[1], arr[2]).linear_to_srgb(); + } else { + ERR_PRINT("Error parsing GLTF light: The color must have exactly 3 numbers."); + } + } + if (p_dictionary.has("intensity")) { + light->intensity = p_dictionary["intensity"]; + } + if (p_dictionary.has("range")) { + light->range = p_dictionary["range"]; + } + if (type == "spot") { + const Dictionary &spot = p_dictionary["spot"]; + light->inner_cone_angle = spot["innerConeAngle"]; + light->outer_cone_angle = spot["outerConeAngle"]; + if (light->inner_cone_angle >= light->outer_cone_angle) { + ERR_PRINT("Error parsing GLTF light: The inner angle must be smaller than the outer angle."); + } + } else if (type != "point" && type != "directional") { + ERR_PRINT("Error parsing GLTF light: Light type '" + type + "' is unknown."); + } + return light; +} + +Dictionary GLTFLight::to_dictionary() const { + Dictionary d; + Array color_array; + color_array.resize(3); + color_array[0] = color.r; + color_array[1] = color.g; + color_array[2] = color.b; + d["color"] = color_array; + d["type"] = light_type; + if (light_type == "spot") { + Dictionary spot_dict; + spot_dict["innerConeAngle"] = inner_cone_angle; + spot_dict["outerConeAngle"] = outer_cone_angle; + d["spot"] = spot_dict; + } + d["intensity"] = intensity; + d["range"] = range; + return d; +} diff --git a/modules/gltf/extensions/gltf_light.h b/modules/gltf/extensions/gltf_light.h index f0765a1bbc..04980e144c 100644 --- a/modules/gltf/extensions/gltf_light.h +++ b/modules/gltf/extensions/gltf_light.h @@ -70,6 +70,12 @@ public: float get_outer_cone_angle(); void set_outer_cone_angle(float p_outer_cone_angle); + + static Ref<GLTFLight> from_node(const Light3D *p_light); + Light3D *to_node() const; + + static Ref<GLTFLight> from_dictionary(const Dictionary p_dictionary); + Dictionary to_dictionary() const; }; #endif // GLTF_LIGHT_H diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 6a6de40fbb..61af58bda7 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -50,7 +50,6 @@ #include "core/version.h" #include "drivers/png/png_driver_common.h" #include "scene/2d/node_2d.h" -#include "scene/3d/camera_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/multimesh_instance_3d.h" #include "scene/3d/node_3d.h" @@ -436,7 +435,7 @@ Error GLTFDocument::_serialize_nodes(Ref<GLTFState> state) { node["scale"] = _vec3_to_arr(n->scale); } - if (!n->position.is_equal_approx(Vector3())) { + if (!n->position.is_zero_approx()) { node["translation"] = _vec3_to_arr(n->position); } if (n->children.size()) { @@ -787,7 +786,7 @@ Error GLTFDocument::_parse_buffers(Ref<GLTFState> state, const String &p_base_pa } else { // Relative path to an external image file. ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER); uri = uri.uri_decode(); - uri = p_base_path.plus_file(uri).replace("\\", "/"); // Fix for Windows. + uri = p_base_path.path_join(uri).replace("\\", "/"); // Fix for Windows. buffer_data = FileAccess::get_file_as_array(uri); ERR_FAIL_COND_V_MSG(buffer.size() == 0, ERR_PARSE_ERROR, "glTF: Couldn't load binary file as an array: " + uri); } @@ -3040,8 +3039,8 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> state, const String &p_path if (!da->dir_exists(new_texture_dir)) { da->make_dir(new_texture_dir); } - image->save_png(new_texture_dir.plus_file(name)); - d["uri"] = texture_dir.plus_file(name).uri_encode(); + image->save_png(new_texture_dir.path_join(name)); + d["uri"] = texture_dir.path_join(name).uri_encode(); } images.push_back(d); } @@ -3119,7 +3118,7 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> state, const String &p_base_pat } else { // Relative path to an external image file. ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER); uri = uri.uri_decode(); - uri = p_base_path.plus_file(uri).replace("\\", "/"); // Fix for Windows. + uri = p_base_path.path_join(uri).replace("\\", "/"); // Fix for Windows. // ResourceLoader will rely on the file extension to use the relevant loader. // The spec says that if mimeType is defined, it should take precedence (e.g. // there could be a `.png` image which is actually JPEG), but there's no easy @@ -4535,28 +4534,7 @@ Error GLTFDocument::_serialize_lights(Ref<GLTFState> state) { } Array lights; for (GLTFLightIndex i = 0; i < state->lights.size(); i++) { - Dictionary d; - Ref<GLTFLight> light = state->lights[i]; - Array color; - color.resize(3); - color[0] = light->color.r; - color[1] = light->color.g; - color[2] = light->color.b; - d["color"] = color; - d["type"] = light->light_type; - if (light->light_type == "spot") { - Dictionary s; - float inner_cone_angle = light->inner_cone_angle; - s["innerConeAngle"] = inner_cone_angle; - float outer_cone_angle = light->outer_cone_angle; - s["outerConeAngle"] = outer_cone_angle; - d["spot"] = s; - } - float intensity = light->intensity; - d["intensity"] = intensity; - float range = light->range; - d["range"] = range; - lights.push_back(d); + lights.push_back(state->lights[i]->to_dictionary()); } Dictionary extensions; @@ -4578,28 +4556,7 @@ Error GLTFDocument::_serialize_cameras(Ref<GLTFState> state) { Array cameras; cameras.resize(state->cameras.size()); for (GLTFCameraIndex i = 0; i < state->cameras.size(); i++) { - Dictionary d; - - Ref<GLTFCamera> camera = state->cameras[i]; - - if (camera->get_perspective() == false) { - Dictionary og; - og["ymag"] = Math::deg2rad(camera->get_fov_size()); - og["xmag"] = Math::deg2rad(camera->get_fov_size()); - og["zfar"] = camera->get_depth_far(); - og["znear"] = camera->get_depth_near(); - d["orthographic"] = og; - d["type"] = "orthographic"; - } else if (camera->get_perspective()) { - Dictionary ppt; - // GLTF spec is in radians, Godot's camera is in degrees. - ppt["yfov"] = Math::deg2rad(camera->get_fov_size()); - ppt["zfar"] = camera->get_depth_far(); - ppt["znear"] = camera->get_depth_near(); - d["perspective"] = ppt; - d["type"] = "perspective"; - } - cameras[i] = d; + cameras[i] = state->cameras[i]->to_dictionary(); } if (!state->cameras.size()) { @@ -4629,35 +4586,10 @@ Error GLTFDocument::_parse_lights(Ref<GLTFState> state) { const Array &lights = lights_punctual["lights"]; for (GLTFLightIndex light_i = 0; light_i < lights.size(); light_i++) { - const Dictionary &d = lights[light_i]; - - Ref<GLTFLight> light; - light.instantiate(); - ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR); - const String &type = d["type"]; - light->light_type = type; - - if (d.has("color")) { - const Array &arr = d["color"]; - ERR_FAIL_COND_V(arr.size() != 3, ERR_PARSE_ERROR); - const Color c = Color(arr[0], arr[1], arr[2]).linear_to_srgb(); - light->color = c; - } - if (d.has("intensity")) { - light->intensity = d["intensity"]; - } - if (d.has("range")) { - light->range = d["range"]; + Ref<GLTFLight> light = GLTFLight::from_dictionary(lights[light_i]); + if (light.is_null()) { + return Error::ERR_PARSE_ERROR; } - if (type == "spot") { - const Dictionary &spot = d["spot"]; - light->inner_cone_angle = spot["innerConeAngle"]; - light->outer_cone_angle = spot["outerConeAngle"]; - ERR_CONTINUE_MSG(light->inner_cone_angle >= light->outer_cone_angle, "The inner angle must be smaller than the outer angle."); - } else if (type != "point" && type != "directional") { - ERR_CONTINUE_MSG(true, "Light type is unknown."); - } - state->lights.push_back(light); } @@ -4674,39 +4606,7 @@ Error GLTFDocument::_parse_cameras(Ref<GLTFState> state) { const Array cameras = state->json["cameras"]; for (GLTFCameraIndex i = 0; i < cameras.size(); i++) { - const Dictionary &d = cameras[i]; - - Ref<GLTFCamera> camera; - camera.instantiate(); - ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR); - const String &type = d["type"]; - if (type == "orthographic") { - camera->set_perspective(false); - if (d.has("orthographic")) { - const Dictionary &og = d["orthographic"]; - // GLTF spec is in radians, Godot's camera is in degrees. - camera->set_fov_size(Math::rad2deg(real_t(og["ymag"]))); - camera->set_depth_far(og["zfar"]); - camera->set_depth_near(og["znear"]); - } else { - camera->set_fov_size(10); - } - } else if (type == "perspective") { - camera->set_perspective(true); - if (d.has("perspective")) { - const Dictionary &ppt = d["perspective"]; - // GLTF spec is in radians, Godot's camera is in degrees. - camera->set_fov_size(Math::rad2deg(real_t(ppt["yfov"]))); - camera->set_depth_far(ppt["zfar"]); - camera->set_depth_near(ppt["znear"]); - } else { - camera->set_fov_size(10); - } - } else { - ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "Camera3D should be in 'orthographic' or 'perspective'"); - } - - state->cameras.push_back(camera); + state->cameras.push_back(GLTFCamera::from_dictionary(cameras[i])); } print_verbose("glTF: Total cameras: " + itos(state->cameras.size())); @@ -5107,7 +5007,7 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> state, MeshInst Ref<GLTFMesh> gltf_mesh; gltf_mesh.instantiate(); - Array instance_materials; + TypedArray<Material> instance_materials; for (int32_t surface_i = 0; surface_i < current_mesh->get_surface_count(); surface_i++) { Ref<Material> mat = current_mesh->get_surface_material(surface_i); if (p_mesh_instance->get_surface_override_material(surface_i).is_valid()) { @@ -5154,45 +5054,7 @@ Node3D *GLTFDocument::_generate_light(Ref<GLTFState> state, const GLTFNodeIndex print_verbose("glTF: Creating light for: " + gltf_node->get_name()); Ref<GLTFLight> l = state->lights[gltf_node->light]; - - float intensity = l->intensity; - if (intensity > 10) { - // GLTF spec has the default around 1, but Blender defaults lights to 100. - // The only sane way to handle this is to check where it came from and - // handle it accordingly. If it's over 10, it probably came from Blender. - intensity /= 100; - } - - if (l->light_type == "directional") { - DirectionalLight3D *light = memnew(DirectionalLight3D); - light->set_param(Light3D::PARAM_ENERGY, intensity); - light->set_color(l->color); - return light; - } - - const float range = CLAMP(l->range, 0, 4096); - if (l->light_type == "point") { - OmniLight3D *light = memnew(OmniLight3D); - light->set_param(OmniLight3D::PARAM_ENERGY, intensity); - light->set_param(OmniLight3D::PARAM_RANGE, range); - light->set_color(l->color); - return light; - } - if (l->light_type == "spot") { - SpotLight3D *light = memnew(SpotLight3D); - light->set_param(SpotLight3D::PARAM_ENERGY, intensity); - light->set_param(SpotLight3D::PARAM_RANGE, range); - light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad2deg(l->outer_cone_angle)); - light->set_color(l->color); - - // Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b - // The points in desmos are not exact, except for (1, infinity). - float angle_ratio = l->inner_cone_angle / l->outer_cone_angle; - float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1; - light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation); - return light; - } - return memnew(Node3D); + return l->to_node(); } Camera3D *GLTFDocument::_generate_camera(Ref<GLTFState> state, const GLTFNodeIndex node_index) { @@ -5200,31 +5062,16 @@ Camera3D *GLTFDocument::_generate_camera(Ref<GLTFState> state, const GLTFNodeInd ERR_FAIL_INDEX_V(gltf_node->camera, state->cameras.size(), nullptr); - Camera3D *camera = memnew(Camera3D); print_verbose("glTF: Creating camera for: " + gltf_node->get_name()); Ref<GLTFCamera> c = state->cameras[gltf_node->camera]; - if (c->get_perspective()) { - camera->set_perspective(c->get_fov_size(), c->get_depth_near(), c->get_depth_far()); - } else { - camera->set_orthogonal(c->get_fov_size(), c->get_depth_near(), c->get_depth_far()); - } - - return camera; + return c->to_node(); } GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> state, Camera3D *p_camera) { print_verbose("glTF: Converting camera: " + p_camera->get_name()); - Ref<GLTFCamera> c; - c.instantiate(); - - if (p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE) { - c->set_perspective(true); - } - c->set_fov_size(p_camera->get_fov()); - c->set_depth_far(p_camera->get_far()); - c->set_depth_near(p_camera->get_near()); + Ref<GLTFCamera> c = GLTFCamera::from_node(p_camera); GLTFCameraIndex camera_index = state->cameras.size(); state->cameras.push_back(c); return camera_index; @@ -5233,31 +5080,7 @@ GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> state, Camera3D *p_ GLTFLightIndex GLTFDocument::_convert_light(Ref<GLTFState> state, Light3D *p_light) { print_verbose("glTF: Converting light: " + p_light->get_name()); - Ref<GLTFLight> l; - l.instantiate(); - l->color = p_light->get_color(); - if (cast_to<DirectionalLight3D>(p_light)) { - l->light_type = "directional"; - DirectionalLight3D *light = cast_to<DirectionalLight3D>(p_light); - l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY); - l->range = FLT_MAX; // Range for directional lights is infinite in Godot. - } else if (cast_to<OmniLight3D>(p_light)) { - l->light_type = "point"; - OmniLight3D *light = cast_to<OmniLight3D>(p_light); - l->range = light->get_param(OmniLight3D::PARAM_RANGE); - l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY); - } else if (cast_to<SpotLight3D>(p_light)) { - l->light_type = "spot"; - SpotLight3D *light = cast_to<SpotLight3D>(p_light); - l->range = light->get_param(SpotLight3D::PARAM_RANGE); - l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY); - l->outer_cone_angle = Math::deg2rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE)); - - // This equation is the inverse of the import equation (which has a desmos link). - float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION))); - angle_ratio = MAX(0, angle_ratio); - l->inner_cone_angle = l->outer_cone_angle * angle_ratio; - } + Ref<GLTFLight> l = GLTFLight::from_node(p_light); GLTFLightIndex light_index = state->lights.size(); state->lights.push_back(l); diff --git a/modules/gltf/gltf_state.cpp b/modules/gltf/gltf_state.cpp index 8212e4c22f..85bac446cc 100644 --- a/modules/gltf/gltf_state.cpp +++ b/modules/gltf/gltf_state.cpp @@ -152,51 +152,51 @@ void GLTFState::set_use_named_skin_binds(bool p_use_named_skin_binds) { use_named_skin_binds = p_use_named_skin_binds; } -Array GLTFState::get_nodes() { +TypedArray<GLTFNode> GLTFState::get_nodes() { return GLTFTemplateConvert::to_array(nodes); } -void GLTFState::set_nodes(Array p_nodes) { +void GLTFState::set_nodes(TypedArray<GLTFNode> p_nodes) { GLTFTemplateConvert::set_from_array(nodes, p_nodes); } -Array GLTFState::get_buffers() { +TypedArray<PackedByteArray> GLTFState::get_buffers() { return GLTFTemplateConvert::to_array(buffers); } -void GLTFState::set_buffers(Array p_buffers) { +void GLTFState::set_buffers(TypedArray<PackedByteArray> p_buffers) { GLTFTemplateConvert::set_from_array(buffers, p_buffers); } -Array GLTFState::get_buffer_views() { +TypedArray<GLTFBufferView> GLTFState::get_buffer_views() { return GLTFTemplateConvert::to_array(buffer_views); } -void GLTFState::set_buffer_views(Array p_buffer_views) { +void GLTFState::set_buffer_views(TypedArray<GLTFBufferView> p_buffer_views) { GLTFTemplateConvert::set_from_array(buffer_views, p_buffer_views); } -Array GLTFState::get_accessors() { +TypedArray<GLTFAccessor> GLTFState::get_accessors() { return GLTFTemplateConvert::to_array(accessors); } -void GLTFState::set_accessors(Array p_accessors) { +void GLTFState::set_accessors(TypedArray<GLTFAccessor> p_accessors) { GLTFTemplateConvert::set_from_array(accessors, p_accessors); } -Array GLTFState::get_meshes() { +TypedArray<GLTFMesh> GLTFState::get_meshes() { return GLTFTemplateConvert::to_array(meshes); } -void GLTFState::set_meshes(Array p_meshes) { +void GLTFState::set_meshes(TypedArray<GLTFMesh> p_meshes) { GLTFTemplateConvert::set_from_array(meshes, p_meshes); } -Array GLTFState::get_materials() { +TypedArray<BaseMaterial3D> GLTFState::get_materials() { return GLTFTemplateConvert::to_array(materials); } -void GLTFState::set_materials(Array p_materials) { +void GLTFState::set_materials(TypedArray<BaseMaterial3D> p_materials) { GLTFTemplateConvert::set_from_array(materials, p_materials); } @@ -208,75 +208,75 @@ void GLTFState::set_scene_name(String p_scene_name) { scene_name = p_scene_name; } -Array GLTFState::get_root_nodes() { - return GLTFTemplateConvert::to_array(root_nodes); +PackedInt32Array GLTFState::get_root_nodes() { + return root_nodes; } -void GLTFState::set_root_nodes(Array p_root_nodes) { - GLTFTemplateConvert::set_from_array(root_nodes, p_root_nodes); +void GLTFState::set_root_nodes(PackedInt32Array p_root_nodes) { + root_nodes = p_root_nodes; } -Array GLTFState::get_textures() { +TypedArray<GLTFTexture> GLTFState::get_textures() { return GLTFTemplateConvert::to_array(textures); } -void GLTFState::set_textures(Array p_textures) { +void GLTFState::set_textures(TypedArray<GLTFTexture> p_textures) { GLTFTemplateConvert::set_from_array(textures, p_textures); } -Array GLTFState::get_images() { +TypedArray<Texture2D> GLTFState::get_images() { return GLTFTemplateConvert::to_array(images); } -void GLTFState::set_images(Array p_images) { +void GLTFState::set_images(TypedArray<Texture2D> p_images) { GLTFTemplateConvert::set_from_array(images, p_images); } -Array GLTFState::get_skins() { +TypedArray<GLTFSkin> GLTFState::get_skins() { return GLTFTemplateConvert::to_array(skins); } -void GLTFState::set_skins(Array p_skins) { +void GLTFState::set_skins(TypedArray<GLTFSkin> p_skins) { GLTFTemplateConvert::set_from_array(skins, p_skins); } -Array GLTFState::get_cameras() { +TypedArray<GLTFCamera> GLTFState::get_cameras() { return GLTFTemplateConvert::to_array(cameras); } -void GLTFState::set_cameras(Array p_cameras) { +void GLTFState::set_cameras(TypedArray<GLTFCamera> p_cameras) { GLTFTemplateConvert::set_from_array(cameras, p_cameras); } -Array GLTFState::get_lights() { +TypedArray<GLTFLight> GLTFState::get_lights() { return GLTFTemplateConvert::to_array(lights); } -void GLTFState::set_lights(Array p_lights) { +void GLTFState::set_lights(TypedArray<GLTFLight> p_lights) { GLTFTemplateConvert::set_from_array(lights, p_lights); } -Array GLTFState::get_unique_names() { +TypedArray<String> GLTFState::get_unique_names() { return GLTFTemplateConvert::to_array(unique_names); } -void GLTFState::set_unique_names(Array p_unique_names) { +void GLTFState::set_unique_names(TypedArray<String> p_unique_names) { GLTFTemplateConvert::set_from_array(unique_names, p_unique_names); } -Array GLTFState::get_unique_animation_names() { +TypedArray<String> GLTFState::get_unique_animation_names() { return GLTFTemplateConvert::to_array(unique_animation_names); } -void GLTFState::set_unique_animation_names(Array p_unique_animation_names) { +void GLTFState::set_unique_animation_names(TypedArray<String> p_unique_animation_names) { GLTFTemplateConvert::set_from_array(unique_animation_names, p_unique_animation_names); } -Array GLTFState::get_skeletons() { +TypedArray<GLTFSkeleton> GLTFState::get_skeletons() { return GLTFTemplateConvert::to_array(skeletons); } -void GLTFState::set_skeletons(Array p_skeletons) { +void GLTFState::set_skeletons(TypedArray<GLTFSkeleton> p_skeletons) { GLTFTemplateConvert::set_from_array(skeletons, p_skeletons); } @@ -296,11 +296,11 @@ void GLTFState::set_create_animations(bool p_create_animations) { create_animations = p_create_animations; } -Array GLTFState::get_animations() { +TypedArray<GLTFAnimation> GLTFState::get_animations() { return GLTFTemplateConvert::to_array(animations); } -void GLTFState::set_animations(Array p_animations) { +void GLTFState::set_animations(TypedArray<GLTFAnimation> p_animations) { GLTFTemplateConvert::set_from_array(animations, p_animations); } diff --git a/modules/gltf/gltf_state.h b/modules/gltf/gltf_state.h index c08132f874..6b2d1ca228 100644 --- a/modules/gltf/gltf_state.h +++ b/modules/gltf/gltf_state.h @@ -115,23 +115,23 @@ public: bool get_discard_meshes_and_materials(); void set_discard_meshes_and_materials(bool p_discard_meshes_and_materials); - Array get_nodes(); - void set_nodes(Array p_nodes); + TypedArray<GLTFNode> get_nodes(); + void set_nodes(TypedArray<GLTFNode> p_nodes); - Array get_buffers(); - void set_buffers(Array p_buffers); + TypedArray<PackedByteArray> get_buffers(); + void set_buffers(TypedArray<PackedByteArray> p_buffers); - Array get_buffer_views(); - void set_buffer_views(Array p_buffer_views); + TypedArray<GLTFBufferView> get_buffer_views(); + void set_buffer_views(TypedArray<GLTFBufferView> p_buffer_views); - Array get_accessors(); - void set_accessors(Array p_accessors); + TypedArray<GLTFAccessor> get_accessors(); + void set_accessors(TypedArray<GLTFAccessor> p_accessors); - Array get_meshes(); - void set_meshes(Array p_meshes); + TypedArray<GLTFMesh> get_meshes(); + void set_meshes(TypedArray<GLTFMesh> p_meshes); - Array get_materials(); - void set_materials(Array p_materials); + TypedArray<BaseMaterial3D> get_materials(); + void set_materials(TypedArray<BaseMaterial3D> p_materials); String get_scene_name(); void set_scene_name(String p_scene_name); @@ -139,32 +139,32 @@ public: String get_base_path(); void set_base_path(String p_base_path); - Array get_root_nodes(); - void set_root_nodes(Array p_root_nodes); + PackedInt32Array get_root_nodes(); + void set_root_nodes(PackedInt32Array p_root_nodes); - Array get_textures(); - void set_textures(Array p_textures); + TypedArray<GLTFTexture> get_textures(); + void set_textures(TypedArray<GLTFTexture> p_textures); - Array get_images(); - void set_images(Array p_images); + TypedArray<Texture2D> get_images(); + void set_images(TypedArray<Texture2D> p_images); - Array get_skins(); - void set_skins(Array p_skins); + TypedArray<GLTFSkin> get_skins(); + void set_skins(TypedArray<GLTFSkin> p_skins); - Array get_cameras(); - void set_cameras(Array p_cameras); + TypedArray<GLTFCamera> get_cameras(); + void set_cameras(TypedArray<GLTFCamera> p_cameras); - Array get_lights(); - void set_lights(Array p_lights); + TypedArray<GLTFLight> get_lights(); + void set_lights(TypedArray<GLTFLight> p_lights); - Array get_unique_names(); - void set_unique_names(Array p_unique_names); + TypedArray<String> get_unique_names(); + void set_unique_names(TypedArray<String> p_unique_names); - Array get_unique_animation_names(); - void set_unique_animation_names(Array p_unique_names); + TypedArray<String> get_unique_animation_names(); + void set_unique_animation_names(TypedArray<String> p_unique_names); - Array get_skeletons(); - void set_skeletons(Array p_skeletons); + TypedArray<GLTFSkeleton> get_skeletons(); + void set_skeletons(TypedArray<GLTFSkeleton> p_skeletons); Dictionary get_skeleton_to_node(); void set_skeleton_to_node(Dictionary p_skeleton_to_node); @@ -172,8 +172,8 @@ public: bool get_create_animations(); void set_create_animations(bool p_create_animations); - Array get_animations(); - void set_animations(Array p_animations); + TypedArray<GLTFAnimation> get_animations(); + void set_animations(TypedArray<GLTFAnimation> p_animations); Node *get_scene_node(GLTFNodeIndex idx); diff --git a/modules/gltf/gltf_template_convert.h b/modules/gltf/gltf_template_convert.h index c915d3deb0..8a4b595c9f 100644 --- a/modules/gltf/gltf_template_convert.h +++ b/modules/gltf/gltf_template_convert.h @@ -46,8 +46,8 @@ static Array to_array(const Vector<T> &p_inp) { } template <class T> -static Array to_array(const HashSet<T> &p_inp) { - Array ret; +static TypedArray<T> to_array(const HashSet<T> &p_inp) { + TypedArray<T> ret; typename HashSet<T>::Iterator elem = p_inp.begin(); while (elem) { ret.push_back(*elem); @@ -65,7 +65,7 @@ static void set_from_array(Vector<T> &r_out, const Array &p_inp) { } template <class T> -static void set_from_array(HashSet<T> &r_out, const Array &p_inp) { +static void set_from_array(HashSet<T> &r_out, const TypedArray<T> &p_inp) { r_out.clear(); for (int i = 0; i < p_inp.size(); i++) { r_out.insert(p_inp[i]); diff --git a/modules/gltf/register_types.cpp b/modules/gltf/register_types.cpp index 1e1204aa57..dbbccc9bcc 100644 --- a/modules/gltf/register_types.cpp +++ b/modules/gltf/register_types.cpp @@ -66,17 +66,24 @@ static void _editor_init() { bool blend_enabled = GLOBAL_GET("filesystem/import/blender/enabled"); // Defined here because EditorSettings doesn't exist in `register_gltf_types` yet. - EDITOR_DEF_RST("filesystem/import/blender/blender3_path", ""); + String blender3_path = EDITOR_DEF_RST("filesystem/import/blender/blender3_path", ""); EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "filesystem/import/blender/blender3_path", PROPERTY_HINT_GLOBAL_DIR)); if (blend_enabled) { - Ref<EditorSceneFormatImporterBlend> importer; - importer.instantiate(); - ResourceImporterScene::add_importer(importer); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + if (blender3_path.is_empty()) { + WARN_PRINT("Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported."); + } else if (!da->dir_exists(blender3_path)) { + WARN_PRINT("Blend file import is enabled, but the Blender path doesn't point to an accessible directory. Blend files will not be imported."); + } else { + Ref<EditorSceneFormatImporterBlend> importer; + importer.instantiate(); + ResourceImporterScene::add_importer(importer); - Ref<EditorFileSystemImportFormatSupportQueryBlend> blend_import_query; - blend_import_query.instantiate(); - EditorFileSystem::get_singleton()->add_import_format_support_query(blend_import_query); + Ref<EditorFileSystemImportFormatSupportQueryBlend> blend_import_query; + blend_import_query.instantiate(); + EditorFileSystem::get_singleton()->add_import_format_support_query(blend_import_query); + } } // FBX to glTF importer. @@ -89,9 +96,9 @@ static void _editor_init() { if (fbx_enabled) { Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); if (fbx2gltf_path.is_empty()) { - WARN_PRINT("FBX file import is enabled, but no FBX2glTF path is configured. FBX files will not be imported."); + WARN_PRINT("FBX file import is enabled in the project settings, but no FBX2glTF path is configured in the editor settings. FBX files will not be imported."); } else if (!da->file_exists(fbx2gltf_path)) { - WARN_PRINT("FBX file import is enabled, but the FBX2glTF path doesn't point to a valid FBX2glTF executable. FBX files will not be imported."); + WARN_PRINT("FBX file import is enabled, but the FBX2glTF path doesn't point to an accessible file. FBX files will not be imported."); } else { Ref<EditorSceneFormatImporterFBX> importer; importer.instantiate(); diff --git a/modules/gltf/structures/gltf_camera.cpp b/modules/gltf/structures/gltf_camera.cpp index f3ea6a1c4c..5069f39c4b 100644 --- a/modules/gltf/structures/gltf_camera.cpp +++ b/modules/gltf/structures/gltf_camera.cpp @@ -31,17 +31,101 @@ #include "gltf_camera.h" void GLTFCamera::_bind_methods() { + ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node); + ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node); + + ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary); + ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary); + ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective); ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective); - ClassDB::bind_method(D_METHOD("get_fov_size"), &GLTFCamera::get_fov_size); - ClassDB::bind_method(D_METHOD("set_fov_size", "fov_size"), &GLTFCamera::set_fov_size); + ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov); + ClassDB::bind_method(D_METHOD("set_fov", "fov"), &GLTFCamera::set_fov); + ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag); + ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag); ClassDB::bind_method(D_METHOD("get_depth_far"), &GLTFCamera::get_depth_far); ClassDB::bind_method(D_METHOD("set_depth_far", "zdepth_far"), &GLTFCamera::set_depth_far); ClassDB::bind_method(D_METHOD("get_depth_near"), &GLTFCamera::get_depth_near); ClassDB::bind_method(D_METHOD("set_depth_near", "zdepth_near"), &GLTFCamera::set_depth_near); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective"); // bool - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov_size"), "set_fov_size", "get_fov_size"); // float - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far"); // float - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near"); // float + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov"), "set_fov", "get_fov"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_mag"), "set_size_mag", "get_size_mag"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near"); +} + +Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) { + Ref<GLTFCamera> c; + c.instantiate(); + c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE); + // GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees. + c->set_fov(Math::deg_to_rad(p_camera->get_fov())); + // GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters. + c->set_size_mag(p_camera->get_size() * 0.5f); + c->set_depth_far(p_camera->get_far()); + c->set_depth_near(p_camera->get_near()); + return c; +} + +Camera3D *GLTFCamera::to_node() const { + Camera3D *camera = memnew(Camera3D); + camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL); + // GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees. + camera->set_fov(Math::rad_to_deg(fov)); + // GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters. + camera->set_size(size_mag * 2.0f); + camera->set_near(depth_near); + camera->set_far(depth_far); + return camera; +} + +Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary p_dictionary) { + ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse GLTF camera, missing required field 'type'."); + Ref<GLTFCamera> camera; + camera.instantiate(); + const String &type = p_dictionary["type"]; + if (type == "perspective") { + camera->set_perspective(true); + if (p_dictionary.has("perspective")) { + const Dictionary &persp = p_dictionary["perspective"]; + camera->set_fov(persp["yfov"]); + if (persp.has("zfar")) { + camera->set_depth_far(persp["zfar"]); + } + camera->set_depth_near(persp["znear"]); + } + } else if (type == "orthographic") { + camera->set_perspective(false); + if (p_dictionary.has("orthographic")) { + const Dictionary &ortho = p_dictionary["orthographic"]; + camera->set_size_mag(ortho["ymag"]); + camera->set_depth_far(ortho["zfar"]); + camera->set_depth_near(ortho["znear"]); + } + } else { + ERR_PRINT("Error parsing GLTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic."); + } + return camera; +} + +Dictionary GLTFCamera::to_dictionary() const { + Dictionary d; + if (perspective) { + Dictionary persp; + persp["yfov"] = fov; + persp["zfar"] = depth_far; + persp["znear"] = depth_near; + d["perspective"] = persp; + d["type"] = "perspective"; + } else { + Dictionary ortho; + ortho["ymag"] = size_mag; + ortho["xmag"] = size_mag; + ortho["zfar"] = depth_far; + ortho["znear"] = depth_near; + d["orthographic"] = ortho; + d["type"] = "orthographic"; + } + return d; } diff --git a/modules/gltf/structures/gltf_camera.h b/modules/gltf/structures/gltf_camera.h index b7df741825..50ae10e17a 100644 --- a/modules/gltf/structures/gltf_camera.h +++ b/modules/gltf/structures/gltf_camera.h @@ -32,15 +32,22 @@ #define GLTF_CAMERA_H #include "core/io/resource.h" +#include "scene/3d/camera_3d.h" + +// Reference and test file: +// https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md class GLTFCamera : public Resource { GDCLASS(GLTFCamera, Resource); private: + // GLTF has no default camera values, they should always be specified in + // the GLTF file. Here we default to Godot's default camera settings. bool perspective = true; - float fov_size = 75.0; - float depth_far = 4000.0; - float depth_near = 0.05; + real_t fov = Math::deg_to_rad(75.0); + real_t size_mag = 0.5; + real_t depth_far = 4000.0; + real_t depth_near = 0.05; protected: static void _bind_methods(); @@ -48,12 +55,20 @@ protected: public: bool get_perspective() const { return perspective; } void set_perspective(bool p_val) { perspective = p_val; } - float get_fov_size() const { return fov_size; } - void set_fov_size(float p_val) { fov_size = p_val; } - float get_depth_far() const { return depth_far; } - void set_depth_far(float p_val) { depth_far = p_val; } - float get_depth_near() const { return depth_near; } - void set_depth_near(float p_val) { depth_near = p_val; } + real_t get_fov() const { return fov; } + void set_fov(real_t p_val) { fov = p_val; } + real_t get_size_mag() const { return size_mag; } + void set_size_mag(real_t p_val) { size_mag = p_val; } + real_t get_depth_far() const { return depth_far; } + void set_depth_far(real_t p_val) { depth_far = p_val; } + real_t get_depth_near() const { return depth_near; } + void set_depth_near(real_t p_val) { depth_near = p_val; } + + static Ref<GLTFCamera> from_node(const Camera3D *p_light); + Camera3D *to_node() const; + + static Ref<GLTFCamera> from_dictionary(const Dictionary p_dictionary); + Dictionary to_dictionary() const; }; #endif // GLTF_CAMERA_H diff --git a/modules/gltf/structures/gltf_mesh.cpp b/modules/gltf/structures/gltf_mesh.cpp index 3add8304b1..3893f56626 100644 --- a/modules/gltf/structures/gltf_mesh.cpp +++ b/modules/gltf/structures/gltf_mesh.cpp @@ -53,11 +53,11 @@ void GLTFMesh::set_mesh(Ref<ImporterMesh> p_mesh) { mesh = p_mesh; } -Array GLTFMesh::get_instance_materials() { +TypedArray<Material> GLTFMesh::get_instance_materials() { return instance_materials; } -void GLTFMesh::set_instance_materials(Array p_instance_materials) { +void GLTFMesh::set_instance_materials(TypedArray<Material> p_instance_materials) { instance_materials = p_instance_materials; } diff --git a/modules/gltf/structures/gltf_mesh.h b/modules/gltf/structures/gltf_mesh.h index dc26120b48..2fa37fd727 100644 --- a/modules/gltf/structures/gltf_mesh.h +++ b/modules/gltf/structures/gltf_mesh.h @@ -42,7 +42,7 @@ class GLTFMesh : public Resource { private: Ref<ImporterMesh> mesh; Vector<float> blend_weights; - Array instance_materials; + TypedArray<Material> instance_materials; protected: static void _bind_methods(); @@ -52,8 +52,8 @@ public: void set_mesh(Ref<ImporterMesh> p_mesh); Vector<float> get_blend_weights(); void set_blend_weights(Vector<float> p_blend_weights); - Array get_instance_materials(); - void set_instance_materials(Array p_instance_materials); + TypedArray<Material> get_instance_materials(); + void set_instance_materials(TypedArray<Material> p_instance_materials); }; #endif // GLTF_MESH_H diff --git a/modules/gltf/structures/gltf_skeleton.cpp b/modules/gltf/structures/gltf_skeleton.cpp index 90a6b0f50f..0073357eda 100644 --- a/modules/gltf/structures/gltf_skeleton.cpp +++ b/modules/gltf/structures/gltf_skeleton.cpp @@ -72,11 +72,11 @@ Skeleton3D *GLTFSkeleton::get_godot_skeleton() { return godot_skeleton; } -Array GLTFSkeleton::get_unique_names() { +TypedArray<String> GLTFSkeleton::get_unique_names() { return GLTFTemplateConvert::to_array(unique_names); } -void GLTFSkeleton::set_unique_names(Array p_unique_names) { +void GLTFSkeleton::set_unique_names(TypedArray<String> p_unique_names) { GLTFTemplateConvert::set_from_array(unique_names, p_unique_names); } diff --git a/modules/gltf/structures/gltf_skeleton.h b/modules/gltf/structures/gltf_skeleton.h index db88623213..0f20b493b6 100644 --- a/modules/gltf/structures/gltf_skeleton.h +++ b/modules/gltf/structures/gltf_skeleton.h @@ -75,8 +75,8 @@ public: // this->godot_skeleton = p_godot_skeleton; // } - Array get_unique_names(); - void set_unique_names(Array p_unique_names); + TypedArray<String> get_unique_names(); + void set_unique_names(TypedArray<String> p_unique_names); //RBMap<int32_t, GLTFNodeIndex> get_godot_bone_node() { // return this->godot_bone_node; diff --git a/modules/gltf/structures/gltf_skin.cpp b/modules/gltf/structures/gltf_skin.cpp index 2e46ee3be2..9717a66048 100644 --- a/modules/gltf/structures/gltf_skin.cpp +++ b/modules/gltf/structures/gltf_skin.cpp @@ -31,6 +31,7 @@ #include "gltf_skin.h" #include "../gltf_template_convert.h" +#include "core/variant/typed_array.h" #include "scene/resources/skin.h" void GLTFSkin::_bind_methods() { @@ -83,11 +84,11 @@ void GLTFSkin::set_joints_original(Vector<GLTFNodeIndex> p_joints_original) { joints_original = p_joints_original; } -Array GLTFSkin::get_inverse_binds() { +TypedArray<Transform3D> GLTFSkin::get_inverse_binds() { return GLTFTemplateConvert::to_array(inverse_binds); } -void GLTFSkin::set_inverse_binds(Array p_inverse_binds) { +void GLTFSkin::set_inverse_binds(TypedArray<Transform3D> p_inverse_binds) { GLTFTemplateConvert::set_from_array(inverse_binds, p_inverse_binds); } diff --git a/modules/gltf/structures/gltf_skin.h b/modules/gltf/structures/gltf_skin.h index 59b6a300ac..1a4d54b380 100644 --- a/modules/gltf/structures/gltf_skin.h +++ b/modules/gltf/structures/gltf_skin.h @@ -34,6 +34,9 @@ #include "../gltf_defines.h" #include "core/io/resource.h" +template <typename T> +class TypedArray; + class GLTFSkin : public Resource { GDCLASS(GLTFSkin, Resource); friend class GLTFDocument; @@ -82,8 +85,8 @@ public: Vector<GLTFNodeIndex> get_joints_original(); void set_joints_original(Vector<GLTFNodeIndex> p_joints_original); - Array get_inverse_binds(); - void set_inverse_binds(Array p_inverse_binds); + TypedArray<Transform3D> get_inverse_binds(); + void set_inverse_binds(TypedArray<Transform3D> p_inverse_binds); Vector<GLTFNodeIndex> get_joints(); void set_joints(Vector<GLTFNodeIndex> p_joints); |