Example #1
0
        protected override void InitFromMeshData(Device device, GeometryGenerator.MeshData mesh)
        {
            var subset = new MeshGeometry.Subset {
                FaceCount   = mesh.Indices.Count / 3,
                FaceStart   = 0,
                VertexCount = mesh.Vertices.Count,
                VertexStart = 0
            };

            Subsets.Add(subset);

            var max = new Vector3(float.MinValue);
            var min = new Vector3(float.MaxValue);

            foreach (var vertex in mesh.Vertices)
            {
                max = Vector3.Maximize(max, vertex.Position);
                min = Vector3.Minimize(min, vertex.Position);
            }

            BoundingBox = new BoundingBox(min, max);

            Vertices.AddRange(mesh.Vertices.Select(v => new PosNormalTexTan(v.Position, v.Normal, v.TexC, v.TangentU)).ToList());
            Indices.AddRange(mesh.Indices.Select(i => (short)i));

            Materials.Add(new Material {
                Ambient = Color.Gray, Diffuse = Color.White, Specular = new Color4(16, 1, 1, 1)
            });
            DiffuseMapSRV.Add(null);
            NormalMapSRV.Add(null);

            ModelMesh.SetSubsetTable(Subsets);
            ModelMesh.SetVertices(device, Vertices);
            ModelMesh.SetIndices(device, Indices);
        }
Example #2
0
        public BasicModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipUV = false)
        {
            var importer = new AssimpImporter();

            if (!importer.IsImportFormatSupported(Path.GetExtension(filename)))
            {
                throw new ArgumentException("Model format " + Path.GetExtension(filename) + " is not supported!  Cannot load {1}", "filename");
            }
#if DEBUG
            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.GetTextureCoords(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 => (short)(i + (uint)subset.VertexStart)).ToList();
                Indices.AddRange(indices);

                var mat      = model.Materials[mesh.MaterialIndex];
                var material = mat.ToMaterial();

                Materials.Add(material);

                var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).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)));
                }
                var normalPath = mat.GetTexture(TextureType.Normals, 0).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);
        }
Example #3
0
        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);
        }
Example #4
0
        public SkinnedModel(Device device, TextureManager texMgr, string filename, string texturePath, bool flipTexY = false)
        {
            var importer = new AssimpImporter();

        #if DEBUG
            importer.AttachLogStream(new ConsoleLogStream());
            importer.VerboseLoggingEnabled = true;
        #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 => (short)(i + (uint)subset.VertexStart)).ToList();
                Indices.AddRange(indices);

                // extract materials
                var mat      = model.Materials[mesh.MaterialIndex];
                var material = mat.ToMaterial();
                Materials.Add(material);

                // extract material textures
                var diffusePath = mat.GetTexture(TextureType.Diffuse, 0).FilePath;
                if (!string.IsNullOrEmpty(diffusePath))
                {
                    DiffuseMapSRV.Add(texMgr.CreateTexture(Path.Combine(texturePath, diffusePath)));
                }
                var normalPath = mat.GetTexture(TextureType.Normals, 0).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);
        }