// @TODO - temporary processing animation context, need to think about where to put this functionality
        public Boolean ProcessAnimationContext(H1AnimationContext animContext)
        {
            H1AnimMontage montage = new H1AnimMontage();

            foreach (H1AnimationContext.AnimSequence animSeq in animContext.AnimSequences)
            {
                String animSeqName = animSeq.AnimSeqName;
                float  duration    = Convert.ToSingle(animSeq.Duration);
                float  tickInSec   = Convert.ToSingle(animSeq.TicksPerSecond);

                // @TODO - temporary insert duration variable
                Duration  = duration;
                TickInSec = tickInSec;

                foreach (H1AnimationContext.JointAnimation jointAnimation in animSeq.BoneAnimations)
                {
                    String boneName = jointAnimation.BoneName;
                    H1SlotAnimationTrack slotAnimationTrack = new H1SlotAnimationTrack();
                    slotAnimationTrack.SlotName     = boneName;
                    slotAnimationTrack.AnimTrackRef = new H1AnimTrack();

                    H1AnimSegment  animSegment  = new H1AnimSegment();
                    H1AnimSequence animSequence = new H1AnimSequence();

                    // 1. process position keys
                    animSequence.TranslationTracks.Add(new H1TranslationTrack());
                    foreach (H1AnimationContext.PositionKey positionKey in jointAnimation.PosKeys)
                    {
                        animSequence.TranslationTracks[0].PosKeys.Add(positionKey.Value);
                        animSequence.TranslationTracks[0].Times.Add(Convert.ToSingle(positionKey.Time));
                    }

                    // 2. process rotation keys
                    animSequence.RotationTracks.Add(new H1RotationTrack());
                    foreach (H1AnimationContext.QuaternionKey quaternionKey in jointAnimation.RotKeys)
                    {
                        animSequence.RotationTracks[0].RotKeys.Add(quaternionKey.Value);
                        animSequence.RotationTracks[0].Times.Add(Convert.ToSingle(quaternionKey.Time));
                    }

                    // 3. process scale keys
                    animSequence.ScaleTracks.Add(new H1ScalingTrack());
                    foreach (H1AnimationContext.ScalingKey scaleKey in jointAnimation.ScaleKeys)
                    {
                        animSequence.ScaleTracks[0].ScaleKeys.Add(scaleKey.Value);
                        animSequence.ScaleTracks[0].Times.Add(Convert.ToSingle(scaleKey.Time));
                    }

                    animSegment.SetAnimSequence(animSequence);
                    slotAnimationTrack.AnimTrackRef.AnimSegments.Add(animSegment);
                    montage.SlotAnimTracks.Add(slotAnimationTrack);
                }
            }

            H1AnimMontageInstance montageInstance = new H1AnimMontageInstance();

            montageInstance.MontageRef = montage;
            m_MontageInstances.Add(montageInstance);

            return(true);
        }
