Esempio n. 1
0
        private void Bake(MeshGenerator gen)
        {
            gen.Bake(isStatic, lightmapUV);
            MeshRenderer renderer = gen.GetComponent <MeshRenderer>();
            MeshFilter   filter   = gen.GetComponent <MeshFilter>();

            if (saveMesh)
            {
                FileInfo[] files    = dirInfo.GetFiles(filter.sharedMesh.name + "*.obj");
                string     meshName = filter.sharedMesh.name;
                if (files.Length > 0)
                {
                    meshName += "_" + files.Length;
                }
                string path         = savePath + "/" + meshName + ".obj";
                string relativepath = "Assets" + path.Substring(Application.dataPath.Length);
                string objString    = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials);
                File.WriteAllText(path, objString);
                AssetDatabase.ImportAsset(relativepath, ImportAssetOptions.ForceSynchronousImport);
#if UNITY_5_0
                filter.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(relativepath, typeof(Mesh));
#else
                filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativepath);
#endif
            }
            if (removeComputer)
            {
                GameObject.DestroyImmediate(gen.computer);
            }
            if (permanent)
            {
                GameObject.DestroyImmediate(gen);
            }
        }
Esempio n. 2
0
 protected override void FooterGUI()
 {
     base.FooterGUI();
     showInfo = EditorGUILayout.Foldout(showInfo, "Info & Components");
     if (showInfo)
     {
         MeshGenerator generator = (MeshGenerator)target;
         MeshFilter    filter    = generator.GetComponent <MeshFilter>();
         if (filter == null)
         {
             return;
         }
         MeshRenderer renderer = generator.GetComponent <MeshRenderer>();
         string       str      = "";
         if (filter.sharedMesh != null)
         {
             str = "Vertices: " + filter.sharedMesh.vertexCount + "\r\nTriangles: " + (filter.sharedMesh.triangles.Length / 3);
         }
         else
         {
             str = "No info available in prefab mode";
         }
         EditorGUILayout.HelpBox(str, MessageType.Info);
         bool showFilter = filter.hideFlags == HideFlags.None;
         bool last       = showFilter;
         showFilter = EditorGUILayout.Toggle("Show Mesh Filter", showFilter);
         if (last != showFilter)
         {
             if (showFilter)
             {
                 filter.hideFlags = HideFlags.None;
             }
             else
             {
                 filter.hideFlags = HideFlags.HideInInspector;
             }
         }
         bool showRenderer = renderer.hideFlags == HideFlags.None;
         last         = showRenderer;
         showRenderer = EditorGUILayout.Toggle("Show Mesh Renderer", showRenderer);
         if (last != showRenderer)
         {
             if (showRenderer)
             {
                 renderer.hideFlags = HideFlags.None;
             }
             else
             {
                 renderer.hideFlags = HideFlags.HideInInspector;
             }
         }
     }
     if (GUILayout.Button("Bake Mesh"))
     {
         MeshGenerator generator = (MeshGenerator)target;
         bakeWindow = EditorWindow.GetWindow <BakeMeshWindow>();
         bakeWindow.Init(generator);
     }
 }
Esempio n. 3
0
        public virtual void ShowInfo()
        {
            MeshGenerator generator = (MeshGenerator)target;

            EditorGUILayout.Space();
            showInfo = EditorGUILayout.Foldout(showInfo, "Info & Components");
            if (showInfo)
            {
                MeshFilter filter = generator.GetComponent <MeshFilter>();
                if (filter == null)
                {
                    return;
                }
                MeshRenderer renderer = generator.GetComponent <MeshRenderer>();
                string       str      = "";
                if (filter.sharedMesh != null)
                {
                    str = "Vertices: " + filter.sharedMesh.vertexCount + "\r\nTriangles: " + (filter.sharedMesh.triangles.Length / 3);
                }
                else
                {
                    str = "No info available in prefab mode";
                }
                EditorGUILayout.HelpBox(str, MessageType.Info);
                bool showFilter = filter.hideFlags == HideFlags.None;
                bool last       = showFilter;
                showFilter = EditorGUILayout.Toggle("Show Mesh Filter", showFilter);
                if (last != showFilter)
                {
                    if (showFilter)
                    {
                        filter.hideFlags = HideFlags.None;
                    }
                    else
                    {
                        filter.hideFlags = HideFlags.HideInInspector;
                    }
                }
                bool showRenderer = renderer.hideFlags == HideFlags.None;
                last         = showRenderer;
                showRenderer = EditorGUILayout.Toggle("Show Mesh Renderer", showRenderer);
                if (last != showRenderer)
                {
                    if (showRenderer)
                    {
                        renderer.hideFlags = HideFlags.None;
                    }
                    else
                    {
                        renderer.hideFlags = HideFlags.HideInInspector;
                    }
                }
            }
        }
 public void Init(MeshGenerator generator)
 {
     titleContent = new GUIContent("Bake Mesh");
     meshGen      = generator;
     filter       = generator.GetComponent <MeshFilter>();
     if (EditorPrefs.HasKey("BakeWindow_isStatic"))
     {
         isStatic = EditorPrefs.GetBool("BakeWindow_isStatic");
     }
     if (EditorPrefs.HasKey("BakeWindow_copy"))
     {
         copy = EditorPrefs.GetBool("BakeWindow_copy");
     }
     if (EditorPrefs.HasKey("BakeWindow_removeComputer"))
     {
         removeComputer = EditorPrefs.GetBool("BakeWindow_removeComputer");
     }
     if (EditorPrefs.HasKey("BakeWindow_permanent"))
     {
         permanent = EditorPrefs.GetBool("BakeWindow_permanent");
     }
     format  = (SaveFormat)EditorPrefs.GetInt("BakeWindow_format", 0);
     minSize = new Vector2(340, 220);
     maxSize = minSize;
 }
