Ejemplo n.º 1
0
        public void LoadStorageData()
        {
#if UNITY_EDITOR
            //Try to get the terrain scene storage file from the last used GUID first
            if (!String.IsNullOrEmpty(m_lastUsedGUID))
            {
                m_terrainSceneStorage = (TerrainSceneStorage)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(m_lastUsedGUID), typeof(TerrainSceneStorage));
            }

            //No guid / storage object? Then we need to create one in the current session directory
            if (m_terrainSceneStorage == null)
            {
                GaiaSessionManager gsm = GaiaSessionManager.GetSessionManager();
                if (gsm != null && gsm.m_session != null)
                {
                    string path = GaiaDirectories.GetScenePath(gsm.m_session) + "/TerrainScenes.asset";
                    if (File.Exists(path))
                    {
                        m_terrainSceneStorage = (TerrainSceneStorage)AssetDatabase.LoadAssetAtPath(path, typeof(TerrainSceneStorage));
                    }
                    else
                    {
                        m_terrainSceneStorage = ScriptableObject.CreateInstance <TerrainSceneStorage>();
                        if (TerrainHelper.GetWorldMapTerrain() != null)
                        {
                            m_terrainSceneStorage.m_hasWorldMap = true;
                        }
                        AssetDatabase.CreateAsset(m_terrainSceneStorage, path);
                        AssetDatabase.ImportAsset(path);
                    }
                }
                else
                {
                    m_terrainSceneStorage = ScriptableObject.CreateInstance <TerrainSceneStorage>();
                }
            }

            //Check if there are scene files existing already and if they are in the storage data - if not, we should pick them up accordingly
            string directory = GaiaDirectories.GetTerrainScenePathForStorageFile(m_terrainSceneStorage);
            var    dirInfo   = new DirectoryInfo(directory);

            bool madeChanges = false;

            if (dirInfo != null)
            {
                FileInfo[] allFiles = dirInfo.GetFiles();
                foreach (FileInfo fileInfo in allFiles)
                {
                    if (fileInfo.Extension == ".unity")
                    {
                        string path = GaiaDirectories.GetPathStartingAtAssetsFolder(fileInfo.FullName);

                        if (!m_terrainSceneStorage.m_terrainScenes.Exists(x => x.GetTerrainName() == x.GetTerrainName(path)))
                        {
                            string firstSegment = fileInfo.Name.Split('-')[0];
                            int    xCoord = -99;
                            int    zCoord = -99;
                            bool   successX, successZ;
                            try
                            {
                                successX = Int32.TryParse(firstSegment.Substring(firstSegment.IndexOf('_') + 1, firstSegment.LastIndexOf('_') - (firstSegment.IndexOf('_') + 1)), out xCoord);
                                successZ = Int32.TryParse(firstSegment.Substring(firstSegment.LastIndexOf('_') + 1, firstSegment.Length - 1 - firstSegment.LastIndexOf('_')), out zCoord);
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message == "123")
                                {
                                }
                                successX = false;
                                successZ = false;
                            }


                            if (successX && successZ)
                            {
                                //double centerX = (xCoord - (m_terrainSceneStorage.m_terrainTilesX / 2f)) * m_terrainSceneStorage.m_terrainTilesSize + (m_terrainSceneStorage.m_terrainTilesSize /2f);
                                //double centerZ = (zCoord - (m_terrainSceneStorage.m_terrainTilesZ / 2f)) * m_terrainSceneStorage.m_terrainTilesSize + (m_terrainSceneStorage.m_terrainTilesSize / 2f);
                                Vector2       offset   = new Vector2(-m_terrainSceneStorage.m_terrainTilesSize * m_terrainSceneStorage.m_terrainTilesX * 0.5f, -m_terrainSceneStorage.m_terrainTilesSize * m_terrainSceneStorage.m_terrainTilesZ * 0.5f);
                                Vector3Double position = new Vector3(m_terrainSceneStorage.m_terrainTilesSize * xCoord + offset.x, 0, m_terrainSceneStorage.m_terrainTilesSize * zCoord + offset.y);
                                Vector3Double center   = new Vector3Double(position + new Vector3Double(m_terrainSceneStorage.m_terrainTilesSize / 2f, 0f, m_terrainSceneStorage.m_terrainTilesSize / 2f));
                                BoundsDouble  bounds   = new BoundsDouble(center, new Vector3Double(m_terrainSceneStorage.m_terrainTilesSize, m_terrainSceneStorage.m_terrainTilesSize * 4, m_terrainSceneStorage.m_terrainTilesSize));
                                //Use forward slashes in the path - The Unity scene management classes expect it that way
                                path = path.Replace("\\", "/");
                                TerrainScene terrainScene = new TerrainScene()
                                {
                                    m_scenePath           = path,
                                    m_pos                 = position,
                                    m_bounds              = bounds,
                                    m_useFloatingPointFix = m_terrainSceneStorage.m_useFloatingPointFix
                                };

                                if (File.Exists(path.Replace("Terrain", GaiaConstants.ImpostorTerrainName)))
                                {
                                    terrainScene.m_impostorScenePath = path.Replace("Terrain", GaiaConstants.ImpostorTerrainName);
                                }

                                if (File.Exists(path.Replace("Terrain", "Collider")))
                                {
                                    terrainScene.m_colliderScenePath = path.Replace("Terrain", "Collider");
                                }

                                if (File.Exists(path.Replace("Terrain", "Backup")))
                                {
                                    terrainScene.m_backupScenePath = path.Replace("Terrain", "Backup");
                                }

                                m_terrainSceneStorage.m_terrainScenes.Add(terrainScene);
                                madeChanges = true;
                            }
                        }
                    }
                }
                if (madeChanges)
                {
                    EditorUtility.SetDirty(m_terrainSceneStorage);
                    AssetDatabase.SaveAssets();
                }
            }

            m_lastUsedGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_terrainSceneStorage));


            RefreshTerrainsWithCurrentData();

            RefreshSceneViewLoadingRange();

            ////Go over the currently open scene and close the ones that do not seem to have a reference on them
            //for (int i = EditorSceneManager.loadedSceneCount-1; i >= 0; i--)
            //{
            //    Scene scene = EditorSceneManager.GetSceneAt(i);
            //    if (EditorSceneManager.GetActiveScene().Equals(scene))
            //    {
            //        continue;
            //    }
            //      TerrainScene terrainScene = m_terrainSceneStorage.m_terrainScenes.Find(x => x.m_scenePath == scene.path || x.m_impostorScenePath == scene.path || x.m_colliderScenePath == scene.path);
            //      if (terrainScene != null)
            //      {
            //            terrainScene.UpdateWithCurrentData();
            //      }
            //      else
            //      {
            //            EditorSceneManager.UnloadSceneAsync(scene);
            //      }
            //}
