Ejemplo n.º 1
0
        public BoneAnimation LoadBoneAnimation(Assimp.Scene aScene, Assimp.Node aNode, BoneAnimation parent)
        {
            var boneAnimation = new BoneAnimation()
            {
                Parent    = parent,
                Children  = new List <BoneAnimation>(),
                Positions = new List <Vector3>(),
                Rotations = new List <Quaternion>(),
                Scales    = new List <Vector3>()
            };

            boneAnimation.Name = aNode.Name;

            var animationChanel = aScene.Animations[0].NodeAnimationChannels.Where(n => n.NodeName == boneAnimation.Name).FirstOrDefault();

            if (animationChanel != null)
            {
                boneAnimation.IsAnimate = true;

                foreach (var aScale in animationChanel.ScalingKeys)
                {
                    var scale = new Vector3(aScale.Value.X, aScale.Value.Y, aScale.Value.Z);
                    boneAnimation.Scales.Add(scale);
                }

                foreach (var aRotation in animationChanel.RotationKeys)
                {
                    var rotation = new Quaternion(aRotation.Value.X, aRotation.Value.Y, aRotation.Value.Z, aRotation.Value.W);
                    boneAnimation.Rotations.Add(rotation);
                }

                foreach (var aTranslate in animationChanel.PositionKeys)
                {
                    var translate = new Vector3(aTranslate.Value.X, aTranslate.Value.Y, aTranslate.Value.Z);
                    boneAnimation.Positions.Add(translate);
                }
            }
            else
            {
                boneAnimation.IsAnimate      = false;
                boneAnimation.Transformation = Matrix.Transpose(AssimpHelper.MatrixAssimpToXna(aNode.Transform));
            }

            foreach (var child in aNode.Children)
            {
                boneAnimation.Children.Add(LoadBoneAnimation(aScene, child, boneAnimation));
            }
            BoneAnimations.Add(boneAnimation);
            return(boneAnimation);
        }
Ejemplo n.º 2
0
        Bone GetBone(Mesh mesh, Assimp.Bone aBone)
        {
            var bone = mesh.Bones.FirstOrDefault(b => b.Name == aBone.Name);

            if (bone == null)
            {
                var offsetMatrix = aBone.OffsetMatrix;
                offsetMatrix.Transpose();

                bone               = new Bone();
                bone.Name          = aBone.Name;
                bone.Index         = mesh.Bones.Count;
                bone.Offset        = AssimpHelper.MatrixAssimpToXna(offsetMatrix);
                bone.OffsetInverse = Matrix.Invert(AssimpHelper.MatrixAssimpToXna(offsetMatrix));
                mesh.Bones.Add(bone);
            }
            return(bone);
        }
Ejemplo n.º 3
0
        public void Initialize()
        {
            var vertices = new List <MeshVerticeInfo>();
            var indices  = new List <int>();

            AssimpContext importer = new AssimpContext();
            Scene         aScene   = importer.ImportFile(FilePath, PostProcessPreset.TargetRealTimeMaximumQuality);

            foreach (var aMesh in aScene.Meshes)
            {
                for (int faceIndex = 0; faceIndex < aMesh.FaceCount; faceIndex++)
                {
                    for (int vertexNum = 0; vertexNum < 3; vertexNum++)
                    {
                        int     verticeIndice   = aMesh.Faces[faceIndex].Indices[vertexNum];
                        Vector3 verticePosition = AssimpHelper.VectorAssimpToXna(aMesh.Vertices[verticeIndice]);
                        Vector3 verticeNormal   = AssimpHelper.VectorAssimpToXna(aMesh.Normals[verticeIndice]);
                        Vector3 uv        = AssimpHelper.VectorAssimpToXna(aMesh.TextureCoordinateChannels[0][verticeIndice]);
                        var     verticeUv = new Vector2(uv.X, uv.Y);

                        var vertice = new MeshVerticeInfo()
                        {
                            Position          = verticePosition,
                            Normal            = verticeNormal,
                            TextureCoordinate = verticeUv
                        };

                        indices.Add(vertices.Count);
                        vertices.Add(vertice);
                    }
                }
                FaceCount += aMesh.FaceCount;
            }

            VertexBuffer = new VertexBuffer(GraphicsDevice, typeof(SimpleModelVertex), vertices.Count, BufferUsage.WriteOnly);
            VertexBuffer.SetData <SimpleModelVertex>(vertices.Select(v => v.ToSimpleModelVertex()).ToArray());
            IndexBuffer = new IndexBuffer(GraphicsDevice, typeof(int), indices.Count, BufferUsage.WriteOnly);
            IndexBuffer.SetData(indices.ToArray());
        }