Esempio n. 5
0
        protected override void Awake()
        {
            MeshGenerator generator = (MeshGenerator)target;
            MeshRenderer  rend      = generator.GetComponent <MeshRenderer>();

            if (rend == null)
            {
                return;
            }
            base.Awake();
        }
Esempio n. 6
0
        void SaveMeshFile(string savePath)
        {
            if (format == SaveFormat.None)
            {
                return;
            }
            string relativePath = "";

            if (savePath.StartsWith(Application.dataPath))
            {
                relativePath = "Assets" + savePath.Substring(Application.dataPath.Length);
            }

            if (format == SaveFormat.MeshAsset)
            {
                if (copy)
                {
                    Mesh assetMesh = MeshUtility.Copy(filter.sharedMesh);
                    AssetDatabase.CreateAsset(assetMesh, relativePath);
                }
                else
                {
                    AssetDatabase.CreateAsset(filter.sharedMesh, relativePath);
                }
            }

            if (format == SaveFormat.OBJ)
            {
                MeshRenderer renderer  = meshGen.GetComponent <MeshRenderer>();
                string       objString = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials);
                File.WriteAllText(savePath, objString);
                if (!copy)
                {
                    DestroyImmediate(filter.sharedMesh);
                }
                if (relativePath != "") //Import back the OBJ
                {
                    AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceSynchronousImport);
#if UNITY_5_0
                    if (!copy)
                    {
                        filter.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(relativePath, typeof(Mesh));
                    }
#else
                    if (!copy)
                    {
                        filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativePath);
                    }
#endif
                }
            }
        }
Esempio n. 7
0
        protected override void OnDelete()
        {
            base.OnDelete();
            MeshGenerator generator = (MeshGenerator)target;

            if (generator == null)
            {
                return;
            }
            MeshFilter filter = generator.GetComponent <MeshFilter>();

            if (filter != null)
            {
                filter.hideFlags = HideFlags.None;
            }
            MeshRenderer renderer = generator.GetComponent <MeshRenderer>();

            if (renderer != null)
            {
                renderer.hideFlags = HideFlags.None;
            }
        }
Esempio n. 8
0
        protected override void Awake()
        {
            MeshGenerator generator = (MeshGenerator)target;
            MeshRenderer  rend      = generator.GetComponent <MeshRenderer>();

            if (rend == null)
            {
                return;
            }
            if (rend.sharedMaterial == null)
            {
                rend.sharedMaterial = AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            }
            base.Awake();
        }
Esempio n. 9
0
        protected override void OnSceneGUI()
        {
            base.OnSceneGUI();
            MeshGenerator generator = (MeshGenerator)target;

            if (Application.isPlaying)
            {
                return;
            }
            framesPassed++;
            if (framesPassed >= 100)
            {
                framesPassed = 0;
                if (generator != null && generator.GetComponent <MeshCollider>() != null)
                {
                    generator.UpdateCollider();
                }
            }
        }
Esempio n. 10
0
        protected override void OnDestroy()
        {
            MeshGenerator generator = (MeshGenerator)target;

            base.OnDestroy();
            MeshGenerator gen = (MeshGenerator)target;

            if (gen == null)
            {
                return;
            }
            if (gen.GetComponent <MeshCollider>() != null)
            {
                generator.UpdateCollider();
            }
            if (bakeWindow != null)
            {
                bakeWindow.Close();
            }
        }
