Exemple #1
0
        public Boolean ProcessRefSkeletonFromAssetContext(H1SkeletalContext.JointNode node, ref H1ReferenceSkeleton refSkeleton)
        {
            H1SkeletalContext.Joint jointData = null; // node.JointData;

            // add mesh bone info
            H1MeshBoneInfo meshBoneInfo = new H1MeshBoneInfo();

            //meshBoneInfo.Name = jointData.JointName;

            if (refSkeleton.NameToIndexMapIsEmpty())
            {
                // should be root node, so parent index is '0'
                meshBoneInfo.ParentIndex = 0;
            }
            else if (node.Parent == null)
            {
                // should be sub-root node, so parent index is '0' (root motion node)
                meshBoneInfo.ParentIndex = refSkeleton.GetIndexByBoneName("RootMotionNode");
            }
            else
            //meshBoneInfo.ParentIndex = refSkeleton.GetIndexByBoneName(node.Parent.JointData.JointName);

            if (meshBoneInfo.ParentIndex == -1)
            {
                return(false);
            }

            // the new index should be same with RefBoneBases
            Int32 newBoneInfoIndex = refSkeleton.RefBoneInfoList.Count;
            Int32 newBoneBaseIndex = refSkeleton.RefBoneBases.Count;

            if (newBoneBaseIndex != newBoneInfoIndex)
            {
                return(false);
            }

            refSkeleton.RefBoneInfoList.Add(meshBoneInfo);

            // add bone base
            H1Transform boneBase = new H1Transform();

            boneBase.Translation = jointData.OffsetTransformation.Translation;
            boneBase.Scaling     = jointData.OffsetTransformation.Scaling;
            boneBase.Rotation    = jointData.OffsetTransformation.Rotation;

            refSkeleton.RefBoneBases.Add(boneBase);

            // add NameToIndex
            refSkeleton.AddNameToIndexPair(meshBoneInfo.Name, newBoneInfoIndex);

            return(true);
        }
