Ejemplo n.º 1
0
        static Texture LoadTexture(Assimp.Material M)
        {
            Texture Result   = null;
            var     textures = M.GetAllMaterialTextures();

            foreach (var tex in textures)
            {
                var path = tex.FilePath;
                if (path != "")
                {
                    try
                    {
                        int id = (path.IndexOf("./"));
                        if (id >= 0)
                        {
                            path = path.Remove(id, 2);
                        }
                        path   = System.IO.Path.GetFileName(path);
                        Result = new Texture();
                        Result.LoadFromFile(BaseDir + "\\" + path);
                    }
                    catch (Exception E)
                    {
                        System.Windows.Forms.MessageBox.Show(E.Message);
                    }
                }
            }
            return(Result);
        }
Ejemplo n.º 2
0
        private static Material CreateMaterialFromAiMaterial(Ai.Material aiMaterial, TextureSet textureSet, string texturesDirectoryPath)
        {
            var material = new Material {
                Name = aiMaterial.Name
            };

            foreach (string shaderName in Material.ShaderNames)
            {
                if (material.Name.IndexOf(shaderName, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    continue;
                }

                material.ShaderName = shaderName == "CHARA" ? "SKIN" : shaderName;
                break;
            }

            material.Diffuse   = aiMaterial.HasColorDiffuse ? aiMaterial.ColorDiffuse.ToMML() : material.Diffuse;
            material.Ambient   = aiMaterial.HasColorAmbient ? aiMaterial.ColorAmbient.ToMML() : material.Ambient;
            material.Specular  = aiMaterial.HasColorSpecular ? aiMaterial.ColorSpecular.ToMML() : material.Specular;
            material.Emission  = aiMaterial.HasColorEmissive ? aiMaterial.ColorEmissive.ToMML() : material.Emission;
            material.Shininess = aiMaterial.HasShininess ? aiMaterial.Shininess : material.Shininess;

            if (material.ShaderName == "HAIR")
            {
                material.AnisoDirection = AnisoDirection.V;
            }

            material.DoubleSided = aiMaterial.HasTwoSided ? aiMaterial.IsTwoSided : material.DoubleSided;

            foreach (var aiTextureSlot in aiMaterial.GetAllMaterialTextures())
            {
                CreateMaterialTextureFromAiTextureSlot(aiTextureSlot, aiMaterial, material, textureSet, texturesDirectoryPath);
            }

            if (material.Flags.HasFlag(MaterialFlags.Normal))
            {
                material.BumpMapType = material.Flags.HasFlag(MaterialFlags.Environment) ? BumpMapType.Env : BumpMapType.Dot;
            }

            if (material.Flags.HasFlag(MaterialFlags.Specular))
            {
                material.LineLight = 5;
            }

            if (material.Flags.HasFlag(MaterialFlags.Environment))
            {
                material.Flags |= MaterialFlags.ColorL1Alpha | MaterialFlags.ColorL2Alpha | MaterialFlags.OverrideIBL;
            }

            // HAIR without normal map causes the whole screen to become black.
            // TODO: Automatically add a flat normal map in this case.
            if (material.ShaderName == "HAIR" && !material.Flags.HasFlag(MaterialFlags.Normal))
            {
                material.ShaderName = "BLINN";
            }

            material.SortMaterialTextures();

            return(material);
        }
Ejemplo n.º 3
0
        public Model Load(AssetManager assetManager, RenderContext renderContext, Stream stream, string sourcePath)
        {
            Scene scene = Importer.ImportFile(sourcePath, PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs);

            if (scene == null || scene.SceneFlags.HasFlag(SceneFlags.Incomplete) || scene.RootNode == null)
            {
                throw new Exception("Assimp Error.");
            }

            var meshes = new List <Mesh>();

            string directory = Path.GetDirectoryName(sourcePath);

            Texture2D LoadFromTextureSlot(TextureSlot textureSlot, TextureType textureType)
            {
                var texture = assetManager.Load <Texture2D>(Path.Combine(directory, textureSlot.FilePath));

                texture.SetInfoFromTextureSlot(renderContext, ref textureSlot);

                texture.SetMinFilter(renderContext, TextureMinFilter.NearestMipmapLinear);
                return(texture);
            }

            List <Texture2D> LoadMaterialTextures(AssimpMaterial material, TextureType textureType)
            {
                List <Texture2D> textures = new List <Texture2D>();

                for (int i = 0; i < material.GetMaterialTextureCount(textureType); i++)
                {
                    material.GetMaterialTexture(textureType, i, out var textureSlot);

                    if (textureSlot.FilePath != null)
                    {
                        if (textureSlot.FilePath.Contains("*"))
                        {
                            throw new InvalidOperationException();
                        }

                        var texture = assetManager.Load <Texture2D>(Path.Combine(directory, textureSlot.FilePath));
                        texture.SetInfoFromTextureSlot(renderContext, ref textureSlot);
                        textures.Add(texture);
                    }
                }
                return(textures);
            }

            Mesh ProcessMesh(AssimpMesh mesh, System.Numerics.Matrix4x4 transformMatrix)
            {
                VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.VertexCount];
                List <uint> indices = new List <uint>();

                // Process vertices
                for (int i = 0; i < mesh.VertexCount; i++)
                {
                    Vector3D position = mesh.Vertices[i];
                    vertices[i].Position = Unsafe.As <Vector3D, Vector3>(ref position);
                    Vector3D normal = mesh.Normals[i];
                    vertices[i].Normal = Unsafe.As <Vector3D, Vector3>(ref normal);

                    if (mesh.HasTextureCoords(0))
                    {
                        Vector3D texCoord = mesh.TextureCoordinateChannels[0][i];
                        vertices[i].TexCoord = new Vector2(texCoord.X, 1 - texCoord.Y);
                    }
                }

                // Process indices
                for (int i = 0; i < mesh.FaceCount; i++)
                {
                    AssimpFace face = mesh.Faces[i];
                    for (int j = 0; j < face.IndexCount; j++)
                    {
                        indices.Add((uint)face.Indices[j]);
                    }
                }

                Mesh.MeshTextureInfo textures = new Mesh.MeshTextureInfo();

                // Process Material
                if (mesh.MaterialIndex >= 0)
                {
                    AssimpMaterial material = scene.Materials[mesh.MaterialIndex];

                    var texturezzz = material.GetAllMaterialTextures();

                    texturezzz = texturezzz.Where(x => x.FilePath != null).ToArray();

                    if (material.HasTextureDiffuse)
                    {
                        textures.Diffuse = LoadFromTextureSlot(material.TextureDiffuse, TextureType.Diffuse);
                    }

                    if (material.HasTextureSpecular)
                    {
                        textures.Specular = LoadFromTextureSlot(material.TextureSpecular, TextureType.Specular);
                    }

                    if (material.HasTextureLightMap)
                    {
                        textures.MetallicRoughness = LoadFromTextureSlot(material.TextureLightMap, TextureType.Lightmap);
                    }
                }

                return(new Mesh(renderContext, vertices, indices.ToArray(), textures, transformMatrix));
            }

            void ProcessNode(Node node, System.Numerics.Matrix4x4 transform)
            {
                var t             = node.Transform;
                var thisTransform = Unsafe.As <Matrix4x4, System.Numerics.Matrix4x4>(ref t);

                // Process all of the node's meshes
                for (int i = 0; i < node.MeshCount; i++)
                {
                    AssimpMesh mesh = scene.Meshes[node.MeshIndices[i]];
                    meshes.Add(ProcessMesh(mesh, transform * thisTransform));
                }

                // Recurse
                for (int i = 0; i < node.ChildCount; i++)
                {
                    ProcessNode(node.Children[i], transform * thisTransform);
                }
            }

            ProcessNode(scene.RootNode, System.Numerics.Matrix4x4.Identity);

            return(new Model(meshes.ToArray()));
        }
