Example #1
0
        private static Ai.Mesh CreateAiMeshFromSubMesh(SubMesh subMesh, Mesh mesh, Object obj, Ai.Scene aiScene, string name)
        {
            var aiMesh = new Ai.Mesh(name, Ai.PrimitiveType.Triangle);

            if (mesh.Positions != null)
            {
                aiMesh.Vertices.Capacity = mesh.Positions.Length;
                aiMesh.Vertices.AddRange(mesh.Positions.Select(x => x.ToAssimp()));
            }

            if (mesh.Normals != null)
            {
                aiMesh.Normals.Capacity = mesh.Normals.Length;
                aiMesh.Normals.AddRange(mesh.Normals.Select(x => x.ToAssimp()));
            }

            for (int i = 0; i < 4; i++)
            {
                var texCoords = mesh.GetTexCoordsChannel(i);

                if (texCoords == null)
                {
                    continue;
                }

                aiMesh.TextureCoordinateChannels[i].Capacity = texCoords.Length;
                aiMesh.TextureCoordinateChannels[i].AddRange(texCoords.Select(x => new Ai.Vector3D(x.ToAssimp(), 0)));
            }

            for (int i = 0; i < 2; i++)
            {
                var colors = mesh.GetColorsChannel(i);

                if (colors == null)
                {
                    continue;
                }

                aiMesh.VertexColorChannels[i].Capacity = colors.Length;
                aiMesh.VertexColorChannels[i].AddRange(colors.Select(x => x.ToAssimp()));
            }

            if (mesh.BoneWeights != null)
            {
                for (int i = 0; i < subMesh.BoneIndices.Length; i++)
                {
                    ushort boneIndex = subMesh.BoneIndices[i];
                    var    bone      = obj.Skin.Bones[boneIndex];

                    var aiBone = new Ai.Bone();

                    aiBone.Name         = bone.Name;
                    aiBone.OffsetMatrix = bone.InverseBindPoseMatrix.ToAssimpTransposed();

                    for (int j = 0; j < mesh.BoneWeights.Length; j++)
                    {
                        var boneWeight = mesh.BoneWeights[j];

                        if (boneWeight.Index1 == i)
                        {
                            aiBone.VertexWeights.Add(new Ai.VertexWeight(j, boneWeight.Weight1));
                        }

                        if (boneWeight.Index2 == i)
                        {
                            aiBone.VertexWeights.Add(new Ai.VertexWeight(j, boneWeight.Weight2));
                        }

                        if (boneWeight.Index3 == i)
                        {
                            aiBone.VertexWeights.Add(new Ai.VertexWeight(j, boneWeight.Weight3));
                        }

                        if (boneWeight.Index4 == i)
                        {
                            aiBone.VertexWeights.Add(new Ai.VertexWeight(j, boneWeight.Weight4));
                        }
                    }

                    aiMesh.Bones.Add(aiBone);
                }
            }

            var triangles = subMesh.GetTriangles();

            aiMesh.Faces.Capacity = triangles.Count;

            aiMesh.Faces.AddRange(triangles.Select(x =>
            {
                var aiFace = new Ai.Face();
                aiFace.Indices.Capacity = 3;
                aiFace.Indices.Add(( int )x.A);
                aiFace.Indices.Add(( int )x.B);
                aiFace.Indices.Add(( int )x.C);
                return(aiFace);
            }));

            var material = obj.Materials[( int )subMesh.MaterialIndex];

            int materialIndex = aiScene.Materials.FindIndex(x => x.Name == material.Name + "+" + obj.Name);

            if (materialIndex == -1)
            {
                materialIndex = aiScene.Materials.FindIndex(x => x.Name == material.Name);
            }

            aiMesh.MaterialIndex = materialIndex;

            return(aiMesh);
        }