示例#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;
        }
    }
示例#2
0
    public override void OnInspectorGUI()
    {
        bool undo = true;

        MegaCacheOBJ mod = (MegaCacheOBJ)target;

        serializedObject.Update();

#if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019 && !UNITY_2020
        EditorGUIUtility.LookLikeControls();
#endif

        EditorGUILayout.BeginVertical("box");
        mod.showdataimport = EditorGUILayout.Foldout(mod.showdataimport, "Data Import");

        if (mod.showdataimport)
        {
            EditorGUILayout.PropertyField(_prop_firstframe, new GUIContent("First"));
            EditorGUILayout.PropertyField(_prop_lastframe, new GUIContent("Last"));
            EditorGUILayout.PropertyField(_prop_skip, new GUIContent("Skip"));

            //int val = 0;
            //mod.decformat = EditorGUILayout.IntSlider("Format name" + mod.namesplit + val.ToString("D" + mod.decformat) + ".obj", mod.decformat, 1, 6);
            //mod.namesplit = EditorGUILayout.TextField("Name Split Char", mod.namesplit);
            EditorGUILayout.PropertyField(_prop_scale, new GUIContent("Import Scale"));
            EditorGUILayout.PropertyField(_prop_adjustcords, new GUIContent("Adjust Coords"));
            EditorGUILayout.PropertyField(_prop_buildtangents, new GUIContent("Build Tangents"));
            EditorGUILayout.PropertyField(_prop_updatecollider, new GUIContent("Update Collider"));
            EditorGUILayout.PropertyField(_prop_loadmtls, new GUIContent("Load Materials"));
            EditorGUILayout.PropertyField(_prop_recalcnormals, new GUIContent("Recalculate Normals"));

            if (GUILayout.Button("Load Frames"))
            {
                string file = EditorUtility.OpenFilePanel("OBJ File", mod.lastpath, "obj");

                if (file != null && file.Length > 1)
                {
                    mod.lastpath = file;
                    LoadOBJ(mod, file, mod.firstframe, mod.lastframe, mod.skip);
                }
            }

            if (mod.meshes.Count > 0)
            {
                if (GUILayout.Button("Clear Stored Meshes"))
                {
                    mod.DestroyMeshes();
                }
            }
        }
        EditorGUILayout.EndVertical();

        mod.showdata = EditorGUILayout.Foldout(mod.showdata, "Data");

        if (mod.showdata)
        {
            MegaCacheData src = (MegaCacheData)EditorGUILayout.EnumPopup("Data Source", mod.datasource);

            if (src != mod.datasource)
            {
                mod.ChangeSource(src);
            }

            switch (mod.datasource)
            {
            case MegaCacheData.Mesh:
                if (mod.meshes.Count > 0)
                {
                    EditorGUILayout.BeginVertical("box");
                    EditorGUILayout.PropertyField(_prop_saveuvs, new GUIContent("Save Uvs"));
                    EditorGUILayout.PropertyField(_prop_savenormals, new GUIContent("Save Normals"));
                    EditorGUILayout.PropertyField(_prop_savetangents, new GUIContent("Save Tangents"));
                    EditorGUILayout.PropertyField(_prop_optimize, new GUIContent("Optimize Data"));

                    if (GUILayout.Button("Save MegaCache File"))
                    {
                        string file = EditorUtility.SaveFilePanel("MegaCache File", mod.lastpath, mod.name, "mgc");

                        if (file != null && file.Length > 1)
                        {
                            mod.CloseCache();
                            CreateCacheFile(file);
                            if (mod.cachefile.Length == 0)
                            {
                                mod.cachefile = file;
                            }
                        }
                    }

                    if (GUILayout.Button("Create Image"))
                    {
                        CreateCacheImage();
                    }

                    EditorGUILayout.EndVertical();
                }
                break;

            case MegaCacheData.File:
                EditorGUILayout.BeginVertical("box");
                EditorGUILayout.TextArea("Cache File: " + mod.cachefile);

                if (GUILayout.Button("Select MegaCache File"))
                {
                    string file = EditorUtility.OpenFilePanel("MegaCache File", mod.lastpath, "mgc");

                    if (file != null && file.Length > 1)
                    {
                        mod.CloseCache();
                        mod.cachefile = file;
                        mod.update    = true;
                        mod.OpenCache(mod.cachefile);
                    }
                }

                EditorGUILayout.PropertyField(_prop_runtimefolder, new GUIContent("Runtime Folder"));

                if (mod.cachefile.Length > 0)
                {
                    if (GUILayout.Button("Create Image From Cache"))
                    {
                        bool doit = true;
                        if (mod.cacheimage)
                        {
                            if (!EditorUtility.DisplayDialog("Add to or Replace", "Image already loaded do you want to Replace?", "Yes", "No"))
                            {
                                doit = false;
                            }
                        }

                        if (doit)
                        {
                            mod.CreateImageFromCacheFile();
                        }
                    }
                }

                EditorGUILayout.EndVertical();
                break;

            case MegaCacheData.Image:
                if (mod.cacheimage)
                {
                    EditorGUILayout.BeginVertical("box");
#if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
                    mod.cacheimage.threadupdate = EditorGUILayout.Toggle("Preload", mod.cacheimage.threadupdate);
#endif
                    if (GUILayout.Button("Delete Image"))
                    {
                        mod.DestroyImage();                                     // = null;
                    }
                    EditorGUILayout.EndVertical();
                }
                break;
            }

            string info = "";

            info += "Frame Verts: " + mod.framevertcount + "\nFrame Tris: " + (mod.frametricount / 3);

            if (mod.datasource == MegaCacheData.Image)
            {
                if (mod.cacheimage)
                {
                    info += "\nMemory: " + mod.cacheimage.memoryuse / (1024 * 1024) + "MB";
                }
                else
                {
                    info += "\nNo Image File";
                }
            }

            EditorGUILayout.HelpBox(info, MessageType.None);
        }

        mod.showanimation = EditorGUILayout.Foldout(mod.showanimation, "Animation");

        if (mod.showanimation)
        {
            EditorGUILayout.BeginVertical("box");

            int fc = 0;
            switch (mod.datasource)
            {
            case MegaCacheData.Mesh: fc = mod.meshes.Count - 1; break;

            case MegaCacheData.File: fc = mod.framecount - 1; break;

            case MegaCacheData.Image:
                if (mod.cacheimage && mod.cacheimage.frames != null)
                {
                    fc = mod.cacheimage.frames.Count - 1;
                }
                break;
            }

            if (fc > 0)
            {
                EditorGUILayout.IntSlider(_prop_frame, 0, fc);
            }

            mod.animate = EditorGUILayout.BeginToggleGroup("Animate", mod.animate);
            bool timechange = GUI.changed;
            EditorGUILayout.PropertyField(_prop_time, new GUIContent("Time"));
            if (GUI.changed && !timechange)
            {
                undo = false;
            }

            EditorGUILayout.PropertyField(_prop_fps, new GUIContent("Fps"));
            EditorGUILayout.PropertyField(_prop_speed, new GUIContent("Speed"));
            EditorGUILayout.PropertyField(_prop_loopmode, new GUIContent("Loop Mode"));

            EditorGUILayout.EndToggleGroup();
            EditorGUILayout.EndVertical();
        }

        mod.showextras = EditorGUILayout.Foldout(mod.showextras, "Extra Options");

        if (mod.showextras)
        {
            mod.shownormals = EditorGUILayout.BeginToggleGroup("Show Normals", mod.shownormals);
            mod.normallen   = EditorGUILayout.FloatField("Normal Length", mod.normallen);
            EditorGUILayout.EndToggleGroup();
        }

        if (GUI.changed)
        {
            if (undo)
            {
                serializedObject.ApplyModifiedProperties();
            }
            else
            {
#if UNITY_5_3_OR_NEWER || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
                serializedObject.ApplyModifiedPropertiesWithoutUndo();
#else
                serializedObject.ApplyModifiedProperties();
#endif
            }
            EditorUtility.SetDirty(target);
        }
    }
    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;
        }
    }