Ejemplo n.º 4
0
        private static SystemMaterial ConvertMaterial(AssimpMaterial aMaterial, string path)
        {
            MaterialGroup material = new MaterialGroup();

            if (aMaterial.HasColorDiffuse)
            {
                material.Children.Add(new DiffuseMaterial(new SolidColorBrush(
                    ConvertColor(aMaterial.ColorDiffuse, aMaterial))));
            }
            if (aMaterial.HasColorEmissive)
            {
                material.Children.Add(new EmissiveMaterial(new SolidColorBrush(
                    ConvertColor(aMaterial.ColorEmissive, aMaterial))));
            }
            if (aMaterial.HasColorSpecular)
            {
                material.Children.Add(new SpecularMaterial(new SolidColorBrush(
                    ConvertColor(aMaterial.ColorSpecular, aMaterial)), aMaterial.Shininess));
            }

            // Textures
            TextureSlot[] textures = aMaterial.GetAllMaterialTextures();
            foreach (TextureSlot texture in textures)
            {
                string imageFile = Path.Combine(path, texture.FilePath);
                if (File.Exists(imageFile))
                {
                    //material.Children.Clear();
                    ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(imageFile)));
                    brush.ViewportUnits = BrushMappingMode.Absolute;
                    material.Children.Add(new DiffuseMaterial(brush));
                }
            }

            if (material.Children.Count <= 0)
            {
                // paranoia: no material defined
                // -> add default-material
                material.Children.Add(new DiffuseMaterial(Brushes.DarkGray));
            }
            return material;
        }