Example #2
0
        public bool Load(String fileName)
        {
            // @TODO - change the file path for asset folder in the future currently, set as executable path
            String path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets\\");

            m_FileName = path + fileName;

            // setting assimp context
            AssimpContext importer = new AssimpContext();

            importer.SetConfig(new NormalSmoothingAngleConfig(66.0f));

            // loading scene
            Scene scene = null;

            try
            {
                scene = importer.ImportFile(m_FileName, PostProcessPreset.TargetRealTimeMaximumQuality | PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs | PostProcessSteps.FlipWindingOrder);
            }
            catch (Exception e) // handling assimp exception
            {
                if (e.Source != null)
                {
                    Console.WriteLine("Error: {0}", e.Message);
                }
            }

            asset = new H1AssetContext();
            H1ModelContext convertedModel = asset.AddModel();

            // extract the meshes
            if (scene.HasMeshes)
            {
                Int32 meshCount = scene.Meshes.Count;
                List <H1MeshContext> meshContexts = new List <H1MeshContext>();

                ExtractMeshData(scene, ref meshContexts);

                foreach (H1MeshContext meshContext in meshContexts)
                {
                    convertedModel.Meshes.Add(meshContext);
                }
            }

            // extract skeletal data

            // what I learned from this failed algorithm
            // 1. root bone nodes can be multiple
            // 2. for example, blabla_Hub(containing null meshes, only space infos) have null information for weighted vertices, but it should not be considered as leaf node!
            #region Disabled Extracting skeletal mesh data methods

            /*if (scene.HasMeshes)
             * {
             *  // collect all bones from meshes
             *  List<Bone> bones = new List<Bone>();
             *  foreach (Mesh mesh in scene.Meshes)
             *  {
             *      if (mesh.HasBones)
             *      {
             *          bones.AddRange(mesh.Bones);
             *      }
             *  }
             *
             *  // find root node for all meshes
             *  Node boneRootNode = FindRootBone(bones, scene.RootNode);
             *
             *  H1SkeletalContext skeletalContext = null;
             *  skeletalContext = new H1SkeletalContext();
             *  //@TODO - need to optimize for deep copies
             *  ExtractSkeletalMeshData(boneRootNode, bones, ref skeletalContext);
             *
             *  convertedModel.SkeletalContexts.Add(skeletalContext);
             * }*/
            #endregion

            // what I learned from this failed algorithm
            // 1. the counts of bones and bone nodes (joint nodes) can be different
            // 2. bone node can be used for just transforming spaces
            // 3. bone node could have empty meshes (just includes space information)
            // 4. bones are subsidiary for bone nodes
            // 5. don't consider aiBone as bone node in hierarchy rather regards node(aiNode) as bone node in hierarchy
            // 6. consider aiBone as node bone data containing all necessary data
            #region Failed Algorithm
            // new algorithms to handle multiple root node (like pelvis and head) more efficient way

            /*if (scene.HasMeshes)
             * {
             *  // 1. extracted all bones for all meshes in the scene
             *  // NOTICE - multiple meshes in file are considered in same body but chunked several parts
             *  Dictionary<String, Bone> extractedBones = new Dictionary<String, Bone>();
             *  Dictionary<String, H1SkeletalContext.JointNode> jointNodes = new Dictionary<string, H1SkeletalContext.JointNode>();
             *  foreach (Mesh mesh in scene.Meshes)
             *  {
             *      if (mesh.HasBones)
             *      {
             *          foreach (Bone bone in mesh.Bones)
             *          {
             *              String boneName = bone.Name;
             *              if (!extractedBones.ContainsKey(boneName))
             *              {
             *                  extractedBones.Add(boneName, bone);
             *
             *                  H1SkeletalContext.JointNode newJointNode = new H1SkeletalContext.JointNode();
             *                  newJointNode.JointData.JointName = boneName;
             *                  jointNodes.Add(boneName, newJointNode);
             *              }
             *          }
             *      }
             *  }
             *
             *  // 2. process extracted bones
             *  // @TODO - naive algorithm need to optimize by searching tree and set the bones parent
             *  foreach (KeyValuePair<String, Bone> extractedBone in extractedBones)
             *  {
             *      Node node = scene.RootNode.FindNode(extractedBone.Key);
             *      H1SkeletalContext.JointNode jointNode = jointNodes[node.Name];
             *      H1SkeletalContext.JointNode parentJointNode = jointNodes[node.Parent.Name];
             *
             *      // set proper properties
             *      jointNode.Parent = parentJointNode;
             *      foreach (Node child in node.Children)
             *      {
             *          H1SkeletalContext.JointNode childJointNode = null;
             *          if (jointNodes.TryGetValue(child.Name, out childJointNode))
             *          {
             *              jointNode.Children.Add(childJointNode);
             *          }
             *      }
             *      ExtractBone(extractedBone.Value, ref jointNode.JointData);
             *  }
             *
             *  // 3. find root node(s) and set them at the front
             * }*/
            #endregion

            if (scene.HasMeshes)
            {
                H1SkeletalContext skeletalContext = null;
                skeletalContext = new H1SkeletalContext();

                // prepare data for BFS search
                List <H1SkeletalContext.JointNode> jointNodes = skeletalContext.JointNodes;
                Dictionary <String, Int32>         jointNameToJointNodeIndex = new Dictionary <String, Int32>();

                // extractedBones could have same bone name and multiple bone data
                Dictionary <String, List <Bone> > extractedBones = new Dictionary <String, List <Bone> >();
                // boneToMeshContexIndex should be mirrored same as extractedBones (index of list should be same)
                Dictionary <String, List <Int32> > boneToMeshContextIndex = new Dictionary <String, List <Int32> >();

                #region Debug Validation Weight Vertex Counts
#if DEBUG
                List <Boolean> taggedVertexIndex       = new List <Boolean>(); // verification to tag all needed vertices
                List <float>   taggedVertexWeights     = new List <float>();   // verification to tag all vertex weight for sum of each vertex
                List <Int32>   taggedVertexIndexOffset = new List <Int32>();

                // pre-test for validation
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (mesh.HasVertices)
                    {
                        foreach (Vector3D vertex in mesh.Vertices)
                        {
                            taggedVertexIndex.Add(false);
                        }
                    }

                    if (mesh.HasBones)
                    {
                        foreach (Bone bone in mesh.Bones)
                        {
                            if (bone.HasVertexWeights)
                            {
                                foreach (VertexWeight vertexWeight in bone.VertexWeights)
                                {
                                    taggedVertexIndex[vertexWeight.VertexID] = true;
                                }
                            }
                        }
                    }

                    List <Int32> notTaggedVertexIndex0 = new List <Int32>();
                    Int32        currVertexIndex0      = 0;
                    foreach (Boolean isTagged in taggedVertexIndex)
                    {
                        if (isTagged == false)
                        {
                            notTaggedVertexIndex0.Add(currVertexIndex0);
                        }
                        currVertexIndex0++;
                    }

                    if (notTaggedVertexIndex0.Count > 0)
                    {
                        return(false);
                    }

                    taggedVertexIndex.Clear();
                }
#endif
                #endregion

                Int32 validMeshContextIndex = 0;
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (mesh.HasVertices &&
                        mesh.HasNormals &&
                        mesh.HasTangentBasis &&
                        mesh.TextureCoordinateChannels[0].Count > 0 &&
                        mesh.HasFaces) // process vertices in the mesh
                    {
                        if (mesh.HasBones)
                        {
                            foreach (Bone bone in mesh.Bones)
                            {
                                String boneName = bone.Name;
                                if (!extractedBones.ContainsKey(boneName))
                                {
                                    // add new list of bone list
                                    extractedBones.Add(boneName, new List <Bone>());
                                    extractedBones[boneName].Add(bone);
                                    // add new list of mesh context index
                                    boneToMeshContextIndex.Add(boneName, new List <Int32>());
                                    boneToMeshContextIndex[boneName].Add(validMeshContextIndex);
                                }
                                else // same bone name, but different bone data exists
                                {
                                    // no need to create list of data, just add new item
                                    // bone data could have different set of weighted vertices, but with same bone name
                                    extractedBones[boneName].Add(bone);
                                    boneToMeshContextIndex[boneName].Add(validMeshContextIndex);
                                }
                            }

                            validMeshContextIndex++;

                            #region Debug Validation Weight Vertex Count
#if DEBUG
                            taggedVertexIndexOffset.Add(taggedVertexIndex.Count);
                            for (Int32 i = 0; i < mesh.VertexCount; ++i)
                            {
                                taggedVertexIndex.Add(false);
                                taggedVertexWeights.Add(0.0f);
                            }
#endif
                            #endregion
                        }
                    }
                }

                // BFS search to construct bone data
                Stack <Node> nodes = new Stack <Node>();
                nodes.Push(scene.RootNode);

                while (nodes.Count != 0)
                {
                    Node currNode = nodes.Pop();
                    H1SkeletalContext.JointNode jointNode = new H1SkeletalContext.JointNode();

                    H1SkeletalContext.JointNode parentJointNode = jointNodes.Find(x => (x.JointName == currNode.Parent.Name));
                    jointNode.Parent = parentJointNode;
                    ExtractNodeSpace(currNode, ref jointNode);

                    List <Bone>  boneDataList         = null;
                    List <Int32> meshContextIndexList = null;
                    if (extractedBones.TryGetValue(jointNode.JointName, out boneDataList))
                    {
                        meshContextIndexList = boneToMeshContextIndex[jointNode.JointName];

                        // looping bone data list, extract bone data
                        Int32 currBoneIndex = 0;
                        foreach (Bone bone in boneDataList)
                        {
                            H1SkeletalContext.Joint newJointData = new H1SkeletalContext.Joint();
                            ExtractBone(bone, ref newJointData);

                            newJointData.MeshContextIndex = meshContextIndexList[currBoneIndex];
                            currBoneIndex++;

                            // store mesh context local-to-global H1Transform for later transformation of offsetMatrix for animation
                            newJointData.MeshContextLocalToGlobal = convertedModel.Meshes[newJointData.MeshContextIndex].LocalToGlobalTransform;

                            // add new joint data
                            jointNode.JointDataList.Add(newJointData);
                        }

                        // mark this node is bone-space
                        jointNode.MarkedAsBoneSpace = true;

                        // tag its parent until it reaches the state that 'MarkedAsBoneSpace' is true
                        H1SkeletalContext.JointNode markNode = jointNode.Parent;
                        while (markNode != null && markNode.MarkedAsBoneSpace != true)
                        {
                            markNode.MarkedAsBoneSpace = true;
                            markNode = markNode.Parent;
                        }
                    }
                    else
                    {
                        // for debugging
                    }

                    foreach (Node child in currNode.Children)
                    {
                        nodes.Push(child);
                        // tag child names to process child nodes after BFS search
                        jointNode.ChildNodeNames.Add(child.Name);
                    }

                    // add new joint node
                    Int32 newJointNodeIndex = jointNodes.Count;
                    jointNodes.Add(jointNode);
                    jointNameToJointNodeIndex.Add(jointNode.JointName, newJointNodeIndex);
                }

                // process tagged child nodes
                foreach (H1SkeletalContext.JointNode node in jointNodes)
                {
                    foreach (String childName in node.ChildNodeNames)
                    {
                        node.Children.Add(jointNodes[jointNameToJointNodeIndex[childName]]);
                    }
                }

                #region Debug Validation for total weight value of weighted vertices & vertex counts
