Ejemplo n.º 1
0
 public SkeletonModel(Assimp.Scene scene, AllBoneInfos allBoneInfos)
 {
     this.scene    = scene;
     this.allBones = allBoneInfos;
     GeneratePositions(scene);
     GenerateBoneIndexes(this.nodes, allBoneInfos);
 }
Ejemplo n.º 2
0
        private void InitSkinInfo(Assimp.Mesh mesh, AssimpSceneContainer container)
        {
            var          boneIDs     = new uvec4[mesh.VertexCount];
            var          boneWeights = new vec4[mesh.VertexCount];
            AllBoneInfos allBones    = container.GetAllBoneInfos();
            Dictionary <string, uint> nameIndexDict = allBones.nameIndexDict;

            for (int i = 0; i < mesh.BoneCount; i++)
            {
                Assimp.Bone bone      = mesh.Bones[i]; // bones that influence this mesh.
                uint        boneIndex = nameIndexDict[bone.Name];

                for (int j = 0; j < bone.VertexWeightCount; j++)
                {
                    Assimp.VertexWeight vertexWeight = bone.VertexWeights[j];
                    uint vertexID = vertexWeight.VertexID;
                    for (int t = 0; t < 4; t++)
                    {
                        if (boneWeights[vertexID][t] == 0.0f) // fill in x y z w.
                        {
                            boneIDs[vertexID][t]     = boneIndex;
                            boneWeights[vertexID][t] = vertexWeight.Weight;
                            break;
                        }
                    }
                }
            }
            this.boneIDs     = boneIDs;
            this.boneWeights = boneWeights;
        }
Ejemplo n.º 3
0
        public JointModel(Assimp.Scene scene, AllBoneInfos allBoneInfos)
        {
            this.scene        = scene;
            this.allBoneInfos = allBoneInfos;
            this.positions    = GetPositions(scene);
            this.transforms   = GetTransforms(scene);
            this.boneIndexes  = GetIndexes(scene, allBoneInfos);
            //this.inversedTransforms = new mat4[this.transforms.Length];
            //for (int i = 0; i < this.transforms.Length; i++)
            //{
            //    this.inversedTransforms[i] = glm.inverse(this.transforms[i]);
            //}
            //AllBones allBones = container.GetAllBones();
            //BoneInfo[] boneInfos = allBones.boneInfos;
            //mat4[] offsetMats = new mat4[this.boneIndexes.Length];
            //for (int i = 0; i < this.boneIndexes.Length; i++)
            //{
            //    int index = this.boneIndexes[i];
            //    if (index >= 0)
            //    {
            //        offsetMats[i] = boneInfos[index].Bone.OffsetMatrix.ToMat4();
            //    }
            //}
            //this.offsetMats = offsetMats;
            //this.multiplys = new mat4[this.offsetMats.Length];
            //this.inverseMutiplys = new mat4[this.offsetMats.Length];
            //mat4 rootTransform = glm.inverse(scene.RootNode.Transform.ToMat4());
            //for (int i = 0; i < this.offsetMats.Length; i++)
            //{
            //    this.multiplys[i] = rootTransform * this.transforms[i] * this.offsetMats[i];
            //    this.inverseMutiplys[i] = this.inversedTransforms[i] * this.offsetMats[i];
            //}

            //Console.WriteLine("af");
        }
        public AllBoneInfos GetAllBoneInfos()
        {
            if (this.allBoneInfos == null)
            {
                this.allBoneInfos = InitBonesInfo(aiScene);
            }

            return(this.allBoneInfos);
        }
Ejemplo n.º 5
0
        private int[] GetIndexes(Assimp.Scene scene, AllBoneInfos allBoneInfos)
        {
            Dictionary <string, uint> nameIndexDict = allBoneInfos.nameIndexDict;
            var list = new List <int>();

            ParseNodeIndexes(scene.RootNode, list, nameIndexDict);

            return(list.ToArray());
        }
