Ejemplo n.º 1
0
        /// <summary>
        /// Export the bones and their animation for the given skin
        /// </summary>
        /// <param name="skin">The skin to export</param>
        /// <returns></returns>
        private BabylonBone[] ExportBones(IIGameSkin skin)
        {
            List <BabylonBone> bones         = new List <BabylonBone>();
            List <int>         nodeIndices   = GetNodeIndices(skin);
            List <IIGameNode>  revelantNodes = GetSkinnedBones(skin);

            foreach (IIGameNode node in revelantNodes)
            {
                int parentIndex = (node.NodeParent == null) ? -1 : nodeIndices.IndexOf(node.NodeParent.NodeID);

                string boneId = node.MaxNode.GetGuid().ToString();
                // create the bone
                BabylonBone bone = new BabylonBone()
                {
                    id              = (isGltfExported)?boneId:boneId + "-bone",// the suffix "-bone" is added in babylon export format to assure the uniqueness of IDs
                    parentNodeId    = (parentIndex != -1)?node.NodeParent.MaxNode.GetGuid().ToString():null,
                    name            = node.Name,
                    index           = nodeIndices.IndexOf(node.NodeID),
                    parentBoneIndex = parentIndex,
                    matrix          = (parentIndex == -1)?node.GetWorldTM(0).ToArray():node.GetLocalTM(0).ToArray()
                };

                // Apply unit conversion factor to meter
                // Affect translation only
                bone.matrix[12] *= scaleFactorToMeters;
                bone.matrix[13] *= scaleFactorToMeters;
                bone.matrix[14] *= scaleFactorToMeters;

                if (exportParameters.exportAnimations)
                {
                    // export its animation
                    var babylonAnimation = ExportMatrixAnimation("_matrix", key =>
                    {
                        IGMatrix mat = node.GetLocalTM(key);

                        float[] matrix = mat.ToArray();

                        // Apply unit conversion factor to meter
                        // Affect translation only
                        matrix[12] *= scaleFactorToMeters;
                        matrix[13] *= scaleFactorToMeters;
                        matrix[14] *= scaleFactorToMeters;

                        return(matrix);
                    },
                                                                 false); // Do not remove linear animation keys for bones

                    if (babylonAnimation != null)
                    {
                        babylonAnimation.name = node.Name + "Animation"; // override default animation name
                        bone.animation        = babylonAnimation;
                    }
                }

                bones.Add(bone);
            }

            return(bones.ToArray());
        }