#endif
        }
Ejemplo n.º 2
0
        private void GlobalPanel(bool helpEnabled)
        {
            //Text intro
            GUILayout.BeginVertical("Gaia Scanner", m_boxStyle);
            GUILayout.Space(20);
            EditorGUILayout.LabelField("The Gaia Scanner allows you to create new stamps from Windows R16, Windows 16 bit RAW, Mac 16 bit RAW, Terrains, Textures or Meshes. Just drag and drop the file or object onto the area below to scan it.", m_wrapStyle);
            GUILayout.EndVertical();

            DropAreaGUI();

            bool originalGUIState = GUI.enabled;

            if (!m_scanner.m_objectScanned)
            {
                GUI.enabled = false;
            }

            //DrawDefaultInspector();
            GUILayout.BeginVertical("Setup", m_boxStyle);
            GUILayout.Space(20);
            m_editorUtils.Heading("GlobalSettings");
            m_scanner.m_previewMaterial = (Material)m_editorUtils.ObjectField("PreviewMaterial", m_scanner.m_previewMaterial, typeof(Material), false, helpEnabled);
            EditorGUILayout.BeginHorizontal();
            m_scanner.m_exportFolder = m_editorUtils.TextField("ExportPath", m_scanner.m_exportFolder);

            if (m_editorUtils.Button("ExportDirectoryOpen", GUILayout.Width(80)))
            {
                string path = EditorUtility.SaveFolderPanel(m_editorUtils.GetTextValue("ExportDirectoryWindowTitle"), GaiaDirectories.GetUserStampDirectory(), GaiaDirectories.SCANNER_EXPORT_DIRECTORY.Replace("/", ""));
                if (path.Contains(Application.dataPath))
                {
                    m_scanner.m_exportFolder = GaiaDirectories.GetPathStartingAtAssetsFolder(path);
                }
                else
                {
                    EditorUtility.DisplayDialog("Path outside the Assets folder", "The selected path needs to be inside the Assets folder of the project. Please select an appropiate path.", "OK");
                    m_scanner.m_exportFolder = GaiaDirectories.GetScannerExportDirectory();
                }
            }
            EditorGUILayout.EndHorizontal();
            m_editorUtils.InlineHelp("ExportPath", helpEnabled);
            m_scanner.m_exportFileName = m_editorUtils.TextField("ExportFilename", m_scanner.m_exportFileName, helpEnabled);

            float oldBaseLevel = m_scanner.m_baseLevel;

            m_scanner.m_baseLevel = m_editorUtils.Slider("BaseLevel", m_scanner.m_baseLevel, 0f, 1f, helpEnabled);
            if (oldBaseLevel != m_scanner.m_baseLevel)
            {
                SceneView.lastActiveSceneView.Repaint();
            }
            EditorGUILayout.Space(10f);
            m_editorUtils.Heading("ObjectTypeSettings");
            if (m_scanner.m_scannerObjectType == ScannerObjectType.Mesh)
            {
                m_scanner.m_scanResolution = m_editorUtils.Slider("ScanResolution", m_scanner.m_scanResolution, 0.0001f, 1f, helpEnabled);
                if (m_scanner.m_lastScanResolution != m_scanner.m_scanResolution)
                {
                    if (m_editorUtils.Button("RefreshScan"))
                    {
                        m_scanner.LoadGameObject(m_scanner.m_lastScannedMesh);
                    }
                }
            }
            if (m_scanner.m_scannerObjectType == ScannerObjectType.Raw)
            {
                //Drop Options section
                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.PrefixLabel(BYTE_ORDER_LABEL);
                    EditorGUI.BeginChangeCheck();
                    {
                        m_rawByteOrder = (GaiaConstants.RawByteOrder)GUILayout.Toolbar((int)m_rawByteOrder, new string[] { "IBM PC", "Macintosh" });
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        ReloadRawFile();
                    }
                }
                GUILayout.EndHorizontal();
                m_editorUtils.InlineHelp("RawFileByteOrder", helpEnabled);

                EditorGUI.BeginChangeCheck();
                {
                    m_rawBitDepth = (GaiaConstants.RawBitDepth)EditorGUILayout.Popup(BIT_DEPTH_LABEL, (int)m_rawBitDepth, BIT_DEPTHS_LABELS);
                    m_editorUtils.InlineHelp("RawFileBitDepth", helpEnabled);
                }
                if (EditorGUI.EndChangeCheck())
                {
                    ReloadRawFile();
                }
                GUILayout.BeginVertical();
                if (m_showRawInfo)
                {
                    EditorGUILayout.HelpBox("Assumed " + (m_rawBitDepth == GaiaConstants.RawBitDepth.Sixteen ? "16-bit" : "8-bit") + " RAW " + m_assumedRawRes + " x " + m_assumedRawRes, MessageType.Info);
                }
                if (m_showBitDepthWarning)
                {
                    EditorGUILayout.HelpBox(m_editorUtils.GetTextValue("8BitWarning"), MessageType.Warning);
                }
                GUILayout.EndVertical();
            }
            EditorGUILayout.Space(10f);



            m_editorUtils.Heading("ExportSettings");
            //m_scanner.m_textureExportResolution = (GaiaConstants.GaiaProWaterReflectionsQuality) EditorGUILayout.EnumPopup(new GUIContent("Export Resolution", "Sets the export resolution of the texture generated"), m_scanner.m_textureExportResolution);
            m_scanner.m_normalize         = m_editorUtils.Toggle("Normalize", m_scanner.m_normalize, helpEnabled);
            m_scanner.m_exportTextureAlso = m_editorUtils.Toggle("ExportPNG", m_scanner.m_exportTextureAlso, helpEnabled);
            //m_scanner.m_exportBytesData = m_editorUtils.Toggle("ExportBytesData", m_scanner.m_exportBytesData, helpEnabled);

            GUILayout.EndVertical();

            //Terraform section
            GUILayout.BeginVertical("Scanner Controller", m_boxStyle);
            GUILayout.Space(20);
            if (!string.IsNullOrEmpty(m_infoMessage))
            {
                EditorGUILayout.HelpBox(m_infoMessage, MessageType.Info);
            }
            if (m_scanner.m_scanMap == null)
            {
                EditorGUILayout.HelpBox(m_editorUtils.GetTextValue("NoScanDataInfo"), MessageType.Info);
                GUI.enabled = false;
            }
            GUILayout.BeginHorizontal();
            Color normalBGColor = GUI.backgroundColor;

            if (m_settings == null)
            {
                m_settings = GaiaUtils.GetGaiaSettings();
            }

            GUI.backgroundColor = m_settings.GetActionButtonColor();
            if (m_editorUtils.Button("SaveScan"))
            {
                string path = m_scanner.SaveScan();
                AssetDatabase.Refresh();
                path += ".exr";
                Object exportedTexture = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
                if (exportedTexture != null)
                {
                    m_infoMessage = "Scan exported to: " + path;
                    Debug.Log(m_infoMessage);
                    EditorGUIUtility.PingObject(exportedTexture);
                }
            }
            GUI.backgroundColor = normalBGColor;
            GUI.enabled         = true;
            if (m_editorUtils.Button("Clear"))
            {
                m_scanner.Clear();
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(5);
            GUILayout.EndVertical();
            GUILayout.Space(5f);

            m_showRawInfo         = m_assumedRawRes > 0;
            m_showBitDepthWarning = m_rawBitDepth == GaiaConstants.RawBitDepth.Eight;

            GUI.enabled = originalGUIState;
        }