Browse Source

Merge pull request #32213 from marstaik/skin_support

GLTF2 Import Fixes - Skin(s) to Skeleton - Skin Support
Rémi Verschelde 5 years ago
parent
commit
36b5795f47

+ 31 - 0
core/math/disjoint_set.cpp

@@ -0,0 +1,31 @@
+/*************************************************************************/
+/*  disjoint_set.h                                                       */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2019 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 "disjoint_set.h"

+ 155 - 0
core/math/disjoint_set.h

@@ -0,0 +1,155 @@
+/*************************************************************************/
+/*  disjoint_set.h                                                       */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2019 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.                */
+/*************************************************************************/
+
+#ifndef DISJOINT_SET_H
+#define DISJOINT_SET_H
+
+#include "core/map.h"
+#include "core/vector.h"
+
+/**
+	@author Marios Staikopoulos <[email protected]>
+*/
+
+/* This DisjointSet class uses Find with path compression and Union by rank */
+template <typename T, class C = Comparator<T>, class AL = DefaultAllocator>
+class DisjointSet {
+
+	struct Element {
+		T object;
+		Element *parent = nullptr;
+		int rank = 0;
+	};
+
+	typedef Map<T, Element *, C, AL> MapT;
+
+	MapT elements;
+
+	Element *get_parent(Element *element);
+
+	_FORCE_INLINE_ Element *insert_or_get(T object);
+
+public:
+	~DisjointSet();
+
+	_FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); }
+
+	void create_union(T a, T b);
+
+	void get_representatives(Vector<T> &out_roots);
+
+	void get_members(Vector<T> &out_members, T representative);
+};
+
+/* FUNCTIONS */
+
+template <typename T, class C, class AL>
+DisjointSet<T, C, AL>::~DisjointSet() {
+	for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
+		memdelete_allocator<Element, AL>(itr->value());
+	}
+}
+
+template <typename T, class C, class AL>
+typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::get_parent(Element *element) {
+	if (element->parent != element) {
+		element->parent = get_parent(element->parent);
+	}
+
+	return element->parent;
+}
+
+template <typename T, class C, class AL>
+typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::insert_or_get(T object) {
+	typename MapT::Element *itr = elements.find(object);
+	if (itr != nullptr) {
+		return itr->value();
+	}
+
+	Element *new_element = memnew_allocator(Element, AL);
+	new_element->object = object;
+	new_element->parent = new_element;
+	elements.insert(object, new_element);
+
+	return new_element;
+}
+
+template <typename T, class C, class AL>
+void DisjointSet<T, C, AL>::create_union(T a, T b) {
+
+	Element *x = insert_or_get(a);
+	Element *y = insert_or_get(b);
+
+	Element *x_root = get_parent(x);
+	Element *y_root = get_parent(y);
+
+	// Already in the same set
+	if (x_root == y_root)
+		return;
+
+	// Not in the same set, merge
+	if (x_root->rank < y_root->rank) {
+		SWAP(x_root, y_root);
+	}
+
+	// Merge y_root into x_root
+	y_root->parent = x_root;
+	if (x_root->rank == y_root->rank) {
+		++x_root->rank;
+	}
+}
+
+template <typename T, class C, class AL>
+void DisjointSet<T, C, AL>::get_representatives(Vector<T> &out_representatives) {
+	for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
+		Element *element = itr->value();
+		if (element->parent == element) {
+			out_representatives.push_back(element->object);
+		}
+	}
+}
+
+template <typename T, class C, class AL>
+void DisjointSet<T, C, AL>::get_members(Vector<T> &out_members, T representative) {
+	typename MapT::Element *rep_itr = elements.find(representative);
+	ERR_FAIL_COND(rep_itr == nullptr);
+
+	Element *rep_element = rep_itr->value();
+	ERR_FAIL_COND(rep_element->parent != rep_element);
+
+	for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
+		Element *parent = get_parent(itr->value());
+		if (parent == rep_element) {
+			out_members.push_back(itr->key());
+		}
+	}
+}
+
+#endif

File diff suppressed because it is too large
+ 931 - 211
editor/import/editor_scene_importer_gltf.cpp


+ 143 - 72
editor/import/editor_scene_importer_gltf.h

@@ -36,11 +36,26 @@
 #include "scene/3d/spatial.h"
 
 class AnimationPlayer;
