Example #1
0
    static MegaCacheOBJMtl HaveMaterial(string name)
    {
        for (int i = 0; i < mtls.Count; i++)
        {
            if (mtls[i].name == name)
            {
                return(mtls[i]);
            }
        }

        MegaCacheOBJMtl mtl = new MegaCacheOBJMtl();

        mtl.name = name;
        mtls.Add(mtl);
        return(mtl);
    }
    static void LoadMtlLib(string filename)
    {
        string path = Path.GetDirectoryName(filename);

        StreamReader stream     = File.OpenText(filename);
        string       entireText = stream.ReadToEnd();

        stream.Close();

        using (StringReader reader = new StringReader(entireText))
        {
            string   currentText     = reader.ReadLine();
            char[]   splitIdentifier = { ' ' };
            string[] brokenString;

            while (currentText != null)
            {
                currentText  = currentText.Trim();
                brokenString = currentText.Split(splitIdentifier, 50);
                switch (brokenString[0])
                {
                case "newmtl":
                    brokenString[1] = brokenString[1].Replace(':', '_');
                    MegaCacheOBJMtl mtl = HaveMaterial(brokenString[1]);
                    loadmtl = mtl;
                    break;

                case "Ns":
                    loadmtl.Ns = float.Parse(brokenString[1]);
                    break;

                case "Ni":
                    loadmtl.Ni = float.Parse(brokenString[1]);
                    break;

                case "d":
                    loadmtl.d = float.Parse(brokenString[1]);
                    break;

                case "Tr":
                    loadmtl.Tr = float.Parse(brokenString[1]);
                    break;

                case "Tf":
                    loadmtl.Tf.r = float.Parse(brokenString[1]);
                    loadmtl.Tf.g = float.Parse(brokenString[2]);
                    loadmtl.Tf.b = float.Parse(brokenString[3]);
                    break;

                case "illum":
                    loadmtl.illum = int.Parse(brokenString[1]);
                    break;

                case "Ka":
                    loadmtl.Ka.r = float.Parse(brokenString[1]);
                    loadmtl.Ka.g = float.Parse(brokenString[2]);
                    loadmtl.Ka.b = float.Parse(brokenString[3]);
                    break;

                case "Kd":
                    loadmtl.Kd.r = float.Parse(brokenString[1]);
                    loadmtl.Kd.g = float.Parse(brokenString[2]);
                    loadmtl.Kd.b = float.Parse(brokenString[3]);
                    break;

                case "Ks":
                    loadmtl.Ks.r = float.Parse(brokenString[1]);
                    loadmtl.Ks.g = float.Parse(brokenString[2]);
                    loadmtl.Ks.b = float.Parse(brokenString[3]);
                    break;

                case "Ke":
                    loadmtl.Ke.r = float.Parse(brokenString[1]);
                    loadmtl.Ke.g = float.Parse(brokenString[2]);
                    loadmtl.Ke.b = float.Parse(brokenString[3]);
                    break;

                case "map_Ka":
                    loadmtl.map_Ka = brokenString[1];
                    break;

                case "map_Kd":
                    string dir = Path.GetDirectoryName(brokenString[1]);

                    if (dir.Length == 0)
                    {
                        loadmtl.map_Kd = path + "/" + brokenString[1];
                    }
                    else
                    {
                        loadmtl.map_Kd = brokenString[1];
                    }
                    break;
                }

                currentText = reader.ReadLine();
                if (currentText != null)
                {
                    currentText = currentText.Replace("  ", " ");
                }
            }
        }
    }