#if DEBUG
                foreach (H1SkeletalContext.JointNode node in jointNodes)
                {
                    foreach (H1SkeletalContext.Joint jointData in node.JointDataList)
                    {
                        foreach (H1SkeletalContext.WeightedVertex weightedVertex in jointData.WeightedVertices)
                        {
                            // confirm all vertices in Mesh are tagged by weighted vertices
                            taggedVertexIndex[taggedVertexIndexOffset[jointData.MeshContextIndex] + weightedVertex.VertexIndex] = true;
                            // add vertex weight to verify
                            taggedVertexWeights[taggedVertexIndexOffset[jointData.MeshContextIndex] + weightedVertex.VertexIndex] += weightedVertex.Weight;
                        }
                    }
                }

                // verification code to extract not tagged vertex index
                List <Int32> notTaggedVertexIndex = new List <Int32>();
                Int32        currVertexIndex      = 0;
                foreach (Boolean isTagged in taggedVertexIndex)
                {
                    if (isTagged == false)
                    {
                        notTaggedVertexIndex.Add(currVertexIndex);
                    }
                    currVertexIndex++;
                }
                if (notTaggedVertexIndex.Count > 0)
                {
                    return(false);
                }

                // verification code to extract vertex which has invalid vertex weight value
                List <Int32> invalidVertexWeightVertexIndex = new List <Int32>();
                currVertexIndex = 0;
                foreach (float currVertexWeights in taggedVertexWeights)
                {
                    if (currVertexWeights < 0.99f)
                    {
                        invalidVertexWeightVertexIndex.Add(currVertexIndex);
                    }
                }
                if (invalidVertexWeightVertexIndex.Count > 0)
                {
                    return(false);
                }