Ejemplo n.º 6
0
        private void GenerateBoneIndexes(Assimp.Node[] nodes, AllBoneInfos allBones)
        {
            Dictionary <string, uint> nameIndexDict = allBones.nameIndexDict;
            var boneIndexes = new int[nodes.Length];

            for (int i = 0; i < nodes.Length; i++)
            {
                Assimp.Node node = nodes[i];
                uint        index;
                if (nameIndexDict.TryGetValue(node.Name, out index))
                {
                    boneIndexes[i] = (int)index;
                }
                else
                {
                    boneIndexes[i] = -1;
                }
            }
            this.boneIndexes = boneIndexes;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="aiScene"></param>
        /// <param name="TimeInSeconds"></param>
        /// <returns></returns>
        private static mat4[] GetBoneMatrixes(Assimp.Scene aiScene, float TimeInSeconds, AllBoneInfos allBoneInfos)
        {
            if (aiScene.AnimationCount <= 0)
            {
                return(null);
            }
            double ticksPerSecond = aiScene.Animations[0].TicksPerSecond;

            if (ticksPerSecond == 0)
            {
                ticksPerSecond = 25.0;
            }
            double timeInTicks   = TimeInSeconds * ticksPerSecond;
            float  animationTime = (float)(timeInTicks % aiScene.Animations[0].DurationInTicks);

            Assimp.Matrix4x4 transform = aiScene.RootNode.Transform;
            transform.Inverse();
            ReadNodeHeirarchy(animationTime, aiScene.RootNode, aiScene.Animations[0], mat4.identity(), allBoneInfos);

            int boneCount = allBoneInfos.boneInfos.Length;
            var result    = new mat4[boneCount];

            for (int i = 0; i < boneCount; i++)
            {
                result[i] = allBoneInfos.boneInfos[i].finalTransformation;
            }

            return(result);
        }
        private static void ReadNodeHeirarchy(float animationTime, Assimp.Node node, Assimp.Animation animation, mat4 parentTransform, AllBoneInfos allBones)
        {
            string nodeName      = node.Name;
            mat4   nodeTransform = node.Transform.ToMat4();

            Assimp.NodeAnimationChannel nodeAnim = FineNodeAnim(animation, nodeName);
            if (nodeAnim != null)
            {
                mat4 mat = mat4.identity();
                // Interpolate scaling and generate scaling transformation matrix
                Assimp.Vector3D Scaling  = CalcInterpolatedScaling(animationTime, nodeAnim);
                mat4            ScalingM = glm.scale(mat, new vec3(Scaling.X, Scaling.Y, Scaling.Z));

                // Interpolate rotation and generate rotation transformation matrix
                Assimp.Quaternion RotationQ = CalcInterpolatedRotation(animationTime, nodeAnim);
                mat4 RotationM = new Assimp.Matrix4x4(RotationQ.GetMatrix()).ToMat4();

                // Interpolate translation and generate translation transformation matrix
                Assimp.Vector3D Translation  = CalcInterpolatedPosition(animationTime, nodeAnim);
                mat4            TranslationM = glm.translate(mat4.identity(), new vec3(Translation.X, Translation.Y, Translation.Z));

                // Combine the above transformations
                nodeTransform = TranslationM * RotationM * ScalingM;
            }

            mat4 transform = parentTransform * nodeTransform;

            if (allBones.nameIndexDict.ContainsKey(nodeName))
            {
                uint BoneIndex = allBones.nameIndexDict[nodeName];
                allBones.boneInfos[BoneIndex].finalTransformation = transform;
            }

            for (int i = 0; i < node.ChildCount; i++)
            {
                ReadNodeHeirarchy(animationTime, node.Children[i], animation, transform, allBones);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aiScene"></param>
        /// <param name="TimeInSeconds"></param>
        /// <param name="animationIndex"></param>
        /// <returns></returns>
        public static mat4[] GetBoneMatrixes(this Assimp.Scene aiScene, float TimeInSeconds, AllBoneInfos allBones, int animationIndex)
        {
            if (!aiScene.HasAnimations)
            {
                return(null);
            }

            if (animationIndex < 0)
            {
                animationIndex = 0;
            }
            else if (animationIndex >= aiScene.AnimationCount)
            {
                animationIndex = aiScene.AnimationCount - 1;
            }

            double ticksPerSecond = aiScene.Animations[animationIndex].TicksPerSecond;

            if (ticksPerSecond == 0)
            {
                ticksPerSecond = 25.0;
            }
            double timeInTicks   = TimeInSeconds * ticksPerSecond;
            float  animationTime = (float)(timeInTicks % aiScene.Animations[0].DurationInTicks);

            Assimp.Matrix4x4 transform = aiScene.RootNode.Transform;
            transform.Inverse();
            ReadNodeHeirarchy(animationTime, aiScene.RootNode, aiScene.Animations[0], transform.ToMat4(), allBones);

            int boneCount = allBones.boneInfos.Length;
            var result    = new mat4[boneCount];

            for (int i = 0; i < boneCount; i++)
            {
                result[i] = allBones.boneInfos[i].finalTransformation;
            }

            return(result);
        }