Example #3
0
    public void LoadOBJ(MegaCacheOBJ mod, string filename, int first, int last, int step)
    {
        if (mod.meshes.Count > 0)
        {
            if (!EditorUtility.DisplayDialog("Add to or Replace", "Add new OBJ meshes to existing list, or Replace All", "Add", "Replace"))
            {
                mod.DestroyMeshes();
            }
        }

        if (step < 1)
        {
            step = 1;
        }

        mod.InitImport();

        for (int i = first; i <= last; i += step)
        {
            mod.LoadMtl(filename, i);
        }

        for (int i = first; i <= last; i += step)
        {
            float a = (float)(i + 1 - first) / (last - first);
            if (!EditorUtility.DisplayCancelableProgressBar("Loading OBJ Meshes", "Frame " + i, a))
            {
                Mesh ms = mod.LoadFrame(filename, i);
                if (ms)
                {
                    mod.AddMesh(ms);
                }
                else
                {
                    EditorUtility.DisplayDialog("Can't Load File", "Could not load frame " + i + " of sequence! Import Stopped.", "OK");
                    break;
                }
            }
            else
            {
                break;
            }
        }

        EditorUtility.ClearProgressBar();

        if (mod.loadmtls)
        {
            int        count = MegaCacheObjImporter.NumMtls();
            Material[] mats  = new Material[count];

            for (int i = 0; i < count; i++)
            {
                MegaCacheOBJMtl mtl = MegaCacheObjImporter.GetMtl(i);

                switch (mtl.illum)
                {
                case 0:
                case 1:
                    mats[i] = CreateMaterial(mtl.name, "Diffuse");
                    break;

                case 2:
                    mats[i] = CreateMaterial(mtl.name, "Specular");
#if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
                    mats[i].SetColor("_SpecColor", mtl.Ks);
#else
                    mats[i].SetColor("_SpecCol", mtl.Ks);
#endif
                    mats[i].SetFloat("_Shininess", mtl.Ns);
                    break;

                case 4:
                case 6:
                case 7:
                case 9:
                    mats[i] = CreateMaterial(mtl.name, "Transparent/Specular");
#if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
                    mats[i].SetColor("_SpecColor", mtl.Ks);
#else
                    mats[i].SetColor("_SpecCol", mtl.Ks);
#endif
                    mats[i].SetFloat("_Shininess", mtl.Ns);
                    break;

                case 3:
                case 5:
                case 8:
                    mats[i] = CreateMaterial(mtl.name, "Reflection/Specular");
#if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
                    mats[i].SetColor("_SpecColor", mtl.Ks);
#else
                    mats[i].SetColor("_SpecCol", mtl.Ks);
#endif
                    mats[i].SetFloat("_Shininess", mtl.Ns);
                    break;
                }

                mats[i].name = mtl.name;

                mats[i].color = mtl.Kd;
                if (mtl.map_Kd != null)
                {
                    mats[i].mainTexture = LoadTexture(mtl.map_Kd);
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            mod.GetComponent <Renderer>().sharedMaterials = mats;
        }
    }
    static void LoadMtlLib(string filename)
    {
        string path = Path.GetDirectoryName(filename);

        StreamReader stream = File.OpenText(filename);
        string entireText = stream.ReadToEnd();
        stream.Close();

        using ( StringReader reader = new StringReader(entireText) )
        {
            string currentText = reader.ReadLine();
            char[] splitIdentifier = { ' ' };
            string[] brokenString;

            while ( currentText != null )
            {
                currentText = currentText.Trim();
                brokenString = currentText.Split(splitIdentifier, 50);
                switch ( brokenString[0] )
                {
                    case "newmtl":
                        MegaCacheOBJMtl mtl = HaveMaterial(brokenString[1]);
                        loadmtl = mtl;
                        break;

                    case "Ns":
                        loadmtl.Ns = float.Parse(brokenString[1]);
                        break;

                    case "Ni":
                        loadmtl.Ni = float.Parse(brokenString[1]);
                        break;

                    case "d":
                        loadmtl.d = float.Parse(brokenString[1]);
                        break;

                    case "Tr":
                        loadmtl.Tr = float.Parse(brokenString[1]);
                        break;

                    case "Tf":
                        loadmtl.Tf.r = float.Parse(brokenString[1]);
                        loadmtl.Tf.g = float.Parse(brokenString[2]);
                        loadmtl.Tf.b = float.Parse(brokenString[3]);
                        break;

                    case "illum":
                        loadmtl.illum = int.Parse(brokenString[1]);
                        break;

                    case "Ka":
                        loadmtl.Ka.r = float.Parse(brokenString[1]);
                        loadmtl.Ka.g = float.Parse(brokenString[2]);
                        loadmtl.Ka.b = float.Parse(brokenString[3]);
                        break;

                    case "Kd":
                        loadmtl.Kd.r = float.Parse(brokenString[1]);
                        loadmtl.Kd.g = float.Parse(brokenString[2]);
                        loadmtl.Kd.b = float.Parse(brokenString[3]);
                        break;

                    case "Ks":
                        loadmtl.Ks.r = float.Parse(brokenString[1]);
                        loadmtl.Ks.g = float.Parse(brokenString[2]);
                        loadmtl.Ks.b = float.Parse(brokenString[3]);
                        break;

                    case "Ke":
                        loadmtl.Ke.r = float.Parse(brokenString[1]);
                        loadmtl.Ke.g = float.Parse(brokenString[2]);
                        loadmtl.Ke.b = float.Parse(brokenString[3]);
                        break;

                    case "map_Ka":
                        loadmtl.map_Ka = brokenString[1];
                        break;

                    case "map_Kd":
                        string dir = Path.GetDirectoryName(brokenString[1]);

                        if ( dir.Length == 0 )
                            loadmtl.map_Kd = path + "/" + brokenString[1];
                        else
                            loadmtl.map_Kd = brokenString[1];
                        break;
                }

                currentText = reader.ReadLine();
                if ( currentText != null )
                    currentText = currentText.Replace("  ", " ");
            }
        }
    }
    static MegaCacheOBJMtl HaveMaterial(string name)
    {
        for ( int i = 0; i < mtls.Count; i++ )
        {
            if ( mtls[i].name == name )
            {
                return mtls[i];
            }
        }

        MegaCacheOBJMtl mtl = new MegaCacheOBJMtl();
        mtl.name = name;
        mtls.Add(mtl);
        return mtl;
    }