#ifndef GLTF_DOCUMENT_H #define GLTF_DOCUMENT_H /*************************************************************************/ /* gltf_document.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "scene/3d/bone_attachment.h" #include "scene/3d/camera.h" #include "scene/3d/light.h" #include "scene/3d/mesh_instance.h" #include "scene/3d/skeleton.h" #include "scene/3d/spatial.h" #include "scene/animation/animation_player.h" #include "scene/resources/material.h" #include "scene/resources/texture.h" #include "gltf_animation.h" #include "modules/modules_enabled.gen.h" // For csg, gridmap. class GLTFState; class GLTFSkin; class GLTFNode; class GLTFSpecGloss; class GLTFSkeleton; class MultiMeshInstance; using GLTFAccessorIndex = int; using GLTFAnimationIndex = int; using GLTFBufferIndex = int; using GLTFBufferViewIndex = int; using GLTFCameraIndex = int; using GLTFImageIndex = int; using GLTFMaterialIndex = int; using GLTFMeshIndex = int; using GLTFLightIndex = int; using GLTFNodeIndex = int; using GLTFSkeletonIndex = int; using GLTFSkinIndex = int; using GLTFTextureIndex = int; class GLTFDocument : public Resource { GDCLASS(GLTFDocument, Resource); friend class GLTFState; friend class GLTFSkin; friend class GLTFSkeleton; private: const float BAKE_FPS = 30.0f; public: const int32_t JOINT_GROUP_SIZE = 4; enum GLTFType { TYPE_SCALAR, TYPE_VEC2, TYPE_VEC3, TYPE_VEC4, TYPE_MAT2, TYPE_MAT3, TYPE_MAT4, }; enum { ARRAY_BUFFER = 34962, ELEMENT_ARRAY_BUFFER = 34963, TYPE_BYTE = 5120, TYPE_UNSIGNED_BYTE = 5121, TYPE_SHORT = 5122, TYPE_UNSIGNED_SHORT = 5123, TYPE_UNSIGNED_INT = 5125, TYPE_FLOAT = 5126, COMPONENT_TYPE_BYTE = 5120, COMPONENT_TYPE_UNSIGNED_BYTE = 5121, COMPONENT_TYPE_SHORT = 5122, COMPONENT_TYPE_UNSIGNED_SHORT = 5123, COMPONENT_TYPE_INT = 5125, COMPONENT_TYPE_FLOAT = 5126, }; private: template static Array to_array(const Vector &p_inp) { Array ret; for (int i = 0; i < p_inp.size(); i++) { ret.push_back(p_inp[i]); } return ret; } template static Array to_array(const Set &p_inp) { Array ret; typename Set::Element *elem = p_inp.front(); while (elem) { ret.push_back(elem->get()); elem = elem->next(); } return ret; } template static void set_from_array(Vector &r_out, const Array &p_inp) { r_out.clear(); for (int i = 0; i < p_inp.size(); i++) { r_out.push_back(p_inp[i]); } } template static void set_from_array(Set &r_out, const Array &p_inp) { r_out.clear(); for (int i = 0; i < p_inp.size(); i++) { r_out.insert(p_inp[i]); } } template static Dictionary to_dict(const Map &p_inp) { Dictionary ret; for (typename Map::Element *E = p_inp.front(); E; E = E->next()) { ret[E->key()] = E->value(); } return ret; } template static void set_from_dict(Map &r_out, const Dictionary &p_inp) { r_out.clear(); Array keys = p_inp.keys(); for (int i = 0; i < keys.size(); i++) { r_out[keys[i]] = p_inp[keys[i]]; } } double _filter_number(double p_float); String _get_component_type_name(const uint32_t p_component); int _get_component_type_size(const int component_type); Error _parse_scenes(Ref state); Error _parse_nodes(Ref state); String _get_type_name(const GLTFType p_component); String _get_accessor_type_name(const GLTFDocument::GLTFType p_type); String _gen_unique_name(Ref state, const String &p_name); String _sanitize_animation_name(const String &name); String _gen_unique_animation_name(Ref state, const String &p_name); String _sanitize_bone_name(Ref state, const String &name); String _gen_unique_bone_name(Ref state, const GLTFSkeletonIndex skel_i, const String &p_name); GLTFTextureIndex _set_texture(Ref state, Ref p_texture); Ref _get_texture(Ref state, const GLTFTextureIndex p_texture); Error _parse_json(const String &p_path, Ref state); Error _parse_glb(const String &p_path, Ref state); void _compute_node_heights(Ref state); Error _parse_buffers(Ref state, const String &p_base_path); Error _parse_buffer_views(Ref state); GLTFType _get_type_from_str(const String &p_string); Error _parse_accessors(Ref state); Error _decode_buffer_view(Ref state, double *dst, const GLTFBufferViewIndex p_buffer_view, const int skip_every, const int skip_bytes, const int element_size, const int count, const GLTFType type, const int component_count, const int component_type, const int component_size, const bool normalized, const int byte_offset, const bool for_vertex); Vector _decode_accessor(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_floats(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_ints(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_vec2(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_vec3(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_color(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_quat(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_xform2d(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_basis(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Vector _decode_accessor_as_xform(Ref state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex); Error _parse_meshes(Ref state); Error _serialize_textures(Ref state); Error _serialize_images(Ref state, const String &p_path); Error _serialize_lights(Ref state); Error _parse_images(Ref state, const String &p_base_path); Error _parse_textures(Ref state); Error _parse_materials(Ref state); void _set_texture_transform_uv1(const Dictionary &d, Ref material); void spec_gloss_to_rough_metal(Ref r_spec_gloss, Ref p_material); static void spec_gloss_to_metal_base_color(const Color &p_specular_factor, const Color &p_diffuse, Color &r_base_color, float &r_metallic); GLTFNodeIndex _find_highest_node(Ref state, const Vector &subset); bool _capture_nodes_in_skin(Ref state, Ref skin, const GLTFNodeIndex node_index); void _capture_nodes_for_multirooted_skin(Ref state, Ref skin); Error _expand_skin(Ref state, Ref skin); Error _verify_skin(Ref state, Ref skin); Error _parse_skins(Ref state); Error _determine_skeletons(Ref state); Error _reparent_non_joint_skeleton_subtrees( Ref state, Ref skeleton, const Vector &non_joints); Error _determine_skeleton_roots(Ref state, const GLTFSkeletonIndex skel_i); Error _create_skeletons(Ref state); Error _map_skin_joints_indices_to_skeleton_bone_indices(Ref state); Error _serialize_skins(Ref state); Error _create_skins(Ref state); bool _skins_are_same(const Ref skin_a, const Ref skin_b); void _remove_duplicate_skins(Ref state); Error _serialize_cameras(Ref state); Error _parse_cameras(Ref state); Error _parse_lights(Ref state); Error _parse_animations(Ref state); Error _serialize_animations(Ref state); BoneAttachment *_generate_bone_attachment(Ref state, Skeleton *skeleton, const GLTFNodeIndex node_index, const GLTFNodeIndex bone_index); Spatial *_generate_mesh_instance(Ref state, Node *scene_parent, const GLTFNodeIndex node_index); Camera *_generate_camera(Ref state, Node *scene_parent, const GLTFNodeIndex node_index); Spatial *_generate_light(Ref state, Node *scene_parent, const GLTFNodeIndex node_index); Spatial *_generate_spatial(Ref state, Node *scene_parent, const GLTFNodeIndex node_index); void _assign_scene_names(Ref state); template T _interpolate_track(const Vector &p_times, const Vector &p_values, const float p_time, const GLTFAnimation::Interpolation p_interp); GLTFAccessorIndex _encode_accessor_as_quats(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_weights(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_joints(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_floats(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_vec2(Ref state, const Vector p_attribs, const bool p_for_vertex); void _calc_accessor_vec2_min_max(int i, const int element_count, Vector &type_max, Vector2 attribs, Vector &type_min) { if (i == 0) { for (int32_t type_i = 0; type_i < element_count; type_i++) { type_max.write[type_i] = attribs[(i * element_count) + type_i]; type_min.write[type_i] = attribs[(i * element_count) + type_i]; } } for (int32_t type_i = 0; type_i < element_count; type_i++) { type_max.write[type_i] = MAX(attribs[(i * element_count) + type_i], type_max[type_i]); type_min.write[type_i] = MIN(attribs[(i * element_count) + type_i], type_min[type_i]); type_max.write[type_i] = _filter_number(type_max.write[type_i]); type_min.write[type_i] = _filter_number(type_min.write[type_i]); } } GLTFAccessorIndex _encode_accessor_as_vec3(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_color(Ref state, const Vector p_attribs, const bool p_for_vertex); void _calc_accessor_min_max(int p_i, const int p_element_count, Vector &p_type_max, Vector p_attribs, Vector &p_type_min); GLTFAccessorIndex _encode_accessor_as_ints(Ref state, const Vector p_attribs, const bool p_for_vertex); GLTFAccessorIndex _encode_accessor_as_xform(Ref state, const Vector p_attribs, const bool p_for_vertex); Error _encode_buffer_view(Ref state, const double *src, const int count, const GLTFType type, const int component_type, const bool normalized, const int byte_offset, const bool for_vertex, GLTFBufferViewIndex &r_accessor); Error _encode_accessors(Ref state); Error _encode_buffer_views(Ref state); Error _serialize_materials(Ref state); Error _serialize_meshes(Ref state); Error _serialize_nodes(Ref state); Error _serialize_scenes(Ref state); String interpolation_to_string(const GLTFAnimation::Interpolation p_interp); GLTFAnimation::Track _convert_animation_track(Ref state, GLTFAnimation::Track p_track, Ref p_animation, Transform p_bone_rest, int32_t p_track_i, GLTFNodeIndex p_node_i); Error _encode_buffer_bins(Ref state, const String &p_path); Error _encode_buffer_glb(Ref state, const String &p_path); Dictionary _serialize_texture_transform_uv1(Ref p_material); Dictionary _serialize_texture_transform_uv2(Ref p_material); Error _serialize_version(Ref state); Error _serialize_file(Ref state, const String p_path); Error _serialize_extensions(Ref state) const; public: // http://www.itu.int/rec/R-REC-BT.601 // http://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf static constexpr float R_BRIGHTNESS_COEFF = 0.299f; static constexpr float G_BRIGHTNESS_COEFF = 0.587f; static constexpr float B_BRIGHTNESS_COEFF = 0.114f; private: // https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9 // https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js static float solve_metallic(float p_dielectric_specular, float diffuse, float specular, float p_one_minus_specular_strength); static float get_perceived_brightness(const Color p_color); static float get_max_component(const Color &p_color); public: String _sanitize_scene_name(Ref state, const String &p_name); String _legacy_validate_node_name(const String &p_name); Error _parse_gltf_extensions(Ref state); void _process_mesh_instances(Ref state, Node *scene_root); void _generate_scene_node(Ref state, Node *scene_parent, Spatial *scene_root, const GLTFNodeIndex node_index); void _generate_skeleton_bone_node(Ref state, Node *scene_parent, Spatial *scene_root, const GLTFNodeIndex node_index); void _import_animation(Ref state, AnimationPlayer *ap, const GLTFAnimationIndex index, const int bake_fps); void _convert_mesh_instances(Ref state); GLTFCameraIndex _convert_camera(Ref state, Camera *p_camera); void _convert_light_to_gltf(Light *light, Ref state, Ref gltf_node); GLTFLightIndex _convert_light(Ref state, Light *p_light); void _convert_spatial(Ref state, Spatial *p_spatial, Ref p_node); void _convert_scene_node(Ref state, Node *p_current, const GLTFNodeIndex p_gltf_current, const GLTFNodeIndex p_gltf_root); void _create_gltf_node(Ref state, Node *p_scene_parent, GLTFNodeIndex current_node_i, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_gltf_node, Ref gltf_node); void _convert_animation_player_to_gltf( AnimationPlayer *animation_player, Ref state, GLTFNodeIndex p_gltf_current, GLTFNodeIndex p_gltf_root_index, Ref p_gltf_node, Node *p_scene_parent); void _check_visibility(Node *p_node, bool &retflag); void _convert_camera_to_gltf(Camera *camera, Ref state, Ref gltf_node); void _convert_mult_mesh_instance_to_gltf( MultiMeshInstance *p_scene_parent, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref gltf_node, Ref state); void _convert_skeleton_to_gltf( Skeleton *p_scene_parent, Ref state, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref gltf_node); void _convert_bone_attachment_to_gltf(BoneAttachment *p_bone_attachment, Ref state, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref gltf_node); void _convert_mesh_instance_to_gltf(MeshInstance *p_mesh_instance, Ref state, Ref gltf_node); GLTFMeshIndex _convert_mesh_to_gltf(Ref state, MeshInstance *p_mesh_instance); void _convert_animation(Ref state, AnimationPlayer *ap, String p_animation_track_name); Error serialize(Ref state, Node *p_root, const String &p_path); Error parse(Ref state, String p_paths, bool p_read_binary = false); }; #endif // GLTF_DOCUMENT_H