Esempio n. 1
0
        private void StoreRestPose(FBXScene pScene, FBXNode pSkeletonRoot, SkeletonAsset Skeleton)
        {
            Dictionary <String, FBBone> pose = Skeleton.ModelBones;
            FBXAMatrix lTransformMatrix = new FBXAMatrix();
            FBXVector4 lT, lR, lS;

            lS = new FBXVector4(1.0, 1.0, 1.0, 0);
            FBXPose lPose = FBXPose.Create(pScene, "A Bind Pose");

            lT = new FBXVector4();
            lR = new FBXVector4();
            lTransformMatrix.SetTRS(lT, lR, lS);
            FBXNode lKFbxNode = pSkeletonRoot;

            lPose.Add(lKFbxNode, lTransformMatrix);
            foreach (string key in pose.Keys)
            {
                FBBone     bonePose      = pose[key];
                FBXNode    fbxBone       = pSkeletonRoot.FindChild(key);
                FBXVector4 Forward       = new FBXVector4(bonePose.Forward.members[0], bonePose.Forward.members[1], bonePose.Forward.members[2], 0);
                FBXVector4 Right         = new FBXVector4(bonePose.Right.members[0], bonePose.Right.members[1], bonePose.Right.members[2], 0);
                FBXVector4 Up            = new FBXVector4(bonePose.Up.members[0], bonePose.Up.members[1], bonePose.Up.members[2], 0);
                FBXVector4 Trans         = new FBXVector4(bonePose.Location.members[0] * exportScale, bonePose.Location.members[1] * exportScale, bonePose.Location.members[2] * exportScale, 1);
                FBXAMatrix boneTransform = new FBXAMatrix();
                boneTransform.SetRow(0, Right);
                boneTransform.SetRow(1, Up);
                boneTransform.SetRow(2, Forward);
                boneTransform.SetRow(3, Trans);
                lPose.Add(fbxBone, boneTransform);
            }
            lPose.SetIsBindPose(true);
            pScene.AddPose(lPose);
        }
Esempio n. 2
0
 public void ApplyMorphToSkeleton(SkeletonAsset skeleton)
 {
     for (int i = 0; i < skeleton.Bones.Count; i++)
     {
         FBBone fbbone     = skeleton.Bones[i];
         Vector boneOffset = BonesMorph[i];
         //var tmp = fbbone.Location.members;
         fbbone.Location.members[0] += boneOffset.members[0];
         fbbone.Location.members[1] += boneOffset.members[1];
         fbbone.Location.members[2] += boneOffset.members[2];
         //fbbone.Location.members = tmp;
     }
 }
Esempio n. 3
0
 private void UpdateSkeletonWithMorph(SkeletonAsset Skeleton, FBXNode pSkeletonNode, List <Vector> morphBones)
 {
     for (int i = 0; i < Skeleton.Bones.Count; i++)
     {
         FBBone        fbbone     = Skeleton.Bones[i];
         FBXNode       fbxBone    = pSkeletonNode.FindChild(fbbone.Name);
         Vector        boneOffset = morphBones[i];
         List <double> tmp        = fbxBone.LclTranslation;
         tmp[0] += boneOffset.members[0] * exportScale;
         tmp[1] += boneOffset.members[1] * exportScale;
         tmp[2] += boneOffset.members[2] * exportScale;
         fbxBone.LclTranslation = tmp;
     }
 }
Esempio n. 4
0
        private FBXVector4 CalculateBoneRotation(FBBone bone)
        {
            FBXAMatrix fbxrotMat = new FBXAMatrix();
            FBXVector4 Forward   = new FBXVector4(bone.Forward.members[0], bone.Forward.members[1], bone.Forward.members[2], 0);
            FBXVector4 Right     = new FBXVector4(bone.Right.members[0], bone.Right.members[1], bone.Right.members[2], 0);
            FBXVector4 Up        = new FBXVector4(bone.Up.members[0], bone.Up.members[1], bone.Up.members[2], 0);

            fbxrotMat.SetRow(0, Right);
            fbxrotMat.SetRow(1, Up);
            fbxrotMat.SetRow(2, Forward);
            fbxrotMat.SetRow(3, new FBXVector4(0, 0, 0, 1));
            FBXVector4 boneRot = fbxrotMat.getR();

            return(boneRot);
        }
Esempio n. 5
0
        private void SetBoneTransform(FBBone bone, FBXNode fbxBoneNode)
        {
            List <double> tmp = new List <double>();

            tmp.Add(bone.Location.members[0] * exportScale);
            tmp.Add(bone.Location.members[1] * exportScale);
            tmp.Add(bone.Location.members[2] * exportScale);
            fbxBoneNode.LclTranslation = tmp;

            FBXVector4 rot = CalculateBoneRotation(bone);

            tmp = new List <double>();
            tmp.Add(rot.X);
            tmp.Add(rot.Y);
            tmp.Add(rot.Z);
            fbxBoneNode.LclRotation = tmp;
        }
Esempio n. 6
0
        private FBXNode CreateFbxBone(FBBone bone, FBXScene pScene, FBXNode parent)
        {
            FBXSkeleton lSkeletonLimbNodeAttribute1 = FBXSkeleton.Create(pScene, bone.Name);

            lSkeletonLimbNodeAttribute1.SetSkeletonType(FBXWrapper.SkelType.eLimbNode);
            lSkeletonLimbNodeAttribute1.SetSize(1.0);
            FBXNode lSkeletonLimbNode1 = FBXNode.Create(pScene, bone.Name);

            lSkeletonLimbNode1.SetNodeAttribute(lSkeletonLimbNodeAttribute1);
            for (int i = 0; i < bone.Children.Count; i++)
            {
                FBBone  childBone    = bone.Children[i];
                FBXNode fbxChildBone = CreateFbxBone(childBone, pScene, lSkeletonLimbNode1);
                lSkeletonLimbNode1.AddChild(fbxChildBone);
            }
            return(lSkeletonLimbNode1);
        }
