Beispiel #1
0
        private void CreateReplacePrefab(string meshPath, string preFabPath, ProgressiveMesh progMesh, int iteration)
        {
            string newPathMesh   = meshPath.Replace(Application.dataPath, "Assets");
            string newPathPrefab = preFabPath.Replace(Application.dataPath, "Assets");

            string finalMeshPath   = newPathMesh + "/" + meshFbxFiles[iteration].Name;
            string finalPrefabPath = newPathPrefab + "/" + progMeshes[iteration].name.Replace("_Progressive_Mesh", "") + addedToFilename + ".prefab";

            GameObject             objToPrefab = null;
            ProgressiveMeshRuntime progMeshRun = null;

            try
            {
                objToPrefab = AssetDatabase.LoadAssetAtPath(finalMeshPath, typeof(GameObject)) as GameObject;
            }
            catch (UnityException e)
            {
                Debug.Log(e);
            }

            if (objToPrefab == null)
            {
                var sceneObj = PrefabUtility.CreatePrefab(finalPrefabPath, objToPrefab, ReplacePrefabOptions.ReplaceNameBased) as GameObject;

                SetSettings(sceneObj, progMeshRun, progMesh);
            }
            else
            {
                var sceneObj = PrefabUtility.CreatePrefab(finalPrefabPath, objToPrefab, ReplacePrefabOptions.ReplaceNameBased) as GameObject;

                SetSettings(sceneObj, progMeshRun, progMesh);
            }
        }
