예제 #1
0
        private void BuildSkeletonNodes(Node node, List <string> boneNames, STSkeleton skeleton, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo   = node.Transform;
            Matrix4x4 world   = trafo * rootTransform;
            Matrix4   worldTK = AssimpHelper.TKMatrix(world);

            string Name = node.Name;
            string ParentArmatureName = node.Parent != null ? node.Parent.Name : "";

            if (DaeHelper.IDMapToName.ContainsKey(node.Name))
            {
                Name = DaeHelper.IDMapToName[node.Name];
            }

            if (ParentArmatureName != string.Empty && DaeHelper.IDMapToName.ContainsKey(ParentArmatureName))
            {
                ParentArmatureName = DaeHelper.IDMapToName[ParentArmatureName];
            }

            bool IsBone = boneNames.Contains(Name) && !boneNames.Contains(ParentArmatureName) ||
                          Name.Contains("Skl_Root") || Name.Contains("nw4f_root") ||
                          Name.Contains("skl_root") || Name.Contains("all_root") || Name.Contains("_root") || Name.Contains("Root");

            if (DaeHelper.VisualSceneNodeTypes.ContainsKey(Name))
            {
                if (DaeHelper.VisualSceneNodeTypes[Name] == "JOINT")
                {
                    IsBone = true;
                }
            }

            //Root set saved by this tool
            //Get our root manually as it's a child to this
            bool IsRootSkeleton = Name == "skeleton_root";

            short SmoothIndex = 0;
            short RigidIndex  = -1;

            //Loop through all the bones. If the parent is not in the bone list, then it's Parent is the root
            if (IsBone)
            {
                var idenity = Matrix4x4.Identity;
                CreateByNode(node, skeleton, ParentArmatureName, SmoothIndex, RigidIndex, true, ref rootTransform);
            }
            else if (IsRootSkeleton && node.HasChildren)
            {
                var idenity = Matrix4x4.Identity;
                CreateByNode(node.Children[0], skeleton, ParentArmatureName, SmoothIndex, RigidIndex, true, ref world);
            }
            else
            {
                if (node.HasChildren)
                {
                    foreach (Node child in node.Children)
                    {
                        BuildSkeletonNodes(child, boneNames, skeleton, ref world);
                    }
                }
            }
        }
예제 #2
0
        private void BuildNode(Node parent, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 world   = rootTransform;
            Matrix4   worldTK = Matrix4.Identity;

            if (UseTransformMatrix)
            {
                Matrix4x4 trafo = parent.Transform;
                world   = trafo * rootTransform;
                worldTK = AssimpHelper.TKMatrix(world);
            }

            if (parent.MeshCount > 0)
            {
                STConsole.WriteLine($"Use Transform Matrix {UseTransformMatrix}");
                STConsole.WriteLine($"Transform node {parent.Name}");
                STConsole.WriteLine($"Translation {worldTK.ExtractTranslation()}");
                STConsole.WriteLine($"Rotation {worldTK.ExtractRotation()}");
                STConsole.WriteLine($"Scale {worldTK.ExtractScale()}");
            }

            foreach (int index in parent.MeshIndices)
            {
                objects.Add(CreateGenericObject(parent, scene.Meshes[index], index, worldTK));
            }

            foreach (Node child in parent.Children)
            {
                BuildNode(child, ref world);
            }
        }
예제 #3
0
        public void LoadScene()
        {
            objects.Clear();
            materials.Clear();
            BoneNames.Clear();
            skeleton = new STSkeleton();

            processNode();

            if (scene.RootNode != null)
            {
                var     rootTransform = scene.RootNode.Transform;
                Matrix4 transformMat  = AssimpHelper.TKMatrix(rootTransform);

                var scale    = transformMat.ExtractScale();
                var rotation = transformMat.ExtractRotation();
                var position = transformMat.ExtractTranslation();

                STConsole.WriteLine($"-".Repeat(30));
                STConsole.WriteLine($"rootTransform {transformMat}");
                STConsole.WriteLine($"scale {scale}");
                STConsole.WriteLine($"rotation {rotation}");
                STConsole.WriteLine($"position {position}");
                STConsole.WriteLine($"-".Repeat(30));

                var SklRoot = GetSklRoot(scene.RootNode, BoneNames);
                if (SklRoot != null)
                {
                    BuildSkeletonNodes(SklRoot, BoneNames, skeleton, ref rootTransform);
                }
                else
                {
                    BuildSkeletonNodes(scene.RootNode, BoneNames, skeleton, ref rootTransform);
                }

                skeleton.update();
                skeleton.reset();
            }


            if (scene.HasMaterials)
            {
                foreach (Material mat in scene.Materials)
                {
                    materials.Add(CreateGenericMaterial(mat));
                }
            }
            foreach (Assimp.Animation animation in scene.Animations)
            {
            }
            foreach (var tex in scene.Textures)
            {
            }
        }
예제 #4
0
        private void CreateByNode(Node node, STSkeleton skeleton, string ParentArmatureName,
                                  short SmoothIndex, short RigidIndex, bool IsRoot, ref Assimp.Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo        = node.Transform;
            Matrix4x4 world        = trafo * rootTransform;
            var       transformMat = AssimpHelper.TKMatrix(world);

            int matchedBoneIndex = skeleton.bones.FindIndex(item => item.Name == node.Name);

            if (matchedBoneIndex < 0)
            {
                tempBoneNodes.Add(node);

                STBone bone = new STBone();
                bone.skeletonParent = skeleton;
                bone.RotationType   = STBone.BoneRotationType.Euler;
                skeleton.bones.Add(bone);

                if (DaeHelper.IDMapToName.ContainsKey(node.Name))
                {
                    bone.Text = DaeHelper.IDMapToName[node.Name];
                }
                else
                {
                    bone.Text = node.Name;
                }

                bone.SmoothMatrixIndex = (short)skeleton.bones.IndexOf(bone);
                bone.RigidMatrixIndex  = -1; //Todo calculate these

                if (IsRoot)
                {
                    bone.parentIndex = -1;

                    if (RotateSkeleton)
                    {
                        transformMat = AssimpHelper.TKMatrix(world * Matrix4x4.FromRotationX(MathHelper.DegreesToRadians(RotateSkeletonAmount)));
                    }
                    else
                    {
                        transformMat = AssimpHelper.TKMatrix(world);
                    }
                }
                else
                {
                    if (tempBoneNodes.Contains(node.Parent))
                    {
                        bone.parentIndex = tempBoneNodes.IndexOf(node.Parent);
                    }
                }


                var scale    = transformMat.ExtractScale();
                var rotation = transformMat.ExtractRotation();
                var position = transformMat.ExtractTranslation();

                STConsole.WriteLine($"-".Repeat(30));
                STConsole.WriteLine($"Processing Bone {bone.Text}");
                STConsole.WriteLine($"scale {scale}");
                STConsole.WriteLine($"rotation {rotation}");
                STConsole.WriteLine($"position {position}");
                STConsole.WriteLine($"-".Repeat(30));

                bone.FromTransform(transformMat);
            }
            else
            {
                STConsole.WriteLine($"Duplicate node name found for bone {node.Name}!", Color.Red);
            }

            var identity = Matrix4x4.Identity;

            foreach (Node child in node.Children)
            {
                CreateByNode(child, skeleton, ParentArmatureName, SmoothIndex, RigidIndex, false, ref identity);
            }
        }