Esempio n. 1
0
 void TestAnimationAttachment(Assimp.MeshAnimationAttachment netAnimMesh, AssimpSharp.AnimMesh sharpAnimMesh)
 {
     Assert.AreEqual(netAnimMesh.HasVertices, sharpAnimMesh.HasPosition);
     if (netAnimMesh.HasVertices)
     {
         Assert.AreEqual(netAnimMesh.Vertices, sharpAnimMesh.Vertices);
     }
     Assert.AreEqual(netAnimMesh.HasNormals, sharpAnimMesh.HasNormals);
     if (netAnimMesh.HasNormals)
     {
         Assert.AreEqual(netAnimMesh.Normals, sharpAnimMesh.Normals);
     }
     Assert.AreEqual(netAnimMesh.HasTangentBasis, sharpAnimMesh.HasTangentsAndBitangets);
     if (netAnimMesh.HasTangentBasis)
     {
         Assert.AreEqual(netAnimMesh.Tangents, sharpAnimMesh.Tangents);
         Assert.AreEqual(netAnimMesh.BiTangents, sharpAnimMesh.Bitangents);
     }
     for (int i = 0; i < netAnimMesh.TextureCoordinateChannelCount; i++)
     {
         Assert.AreEqual(netAnimMesh.HasTextureCoords(i), sharpAnimMesh.HasTextureCoords(i));
         if (netAnimMesh.HasTextureCoords(i))
         {
             Assert.AreEqual(netAnimMesh.TextureCoordinateChannels[i], sharpAnimMesh.TextureCoords[i]);
         }
     }
     for (int i = 0; i < netAnimMesh.VertexColorChannelCount; i++)
     {
         Assert.AreEqual(netAnimMesh.HasVertexColors(i), sharpAnimMesh.HasVertexColors(i));
         if (netAnimMesh.HasVertexColors(i))
         {
             Assert.AreEqual(netAnimMesh.VertexColorChannels[i], sharpAnimMesh.Colors[i]);
         }
     }
 }
Esempio n. 2
0
        void TestMesh(Assimp.Mesh netMesh, AssimpSharp.Mesh sharpMesh)
        {
            // Vertices
            Assert.AreEqual(netMesh.HasVertices, sharpMesh.Vertices != null);
            if (netMesh.HasVertices)
            {
                Assert.AreEqual(netMesh.VertexCount, sharpMesh.Vertices.Length);
                MathAssert.AreEqual(netMesh.Vertices, sharpMesh.Vertices);
            }
            // Faces
            Assert.AreEqual(netMesh.HasFaces, sharpMesh.Faces != null);
            if (netMesh.HasFaces)
            {
                Assert.AreEqual(netMesh.FaceCount, sharpMesh.Faces.Length);
                for (int i = 0; i < netMesh.FaceCount; i++)
                {
                    Assert.AreEqual(netMesh.Faces[i].HasIndices, sharpMesh.Faces[i].Indices != null);
                    Assert.AreEqual(netMesh.Faces[i].IndexCount, sharpMesh.Faces[i].Indices.Length);
                    Assert.AreEqual(netMesh.Faces[i].Indices, sharpMesh.Faces[i].Indices);
                }
            }
            // Normals
            Assert.AreEqual(netMesh.HasNormals, sharpMesh.Normals != null);
            if (netMesh.HasNormals)
            {
                MathAssert.AreEqual(netMesh.Normals, sharpMesh.Normals);
            }

            // BiTangents
            Assert.AreEqual(netMesh.HasTangentBasis, sharpMesh.Bitangents != null && sharpMesh.Bitangents.Length > 0);
            if (netMesh.HasTangentBasis)
            {
                MathAssert.AreEqual(netMesh.BiTangents, sharpMesh.Bitangents);
            }
            // PrimitiveType
            // AssimpNet BUG!!
            //Assert.AreEqual(netMesh.PrimitiveType.ToString(), Enum.GetName(typeof(AssimpSharp.PrimitiveType), sharpMesh.PrimitiveTypes));
            // TexureCoord
            for (int i = 0; i < netMesh.TextureCoordinateChannelCount; i++)
            {
                Assert.AreEqual(netMesh.HasTextureCoords(i), sharpMesh.HasTextureCoords(i));
                MathAssert.AreEqual(netMesh.TextureCoordinateChannels[i], sharpMesh.TextureCoords[i]);
            }
            // VertexColorChannels
            for (int i = 0; i < netMesh.VertexColorChannelCount; i++)
            {
                Assert.AreEqual(netMesh.HasVertexColors(i), sharpMesh.HasVertexColors(i));
                if (netMesh.HasVertexColors(i))
                {
                    Assert.AreEqual(netMesh.VertexColorChannels[i], sharpMesh.Colors[i]);
                }
            }
            // MaterialIndex
            Assert.AreEqual(netMesh.MaterialIndex, sharpMesh.MaterialIndex);
            // Name
            Assert.AreEqual(netMesh.Name, sharpMesh.Name);
            // UVComponentCount
            Assert.AreEqual(netMesh.UVComponentCount, sharpMesh.NumUVComponents);
            // MeshAnimationAttachments
            Assert.AreEqual(netMesh.HasMeshAnimationAttachments, sharpMesh.AnimMeshes != null);
            if (netMesh.HasMeshAnimationAttachments)
            {
                for (int i = 0; i < netMesh.MeshAnimationAttachmentCount; i++)
                {
                    TestAnimationAttachment(netMesh.MeshAnimationAttachments[i], sharpMesh.AnimMeshes[i]);
                }
            }
        }