Beispiel #2
0
        public void CreateAsset(string filename)
        {
            ProgressiveMesh pm = (ProgressiveMesh)ScriptableObject.CreateInstance(typeof(ProgressiveMesh));

            init_all(filename);
            optimize();
            fill_progressive_mesh(pm);
            clean_all();

            string toBeSearched = "Assets/";

            string placeToSave = meshPut.Substring(meshPut.IndexOf(toBeSearched) + toBeSearched.Length);

            string combined = toBeSearched + placeToSave + "/";

            string filePath = combined + filename + "_Progressive_Mesh.asset";

            AssetDatabase.CreateAsset(pm, filePath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
Beispiel #3
0
        private void CreatePrefab(string meshPath, string preFabPath, ProgressiveMesh progMesh, int iteration)
        {
            string newPathMesh   = meshPath.Replace(Application.dataPath, "Assets");
            string newPathPrefab = preFabPath.Replace(Application.dataPath, "Assets");

            string finalMeshPath   = newPathMesh + "/" + meshFbxFiles[iteration].Name;
            string finalPrefabPath = newPathPrefab + "/" + progMeshes[iteration].name + ".prefab";

            GameObject objToPrefab = AssetDatabase.LoadAssetAtPath(finalMeshPath, typeof(GameObject)) as GameObject;

            var progMeshRun = objToPrefab.AddComponent <ProgressiveMeshRuntime>();

            if (progMeshRun != null)
            {
                progMeshRun.progressiveMesh = progMesh;

                if (useRuntimeSettings)
                {
                    progMeshRun.cullRatio = cullRatio;

                    progMeshRun.intensity = lodIntensity;

                    //progMeshRun.lodLevels = lodLevels;

                    progMeshRun.manualMode = manualMode;

                    progMeshRun.maxLod = maxLod;

                    progMeshRun.minLod = minLod;

                    progMeshRun.runInEditor = runInEditor;

                    progMeshRun.notes = notes;
                }
            }

            PrefabUtility.CreatePrefab(finalPrefabPath, objToPrefab);
        }
Beispiel #4
0
        private void CreatePrefabDepreceated(string preFabPath, ProgressiveMesh progMesh)
        {
            GameObject newObject;

            GameObject prefabCurrent = null;

            ProgressiveMesh progMeshCurrent = null;

            string combined     = preFabPath + "/" + progMesh.name;
            string toBeSearched = "Resources/";
            string placeToLoad  = combined.Substring(combined.IndexOf(toBeSearched) + toBeSearched.Length);

            try
            {
                prefabCurrent = Resources.Load(preFabPath) as GameObject;
            }
            catch (UnityException e)
            {
                Debug.Log(e);
            }

            if (prefabCurrent == null)
            {
                newObject      = new GameObject();
                newObject.name = progMesh.name;

                string combinedPrepath     = preFabPath + "/" + progMesh.name;
                string toBeSearchedPrePath = "Resources/";
                string newPlace            = combinedPrepath.Substring(combinedPrepath.IndexOf(toBeSearchedPrePath) + toBeSearchedPrePath.Length);

                string filePath      = newPlace + ".prefab";
                string newPrefabPath = preFabPath + "/" + progMesh.name + ".prefab";

                string searchedAssetsPath = "Resources/";
                string newPrefabPathing   = newPrefabPath.Substring(newPrefabPath.IndexOf(searchedAssetsPath) + searchedAssetsPath.Length);

                Debug.Log(preFabPath);
                PrefabUtility.CreateEmptyPrefab(newPrefabPathing);
                //PrefabUtility.CreateEmptyPrefab(filePath);
                // Save the mesh as an asset.
                Debug.Log(newPlace);
                Debug.Log(newPrefabPathing);
                //AssetDatabase.CreateAsset(newObject, newPlace);
                //AssetDatabase.SaveAssets();
                //AssetDatabase.Refresh();
                //doesn't exist
            }
            else
            {
                Debug.Log("Prefab exists");
                var newProgRunTime = prefabCurrent.AddComponent <ProgressiveMeshRuntime>();
                if (newProgRunTime != null)
                {
                    if (newProgRunTime.progressiveMesh != null)
                    {
                        newProgRunTime.progressiveMesh = progMeshCurrent;
                    }
                }
            }

            //
        }
Beispiel #5
0
        private void SetSettings(GameObject sceneObj, ProgressiveMeshRuntime progMeshRun, ProgressiveMesh progMesh)
        {
            if (sceneObj != null)
            {
                progMeshRun = sceneObj.AddComponent <ProgressiveMeshRuntime>();

                if (progMeshRun != null)
                {
                    progMeshRun.progressiveMesh = progMesh;

                    if (useRuntimeSettings)
                    {
                        progMeshRun.cullRatio = cullRatio;

                        progMeshRun.intensity = lodIntensity;

                        //progMeshRun.lodLevels = lodLevels;

                        progMeshRun.manualMode = manualMode;

                        progMeshRun.maxLod = maxLod;

                        progMeshRun.minLod = minLod;

                        progMeshRun.runInEditor = runInEditor;

                        progMeshRun.notes = notes;
                    }
                }
                else
                {
                    Debug.LogError(string.Format("Failed to add ProgressiveMesh Runtime Component to: {0}", sceneObj.name));
                }
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
            }
            else
            {
                Debug.LogError(string.Format("Failed to create new prefab instance for: {0}", progMesh.name));
            }
        }
Beispiel #6
0
        private void fill_progressive_mesh(ProgressiveMesh pm)
        {
            int triangle_count = 0;

            int[][][][] temp_triangles;
            temp_triangles = new int[ProgressiveMesh.max_lod_count][][][];
            // max lod count
            triangle_count++;
            for (int lod = 0; lod < temp_triangles.Length; lod++)
            {
                float quality = 100.0f * (temp_triangles.Length - lod) / temp_triangles.Length;

                temp_triangles[lod] = new int[Desysia_Meshes.Length][][];

                // mesh count
                triangle_count++;
                int mesh_count = 0;
                foreach (Desysia_Mesh child in Desysia_Meshes)
                {
                    // get triangle list by quality value
                    if (child.index != -1 && get_triangle_list(child.index, quality, child.out_triangles, ref child.out_count) == 1)
                    {
                        if (child.out_count > 0)
                        {
                            int counter = 0;
                            int mat     = 0;
                            temp_triangles[lod][mesh_count] = new int[child.mesh.subMeshCount][];
                            // sub mesh count
                            triangle_count++;
                            while (counter < child.out_count)
                            {
                                int len = child.out_triangles[counter];
                                // triangle count
                                triangle_count++;
                                // triangle list count
                                triangle_count += len;
                                counter++;
                                int[] new_triangles = new int[len];
                                Array.Copy(child.out_triangles, counter, new_triangles, 0, len);
                                temp_triangles[lod][mesh_count][mat] = new_triangles;
                                counter += len;
                                mat++;
                            }
                        }
                        else
                        {
                            temp_triangles[lod][mesh_count] = new int[child.mesh.subMeshCount][];
                            // sub mesh count
                            triangle_count++;
                            for (int mat = 0; mat < temp_triangles[lod][mesh_count].Length; mat++)
                            {
                                temp_triangles[lod][mesh_count][mat] = new int[0];
                                // triangle count
                                triangle_count++;
                            }
                        }
                    }
                    mesh_count++;
                }
            }
            // create fix size array
            pm.triangles = new int[triangle_count];

            // reset the counter
            triangle_count = 0;
            // max lod count
            pm.triangles[triangle_count] = temp_triangles.Length;
            triangle_count++;
            for (int lod = 0; lod < temp_triangles.Length; lod++)
            {
                // mesh count
                pm.triangles[triangle_count] = temp_triangles[lod].Length;
                triangle_count++;
                for (int mesh_count = 0; mesh_count < temp_triangles[lod].Length; mesh_count++)
                {
                    // sub mesh count
                    pm.triangles[triangle_count] = temp_triangles[lod][mesh_count].Length;
                    triangle_count++;
                    for (int mat = 0; mat < temp_triangles[lod][mesh_count].Length; mat++)
                    {
                        // triangle count
                        pm.triangles[triangle_count] = temp_triangles[lod][mesh_count][mat].Length;
                        triangle_count++;
                        Array.Copy(temp_triangles[lod][mesh_count][mat], 0, pm.triangles, triangle_count, temp_triangles[lod][mesh_count][mat].Length);
                        // triangle list count
                        triangle_count += temp_triangles[lod][mesh_count][mat].Length;
                    }
                }
            }
        }
Beispiel #7
0
 public override void OnInspectorGUI()
 {
     DrawDefaultInspector();
     if (target)
     {
         if (Desysia_Meshes != null)
         {
             show_title = EditorGUILayout.Foldout(show_title, "Desysia LOD Editor - V2.0");
             if (show_title)
             {
                 // A decent style.  Light grey text inside a border.
                 GUIStyle helpStyle = new GUIStyle(GUI.skin.box);
                 helpStyle.wordWrap  = true;
                 helpStyle.alignment = TextAnchor.UpperLeft;
                 show_help           = EditorGUILayout.Foldout(show_help, show_help?"Hide Help":"Show Help");
                 // save all triangle lists as progressive mesh
                 if (GUILayout.Button("Save Progressive Mesh", GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true)))
                 {
                     ProgressiveMesh pm = (ProgressiveMesh)ScriptableObject.CreateInstance(typeof(ProgressiveMesh));
                     fill_progressive_mesh(pm);
                     string filePath = EditorUtility.SaveFilePanelInProject(
                         "Save Progressive Mesh",
                         ((Component)target).gameObject.name + "_Progressive_Mesh.asset",
                         "asset",
                         "Choose a file location"
                         );
                     if (filePath != "")
                     {
                         AssetDatabase.CreateAsset(pm, filePath);
                         AssetDatabase.SaveAssets();
                         AssetDatabase.Refresh();
                     }
                 }
                 if (show_help)
                 {
                     GUILayout.Label(
                         "When Clicked, save the progressive meshes as a single asset file for runtime use."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 // save current mesh as a lod asset file
                 if (GUILayout.Button("Save Current Mesh", GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true)))
                 {
                     foreach (Desysia_Mesh child in Desysia_Meshes)
                     {
                         // clone the mesh
                         Mesh new_mesh = (Mesh)Instantiate(child.mesh);
                         // remove unused vertices
                         if (new_mesh.blendShapeCount == 0)
                         {
                             shrink_mesh(new_mesh);
                         }
                         string filePath = EditorUtility.SaveFilePanelInProject(
                             "Save Current Mesh",
                             new_mesh.name + "_Quality_" + quality.ToString() + ".asset",
                             "asset",
                             "Choose a file location"
                             );
                         if (filePath != "")
                         {
                             AssetDatabase.CreateAsset(new_mesh, filePath);
                             AssetDatabase.SaveAssets();
                             AssetDatabase.Refresh();
                         }
                     }
                 }
                 if (show_help)
                 {
                     GUILayout.Label(
                         "When Clicked, save the meshes of current quality as LOD asset files."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 protect_boundary = EditorGUILayout.Toggle("Protect Boundary", protect_boundary);
                 if (show_help)
                 {
                     GUILayout.Label(
                         "When checked, all open boundaries will be protected; Otherwise, some smooth parts of open boundaries will be smartly merged. Both way, uv boundaries and material boundaries will be protected."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 protect_detail = EditorGUILayout.Toggle("More Details", protect_detail);
                 if (show_help)
                 {
                     GUILayout.Label(
                         "When checked, more details will be preserved, toggle it only if when making the highest LOD, otherwise, please leave it unchecked to get best results."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 protect_symmetry = EditorGUILayout.Toggle("Protect Symmetry", protect_symmetry);
                 if (show_help)
                 {
                     GUILayout.Label(
                         "When checked, all symmetric uv mapping will be preserved, you should check it only if you are making the higher LODs; Otherwise, please leave it unchecked to get best results."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 EditorGUILayout.LabelField("Triangles", face_number.ToString() + "/" + origin_face_number.ToString());
                 if (show_help)
                 {
                     GUILayout.Label(
                         "Display current triangle number and total triangle number."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 quality = EditorGUILayout.Slider("Quality", quality, 0.0f, 100.0f);
                 if (show_help)
                 {
                     GUILayout.Label(
                         "Drag to change the quality, the mesh will change in real time."
                         , helpStyle
                         , GUILayout.ExpandWidth(true));
                 }
                 if (GUI.changed)
                 {
                     if (save_show_help != show_help)
                     {
                         string filename = "desysialod_not_show_help";
                         if (show_help)
                         {
                             if (System.IO.File.Exists(filename))
                             {
                                 System.IO.File.Delete(filename);
                             }
                             ;
                         }
                         else
                         {
                             //ADDED .DISPOSE to stop sharing violation from happening. This stops access to the file right away after creation
                             if (!System.IO.File.Exists(filename))
                             {
                                 System.IO.File.Create(filename).Dispose();
                             }
                             ;
                         }
                         save_show_help = show_help;
                     }
                     if (save_protect_boundary != protect_boundary)
                     {
                         quality = 100.0f;
                         // delete old progressive mesh
                         clean_all();
                         init_all();
                         // create new progressive mesh
                         optimize();
                         save_protect_boundary = protect_boundary;
                     }
                     if (save_protect_detail != protect_detail)
                     {
                         quality = 100.0f;
                         // delete old progressive mesh
                         clean_all();
                         init_all();
                         // create new progressive mesh
                         optimize();
                         save_protect_detail = protect_detail;
                     }
                     if (save_protect_symmetry != protect_symmetry)
                     {
                         quality = 100.0f;
                         // delete old progressive mesh
                         clean_all();
                         init_all();
                         // create new progressive mesh
                         optimize();
                         save_protect_symmetry = protect_symmetry;
                     }
                     if (save_quality != quality)
                     {
                         face_number = 0;
                         foreach (Desysia_Mesh child in Desysia_Meshes)
                         {
                             // get triangle list by quality value
                             if (child.index != -1 && get_triangle_list(child.index, quality, child.out_triangles, ref child.out_count) == 1)
                             {
                                 if (child.out_count > 0)
                                 {
                                     int counter = 0;
                                     int mat     = 0;
                                     while (counter < child.out_count)
                                     {
                                         int len = child.out_triangles[counter];
                                         counter++;
                                         if (len > 0)
                                         {
                                             int[] new_triangles = new int[len];
                                             Array.Copy(child.out_triangles, counter, new_triangles, 0, len);
                                             child.mesh.SetTriangles(new_triangles, mat);
                                             counter += len;
                                         }
                                         else
                                         {
                                             child.mesh.SetTriangles((int[])null, mat);
                                         }
                                         mat++;
                                     }
                                     face_number += child.mesh.triangles.Length / 3;
                                     // refresh normals and bounds
                                     child.mesh.RecalculateNormals();
                                     child.mesh.RecalculateBounds();
                                     EditorUtility.SetDirty(target);
                                 }
                             }
                         }
                         save_quality = quality;
                     }
                 }
             }
         }
     }
 }