Ejemplo n.º 1
0
        private void GetMaterialsAndLayers()
        {
            List <UnifyMaterial> matList     = new List <UnifyMaterial>();
            List <UnifyLayer>    unifyLayers = new List <UnifyLayer>();

            this.NestingLevel = 0;

            // get all materials by layers
            foreach (Layer layer in doc.Layers)
            {
                // process Layer info
                UnifyLayer ul = new UnifyLayer();
                ul.ObjType      = "Layer";
                ul.Guid         = layer.Id;
                ul.Name         = layer.FullPath.Replace(":", "_");
                ul.MeshCollider = false;
                ul.Parent       = layer.ParentLayerId;
                ul.Level        = layer.FullPath.Split(new string[] { "::" }, StringSplitOptions.None).Count();
                ul.ShortName    = layer.Name;

                if (ul.Level > this.NestingLevel)
                {
                    this.NestingLevel = ul.Level;
                }
                unifyLayers.Add(ul);

                // process material info
                int           renderMatIndex = layer.RenderMaterialIndex;
                Material      rhinoMat       = doc.Materials[renderMatIndex];
                UnifyMaterial mat            = new UnifyMaterial();

                mat.ObjType         = "MaterialObject";
                mat.Guid            = rhinoMat.Id;
                mat.Name            = rhinoMat.Name;
                mat.Diffuse         = Utility.ColorToString(rhinoMat.DiffuseColor);
                mat.SpecularColor   = Utility.ColorToString(rhinoMat.SpecularColor);
                mat.EmissionColor   = Utility.ColorToString(rhinoMat.EmissionColor);
                mat.ReflectionColor = Utility.ColorToString(rhinoMat.ReflectionColor);
                mat.Metallic        = rhinoMat.Shine;

                if (rhinoMat.GetBitmapTexture() != null)
                {
                    mat.DiffuseTexture = rhinoMat.GetBitmapTexture().FileName;
                }
                if (rhinoMat.GetTransparencyTexture() != null)
                {
                    mat.TransparencyTexture = rhinoMat.GetTransparencyTexture().FileName;
                }
                if (rhinoMat.GetEnvironmentTexture() != null)
                {
                    mat.EnvironmentTexture = rhinoMat.GetEnvironmentTexture().FileName;
                }
                if (rhinoMat.GetBumpTexture() != null)
                {
                    mat.BumpTexture = rhinoMat.GetBumpTexture().FileName;
                }

                mat.Transparency = rhinoMat.Transparency;
                mat.UniqueName   = layer.FullPath.Replace("::", "__");

                matList.Add(mat);
            }

            this.Materials = matList;
            this.Layers    = unifyLayers;
        }
Ejemplo n.º 2
0
    private static Material OverrideMaterial(Material m, UnifyMaterial obj, MeshRenderer renderer)
    {
        renderer.material = m;
        // set main color
        // set transparency
        if (obj.Transparency != 0)
        {
            Color newColor = Utilities.ConvertToUnityColor(obj.Diffuse, obj.Transparency);
            renderer.sharedMaterial.SetFloat("_Mode", 3);
            renderer.sharedMaterial.SetColor("_Color", newColor);
        }
        else
        {
            Color newColor = Utilities.ConvertToUnityColor(obj.Diffuse);
            renderer.sharedMaterial.SetColor("_Color", newColor);
        }

        // set main texture
        if (obj.DiffuseTexture != null)
        {
            // load texture from assets
            string    textureName = System.IO.Path.GetFileName(obj.DiffuseTexture);
            string    texturePath = "Assets/Resources/" + textureName;
            Texture2D texture     = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

            renderer.sharedMaterial.mainTexture = texture;
        }

        //set bump map
        if (obj.BumpTexture != null)
        {
            // load texture from assets
            string    textureName = System.IO.Path.GetFileName(obj.BumpTexture);
            string    texturePath = "Assets/Resources/" + textureName;
            Texture2D bumpTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

            // re-import with readable flag set to true
            string          p  = AssetDatabase.GetAssetPath(bumpTexture);
            TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(p);
            ti.isReadable = true;
            AssetDatabase.ImportAsset(p);

            // convert texture to normal map and save to assets
            Texture2D normalBump     = Utilities.CreateNormalMap(bumpTexture, 1.0f);
            string    sysTexturePath = Application.dataPath + "/Resources/" + textureName.Remove(textureName.IndexOf(".")) + "_normal.png";
            File.WriteAllBytes(sysTexturePath, normalBump.EncodeToPNG());
            AssetDatabase.Refresh();

            // re-import normal texture with normalmap flag set to true
            string          relativeTexturePath = "Assets/Resources/" + textureName.Remove(textureName.IndexOf(".")) + "_normal.png";
            Texture2D       newBumpTexture      = (Texture2D)AssetDatabase.LoadAssetAtPath(relativeTexturePath, typeof(Texture2D));
            string          p2  = AssetDatabase.GetAssetPath(newBumpTexture);
            TextureImporter ti2 = (TextureImporter)TextureImporter.GetAtPath(p2);
            ti2.normalmap = true;
            AssetDatabase.ImportAsset(p2);

            // assign normal map to material
            renderer.sharedMaterial.SetTexture("_BumpMap", newBumpTexture);

            // set bump scale
            renderer.sharedMaterial.SetFloat("_BumpScale", 0.3f);
        }

        // set metallic
        renderer.sharedMaterial.SetFloat("_Metallic", Utilities.ConvertRange(0, 255, 0, 1, Convert.ToSingle(obj.Metallic)));

        // set emission color
        Color emissionColor = Utilities.ConvertToUnityColor(obj.EmissionColor);

        renderer.sharedMaterial.SetColor("_EmissionColor", emissionColor);
        return(renderer.sharedMaterial);
    }