Exemple #2
0
        public H1StaticLODModel PrepareProcessAssetContext(H1SkeletalContext skeletalContext)
        {
            if (m_SkeletalMeshResource == null)
            {
                return(null);
            }

            // @TODO - temporary low LOD model, fix this
            H1StaticLODModel staticLODModel = m_SkeletalMeshResource.AddLODModel();

            // add root node that controls all animation motion
            H1ReferenceSkeleton refSkeleton = m_Skeleton.RefSkeleton;

            // construct refSkeleton
            List <H1SkeletalContext.JointNode> jointNodes = skeletalContext.JointNodes;

            // to construct local transforms for each bone
            List <BoneOffsetTransformSet> boneOffsetTransforms = new List <BoneOffsetTransformSet>();

            foreach (H1SkeletalContext.JointNode jointNode in jointNodes)
            {
                // if is not marked as bone node space, skip
                if (!jointNode.MarkedAsBoneSpace)
                {
                    continue;
                }

                // prepare data to process bone node
                H1MeshBoneInfo meshBoneInfo = new H1MeshBoneInfo();
                meshBoneInfo.Name = jointNode.JointName;

                // recursively find bone-space marked parent node (based on H1SkeletalContext.JointNode)
                // we need to reapply parent index based on 'refSkeleton.RefBoneInfoList'
                Int32 currParentIndex = jointNodes.FindIndex(x =>
                {
                    if (jointNode.Parent == null)
                    {
                        return(false);
                    }
                    return(x.JointName == jointNode.Parent.JointName);
                });
                while (currParentIndex != -1 && // if not reaching to root node
                       !jointNodes[currParentIndex].MarkedAsBoneSpace)  // and if is not marked by bone-space
                {
                    currParentIndex = jointNodes.FindIndex(x =>
                    {
                        if (jointNodes[currParentIndex].Parent == null)
                        {
                            return(false);
                        }
                        return(x.JointName == jointNodes[currParentIndex].Parent.JointName);
                    });
                }
                meshBoneInfo.ParentIndex = currParentIndex;

                // the new index should be same with RefBoneBases
                Int32 newBoneInfoIndex = refSkeleton.RefBoneInfoList.Count;
                Int32 newBoneBaseIndex = refSkeleton.RefBoneBases.Count;
                if (newBoneBaseIndex != newBoneInfoIndex)
                {
                    return(null);
                }

                refSkeleton.RefBoneInfoList.Add(meshBoneInfo);

                // what I learned from this disabled code section
                // 1. ASSIMP has separate node space and bone node space (transformation matrix)
                // 2. we need to handle those differently
                //  - vertices resided in particular node space (mesh space we called) should be transformed into root node space (world space)
                //  - localTransform in refSkeleton should be set by offsetMatrix in JointNode.JointData
                H1Transform boneBase = new H1Transform();
                boneBase.Translation = jointNode.NodeLocalTransform.Translation;
                boneBase.Scaling     = jointNode.NodeLocalTransform.Scaling;
                boneBase.Rotation    = jointNode.NodeLocalTransform.Rotation;

                // allocate the space in advance
                refSkeleton.RefBoneBases.Add(boneBase);

                refSkeleton.AddNameToIndexPair(meshBoneInfo.Name, newBoneInfoIndex);

                // add bone offset transform set
                boneOffsetTransforms.Add(new BoneOffsetTransformSet());

                // looping joint data
                foreach (H1SkeletalContext.Joint jointData in jointNode.JointDataList)
                {
                    H1Transform meshContextLocalToGlobalTransform = jointData.MeshContextLocalToGlobal;

                    // convert 'offsetTransformation' to H1Transform
                    H1SkeletalContext.Transformation offsetTransformation = jointData.OffsetTransformation;
                    H1Transform offsetTransform = new H1Transform();
                    offsetTransform.Translation = offsetTransformation.Translation;
                    offsetTransform.Rotation    = offsetTransformation.Rotation;
                    offsetTransform.Scaling     = offsetTransformation.Scaling;

                    // apply meshContextLocalToGlobal
                    Matrix     result = Matrix.Multiply(Matrix.Invert(meshContextLocalToGlobalTransform.Transformation), offsetTransform.Transformation);
                    Vector3    resultTranslation;
                    Vector3    resultScaling;
                    Quaternion resultRotation;
                    result.Decompose(out resultScaling, out resultRotation, out resultTranslation);

                    offsetTransform.Translation = resultTranslation;
                    offsetTransform.Rotation    = resultRotation;
                    offsetTransform.Scaling     = resultScaling;

                    boneOffsetTransforms.Last().BoneOffsetTransforms.Add(offsetTransform);
                }
            }

            // construct parent index for H1MeshBoneInfo
            foreach (H1MeshBoneInfo meshBoneInfo in refSkeleton.RefBoneInfoList)
            {
                if (meshBoneInfo.ParentIndex == -1) // not handling root node case
                {
                    continue;
                }

                String parentName = jointNodes[meshBoneInfo.ParentIndex].JointName;
                meshBoneInfo.ParentIndex = refSkeleton.RefBoneInfoList.FindIndex(x =>
                {
                    return(x.Name == parentName);
                });
            }

            // @TODO - handle multiple offset transformations
            // set offset matrix
            Int32 currBoneIndex = 0;

            foreach (H1MeshBoneInfo meshBoneInfo in refSkeleton.RefBoneInfoList)
            {
                // add to global offset transform
                H1Transform globalOffsetTransform = boneOffsetTransforms[currBoneIndex].BoneOffsetTransforms.Count > 0 ? boneOffsetTransforms[currBoneIndex].BoneOffsetTransforms[0] : new H1Transform();
                refSkeleton.RefOffsetBases.Add(globalOffsetTransform);

                currBoneIndex++;
            }

            return(staticLODModel);
        }