#endif
                #endregion

                convertedModel.SkeletalContexts.Add(skeletalContext);
            }

            // extract the animations
            if (scene.HasAnimations)
            {
                H1AnimationContext newAnimContext = new H1AnimationContext();
                for (Int32 animIndex = 0; animIndex < scene.AnimationCount; ++animIndex)
                {
                    Animation currAnimation = scene.Animations[animIndex];
                    if (currAnimation.HasNodeAnimations) // we only handle node animations (not vertex animation)
                    {
                        // create new animation sequence
                        H1AnimationContext.AnimSequence newAnimSeq = new H1AnimationContext.AnimSequence();
                        newAnimSeq.AnimSeqName    = currAnimation.Name;
                        newAnimSeq.Duration       = currAnimation.DurationInTicks;
                        newAnimSeq.TicksPerSecond = currAnimation.TicksPerSecond;

                        foreach (NodeAnimationChannel animChannel in currAnimation.NodeAnimationChannels)
                        {
                            H1AnimationContext.JointAnimation newJointAnim = new H1AnimationContext.JointAnimation();
                            newJointAnim.BoneName = animChannel.NodeName;

                            // calculate maximum number of keys to fill up the special case (only holding one key)
                            Int32 maxNumKeys = Math.Max(Math.Max(animChannel.ScalingKeyCount, animChannel.RotationKeyCount), animChannel.PositionKeyCount);

                            if (animChannel.HasPositionKeys)
                            {
                                if (animChannel.PositionKeyCount == 1) // special handling (only holding one key)
                                {
                                    VectorKey positionKey = animChannel.PositionKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.PositionKey newPosKey = new H1AnimationContext.PositionKey();
                                        newPosKey.Time  = positionKey.Time;
                                        newPosKey.Value = new Vector3(positionKey.Value.X, positionKey.Value.Y, positionKey.Value.Z);

                                        newJointAnim.PosKeys.Add(newPosKey);
                                    }
                                }

                                else
                                {
                                    foreach (VectorKey positionKey in animChannel.PositionKeys)
                                    {
                                        H1AnimationContext.PositionKey newPosKey = new H1AnimationContext.PositionKey();
                                        newPosKey.Time  = positionKey.Time;
                                        newPosKey.Value = new Vector3(positionKey.Value.X, positionKey.Value.Y, positionKey.Value.Z);

                                        newJointAnim.PosKeys.Add(newPosKey);
                                    }
                                }
                            }

                            if (animChannel.HasRotationKeys)
                            {
                                if (animChannel.RotationKeyCount == 1) // special handling (only holding one key)
                                {
                                    QuaternionKey quatKey = animChannel.RotationKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.QuaternionKey newQuatKey = new H1AnimationContext.QuaternionKey();
                                        newQuatKey.Time  = quatKey.Time;
                                        newQuatKey.Value = new SharpDX.Quaternion(quatKey.Value.X, quatKey.Value.Y, quatKey.Value.Z, quatKey.Value.W);

                                        newJointAnim.RotKeys.Add(newQuatKey);
                                    }
                                }

                                else
                                {
                                    foreach (QuaternionKey quatKey in animChannel.RotationKeys)
                                    {
                                        H1AnimationContext.QuaternionKey newQuatKey = new H1AnimationContext.QuaternionKey();
                                        newQuatKey.Time  = quatKey.Time;
                                        newQuatKey.Value = new SharpDX.Quaternion(quatKey.Value.X, quatKey.Value.Y, quatKey.Value.Z, quatKey.Value.W);

                                        newJointAnim.RotKeys.Add(newQuatKey);
                                    }
                                }
                            }

                            if (animChannel.HasScalingKeys)
                            {
                                if (animChannel.ScalingKeyCount == 1) // special handling (only holding one key)
                                {
                                    VectorKey scalingKey = animChannel.ScalingKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.ScalingKey newScalingKey = new H1AnimationContext.ScalingKey();
                                        newScalingKey.Time  = scalingKey.Time;
                                        newScalingKey.Value = new Vector3(scalingKey.Value.X, scalingKey.Value.Y, scalingKey.Value.Z);

                                        newJointAnim.ScaleKeys.Add(newScalingKey);
                                    }
                                }

                                else
                                {
                                    foreach (VectorKey scalingKey in animChannel.ScalingKeys)
                                    {
                                        H1AnimationContext.ScalingKey newScalingKey = new H1AnimationContext.ScalingKey();
                                        newScalingKey.Time  = scalingKey.Time;
                                        newScalingKey.Value = new Vector3(scalingKey.Value.X, scalingKey.Value.Y, scalingKey.Value.Z);

                                        newJointAnim.ScaleKeys.Add(newScalingKey);
                                    }
                                }
                            }

                            newAnimSeq.BoneAnimations.Add(newJointAnim);
                        }

                        newAnimContext.AnimSequences.Add(newAnimSeq);
                    }
                }

                // set the animation context
                convertedModel.AnimationContext = newAnimContext;
            }

            return(true);
        }