public void TestFreeLogStreams() { ConsoleLogStream console1 = new ConsoleLogStream(); ConsoleLogStream console2 = new ConsoleLogStream(); ConsoleLogStream console3 = new ConsoleLogStream(); console1.Attach(); console2.Attach(); console3.Attach(); AssimpLibrary.Instance.FreeLibrary(); IEnumerable <LogStream> logs = LogStream.GetAttachedLogStreams(); Assert.IsEmpty(logs); Assert.IsFalse(console1.IsAttached); Assert.IsFalse(console2.IsAttached); Assert.IsFalse(console3.IsAttached); }
public void TestIOSystem_ImportObj() { String dir = Path.Combine(TestHelper.RootPath, "TestFiles"); LogStream.IsVerboseLoggingEnabled = true; ConsoleLogStream log = new ConsoleLogStream(); log.Attach(); using (AssimpContext importer = new AssimpContext()) { FileIOSystem iOSystem = new FileIOSystem(dir); importer.SetIOSystem(iOSystem); //Using stream does not use the IO system... using (Stream fs = File.OpenRead(Path.Combine(dir, "sphere.obj"))) { Scene scene = importer.ImportFileFromStream(fs, "obj"); Assert.IsTrue(scene != null); Assert.IsTrue(scene.HasMeshes); Assert.IsTrue(scene.HasMaterials); //No material file, so the mesh will always use the default material Assert.IsTrue(scene.Materials[scene.Meshes[0].MaterialIndex].Name == "DefaultMaterial"); } //Using custom IO system requires us to pass in the file name, assimp will ask the io system to get a stream Scene scene2 = importer.ImportFile("sphere.obj"); Assert.IsTrue(scene2 != null); Assert.IsTrue(scene2.HasMeshes); Assert.IsTrue(scene2.HasMaterials); //Should have found a material with the name "SphereMaterial" in the mtl file Assert.IsTrue(scene2.Materials[scene2.Meshes[0].MaterialIndex].Name == "SphereMaterial"); } }
public void TestFreeLogStreams() { ConsoleLogStream console1 = new ConsoleLogStream(); ConsoleLogStream console2 = new ConsoleLogStream(); ConsoleLogStream console3 = new ConsoleLogStream(); console1.Attach(); console2.Attach(); console3.Attach(); AssimpLibrary.Instance.FreeLibrary(); IEnumerable<LogStream> logs = LogStream.GetAttachedLogStreams(); Assert.IsEmpty(logs); Assert.IsFalse(console1.IsAttached); Assert.IsFalse(console2.IsAttached); Assert.IsFalse(console3.IsAttached); }
private BasicModel(Device device, TextureManager11 textureManager, string filename, string texturePath, bool autoLoadTextures, bool flipUv, bool tex1By1) { var importer = new AssimpContext(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException($"Model format {Path.GetExtension(filename)} is not supported. Cannot load {filename}.", nameof(filename)); } #if DEBUG var logStream = new ConsoleLogStream(); logStream.Attach(); #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUv) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); _meshCount = model.Meshes.Count; foreach (var mesh in model.Meshes) { var verts = new List <VertPosNormTexTan>(); var subset = new MeshSubset() { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = MathF.Minimize(min, pos); max = MathF.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : (tex1By1 ? new Vector3D(1, 1, 0) : new Vector3D()); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new VertPosNormTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => i + subset.VertexStart).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); if (autoLoadTextures) { TextureSlot diffuseSlot; mat.GetMaterialTexture(TextureType.Diffuse, 0, out diffuseSlot); var diffusePath = diffuseSlot.FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } var fullDiffusePath = diffusePath == null ? null : Path.Combine(texturePath, diffusePath); if (File.Exists(fullDiffusePath)) { DiffuseMapSRV.Add(textureManager.CreateTexture(fullDiffusePath)); } else { DiffuseMapSRV.Add(textureManager.CreateColor1By1(material.Diffuse.ToColor())); } TextureSlot normalSlot; mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot); var normalPath = normalSlot.FilePath; var fullNormalPath = normalPath == null ? null : Path.Combine(texturePath, normalPath); string textureName; if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { if (File.Exists(fullDiffusePath)) { var normalExt = Path.GetExtension(fullDiffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; fullNormalPath = Path.Combine(texturePath, normalPath); if (File.Exists(fullNormalPath)) { textureName = fullNormalPath; } else { textureName = TextureManager11.TexDefaultNorm; } } else { textureName = TextureManager11.TexDefaultNorm; } } NormalMapSRV.Add(textureManager.CreateTexture(textureName)); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipUv = false) { var importer = new AssimpContext(); if (!importer.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported! Cannot load {1}", "filename"); } #if DEBUG var logStream = new ConsoleLogStream(); logStream.Attach(); //importer. .AttachLogStream(new ConsoleLogStream()); //importer.VerboseLoggingEnabled = true; #endif var postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUv) { postProcessFlags |= PostProcessSteps.FlipUVs; } var model = importer.ImportFile(filename, postProcessFlags); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { var verts = new List <PosNormalTexTan>(); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); // bounding box corners for (var i = 0; i < mesh.VertexCount; i++) { var pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3(); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); var norm = mesh.HasNormals ? mesh.Normals[i] : new Vector3D(); var texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : new Vector3D(); var tan = mesh.HasTangentBasis ? mesh.Tangents[i] : new Vector3D(); var v = new PosNormalTexTan(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3()); verts.Add(v); } Vertices.AddRange(verts); var indices = mesh.GetIndices().Select(i => ((int)i + subset.VertexStart)).ToList(); Indices.AddRange(indices); var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); TextureSlot diffuseSlot; mat.GetMaterialTexture(TextureType.Diffuse, 0, out diffuseSlot); var diffusePath = diffuseSlot.FilePath; if (Path.GetExtension(diffusePath) == ".tga") { // DirectX doesn't like to load tgas, so you will need to convert them to pngs yourself with an image editor diffusePath = diffusePath.Replace(".tga", ".png"); } if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } TextureSlot normalSlot; mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot); var normalPath = normalSlot.FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } BoundingBox = new BoundingBox(min, max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false) { var importer = new AssimpContext(); #if DEBUG var logstream = new ConsoleLogStream(); logstream.Attach(); #endif var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace); // Load animation data Animator = new SceneAnimator(); Animator.Init(model); // create our vertex-to-boneweights lookup var vertToBoneWeight = new Dictionary <uint, List <VertexWeight> >(); // create bounding box extents _min = new Vector3(float.MaxValue); _max = new Vector3(float.MinValue); foreach (var mesh in model.Meshes) { ExtractBoneWeightsFromMesh(mesh, vertToBoneWeight); var subset = new MeshGeometry.Subset { VertexCount = mesh.VertexCount, VertexStart = Vertices.Count, FaceStart = Indices.Count / 3, FaceCount = mesh.FaceCount }; Subsets.Add(subset); var verts = ExtractVertices(mesh, vertToBoneWeight, flipTexY); Vertices.AddRange(verts); // extract indices and shift them to the proper offset into the combined vertex buffer var indices = mesh.GetIndices().Select(i => ((int)i + subset.VertexStart)).ToList(); Indices.AddRange(indices); // extract materials var mat = model.Materials[mesh.MaterialIndex]; var material = mat.ToMaterial(); Materials.Add(material); // extract material textures TextureSlot diffuseSlot; mat.GetMaterialTexture(TextureType.Diffuse, 0, out diffuseSlot); var diffusePath = diffuseSlot.FilePath; if (!string.IsNullOrEmpty(diffusePath)) { DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath))); } TextureSlot normalSlot; mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot); var normalPath = normalSlot.FilePath; if (!string.IsNullOrEmpty(normalPath)) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } else { // for models created without a normal map baked, we'll check for a texture with the same // filename as the diffure texture, and _nmap suffixed // this lets us add our own normal maps easily var normalExt = Path.GetExtension(diffusePath); normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt; if (File.Exists(Path.Combine(texturePath, normalPath))) { NormalMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, normalPath))); } } } BoundingBox = new BoundingBox(_min, _max); ModelMesh.SetSubsetTable(Subsets); ModelMesh.SetVertices(device, Vertices); ModelMesh.SetIndices(device, Indices); }
public Mesh(string FileName, bool flipUv = false) { Vertices = new List <Vertex>(); Indices = new List <int>(); AssimpContext importer = new AssimpContext(); if (!importer.IsImportFormatSupported(Path.GetExtension(FileName))) { throw new ArgumentException("Model format " + Path.GetExtension(FileName) + " is not supported! Cannot load {1}", "filename"); } ConsoleLogStream logStream = new ConsoleLogStream(); logStream.Attach(); PostProcessSteps postProcessFlags = PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace; if (flipUv) { postProcessFlags |= PostProcessSteps.FlipUVs; } Scene model = importer.ImportFile(FileName, postProcessFlags); int[] indices = new int[model.Meshes.Sum(m => m.FaceCount * 3)]; int vertexOffSet = 0; int indexOffSet = 0; foreach (Assimp.Mesh mesh in model.Meshes) { List <Vertex> verts = new List <Vertex>(); List <Face> faces = mesh.Faces; for (int i = 0; i < mesh.VertexCount; i++) { Vector3 pos = mesh.HasVertices ? new Vector3(mesh.Vertices[i].X, mesh.Vertices[i].Y, mesh.Vertices[i].Z) : new Vector3(); Vector3D texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i] : new Vector3D(); Vector2 TeC = new Vector2(texC.X, texC.Y); Vertex v = new Vertex(pos, TeC); verts.Add(v); } Vertices.AddRange(verts); for (int i = 0; i < mesh.FaceCount; i++) { indices[i * 3 + 0] = (int)faces[i].Indices[2]; indices[i * 3 + 1] = (int)faces[i].Indices[1]; indices[i * 3 + 2] = (int)faces[i].Indices[0]; Indices.Add(indices[i * 3 + 2] + vertexOffSet); Indices.Add(indices[i * 3 + 1] + vertexOffSet); Indices.Add(indices[i * 3 + 0] + vertexOffSet); } vertexOffSet += mesh.VertexCount; indexOffSet += mesh.FaceCount * 3; } VertexCount = Vertices.Count(); IndexCount = Indices.Count(); Size = Utilities.SizeOf <Vertex>(); SizeInBytes = Utilities.SizeOf <Vertex>() * Vertices.Count(); IndexSize = Utilities.SizeOf <int>(); IndexSizeInBytes = Utilities.SizeOf <int>() * Indices.Count(); }