Ejemplo n.º 1
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;
        }
    }
Ejemplo n.º 2
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");
                        mats[i].SetColor("_SpecCol", mtl.Ks);
                        mats[i].SetFloat("_Shininess", mtl.Ns);
                        break;

                    case 4:
                    case 6:
                    case 7:
                    case 9:
                        mats[i] = CreateMaterial(mtl.name, "Transparent/Specular");
                        mats[i].SetColor("_SpecCol", mtl.Ks);
                        mats[i].SetFloat("_Shininess", mtl.Ns);
                        break;

                    case 3:
                    case 5:
                    case 8:
                        mats[i] = CreateMaterial(mtl.name, "Reflection/Specular");
                        mats[i].SetColor("_SpecCol", mtl.Ks);
                        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;
        }
    }