Example #1
0
 public void SetUp()
 {
     var assimpNetimporter = new Assimp.AssimpContext();
     Assimp.LogStream.IsVerboseLoggingEnabled = true;
     var logger = new Assimp.ConsoleLogStream();
     logger.Attach();
     assimpNetScene = assimpNetimporter.ImportFile(filename);
     logger.Detach();
     var assimpSharpImporter = new AssimpSharp.XFile.XFileImporter();
     assimpSharpScene = new AssimpSharp.Scene();
     assimpSharpImporter.InternReadFile(filename, assimpSharpScene);
 }
        /// <summary>
        /// Initialize our basic model
        /// </summary>
        /// <param name="device">Direct3D device for use</param>
        /// <param name="texMgr">Texture manager from which to load texture data</param>
        /// <param name="filename">Filename of the ASSIMP resource we would like to load (see ASSIMP documentation for supported formats)</param>
        /// <param name="texturePath">Texture path - base path for textures used by the ASSIMP model</param>
        public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath)
        {
            _subsets = new List<MeshGeometry.Subset>();
            _vertices = new List<BasicEffectVertex>();
            _indices = new List<ushort>();
            DiffuseMapSRV = new List<ShaderResourceView>();
            NormalMapSRV = new List<ShaderResourceView>();
            Materials = new List<Lighting.Material>();
            _modelMesh = new MeshGeometry();

            var importer = new AssimpContext();
            if (!importer.IsImportFormatSupported(Path.GetExtension(filename)))
            {
                throw new ArgumentException($"Model format {Path.GetExtension(filename)} is not supported! Cannot load {filename}", "Outside Engine");
            }
            var model = importer.ImportFile(filename, PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.CalculateTangentSpace);
            #if DEBUG
            var logStream = new ConsoleLogStream();
            logStream.Attach();
            #endif

            foreach (var mesh in model.Meshes)
            {
                //
                // Vertex processing
                //
                var verts = new List<BasicEffectVertex>();
                var subset = new MeshGeometry.Subset
                {
                    VertexCount = mesh.VertexCount,
                    VertexStart = _vertices.Count,
                    FaceStart = _indices.Count / 3,
                    FaceCount = mesh.FaceCount
                };
                _subsets.Add(subset);

                // TODO KAM: Process bounding box corners

                for (var i = 0; i < mesh.VertexCount; ++i)
                {
                    Vector3 pos = mesh.HasVertices ? mesh.Vertices[i].ToVector3() : new Vector3();

                    var norml = mesh.HasNormals ? mesh.Normals[i].ToVector3() : new Vector3();
                    var texC = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0][i].ToVector3() : new Vector3();
                    var tan = mesh.HasTangentBasis ? mesh.Tangents[i].ToVector3() : new Vector3();
                    var v = new BasicEffectVertex(pos, norml, new Vector2(texC.X, texC.Y));
                    verts.Add(v);
                }

                _vertices.AddRange(verts);

                var indices = mesh.GetIndices().Select(i => (ushort)(i + (uint)subset.VertexStart)).ToList();
                _indices.AddRange(indices);

                //
                // Material processing
                //
                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")
                {
                    throw new InvalidDataException("Cannot use TGA files for textures with DirectX. Sorry about that.");
                }

                if (!string.IsNullOrEmpty(diffusePath))
                {
                    DiffuseMapSRV.Add(texMgr.GetTexture(Path.Combine(texturePath, diffusePath)));
                }

                TextureSlot normalSlot;
                mat.GetMaterialTexture(TextureType.Normals, 0, out normalSlot);
                var normalPath = normalSlot.FilePath;
                if (!string.IsNullOrEmpty(normalPath))
                {
                    NormalMapSRV.Add(texMgr.GetTexture(Path.Combine(texturePath, normalPath)));
                }
                else
                {
                    var normalExt = Path.GetExtension(diffusePath);
                    normalPath = Path.GetFileNameWithoutExtension(diffusePath) + "_nmap" + normalExt;
                    NormalMapSRV.Add(texMgr.GetTexture(Path.Combine(texturePath, normalPath)));
                }
            }

            _modelMesh.SetSubsetTable(_subsets);
            _modelMesh.SetVertices(device, _vertices);
            _modelMesh.SetIndices(device, _indices);
        }