public static void ToAssimp(Module.Export.Assimp.Context context, Scene scene) { if (context.meshes != null && context.meshes.Count > 0) { using (aiMeshArray meshes = context.scene.Meshes) { uint count = (uint)context.meshes.Count; meshes.Reserve(count, true); foreach (KeyValuePair <Module.Export.Assimp.Mesh, uint> indexes in context.meshes) { if (indexes.Value >= 0 && indexes.Value < count) { // Save the values to local variables to avoid the problem of variables passed by reference to lambda functions. Module.Export.Assimp.Mesh mesh_indexes = indexes.Key; aiMesh assimp_mesh = new aiMesh(); // Allocation in another thread fails so we must do it before starting the task meshes.Set(indexes.Value, assimp_mesh.Unmanaged()); context.threads.AddTask(() => ToAssimp(context, scene, mesh_indexes, assimp_mesh)); } } } } }
private MaterialContent GetMaterial(aiMesh aiMesh) { aiString difuse = new aiString(); scene.mMaterials[(int)aiMesh.mMaterialIndex].GetTextureDiffuse0(difuse); if (!String.IsNullOrEmpty(difuse.Data)) { String original = difuse.Data; difuse.Data = Path.GetFileName(difuse.Data); importerContext.AddDependency(Path.Combine(directory, difuse.Data)); try { BasicMaterialContent materialContent = new BasicMaterialContent(); materialContent.Name = difuse.Data; materialContent.Texture = new ExternalReference <TextureContent>(difuse.Data); return(materialContent); } catch (InvalidContentException) { // InvalidContentExceptions do not need further processing throw; } catch (Exception e) { // Wrap exception with content identity (includes line number) throw new InvalidContentException(e.ToString()); } } return(null); }
private Mesh CreateMesh(aiMesh aiMesh, out Vector3 min, out Vector3 max) { var numFaces = (int)aiMesh.mNumFaces; var numVertices = (int)aiMesh.mNumVertices; var dxMesh = new Mesh(numFaces, numVertices, MeshFlags.Managed | MeshFlags.Use32Bit, CustomVertex.PositionNormalTextured.Format, device); var aiPositions = aiMesh.mVertices; var aiNormals = aiMesh.mNormals; var aiTextureCoordsAll = aiMesh.mTextureCoords; var aiTextureCoords = (aiTextureCoordsAll != null) ? aiTextureCoordsAll[0] : null; var dxVertices = new CustomVertex.PositionNormalTextured[numVertices]; for (int i = 0; i < numVertices; ++i) { dxVertices[i].Position = aiPositions[i].ToVector3(); if (aiNormals != null) { dxVertices[i].Normal = aiNormals[i].ToVector3(); } if (aiTextureCoords != null) { var uv = aiTextureCoords[i]; dxVertices[i].Tu = uv.x; dxVertices[i].Tv = uv.y; } } dxMesh.VertexBuffer.SetData(dxVertices, 0, LockFlags.None); var aiFaces = aiMesh.mFaces; var dxIndices = new uint[numFaces * 3]; for (int i = 0; i < numFaces; ++i) { var aiFace = aiFaces[i]; var aiIndices = aiFace.mIndices; for (int j = 0; j < 3; ++j) { dxIndices[i * 3 + j] = aiIndices[j]; } } dxMesh.IndexBuffer.SetData(dxIndices, 0, LockFlags.None); var dxAttributes = dxMesh.LockAttributeBufferArray(LockFlags.None); // TODO: Set face material index for attributes dxMesh.UnlockAttributeBuffer(dxAttributes); var adjacency = new int[numFaces * 3]; dxMesh.GenerateAdjacency(0.0f, adjacency); dxMesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort, adjacency); Geometry.ComputeBoundingBox(dxVertices, CustomVertex.PositionNormalTextured.StrideSize, out min, out max); return(dxMesh); }
public model CreateMesh(Device device, aiMesh aiMesh, aiMaterialVector mMaterials, String directory) { 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; VertexPostitionTexture[] VertexPostitionTextures = new VertexPostitionTexture[aiMesh.mNumVertices]; for (int j = 0; j < aiMesh.mNumVertices; j++) { VertexPostitionTextures[j].position = new Vector3(aiMesh.mVertices[j].x, aiMesh.mVertices[j].y, aiMesh.mVertices[j].z); VertexPostitionTextures[j].textcoord = new Vector2(aiMesh.mTextureCoords[0][j].x, aiMesh.mTextureCoords[0][j].y); } ///being brute =P int SizeInBytes = Marshal.SizeOf(typeof(VertexPostitionTexture)); BufferDescription bd = new BufferDescription(SizeInBytes * (int)aiMesh.mNumVertices, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, SizeInBytes); var vertices = Buffer.Create <VertexPostitionTexture>(device, VertexPostitionTextures, bd); var aiFaces = aiMesh.mFaces; var dxIndices = new uint[numFaces * 3]; for (int i = 0; i < numFaces; ++i) { var aiFace = aiFaces[i]; var aiIndices = aiFace.mIndices; for (int j = 0; j < 3; ++j) { dxIndices[i * 3 + j] = (uint)aiIndices[j]; } } BufferDescription bi = new BufferDescription(sizeof(uint) * numFaces * 3, ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, sizeof(uint)); var indices = Buffer.Create <uint>(device, dxIndices, bd); model modelteste = new model(); modelteste.indices = indices; modelteste.numberIndices = numFaces * 3; modelteste.vertex = vertices; modelteste.numberVertices = numVertices; aiString difuse = new aiString(); mMaterials[(int)aiMesh.mMaterialIndex].GetTextureDiffuse0(difuse); modelteste.difuseTextureName = difuse.Data; String fullPath = String.IsNullOrEmpty(directory) ? modelteste.difuseTextureName : Path.Combine(directory, modelteste.difuseTextureName); modelteste.ShaderResourceView = ShaderResourceView.FromFile(device, fullPath); return(modelteste); }
public model CreateMesh(Device device, aiMesh aiMesh, aiMaterialVector mMaterials, String directory) { 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; VertexPostitionTexture[] VertexPostitionTextures = new VertexPostitionTexture[aiMesh.mNumVertices]; for (int j = 0; j < aiMesh.mNumVertices; j++) { VertexPostitionTextures[j].position = new Vector3(aiMesh.mVertices[j].x, aiMesh.mVertices[j].y, aiMesh.mVertices[j].z); VertexPostitionTextures[j].textcoord = new Vector2(aiMesh.mTextureCoords[0][j].x, aiMesh.mTextureCoords[0][j].y); } ///being brute =P int SizeInBytes = Marshal.SizeOf(typeof(VertexPostitionTexture)); BufferDescription bd = new BufferDescription(SizeInBytes * (int)aiMesh.mNumVertices, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, SizeInBytes); var vertices = Buffer.Create<VertexPostitionTexture>(device, VertexPostitionTextures, bd); var aiFaces = aiMesh.mFaces; var dxIndices = new uint[numFaces * 3]; for (int i = 0; i < numFaces; ++i) { var aiFace = aiFaces[i]; var aiIndices = aiFace.mIndices; for (int j = 0; j < 3; ++j) { dxIndices[i * 3 + j] = (uint)aiIndices[j]; } } BufferDescription bi = new BufferDescription(sizeof(uint) * numFaces * 3, ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, sizeof(uint)); var indices = Buffer.Create<uint>(device, dxIndices, bd); model modelteste = new model(); modelteste.indices = indices; modelteste.numberIndices = numFaces * 3; modelteste.vertex = vertices; modelteste.numberVertices = numVertices; aiString difuse = new aiString(); mMaterials[(int)aiMesh.mMaterialIndex].GetTextureDiffuse0(difuse); modelteste.difuseTextureName = difuse.Data; String fullPath = String.IsNullOrEmpty(directory) ? modelteste.difuseTextureName : Path.Combine(directory, modelteste.difuseTextureName); modelteste.ShaderResourceView = ShaderResourceView.FromFile(device, fullPath); return modelteste; }
public static void FromAssimp(Module.Import.Assimp.Context context, aiScene scene) { if (scene.HasMeshes()) { using (aiMeshArray meshes = scene.Meshes) { uint meshes_size = meshes.Size(); // Reserve the right amount of memory context.scene.meshes = new Mesh[(int)meshes_size]; // Load all the meshes for (uint i = 0; i < meshes_size; i++) { aiMesh mesh = meshes.Get(i); // LoadGeometry must dispose of the given mesh afterward // We must use a proxy method for saving the result into the array because the index i is captured by the lambda otherwise and it's value is indefinite across multiple threads. context.threads.ExecAndSaveToArray(context.scene.meshes, (int)i, () => FromAssimp(context, mesh)); } } } }
private Mesh CreateMesh(aiMesh aiMesh, out Vector3 min, out Vector3 max) { var numFaces = (int) aiMesh.mNumFaces; var numVertices = (int) aiMesh.mNumVertices; var dxMesh = new Mesh(numFaces, numVertices, MeshFlags.Managed | MeshFlags.Use32Bit, CustomVertex.PositionNormalTextured.Format, device); var aiPositions = aiMesh.mVertices; var aiNormals = aiMesh.mNormals; var aiTextureCoordsAll = aiMesh.mTextureCoords; var aiTextureCoords = (aiTextureCoordsAll!=null) ? aiTextureCoordsAll[0] : null; var dxVertices = new CustomVertex.PositionNormalTextured[numVertices]; for (int i = 0; i < numVertices; ++i) { dxVertices[i].Position = aiPositions[i].ToVector3(); if (aiNormals != null) { dxVertices[i].Normal = aiNormals[i].ToVector3(); } if (aiTextureCoords != null) { var uv = aiTextureCoords[i]; dxVertices[i].Tu = uv.x; dxVertices[i].Tv = uv.y; } } dxMesh.VertexBuffer.SetData(dxVertices, 0, LockFlags.None); var aiFaces = aiMesh.mFaces; var dxIndices = new uint[numFaces * 3]; for (int i = 0; i < numFaces; ++i) { var aiFace = aiFaces[i]; var aiIndices = aiFace.mIndices; for (int j = 0; j < 3; ++j) { dxIndices[i * 3 + j] = aiIndices[j]; } } dxMesh.IndexBuffer.SetData(dxIndices, 0, LockFlags.None); var dxAttributes = dxMesh.LockAttributeBufferArray(LockFlags.None); // TODO: Set face material index for attributes dxMesh.UnlockAttributeBuffer(dxAttributes); var adjacency = new int[numFaces * 3]; dxMesh.GenerateAdjacency(0.0f, adjacency); dxMesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort, adjacency); Geometry.ComputeBoundingBox(dxVertices, CustomVertex.PositionNormalTextured.StrideSize, out min, out max); return dxMesh; }
internal static HandleRef getCPtr(aiMesh obj) { return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; }
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 static void ToAssimp(Module.Export.Assimp.Context context, Scene scene, Module.Export.Assimp.Mesh mesh_indexes, aiMesh assimp_mesh) { Mesh mesh = scene.meshes[mesh_indexes.mesh]; assimp_mesh.mMaterialIndex = (uint)mesh_indexes.material; using (aiString assimp_mesh_name = new aiString(mesh.name)) { assimp_mesh.mName = assimp_mesh_name.Unmanaged(); } if (mesh.vertices.Length > 0) { using (aiVector3DArray vertices = assimp_mesh.Vertices) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Vector3, mesh.vertices, vertices); } } if (mesh.normals.Length > 0) { using (aiVector3DArray normals = assimp_mesh.Normals) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Vector3, mesh.normals, normals); } } if (mesh.tangents.Length > 0) { using (aiVector3DArray tangents = assimp_mesh.Tangents) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Tangent, mesh.tangents, tangents); } } if (mesh_indexes.submesh < mesh.submeshes.Length) { // Support for submeshes: this mesh represent only one submesh of the original mesh SubMesh sub_mesh = mesh.submeshes[mesh_indexes.submesh]; if (sub_mesh != null && sub_mesh.triangles != null && sub_mesh.triangles.Length > 0) { using (aiFaceArray faces = assimp_mesh.Faces) { Assimp.Convert.UnityToAssimp.Face(sub_mesh.triangles, sub_mesh.topology, faces); } } } if (mesh.uv1.Length > 0 || mesh.uv2.Length > 0) { using (aiVector3DMultiArray texture_coords = assimp_mesh.TextureCoords) { if (mesh.uv1.Length > 0) { using (aiVector3DArray texture_coords0 = texture_coords.Get(0)) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.UV, mesh.uv1, texture_coords0); } } if (mesh.uv2.Length > 0) { using (aiVector3DArray texture_coords1 = texture_coords.Get(1)) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.UV, mesh.uv2, texture_coords1); } } } } if (mesh.colors.Length > 0) { using (aiColor4DMultiArray colors = assimp_mesh.Colors) { using (aiColor4DArray colors0 = colors.Get(0)) { Assimp.Convert.UnityToAssimp.Array(Assimp.Convert.UnityToAssimp.Color, mesh.colors, colors0); } } } context.progress.Update(ASSIMP_PROGRESS_FACTOR); }
internal static HandleRef getCPtr(aiMesh obj) { return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr); }
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); }