Esempio n. 11
0
        public void Init(MeshGenerator generator)
        {
#if UNITY_5_0
            title = "Bake Mesh";
#else
            titleContent = new GUIContent("Bake Mesh");
#endif
            meshGen = generator;
            filter  = generator.GetComponent <MeshFilter>();
            if (EditorPrefs.HasKey("BakeWindow_isStatic"))
            {
                isStatic = EditorPrefs.GetBool("BakeWindow_isStatic");
            }
            if (EditorPrefs.HasKey("BakeWindow_lightmapUV"))
            {
                lightmapUV = EditorPrefs.GetBool("BakeWindow_lightmapUV");
            }
            if (EditorPrefs.HasKey("BakeWindow_saveMesh"))
            {
                saveMesh = EditorPrefs.GetBool("BakeWindow_saveMesh");
            }
            if (EditorPrefs.HasKey("BakeWindow_copy"))
            {
                copy = EditorPrefs.GetBool("BakeWindow_copy");
            }
            if (EditorPrefs.HasKey("BakeWindow_removeComputer"))
            {
                removeComputer = EditorPrefs.GetBool("BakeWindow_removeComputer");
            }
            if (EditorPrefs.HasKey("BakeWindow_permanent"))
            {
                permanent = EditorPrefs.GetBool("BakeWindow_permanent");
            }
            minSize = new Vector2(340, 190);
            maxSize = minSize;
        }
Esempio n. 12
0
        public override void BaseGUI()
        {
            MeshGenerator generator = (MeshGenerator)target;

            base.BaseGUI();
            if (showTangents || showOptimize)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Mesh", EditorStyles.boldLabel);
                if (showOptimize)
                {
                    generator.optimize = EditorGUILayout.Toggle("Optimize", generator.optimize);
                }
                if (showTangents)
                {
                    generator.calculateTangents = EditorGUILayout.Toggle("Calculate Tangents", generator.calculateTangents);
                }
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Vertices", EditorStyles.boldLabel);
            if (showSize)
            {
                generator.size = EditorGUILayout.FloatField("Size", generator.size);
            }
            if (showColor)
            {
                generator.color = EditorGUILayout.ColorField("Color", generator.color);
            }
            if (showNormalMethod)
            {
                generator.normalMethod = EditorGUILayout.Popup("Normal Method", generator.normalMethod, normalMethods);
            }
            if (showOffset)
            {
                generator.offset = EditorGUILayout.Vector3Field("Offset", generator.offset);
            }
            if (showRotation)
            {
                generator.rotation = EditorGUILayout.Slider("Rotation", generator.rotation, -180f, 180f);
            }

            if (showDoubleSided || showFlipFaces)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Faces", EditorStyles.boldLabel);
                if (showDoubleSided)
                {
                    generator.doubleSided = EditorGUILayout.Toggle("Double-sided", generator.doubleSided);
                }
                if (!generator.doubleSided && showFlipFaces)
                {
                    generator.flipFaces = EditorGUILayout.Toggle("Flip faces", generator.flipFaces);
                }
            }

            if (generator.GetComponent <MeshCollider>() != null)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Mesh Collider", EditorStyles.boldLabel);
                generator.colliderUpdateRate = EditorGUILayout.FloatField("Collider Update Iterval", generator.colliderUpdateRate);
            }
        }
Esempio n. 13
0
        private void Bake(MeshGenerator gen)
        {
            MeshFilter filter = gen.GetComponent <MeshFilter>();

            if (filter == null)
            {
                EditorUtility.DisplayDialog("Save error", "No mesh present in " + gen.name, "OK");
                return;
            }
            if (copy)
            {
                UnityEditor.MeshUtility.Optimize(filter.sharedMesh);
                Unwrapping.GenerateSecondaryUVSet(filter.sharedMesh);
            }
            else
            {
                gen.Bake(isStatic, true);
            }

            if (format == BakeMeshWindow.SaveFormat.OBJ)
            {
                MeshRenderer renderer = gen.GetComponent <MeshRenderer>();
                dirInfo = new DirectoryInfo(savePath);
                FileInfo[] files    = dirInfo.GetFiles(filter.sharedMesh.name + "*.obj");
                string     meshName = filter.sharedMesh.name;
                if (files.Length > 0)
                {
                    meshName += "_" + files.Length;
                }
                string path      = savePath + "/" + meshName + ".obj";
                string objString = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials);
                File.WriteAllText(path, objString);
                if (copy)
                {
                    string relativepath = "Assets" + path.Substring(Application.dataPath.Length);
                    AssetDatabase.ImportAsset(relativepath, ImportAssetOptions.ForceSynchronousImport);
                    filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativepath);
                }
            }

            if (format == BakeMeshWindow.SaveFormat.MeshAsset)
            {
                dirInfo = new DirectoryInfo(savePath);
                FileInfo[] files    = dirInfo.GetFiles(filter.sharedMesh.name + "*.asset");
                string     meshName = filter.sharedMesh.name;
                if (files.Length > 0)
                {
                    meshName += "_" + files.Length;
                }
                string path         = savePath + "/" + meshName + ".asset";
                string relativepath = "Assets" + path.Substring(Application.dataPath.Length);
                if (copy)
                {
                    Mesh assetMesh = MeshUtility.Copy(filter.sharedMesh);
                    AssetDatabase.CreateAsset(assetMesh, relativepath);
                }
                else
                {
                    AssetDatabase.CreateAsset(filter.sharedMesh, relativepath);
                }
            }

            if (permanent && !copy)
            {
                SplineComputer meshGenComputer = gen.computer;
                if (permanent)
                {
                    meshGenComputer.Unsubscribe(gen);
                    Object.DestroyImmediate(gen);
                }
                if (removeComputer)
                {
                    if (meshGenComputer.GetComponents <Component>().Length == 2)
                    {
                        Object.DestroyImmediate(meshGenComputer.gameObject);
                    }
                    else
                    {
                        Object.DestroyImmediate(meshGenComputer);
                    }
                }
            }
        }
