public static DataStream LoadVertices(this Assimp.Mesh mesh, AssimpLoadInformation loadInfo, int vertexsize, out BoundingBox bb) { bb = new BoundingBox(); bb.Maximum = new Vector3(float.MinValue, float.MinValue, float.MinValue); bb.Minimum = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); DataStream ds = new DataStream(mesh.VertexCount * vertexsize, true, true); for (int i = 0; i < mesh.VertexCount; i++) { var vert = mesh.Vertices[i]; ds.Write <Assimp.Vector3D>(vert); bb.Minimum.X = vert.X < bb.Minimum.X ? vert.X : bb.Minimum.X; bb.Minimum.Y = vert.Y < bb.Minimum.Y ? vert.Y : bb.Minimum.Y; bb.Minimum.Z = vert.Z < bb.Minimum.Z ? vert.Z : bb.Minimum.Z; bb.Maximum.X = vert.X > bb.Maximum.X ? vert.X : bb.Maximum.X; bb.Maximum.Y = vert.Y > bb.Maximum.Y ? vert.Y : bb.Maximum.X; bb.Maximum.Z = vert.Z > bb.Maximum.Z ? vert.Z : bb.Maximum.Z; if (mesh.HasNormals && loadInfo.Normals) { ds.Write <Assimp.Vector3D>(mesh.Normals[i]); } if (mesh.HasTangentBasis && loadInfo.Tangents) { ds.Write <Assimp.Vector3D>(mesh.Tangents[i]); ds.Write <Assimp.Vector3D>(mesh.BiTangents[i]); } if (loadInfo.VertexColors) { for (int j = 0; j < mesh.VertexColorChannelCount; j++) { ds.Write <Assimp.Color4D>(mesh.GetVertexColors(j)[i]); } } if (loadInfo.TexCoords) { for (int j = 0; j < mesh.TextureCoordsChannelCount; j++) { int numcomp = mesh.GetUVComponentCount(j); if (numcomp == 1) { ds.Write <float>(mesh.GetTextureCoords(j)[i].X); } if (numcomp == 2) { ds.Write <float>(mesh.GetTextureCoords(j)[i].X); ds.Write <float>(mesh.GetTextureCoords(j)[i].Y); } if (numcomp == 3) { ds.Write <Assimp.Vector3D>(mesh.GetTextureCoords(j)[i]); } } } } return(ds); }
private bool ParseMeshData(Assimp.Scene scene) { if (!scene.HasMeshes) { return(false); } Assimp.Mesh[] meshes = scene.Meshes; if (meshes.Length == 0) { return(false); } m_meshData = new MeshData[meshes.Length]; m_meshMatInfo = new MeshMaterialInfo[meshes.Length]; for (int i = 0; i < meshes.Length; i++) { Assimp.Mesh mesh = meshes[i]; MeshData md = new MeshData(); MeshMaterialInfo info = new MeshMaterialInfo(); if (mesh.HasVertices) { Assimp.Vector3D[] assimpPos = mesh.Vertices; DataBuffer <Vector3> positions = new DataBuffer <Vector3>(assimpPos.Length); MemoryHelper.Copy(assimpPos, positions.Buffer, Vector3.SizeInBytes * assimpPos.Length); md.Positions = positions; } if (mesh.HasNormals) { Assimp.Vector3D[] assimpNorms = mesh.Normals; DataBuffer <Vector3> normals = new DataBuffer <Vector3>(assimpNorms.Length); MemoryHelper.Copy(assimpNorms, normals.Buffer, Vector3.SizeInBytes * assimpNorms.Length); md.Normals = normals; info.HasNormals = true; } if (mesh.HasTangentBasis) { Assimp.Vector3D[] assimpTangs = mesh.Tangents; Assimp.Vector3D[] assimpBitangs = mesh.BiTangents; DataBuffer <Vector3> tangents = new DataBuffer <Vector3>(assimpTangs.Length); DataBuffer <Vector3> bitangents = new DataBuffer <Vector3>(assimpBitangs.Length); MemoryHelper.Copy(assimpTangs, tangents.Buffer, Vector3.SizeInBytes * assimpTangs.Length); MemoryHelper.Copy(assimpBitangs, bitangents.Buffer, Vector3.SizeInBytes * assimpBitangs.Length); md.Tangents = tangents; md.Binormals = bitangents; info.HasTangentBasis = true; } if (mesh.HasVertexColors(0)) { Assimp.Color4D[] assimpColors = mesh.GetVertexColors(0); md.Colors = new DataBuffer <Color>(ConvertColors(assimpColors)); info.HasVertexColors = true; } if (mesh.HasTextureCoords(0)) { Assimp.Vector3D[] assimpUV = mesh.GetTextureCoords(0); md.TextureCoordinates = new DataBuffer <Vector2>(ConvertVectors(assimpUV)); info.HasTexCoords = true; } md.Indices = new DataBuffer <int>(mesh.GetIntIndices()); md.Reconstruct(); m_meshMatInfo[i] = info; m_meshData[i] = md; } ParseMaterials(scene); return(true); }