private static Mesh FromAssimp(Module.Import.Assimp.Context context, aiMesh mesh_data) { // Create new mesh Mesh mesh = new Mesh(); // Assimp does not support submeshes mesh.submeshes = new SubMesh[1]; mesh.submeshes[0] = new SubMesh(); // Get material associated to this mesh mesh.assimpMaterial = (int)mesh_data.mMaterialIndex; // Get mesh name using (aiString mesh_name = mesh_data.mName) { mesh.name = Assimp.Convert.Name(mesh_name, "mesh"); } // Get vertices if (mesh_data.HasPositions()) { using (aiVector3DArray vertices = mesh_data.Vertices) { mesh.vertices = Assimp.Convert.AssimpToUnity.Array <aiVector3D, Vector3>(Assimp.Convert.AssimpToUnity.Vector3, vertices); } } // Get normals if (mesh_data.HasNormals()) { using (aiVector3DArray normals = mesh_data.Normals) { mesh.normals = Assimp.Convert.AssimpToUnity.Array <aiVector3D, Vector3>(Assimp.Convert.AssimpToUnity.Vector3, normals); } } // Get tangents if (mesh_data.HasTangentsAndBitangents()) { using (aiVector3DArray tangents = mesh_data.Tangents) { mesh.tangents = Assimp.Convert.AssimpToUnity.Array <aiVector3D, Vector4>(Assimp.Convert.AssimpToUnity.Tangent, tangents); } } // Get faces if (mesh_data.HasFaces()) { using (aiFaceArray faces = mesh_data.Faces) { mesh.submeshes[0].triangles = Assimp.Convert.AssimpToUnity.Face(faces, out mesh.submeshes[0].topology); } } // Get UV coords if (mesh_data.GetNumUVChannels() > 0 && mesh_data.HasTextureCoords(0)) { using (aiVector3DMultiArray texture_coords = mesh_data.TextureCoords) { using (aiVector3DArray texture_coords0 = texture_coords.Get(0)) { mesh.uv1 = Assimp.Convert.AssimpToUnity.Array <aiVector3D, Vector2>(Assimp.Convert.AssimpToUnity.UV, texture_coords0); } if (mesh_data.GetNumUVChannels() > 1 && mesh_data.HasTextureCoords(1)) { using (aiVector3DArray texture_coords1 = texture_coords.Get(1)) { mesh.uv2 = Assimp.Convert.AssimpToUnity.Array <aiVector3D, Vector2>(Assimp.Convert.AssimpToUnity.UV, texture_coords1); } } } } else { // No texture UVs. We need to generate some to avoid problems with most default unity shaders int size = mesh.vertices.Length; mesh.uv1 = new Vector2[size]; } // Get vertex colors if (mesh_data.GetNumColorChannels() > 0 && mesh_data.HasVertexColors(0)) { using (aiColor4DMultiArray colors = mesh_data.Colors) { using (aiColor4DArray colors0 = colors.Get(0)) { mesh.colors = Assimp.Convert.AssimpToUnity.Array <aiColor4D, Color>(Assimp.Convert.AssimpToUnity.Color, colors0); } } } // TODO: anims + bones // We must dispose of the given parameter to free unused memory mesh_data.Dispose(); context.progress.Update(ASSIMP_PROGRESS_FACTOR); return(mesh); }
private MeshContent ExtractMesh(aiMesh aiMesh) { if (!String.IsNullOrEmpty(aiMesh.mName.Data)) { log("modelname " + aiMesh.mName.Data); meshBuilder = MeshBuilder.StartMesh(aiMesh.mName.Data); } else { meshBuilder = MeshBuilder.StartMesh(Path.GetFileNameWithoutExtension(filename)); } if (!aiMesh.HasPositions()) { throw new Exception("MOdel does not have Position"); } // Add additional vertex channels for texture coordinates and normals if (aiMesh.HasTextureCoords(0)) { textureCoordinateDataIndex = meshBuilder.CreateVertexChannel <Vector2>(VertexChannelNames.TextureCoordinate(0)); } else if (aiMesh.HasVertexColors(0)) { colorCoordinateDataIndex = meshBuilder.CreateVertexChannel <Vector4>(VertexChannelNames.Color(0)); } if (aiMesh.HasNormals()) { normalDataIndex = meshBuilder.CreateVertexChannel <Vector3>(VertexChannelNames.Normal()); } if (aiMesh.HasTangentsAndBitangents()) { tangentDataIndex = meshBuilder.CreateVertexChannel <Vector3>(VertexChannelNames.Tangent(0)); binormalDataIndex = meshBuilder.CreateVertexChannel <Vector3>(VertexChannelNames.Binormal(0)); } if (aiMesh.HasBones()) { boneDataIndex = meshBuilder.CreateVertexChannel <BoneWeightCollection>(VertexChannelNames.Weights(0)); } var numFaces = (int)aiMesh.mNumFaces; var numVertices = (int)aiMesh.mNumVertices; var aiPositions = aiMesh.mVertices; var aiNormals = aiMesh.mNormals; var aiTextureCoordsAll = aiMesh.mTextureCoords; var aiTextureCoords = (aiTextureCoordsAll != null) ? aiTextureCoordsAll[0] : null; for (int j = 0; j < aiMesh.mNumVertices; j++) { meshBuilder.CreatePosition(aiMesh.mVertices[j].x, aiMesh.mVertices[j].y, aiMesh.mVertices[j].z); } meshBuilder.SetMaterial(GetMaterial(aiMesh)); var aiFaces = aiMesh.mFaces; var dxIndices = new uint[numFaces * 3]; for (int k = 0; k < numFaces; ++k) { var aiFace = aiFaces[k]; var aiIndices = aiFace.mIndices; for (int j = 0; j < 3; ++j) { int index = (int)aiIndices[j]; if (aiMesh.HasTextureCoords(0)) { meshBuilder.SetVertexChannelData(textureCoordinateDataIndex, new Vector2(aiMesh.mTextureCoords[0][index].x, aiMesh.mTextureCoords[0][index].y)); } else if (aiMesh.HasVertexColors(0)) { meshBuilder.SetVertexChannelData(colorCoordinateDataIndex, new Vector4(aiMesh.mColors[0][index].r, aiMesh.mColors[0][index].g, aiMesh.mColors[0][index].b, aiMesh.mColors[0][index].a)); } if (aiMesh.HasNormals()) { meshBuilder.SetVertexChannelData(normalDataIndex, new Vector3(aiMesh.mNormals[index].x, aiMesh.mNormals[index].y, aiMesh.mNormals[index].z)); } if (aiMesh.HasTangentsAndBitangents()) { meshBuilder.SetVertexChannelData(tangentDataIndex, new Vector3(aiMesh.mTangents[index].x, aiMesh.mTangents[index].y, aiMesh.mTangents[index].z)); meshBuilder.SetVertexChannelData(binormalDataIndex, new Vector3(aiMesh.mBitangents[index].x, aiMesh.mBitangents[index].y, aiMesh.mBitangents[index].z)); } if (aiMesh.HasBones()) { BoneWeightCollection BoneWeightCollection = new BoneWeightCollection(); if (wbone.ContainsKey(index)) { foreach (var item in wbone[index]) { BoneWeightCollection.Add(new BoneWeight(item.Key, item.Value)); } } meshBuilder.SetVertexChannelData(boneDataIndex, BoneWeightCollection); } meshBuilder.AddTriangleVertex(index); } } MeshContent meshContent = meshBuilder.FinishMesh(); return(meshContent); }