Esempio n. 14
0
        void OnGUI()
        {
            string bakeText = "Bake";

            EditorGUILayout.BeginHorizontal();
            GUIContent saveMeshText = new GUIContent("Save as OBJ [?]", "Saves the mesh as an OBJ file which can then be used in other scenes and prefabs. OBJ files do not support vertex colors and secondary UV sets.");

            saveMesh = EditorGUILayout.Toggle(saveMeshText, saveMesh);
            if (saveMesh)
            {
                copy = EditorGUILayout.Toggle("Save as copy", copy);
                if (copy)
                {
                    bakeText = "Save Copy";
                }
            }
            EditorGUILayout.EndHorizontal();
            bool hold = false;

            if (saveMesh)
            {
                EditorGUILayout.LabelField("Save Path: " + savePath);
                if (GUILayout.Button("Browse Path"))
                {
                    string meshName = "mesh";
                    if (filter != null)
                    {
                        meshName = filter.sharedMesh.name;
                    }
                    savePath = EditorUtility.SaveFilePanel("Save " + meshName + ".obj", Application.dataPath, meshName + ".obj", "obj");
                }
            }

            EditorGUILayout.Space();
            bool isCopy = saveMesh && copy;

            if (!isCopy)
            {
                isStatic = EditorGUILayout.Toggle("Make Static", isStatic);
            }
            if (!saveMesh)
            {
                lightmapUV = EditorGUILayout.Toggle("Generate Lightmap UVs", lightmapUV);
            }
            SplineUser[] users = meshGen.GetComponents <SplineUser>();
            if (users.Length == 1 && !isCopy)
            {
                removeComputer = EditorGUILayout.Toggle("Remove SplineComputer", removeComputer);
            }
            if (!isCopy)
            {
                permanent = EditorGUILayout.Toggle("Permanent", permanent);
            }
            bool _removeComputer = removeComputer;

            if (users.Length != 1)
            {
                _removeComputer = false;
            }
            if (_removeComputer && meshGen.computer.subscriberCount > 1 && !isCopy)
            {
                EditorGUILayout.HelpBox("WARNING: Removing the SplineComputer from this object may cause other SplineUsers to malfunction!", MessageType.Warning);
            }

            if (saveMesh)
            {
                if (savePath == "")
                {
                    hold = true;
                }
                else if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                {
                    hold = true;
                }
            }
            if (hold)
            {
                GUI.color = new Color(1f, 1f, 1f, 0.5f);
            }
            if (GUILayout.Button(bakeText))
            {
                Undo.RecordObject(meshGen.gameObject, "Bake mesh");
                if (hold)
                {
                    return;
                }
                if (!isCopy)
                {
                    meshGen.Bake(isStatic, lightmapUV);
                }
                else if (lightmapUV)
                {
                    Unwrapping.GenerateSecondaryUVSet(filter.sharedMesh);
                }
                MeshRenderer renderer = meshGen.GetComponent <MeshRenderer>();
                if (saveMesh)
                {
                    string relativepath = "Assets" + savePath.Substring(Application.dataPath.Length);
                    string objString    = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials);
                    File.WriteAllText(savePath, objString);
                    AssetDatabase.ImportAsset(relativepath, ImportAssetOptions.ForceSynchronousImport);
#if UNITY_5_0
                    if (!isCopy)
                    {
                        filter.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(relativepath, typeof(Mesh));
                    }
#else
                    if (!isCopy)
                    {
                        filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativepath);
                    }
#endif
                }
                if (removeComputer && !isCopy)
                {
                    DestroyImmediate(meshGen.computer);
                }
                if (permanent && !isCopy)
                {
                    DestroyImmediate(meshGen);
                }
                Close();
            }
            string add = "";
            if (removeComputer)
            {
                add += "It will also remove the SplineComputer component from the object.";
            }
            EditorGUILayout.HelpBox("This operation will remove the mesh generator component and will make the mesh uneditable." + add, MessageType.Info);
        }