+class BoneAttachment;
+class MeshInstance;
 
 class EditorSceneImporterGLTF : public EditorSceneImporter {
 
 	GDCLASS(EditorSceneImporterGLTF, EditorSceneImporter);
 
+	typedef int GLTFAccessorIndex;
+	typedef int GLTFAnimationIndex;
+	typedef int GLTFBufferIndex;
+	typedef int GLTFBufferViewIndex;
+	typedef int GLTFCameraIndex;
+	typedef int GLTFImageIndex;
+	typedef int GLTFMaterialIndex;
+	typedef int GLTFMeshIndex;
+	typedef int GLTFNodeIndex;
+	typedef int GLTFSkeletonIndex;
+	typedef int GLTFSkinIndex;
+	typedef int GLTFTextureIndex;
+
 	enum {
 		ARRAY_BUFFER = 34962,
 		ELEMENT_ARRAY_BUFFER = 34963,
@@ -61,8 +76,8 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 
 	};
 
-	String _get_component_type_name(uint32_t p_component);
-	int _get_component_type_size(int component_type);
+	String _get_component_type_name(const uint32_t p_component);
+	int _get_component_type_size(const int component_type);
 
 	enum GLTFType {
 		TYPE_SCALAR,
@@ -74,60 +89,48 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 		TYPE_MAT4,
 	};
 
-	String _get_type_name(GLTFType p_component);
+	String _get_type_name(const GLTFType p_component);
 
 	struct GLTFNode {
+
 		//matrices need to be transformed to this
-		int parent;
+		GLTFNodeIndex parent;
+		int height;
 
 		Transform xform;
 		String name;
-		//Node *godot_node;
-		//int godot_bone_index;
-
-		int mesh;
-		int camera;
-		int skin;
-		//int skeleton_skin;
-		//int child_of_skeleton; // put as children of skeleton
-		//Vector<int> skeleton_children; //skeleton put as children of this
-
-		struct Joint {
-			int skin;
-			int bone;
-			int godot_bone_index;
-
-			Joint() {
-				skin = -1;
-				bone = -1;
-				godot_bone_index = -1;
-			}
-		};
 
-		Vector<Joint> joints;
+		GLTFMeshIndex mesh;
+		GLTFCameraIndex camera;
+		GLTFSkinIndex skin;
+
+		GLTFSkeletonIndex skeleton;
+		bool joint;
 
-		//keep them for animation
 		Vector3 translation;
 		Quat rotation;
 		Vector3 scale;
 
 		Vector<int> children;
-		Vector<Node *> godot_nodes;
+
+		GLTFNodeIndex fake_joint_parent;
 
 		GLTFNode() :
 				parent(-1),
+				height(-1),
 				mesh(-1),
 				camera(-1),
 				skin(-1),
-				//skeleton_skin(-1),
-				//child_of_skeleton(-1),
-				scale(Vector3(1, 1, 1)) {
-		}
+				skeleton(-1),
+				joint(false),
+				translation(0, 0, 0),
+				scale(Vector3(1, 1, 1)),
+				fake_joint_parent(-1) {}
 	};
 
 	struct GLTFBufferView {
 
-		int buffer;
+		GLTFBufferIndex buffer;
 		int byte_offset;
 		int byte_length;
 		int byte_stride;
@@ -135,7 +138,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 		//matrices need to be transformed to this
 
 		GLTFBufferView() :
-				buffer(0),
+				buffer(-1),
 				byte_offset(0),
 				byte_length(0),
 				byte_stride(0),
@@ -145,7 +148,7 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 
 	struct GLTFAccessor {
 
-		int buffer_view;
+		GLTFBufferViewIndex buffer_view;
 		int byte_offset;
 		int component_type;
 		bool normalized;
@@ -160,8 +163,6 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 		int sparse_values_buffer_view;
 		int sparse_values_byte_offset;
 
-		//matrices need to be transformed to this
-
 		GLTFAccessor() {
 			buffer_view = 0;
 			byte_offset = 0;
@@ -176,27 +177,67 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 		}
 	};
 	struct GLTFTexture {
-		int src_image;
+		GLTFImageIndex src_image;
 	};
 
-	struct GLTFSkin {
+	struct GLTFSkeleton {
+		// The *synthesized* skeletons joints
+		Vector<GLTFNodeIndex> joints;
 
-		String name;
-		struct Bone {
-			Transform inverse_bind;
-			int node;
-		};
+		// The roots of the skeleton. If there are multiple, each root must have the same parent
+		// (ie roots are siblings)
+		Vector<GLTFNodeIndex> roots;
 
-		int skeleton;
-		Vector<Bone> bones;
+		// The created Skeleton for the scene
+		Skeleton *godot_skeleton;
 
-		//matrices need to be transformed to this
+		// Set of unique bone names for the skeleton
+		Set<String> unique_names;
 
-		GLTFSkin() {
-			skeleton = -1;
+		GLTFSkeleton() :
+				godot_skeleton(nullptr) {
 		}
 	};
 
+	struct GLTFSkin {
+		String name;
+
+		// The "skeleton" property defined in the gltf spec. -1 = Scene Root
+		GLTFNodeIndex skin_root;
+
+		Vector<GLTFNodeIndex> joints_original;
+		Vector<Transform> inverse_binds;
+
+		// Note: joints + non_joints should form a complete subtree, or subtrees with a common parent
+
+		// All nodes that are skins that are caught in-between the original joints
+		// (inclusive of joints_original)
+		Vector<GLTFNodeIndex> joints;
+
+		// All Nodes that are caught in-between skin joint nodes, and are not defined
+		// as joints by any skin
+		Vector<GLTFNodeIndex> non_joints;
+
+		// The roots of the skin. In the case of multiple roots, their parent *must*
+		// be the same (the roots must be siblings)
+		Vector<GLTFNodeIndex> roots;
+
+		// The GLTF Skeleton this Skin points to (after we determine skeletons)
+		GLTFSkeletonIndex skeleton;
+
+		// A mapping from the joint indices (in the order of joints_original) to the
+		// Godot Skeleton's bone_indices
+		Map<int, int> joint_i_to_bone_i;
+
+		// The Actual Skin that will be created as a mapping between the IBM's of this skin
+		// to the generated skeleton for the mesh instances.
+		Ref<Skin> godot_skin;
+
+		GLTFSkin() :
+				skin_root(-1),
+				skeleton(-1) {}
+	};
+
 	struct GLTFMesh {
 		Ref<ArrayMesh> mesh;
 		Vector<float> blend_weights;
@@ -272,11 +313,10 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 
 		Set<String> unique_names;
 
+		Vector<GLTFSkeleton> skeletons;
 		Vector<GLTFAnimation> animations;
 
-		Map<int, Vector<int> > skeleton_nodes;
-
-		//Map<int, Vector<int> > skin_users; //cache skin users
+		Map<GLTFNodeIndex, Node *> scene_nodes;
 
 		~GLTFState() {
 			for (int i = 0; i < nodes.size(); i++) {
@@ -285,37 +325,38 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 		}
 	};
 
+	String _sanitize_scene_name(const String &name);
 	String _gen_unique_name(GLTFState &state, const String &p_name);
 
-	Ref<Texture> _get_texture(GLTFState &state, int p_texture);
+	String _sanitize_bone_name(const String &name);
+	String _gen_unique_bone_name(GLTFState &state, const GLTFSkeletonIndex skel_i, const String &p_name);
+
+	Ref<Texture> _get_texture(GLTFState &state, const GLTFTextureIndex p_texture);
 
 	Error _parse_json(const String &p_path, GLTFState &state);
 	Error _parse_glb(const String &p_path, GLTFState &state);
 
 	Error _parse_scenes(GLTFState &state);
 	Error _parse_nodes(GLTFState &state);
+
+	void _compute_node_heights(GLTFState &state);
+
 	Error _parse_buffers(GLTFState &state, const String &p_base_path);
 	Error _parse_buffer_views(GLTFState &state);
 	GLTFType _get_type_from_str(const String &p_string);
 	Error _parse_accessors(GLTFState &state);
-	Error _decode_buffer_view(GLTFState &state, int p_buffer_view, double *dst, int skip_every, int skip_bytes, int element_size, int count, GLTFType type, int component_count, int component_type, int component_size, bool normalized, int byte_offset, bool for_vertex);
-	Vector<double> _decode_accessor(GLTFState &state, int p_accessor, bool p_for_vertex);
-	PoolVector<float> _decode_accessor_as_floats(GLTFState &state, int p_accessor, bool p_for_vertex);
-	PoolVector<int> _decode_accessor_as_ints(GLTFState &state, int p_accessor, bool p_for_vertex);
-	PoolVector<Vector2> _decode_accessor_as_vec2(GLTFState &state, int p_accessor, bool p_for_vertex);
-	PoolVector<Vector3> _decode_accessor_as_vec3(GLTFState &state, int p_accessor, bool p_for_vertex);
-	PoolVector<Color> _decode_accessor_as_color(GLTFState &state, int p_accessor, bool p_for_vertex);
-	Vector<Quat> _decode_accessor_as_quat(GLTFState &state, int p_accessor, bool p_for_vertex);
-	Vector<Transform2D> _decode_accessor_as_xform2d(GLTFState &state, int p_accessor, bool p_for_vertex);
-	Vector<Basis> _decode_accessor_as_basis(GLTFState &state, int p_accessor, bool p_for_vertex);
-	Vector<Transform> _decode_accessor_as_xform(GLTFState &state, int p_accessor, bool p_for_vertex);
-
-	void _reparent_skeleton(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, Node *p_parent_node);
-	void _generate_bone(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, Node *p_parent_node);
-	void _generate_node(GLTFState &state, int p_node, Node *p_parent, Node *p_owner, Vector<Skeleton *> &skeletons);
-	void _import_animation(GLTFState &state, AnimationPlayer *ap, int index, int bake_fps, Vector<Skeleton *> skeletons);
-
-	Spatial *_generate_scene(GLTFState &state, int p_bake_fps);
+	Error _decode_buffer_view(GLTFState &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<double> _decode_accessor(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	PoolVector<float> _decode_accessor_as_floats(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	PoolVector<int> _decode_accessor_as_ints(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	PoolVector<Vector2> _decode_accessor_as_vec2(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	PoolVector<Vector3> _decode_accessor_as_vec3(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	PoolVector<Color> _decode_accessor_as_color(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	Vector<Quat> _decode_accessor_as_quat(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	Vector<Transform2D> _decode_accessor_as_xform2d(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	Vector<Basis> _decode_accessor_as_basis(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
+	Vector<Transform> _decode_accessor_as_xform(GLTFState &state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex);
 
 	Error _parse_meshes(GLTFState &state);
 	Error _parse_images(GLTFState &state, const String &p_base_path);
@@ -323,16 +364,46 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
 
 	Error _parse_materials(GLTFState &state);
 
+	GLTFNodeIndex _find_highest_node(GLTFState &state, const Vector<GLTFNodeIndex> &subset);
+
+	bool _capture_nodes_in_skin(GLTFState &state, GLTFSkin &skin, const GLTFNodeIndex node_index);
+	void _capture_nodes_for_multirooted_skin(GLTFState &state, GLTFSkin &skin);
+	Error _expand_skin(GLTFState &state, GLTFSkin &skin);
+	Error _verify_skin(GLTFState &state, GLTFSkin &skin);
 	Error _parse_skins(GLTFState &state);
 
+	Error _determine_skeletons(GLTFState &state);
+	Error _reparent_non_joint_skeleton_subtrees(GLTFState &state, GLTFSkeleton &skeleton, const Vector<GLTFNodeIndex> &non_joints);
+	Error _reparent_to_fake_joint(GLTFState &state, GLTFSkeleton &skeleton, const GLTFNodeIndex node_index);
+	Error _determine_skeleton_roots(GLTFState &state, const GLTFSkeletonIndex skel_i);
+
+	Error _create_skeletons(GLTFState &state);
+	Error _map_skin_joints_indices_to_skeleton_bone_indices(GLTFState &state);
+
+	Error _create_skins(GLTFState &state);
+	bool _skins_are_same(const Ref<Skin> &skin_a, const Ref<Skin> &skin_b);
+	void _remove_duplicate_skins(GLTFState &state);
+
 	Error _parse_cameras(GLTFState &state);
 
 	Error _parse_animations(GLTFState &state);
 
+	BoneAttachment *_generate_bone_attachment(GLTFState &state, Skeleton *skeleton, const GLTFNodeIndex node_index);
+	MeshInstance *_generate_mesh_instance(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index);
+	Camera *_generate_camera(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index);
+	Spatial *_generate_spatial(GLTFState &state, Node *scene_parent, const GLTFNodeIndex node_index);
+
+	void _generate_scene_node(GLTFState &state, Node *scene_parent, Spatial *scene_root, const GLTFNodeIndex node_index);
+	Spatial *_generate_scene(GLTFState &state, const int p_bake_fps);
+
+	void _process_mesh_instances(GLTFState &state, Spatial *scene_root);
+
 	void _assign_scene_names(GLTFState &state);
 
 	template <class T>
-	T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, float p_time, GLTFAnimation::Interpolation p_interp);
+	T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, const float p_time, const GLTFAnimation::Interpolation p_interp);
+
+	void _import_animation(GLTFState &state, AnimationPlayer *ap, const GLTFAnimationIndex index, const int bake_fps);
 
 public:
 	virtual uint32_t get_import_flags() const;

Some files were not shown because too many files changed in this diff