Esempio n. 7
0
        private void SetBoneTransform(SkeletonAsset Skeleton, FBXNode pSkeletonRoot)
        {
            Dictionary <String, FBBone> pose = Skeleton.ModelBones;

            foreach (string key in pose.Keys)
            {
                FBBone     bonePose      = pose[key];
                FBXNode    fbxBone       = pSkeletonRoot.FindChild(key);
                FBXVector4 ForwardM      = new FBXVector4(bonePose.Forward.members[0], bonePose.Forward.members[1], bonePose.Forward.members[2], 0);
                FBXVector4 RightM        = new FBXVector4(bonePose.Right.members[0], bonePose.Right.members[1], bonePose.Right.members[2], 0);
                FBXVector4 UpM           = new FBXVector4(bonePose.Up.members[0], bonePose.Up.members[1], bonePose.Up.members[2], 0);
                FBXVector4 TransM        = new FBXVector4(bonePose.Location.members[0] * exportScale, bonePose.Location.members[1] * exportScale, bonePose.Location.members[2] * exportScale, 1);
                FBXAMatrix transfoMatrix = new FBXAMatrix();
                transfoMatrix.SetRow(0, RightM);
                transfoMatrix.SetRow(1, UpM);
                transfoMatrix.SetRow(2, ForwardM);
                transfoMatrix.SetRow(3, TransM);
                FBXHelper.SetGlobalDefaultPosition(fbxBone, transfoMatrix);
            }
        }
Esempio n. 8
0
        public SkeletonAsset(EBX Ebx, string PoseNodeName)
        {
            LocalTransform = (PoseNodeName == "LocalPose");

            Bones      = new List <FBBone>();
            ModelBones = new Dictionary <string, FBBone>();

            string    ebxXml = Ebx.ToXML().Replace("&", "_and_").Replace("$", "_dollar_");
            XDocument doc    = XDocument.Parse(ebxXml);

            var skel  = doc.Root.Descendants("SkeletonAsset");
            var skel2 = doc.Root.Element("SkeletonAsset");

            var boneNames = from bn in doc.Root.Descendants("BoneNames").Descendants("member")
                            select bn.Value;

            var hierarchy = from h in doc.Root.Descendants("Hierarchy").Descendants("member")
                            select h.Value;

            var localPoses = from lp in doc.Root.Descendants(PoseNodeName).Descendants("member").Descendants("LinearTransform")
                             select new
            {
                right = new Vector(
                    float.Parse(lp.Element("right").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("right").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("right").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                up = new Vector(
                    float.Parse(lp.Element("up").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("up").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("up").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                forward = new Vector(
                    float.Parse(lp.Element("forward").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("forward").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("forward").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                trans = new Vector(
                    float.Parse(lp.Element("trans").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("trans").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("trans").Element("Vec3").Element("z").Value.Trim().Replace("f", "")))
            };

            var modelPoses = from lp in doc.Root.Descendants("ModelPose").Descendants("member").Descendants("LinearTransform")
                             select new
            {
                right = new Vector(
                    float.Parse(lp.Element("right").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("right").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("right").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                up = new Vector(
                    float.Parse(lp.Element("up").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("up").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("up").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                forward = new Vector(
                    float.Parse(lp.Element("forward").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("forward").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("forward").Element("Vec3").Element("z").Value.Trim().Replace("f", ""))),
                trans = new Vector(
                    float.Parse(lp.Element("trans").Element("Vec3").Element("x").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("trans").Element("Vec3").Element("y").Value.Trim().Replace("f", "")),
                    float.Parse(lp.Element("trans").Element("Vec3").Element("z").Value.Trim().Replace("f", "")))
            };

            var zipped = boneNames.Zip(hierarchy, (b, p) => new { bn = b.Trim(), pi = p.Trim() })
                         .Zip(localPoses, (z, l) => new { bn = z.bn, pi = z.pi, lp = l })
                         .Zip(modelPoses, (z, m) => new { bn = z.bn, pi = z.pi, lp = z.lp, mp = m });

            foreach (var z in zipped)
            {
                FBBone bone = new FBBone(z.bn);
                bone.ParentIndex = Convert.ToInt32(z.pi, 16);
                bone.Right       = z.lp.right;
                bone.Forward     = z.lp.forward;
                bone.Up          = z.lp.up;
                bone.Location    = z.lp.trans;
                Bones.Add(bone);

                FBBone mbone = new FBBone(z.bn);
                mbone.ParentIndex = Convert.ToInt32(z.pi, 16);
                mbone.Right       = z.mp.right;
                mbone.Forward     = z.mp.forward;
                mbone.Up          = z.mp.up;
                mbone.Location    = z.mp.trans;
                ModelBones.Add(z.bn, mbone);
            }

            for (int i = 0; i < Bones.Count; i++)
            {
                Bones[i].Children = new List <FBBone>();
                for (int j = 0; j < Bones.Count; j++)
                {
                    if (Bones[j].ParentIndex == i)
                    {
                        Bones[i].Children.Add(Bones[j]);
                    }
                }

                if (Bones[i].ParentIndex == -1 && RootBone == null)
                {
                    RootBone = Bones[i];
                }
            }
        }