Ejemplo n.º 4
0
        public void Initialize()
        {
            var importer = new AssimpContext();
            var aScene   = importer.ImportFile(FilePath, PostProcessPreset.TargetRealTimeMaximumQuality);

            Meshes = new List <Mesh>();

            foreach (var aMesh in aScene.Meshes)
            {
                var verticesResult = new List <MeshSkinnedVerticeInfo>();
                var indicesResult  = new List <int>();

                var mesh = new Mesh();
                mesh.Bones = new List <Bone>();

                mesh.Name = aMesh.Name;

                Dictionary <int, List <VerticeWeight> > VerticeWeights = new Dictionary <int, List <VerticeWeight> >();
                foreach (var aBone in aMesh.Bones)
                {
                    Bone bone = GetBone(mesh, aBone);

                    foreach (var vw in aBone.VertexWeights)
                    {
                        if (!VerticeWeights.ContainsKey(vw.VertexID))
                        {
                            VerticeWeights.Add(vw.VertexID, new List <VerticeWeight>());
                        }
                        VerticeWeights[vw.VertexID].Add(new VerticeWeight()
                        {
                            Bone = bone, Weight = vw.Weight
                        });
                    }
                }

                var c = aScene.Materials[aMesh.MaterialIndex].ColorDiffuse;

                for (int faceIndex = 0; faceIndex < aMesh.FaceCount; faceIndex++)
                {
                    for (int vertexNum = 0; vertexNum < 3; vertexNum++)
                    {
                        int     verticeIndice   = aMesh.Faces[faceIndex].Indices[vertexNum];
                        Vector3 verticePosition = AssimpHelper.VectorAssimpToXna(aMesh.Vertices[verticeIndice]);
                        Vector3 verticeNormal   = AssimpHelper.VectorAssimpToXna(aMesh.Normals[verticeIndice]);

                        var uv        = AssimpHelper.VectorAssimpToXna(aMesh.TextureCoordinateChannels[0][verticeIndice]);
                        var verticeUv = new Vector2(uv.X, uv.Y);

                        BlendInfo blendInfo = GetBlendInfo(VerticeWeights, verticeIndice);

                        var vertice = new MeshSkinnedVerticeInfo()
                        {
                            Position          = verticePosition,
                            Normal            = verticeNormal,
                            TextureCoordinate = verticeUv,
                            BoneID            = blendInfo.BoneId,
                            BoneWeight        = blendInfo.Weight
                        };

                        indicesResult.Add(verticesResult.Count);
                        verticesResult.Add(vertice);
                    }
                }

                mesh.TextureFilePath = aScene.Materials[aMesh.MaterialIndex].TextureDiffuse.FilePath;

                mesh.VertexBuffer = new VertexBuffer(GraphicsDevice, typeof(SkinnedModelVertex), verticesResult.Count, BufferUsage.WriteOnly);
                mesh.VertexBuffer.SetData <SkinnedModelVertex>(verticesResult.Select(v => v.ToVertexPositionNormalTextureBones()).ToArray());

                mesh.IndexBuffer = new IndexBuffer(GraphicsDevice, typeof(int), indicesResult.Count, BufferUsage.WriteOnly);
                mesh.IndexBuffer.SetData(indicesResult.ToArray());

                mesh.FaceCount = aMesh.FaceCount;

                Meshes.Add(mesh);
            }
        }