Example #1
0
        void SaveSettings()
        {
            string path;

            if (AssetDatabase.Contains(m_Settings))
            {
                bool restoreInspectorSelection = Selection.activeObject == m_Settings;
                path = AssetDatabase.GetAssetPath(m_Settings);
                var newSettings = Instantiate(m_Settings);
                AssetDatabase.DeleteAsset(path);
                newSettings.hideFlags = HideFlags.None;
                AssetDatabase.CreateAsset(newSettings, path);
                m_Settings = newSettings;
                if (restoreInspectorSelection)
                {
                    Selection.activeObject = m_Settings;
                }
                return;
            }

            path = EditorUtility.SaveFilePanelInProject("Save the SDF Baker Settings as", "SDF Settings", "asset", "");
            if (path != "")
            {
                AssetDatabase.DeleteAsset(path);
                m_Settings.hideFlags = HideFlags.None;
                AssetDatabase.CreateAsset(m_Settings, path);
            }
        }
Example #2
0
        void CreateNewSession()
        {
            if (AssetDatabase.Contains(m_Settings))
            {
                m_Settings      = CreateInstance <SdfBakerSettings>();
                m_Settings.name = "None";
            }
            else
            {
                m_Settings.ResetToDefault();
            }
            m_BakedSDF = null;
            if (m_MeshPreview != null)
            {
                m_MeshPreview.Dispose();
                m_MeshPreview = null;
            }

            if (m_TexturePreview != null)
            {
                m_TexturePreview.OnDisable();
                m_TexturePreview = null;
            }

            m_RefreshMeshPreview = false;
            if (m_Baker != null)
            {
                m_Baker.Dispose();
                m_Baker = null;
            }
        }
Example #3
0
 private void OnEnable()
 {
     titleContent = Contents.title;
     minSize      = new Vector2(300.0f, 400.0f);
     if (m_Settings == null)
     {
         m_Settings      = CreateInstance <SdfBakerSettings>();
         m_Settings.name = "None";
     }
     if (m_TexturePreview == null)
     {
         m_TexturePreview = CreateInstance <Texture3DPreview>();
     }
     m_TexturePreview.OnEnable();
     if (m_BakedSDF != null)
     {
         m_TexturePreview.Texture = m_BakedSDF;
     }
     if (m_MeshPreview == null)
     {
         m_MeshPreview = new SdfBakerPreview(mesh);                        // Not sure if necessary
     }
     Undo.undoRedoPerformed += OnUndoRedoPerformed;
     PrefabUtility.prefabInstanceUpdated += OnPrefabInstanceUpdated;
     Selection.selectionChanged          += OnSelectionChanged;
 }
Example #4
0
 void OnSelectionChanged()
 {
     if (Selection.activeObject is SdfBakerSettings)
     {
         SdfBakerSettings settings = Selection.activeObject as SdfBakerSettings;
         LoadSettings(settings);
         Repaint();
     }
 }
Example #5
0
        internal static bool OpenBakeTool(int instanceID, int line)
        {
            SdfBakerSettings settings = EditorUtility.InstanceIDToObject(instanceID) as SdfBakerSettings;

            if (settings != null)
            {
                SDFBakeTool window = EditorWindow.GetWindow <SDFBakeTool>();
                window.Show();
                window.LoadSettings(settings);
                return(true);
            }
            return(false);
        }
Example #6
0
 internal void LoadSettings(SdfBakerSettings newSettings)
 {
     m_Settings      = newSettings;
     m_ActualBoxSize = SnapBoxToVoxels();
     if (m_TexturePreview != null)
     {
         m_TexturePreview.OnDisable();
     }
     m_TexturePreview = CreateInstance <Texture3DPreview>();
     if (m_MeshPreview != null)
     {
         m_MeshPreview.Dispose();
     }
     m_MeshPreview = new SdfBakerPreview(selectedMesh);
     m_BakedSDF    = null;
 }