Esempio n. 3
0
        public void LoadMesh(AssimpSharp.Mesh mesh)
        {
            var vertexSource = new VertexPositionNormalTexture[mesh.Vertices.Length];
            for (int i = 0; i < mesh.Vertices.Length; i++)
            {
                vertexSource[i].Position = mesh.Vertices[i];
                vertexSource[i].Normal = mesh.Normals[i];
                vertexSource[i].TextureCoordinate = Vector2.Zero;
            }
            if (mesh.HasTextureCoords(0))
            {
                var channel = mesh.TextureCoords[0];
                for(int i=0; i<channel.Length; i++)
                {
                    vertexSource[i].TextureCoordinate.X = channel[i].X;
                    vertexSource[i].TextureCoordinate.Y = 1 - channel[i].Y;
                }
            }
            var vbd = new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = VertexPositionNormalTexture.Size * vertexSource.Length,
                Usage = ResourceUsage.Default,
                StructureByteStride = VertexPositionNormalTexture.Size,
            };
            vertices = Buffer.Create(device, vertexSource, vbd);

            var indexSource = new List<int>();
            for(int i=0; i<mesh.Faces.Length; i++)
            {
                var face = mesh.Faces[i];
                if (face.Indices.Length == 3)
                {
                    indexSource.AddRange(face.Indices.Reverse());
                }
                else if (face.Indices.Length == 4)
                {
                    indexSource.Add(face.Indices[2]);
                    indexSource.Add(face.Indices[1]);
                    indexSource.Add(face.Indices[0]);
                    indexSource.Add(face.Indices[0]);
                    indexSource.Add(face.Indices[3]);
                    indexSource.Add(face.Indices[2]);
                }
                else if (face.Indices.Length == 5)
                {
                    indexSource.Add(face.Indices[2]);
                    indexSource.Add(face.Indices[1]);
                    indexSource.Add(face.Indices[0]);
                    indexSource.Add(face.Indices[0]);
                    indexSource.Add(face.Indices[3]);
                    indexSource.Add(face.Indices[2]);
                    indexSource.Add(face.Indices[0]);
                    indexSource.Add(face.Indices[4]);
                    indexSource.Add(face.Indices[3]);
                }
                else
                {
                    throw (new Exception("invalid vertex count of polygon"));
                }
            }
            var ibd = new BufferDescription()
            {
                BindFlags = BindFlags.IndexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = Utilities.SizeOf<int>() * indexSource.Count,
                Usage = ResourceUsage.Default,
                StructureByteStride = Utilities.SizeOf<int>(),
            };
            indices = Buffer.Create(device, indexSource.ToArray(), ibd);
            indexCount = indexSource.Count;
        }