Example #7
0
        protected void OnGUI()
        {
            if (m_Settings == null)
            {
                m_Settings      = CreateInstance <SdfBakerSettings>();
                m_Settings.name = "None";
            }
            bool needsUpdate = false;

            Undo.RecordObject(this, "Settings Asset change");
            Undo.RecordObject(m_Settings, "SDF Baker Parameter change");
            GUILayout.BeginHorizontal();


            EditorGUI.BeginChangeCheck();
            var newSettings = (SdfBakerSettings)EditorGUILayout.ObjectField(Contents.settingsAsset, m_Settings, typeof(SdfBakerSettings), true, GUILayout.MinWidth(20),
                                                                            GUILayout.MaxWidth(400), GUILayout.ExpandWidth(true));

            if (EditorGUI.EndChangeCheck())
            {
                if (newSettings != null)
                {
                    LoadSettings(newSettings);
                }
                else
                {
                    CreateNewSession();
                }
            }
            GUILayout.Space(5);


            GUI.enabled = EditorUtility.IsDirty(m_Settings);
            if (GUILayout.Button(Contents.saveSettings, GUILayout.MinWidth(20), GUILayout.ExpandWidth(true)))
            {
                SaveSettings();
            }
            GUI.enabled = true;
            DrawContextIcon();
            GUILayout.EndHorizontal();
            GUILayout.Space(5);

            EditorGUI.BeginChangeCheck();
            maxResolution = Mathf.Clamp(EditorGUILayout.IntField(Contents.maxResolution, maxResolution), 8, 1024);
            bool maxResChanged = EditorGUI.EndChangeCheck();

            needsUpdate |= maxResChanged;
            if (maxResolution > 255)
            {
                EditorGUILayout.HelpBox(
                    "Higher resolutions are more expensive to calculate and can make Unity unstable.",
                    MessageType.Warning);
            }

            EditorGUI.BeginChangeCheck();
            var prevWideMode = EditorGUIUtility.wideMode;

            EditorGUIUtility.wideMode = true;
            boxCenter    = EditorGUILayout.Vector3Field("Box Center", boxCenter);
            needsUpdate |= EditorGUI.EndChangeCheck();

            EditorGUI.BeginChangeCheck();
            boxSizeReference = EditorGUILayout.Vector3Field(Contents.boxSizeReference, boxSizeReference);
            bool boxSizeChanged = EditorGUI.EndChangeCheck();

            needsUpdate |= boxSizeChanged;

            if (boxSizeChanged || maxResChanged)
            {
                m_ActualBoxSize = SnapBoxToVoxels();
            }

            using (new EditorGUI.DisabledScope(true))
            {
                SerializedObject selfSO = new SerializedObject(this);
                EditorGUILayout.PropertyField(selfSO.FindProperty("m_ActualBoxSize"), Contents.actualBoxSize);
            }


            if (boxSizeReference.x * boxSizeReference.y * boxSizeReference.z <= Single.Epsilon)
            {
                EditorGUILayout.HelpBox("The volume of your bounding box is zero.", MessageType.Warning);
            }

            int estimatedGridSize = EstimateGridSize();

            if (estimatedGridSize > 1 << 21 || selectedMesh == null)
            {
                GUI.enabled  = false;
                m_LiveUpdate = false;
            }

            EditorGUI.BeginChangeCheck();
            m_LiveUpdate = EditorGUILayout.Toggle(Contents.liveUpdate, m_LiveUpdate);
            needsUpdate |= (EditorGUI.EndChangeCheck() & m_LiveUpdate);

            GUI.enabled = true;
            if (m_LiveUpdate)
            {
                EditorGUILayout.HelpBox(
                    "Baking the mesh in real-time might cause slowdowns or instabilities when the resolution and/or the sign passes count is high ",
                    MessageType.Warning);
            }

            bool fitPaddingChanged = false;

            if (m_ShowAdvanced)
            {
                m_FoldOutParameters = EditorGUILayout.BeginFoldoutHeaderGroup(m_FoldOutParameters, Contents.bakingParameters);
                EditorGUI.BeginChangeCheck();
                if (m_FoldOutParameters)
                {
                    EditorGUI.indentLevel++;
                    signPassesCount = Mathf.Clamp(EditorGUILayout.IntField(Contents.signPass, signPassesCount), 0,
                                                  20);
                    inOutThreshold = EditorGUILayout.Slider(Contents.inOutParam, inOutThreshold, 0.0f, 1.0f);
                    surfaceOffset  = EditorGUILayout.Slider(Contents.sdfOffset, surfaceOffset, -0.5f, 0.5f);
                    EditorGUI.indentLevel--;
                }
                EditorGUILayout.EndFoldoutHeaderGroup();
                needsUpdate |= EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                m_Settings.m_FitPaddingVoxel = EditorGUILayout.Vector3IntField(Contents.fitPadding, m_Settings.m_FitPaddingVoxel);
                fitPaddingChanged            = EditorGUI.EndChangeCheck();
            }
            EditorGUIUtility.wideMode = prevWideMode;

            if (selectedMesh != null)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUI.BeginChangeCheck();
                if (GUILayout.Button(Contents.fitBoxToMesh))
                {
                    FitBoxToMesh();
                }
                needsUpdate |= EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                if (GUILayout.Button(Contents.fitCubeToMesh))
                {
                    FitCubeToMesh();
                }
                needsUpdate |= EditorGUI.EndChangeCheck();


                EditorGUILayout.EndHorizontal();
            }

            EditorGUI.BeginChangeCheck();
            modelSource = (ModelSource)EditorGUILayout.EnumPopup(Contents.bakeSource, modelSource);
            bool changedSource = EditorGUI.EndChangeCheck();

            needsUpdate |= changedSource;

            switch (modelSource)
            {
            case ModelSource.Mesh:
                EditorGUI.BeginChangeCheck();
                selectedMesh =
                    (Mesh)EditorGUILayout.ObjectField(Contents.mesh, selectedMesh, typeof(Mesh), false);
                bool meshFieldHasChanged = EditorGUI.EndChangeCheck();
                needsUpdate |= meshFieldHasChanged;

                m_RefreshMeshPreview |= meshFieldHasChanged | changedSource;
                if (m_RefreshMeshPreview)
                {
                    m_Settings.ApplySelectedMesh();
                }

                break;

            case ModelSource.MeshPrefab:
                EditorGUI.BeginChangeCheck();
                meshPrefab =
                    (GameObject)EditorGUILayout.ObjectField(Contents.meshPrefab, meshPrefab, typeof(GameObject),
                                                            false);

                meshFieldHasChanged = EditorGUI.EndChangeCheck() || m_PrefabChanged;
                m_PrefabChanged     = false;
                needsUpdate        |= meshFieldHasChanged;

                m_RefreshMeshPreview |= meshFieldHasChanged | changedSource || (mesh == null);
                bool rebuildMesh = m_RefreshMeshPreview;
                if (rebuildMesh)
                {
                    m_Settings.BuildMeshFromPrefab();
                    rebuildMesh = false;
                }

                break;
            }

            if (mesh == null)
            {
                m_LiveUpdate = false;
            }

            if (m_RefreshMeshPreview && mesh != null)
            {
                FitBoxToMesh();
                m_MeshPreview?.Dispose();
                m_MeshPreview        = new SdfBakerPreview(mesh);
                m_RefreshMeshPreview = false;
            }

            if (mesh == null || (estimatedGridSize > UnityEngine.VFX.SDF.MeshToSDFBaker.kMaxGridSize) || InternalMeshUtil.GetPrimitiveCount(mesh) == 0)
            {
                GUI.enabled = false;
            }

            if (GUILayout.Button("Bake mesh") || m_LiveUpdate && needsUpdate)
            {
                if (m_Baker == null)
                {
                    m_Baker = new MeshToSDFBaker(boxSizeReference, boxCenter, maxResolution, mesh, signPassesCount,
                                                 inOutThreshold, surfaceOffset);
                }
                else
                {
                    m_Baker.Reinit(boxSizeReference, boxCenter, maxResolution, mesh, signPassesCount,
                                   inOutThreshold, surfaceOffset);
                }

                m_Baker.BakeSDF();
                m_BakedSDF = m_Baker.SdfTexture;
            }

            GUI.enabled = true;


            bool canSave = true;

            if ((m_BakedSDF == null) || (m_Baker == null))
            {
                canSave     = false;
                GUI.enabled = false;
            }

            if (GUILayout.Button(canSave ? Contents.saveSDF : Contents.saveSDFBlocked))
            {
                m_Baker.SaveToAsset();
            }

            GUI.enabled = true;

            previewObject = (PreviewChoice)EditorGUILayout.EnumPopup(Contents.previewChoice, previewObject);
            if ((previewObject & PreviewChoice.Mesh) != 0)
            {
                UpdateMeshPreview();
            }

            if ((previewObject & PreviewChoice.Texture) != 0)
            {
                UpdateTexture3dPreview();
            }

            if (needsUpdate)
            {
                EditorUtility.SetDirty(m_Settings);
            }
        }