Exemplo n.º 1
0
        /// <summary>
        /// Create a terrain tile based on these settings
        /// </summary>
        /// <param name="tx">X location</param>
        /// <param name="tz">Z location</param>
        /// <param name="world">The array managing it</param>
        private void CreateTile(int tx, int tz, ref Terrain[,] world, GaiaResource resources)
        {
            if (tx < 0 || tx >= m_tilesX)
            {
                Debug.LogError("X value out of bounds");
                return;
            }

            if (tz < 0 || tz >= m_tilesZ)
            {
                Debug.LogError("Z value out of bounds");
                return;
            }

            //Look for issues in the terrain settings and fix them
            GetAndFixDefaults();

            //this will center terrain at origin
            Vector2 m_offset = new Vector2(-m_terrainSize * m_tilesX * 0.5f, -m_terrainSize * m_tilesZ * 0.5f);

            //create the terrains if they dont already exist
            if (world.Length < m_tilesX)
            {
                world = new Terrain[m_tilesX, m_tilesZ];
            }

            //Create the terrain
            Terrain     terrain;
            TerrainData terrainData = new TerrainData();

            terrainData.name = string.Format("Terrain_{0}_{1}-{2:yyyyMMdd-HHmmss}", tx, tz, DateTime.Now);
            terrainData.alphamapResolution = m_controlTextureResolution;
            terrainData.baseMapResolution  = m_baseMapSize;
            terrainData.SetDetailResolution(m_detailResolution, m_detailResolutionPerPatch);
            terrainData.heightmapResolution = m_heightmapResolution;
            //terrainData.physicsMaterial = m_physicsMaterial;
            terrainData.wavingGrassAmount   = m_bending;
            terrainData.wavingGrassSpeed    = m_size;
            terrainData.wavingGrassStrength = m_speed;
            terrainData.wavingGrassTint     = m_grassTint;
            terrainData.size = new Vector3(m_terrainSize, m_terrainHeight, m_terrainSize);

            #if UNITY_EDITOR
            AssetDatabase.CreateAsset(terrainData, string.Format("Assets/{0}.asset", terrainData.name));
            #endif

            terrain      = Terrain.CreateTerrainGameObject(terrainData).GetComponent <Terrain>();
            terrain.name = terrainData.name;
            terrain.transform.position      = new Vector3(m_terrainSize * tx + m_offset.x, 0, m_terrainSize * tz + m_offset.y);
            terrain.basemapDistance         = m_baseMapDist;
            terrain.castShadows             = m_castShadows;
            terrain.detailObjectDensity     = m_detailDensity;
            terrain.detailObjectDistance    = m_detailDistance;
            terrain.heightmapPixelError     = m_pixelError;
            terrain.treeBillboardDistance   = m_billboardStart;
            terrain.treeCrossFadeLength     = m_fadeLength;
            terrain.treeDistance            = m_treeDistance;
            terrain.treeMaximumFullLODCount = m_maxMeshTrees;
            if (m_material != null)
            {
                terrain.materialType     = Terrain.MaterialType.Custom;
                terrain.materialTemplate = m_material;
            }
            if (m_physicsMaterial != null)
            {
                TerrainCollider collider = terrain.GetComponent <TerrainCollider>();
                if (collider != null)
                {
                    collider.material = m_physicsMaterial;
                }
                else
                {
                    Debug.LogWarning("Unable to assign physics material to terrain!");
                }
            }

            //Assign prototypes
            if (resources != null)
            {
                resources.ApplyPrototypesToTerrain(terrain);
            }
            else
            {
                terrain.Flush();
            }

            //Save the new tile
            world[tx, tz] = terrain;

            //Parent it to the environment
            GameObject gaiaObj = GameObject.Find("Gaia Environment");
            if (gaiaObj == null)
            {
                gaiaObj = new GameObject("Gaia Environment");
            }
            terrain.transform.parent = gaiaObj.transform;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a terrain tile based on these settings
        /// </summary>
        /// <param name="tx">X location</param>
        /// <param name="tz">Z location</param>
        /// <param name="world">The array managing it</param>
        private void CreateTile(int tx, int tz, ref Terrain[,] world, GaiaResource resources)
        {
            if (tx < 0 || tx >= m_tilesX)
            {
                Debug.LogError("X value out of bounds");
                return;
            }

            if (tz < 0 || tz >= m_tilesZ)
            {
                Debug.LogError("Z value out of bounds");
                return;
            }

            //Look for issues in the terrain settings and fix them
            GetAndFixDefaults();

            //this will center terrain at origin
            Vector2 m_offset = new Vector2(-m_terrainSize * m_tilesX * 0.5f, -m_terrainSize * m_tilesZ * 0.5f);

            //create the terrains if they dont already exist
            if (world.Length < m_tilesX)
            {
                world = new Terrain[m_tilesX, m_tilesZ];
            }

            //Create the terrain
            Terrain     terrain;
            TerrainData terrainData = new TerrainData();

            terrainData.name = string.Format("Terrain_{0}_{1}-{2:yyyyMMdd-HHmmss}", tx, tz, DateTime.Now);
            terrainData.alphamapResolution = m_controlTextureResolution;
            terrainData.baseMapResolution  = m_baseMapSize;
            terrainData.SetDetailResolution(m_detailResolution, m_detailResolutionPerPatch);
            terrainData.heightmapResolution = m_heightmapResolution;
            //terrainData.physicsMaterial = m_physicsMaterial;
            terrainData.wavingGrassAmount   = m_bending;
            terrainData.wavingGrassSpeed    = m_size;
            terrainData.wavingGrassStrength = m_speed;
            terrainData.wavingGrassTint     = m_grassTint;
            terrainData.size = new Vector3(m_terrainSize, m_terrainHeight, m_terrainSize);

#if UNITY_EDITOR
            AssetDatabase.CreateAsset(terrainData, string.Format("Assets/{0}.asset", terrainData.name));
#endif

            terrain      = Terrain.CreateTerrainGameObject(terrainData).GetComponent <Terrain>();
            terrain.name = terrainData.name;
            terrain.transform.position =
                new Vector3(m_terrainSize * tx + m_offset.x, 0, m_terrainSize * tz + m_offset.y);
            terrain.basemapDistance = m_baseMapDist;
#if UNITY_2019_1_OR_NEWER
            terrain.shadowCastingMode = m_shaodwCastingMode;
#else
            terrain.castShadows = m_castShadows;
#endif
            terrain.detailObjectDensity     = m_detailDensity;
            terrain.detailObjectDistance    = m_detailDistance;
            terrain.heightmapPixelError     = m_pixelError;
            terrain.treeBillboardDistance   = m_billboardStart;
            terrain.treeCrossFadeLength     = m_fadeLength;
            terrain.treeDistance            = m_treeDistance;
            terrain.treeMaximumFullLODCount = m_maxMeshTrees;
#if UNITY_EDITOR
            GameObjectUtility.SetStaticEditorFlags(terrain.gameObject,
                                                   StaticEditorFlags.BatchingStatic | StaticEditorFlags.NavigationStatic |
                                                   StaticEditorFlags.OccludeeStatic | StaticEditorFlags.OccluderStatic |
                                                   StaticEditorFlags.OffMeshLinkGeneration | StaticEditorFlags.ReflectionProbeStatic | StaticEditorFlags.LightmapStatic
                                                   );
            terrain.bakeLightProbesForTrees = false;
#if UNITY_2018_3_OR_NEWER
            terrain.drawInstanced = true;
#endif
#endif

            GaiaConstants.EnvironmentRenderer rendererType = GaiaConstants.EnvironmentRenderer.BuiltIn;
            GaiaSettings gaiaSettings = GaiaUtils.GetGaiaSettings();
            if (gaiaSettings != null)
            {
                rendererType = gaiaSettings.m_currentRenderer;
#if !UNITY_2018_1_OR_NEWER
                rendererType = GaiaConstants.EnvironmentRenderer.BuiltIn;
#endif
            }

            if (rendererType == GaiaConstants.EnvironmentRenderer.BuiltIn)
            {
                if (m_material != null)
                {
#if UNITY_EDITOR
                    GaiaPipelineUtils.SetupPipeline(rendererType, null, null, null, "Procedural Worlds/Simple Water", false);
#endif
                }
            }
            else
            {
                terrain.materialType = Terrain.MaterialType.Custom;
                if (rendererType == GaiaConstants.EnvironmentRenderer.LightWeight2018x)
                {
#if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
                    GaiaPipelineUtils.SetupPipeline(rendererType, "Procedural Worlds Lightweight Pipeline Profile", "Pipeline Terrain Material", "Lightweight Render Pipeline/Terrain/Lit", "Procedural Worlds/Simple Water LW", false);
#else
                    Debug.LogWarning("Lightweight Pipeline is only supposted in 2018.3 or newer");
#endif
                }
                else
                {
#if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
                    GaiaPipelineUtils.SetupPipeline(rendererType, "Procedural Worlds HDRenderPipelineAsset", "Pipeline Terrain Material", "HDRP/TerrainLit", "Procedural Worlds/Simple Water HD", false);
#else
                    Debug.LogWarning("Lightweight Pipeline is only supposted in 2018.3 or newer");
#endif
                }
            }

            if (m_physicsMaterial != null)
            {
                TerrainCollider collider = terrain.GetComponent <TerrainCollider>();
                if (collider != null)
                {
                    collider.material = m_physicsMaterial;
                }
                else
                {
                    Debug.LogWarning("Unable to assign physics material to terrain!");
                }
            }

            //Assign prototypes
            if (resources != null)
            {
                resources.ApplyPrototypesToTerrain(terrain);
            }
            else
            {
                terrain.Flush();
            }

            //Save the new tile
            world[tx, tz] = terrain;

            //Parent it to the environment
            GameObject gaiaObj = GameObject.Find("Gaia Environment");
            if (gaiaObj == null)
            {
                gaiaObj = new GameObject("Gaia Environment");
            }
            terrain.transform.parent = gaiaObj.transform;
        }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            //Get our resource
            m_resource = (GaiaResource)target;

            //Set up the box style
            if (m_boxStyle == null)
            {
                m_boxStyle = new GUIStyle(GUI.skin.box);
                m_boxStyle.normal.textColor = GUI.skin.label.normal.textColor;
                m_boxStyle.fontStyle        = FontStyle.Bold;
                m_boxStyle.alignment        = TextAnchor.UpperLeft;
            }

            //Setup the wrap style
            if (m_wrapStyle == null)
            {
                m_wrapStyle          = new GUIStyle(GUI.skin.label);
                m_wrapStyle.wordWrap = true;
            }

            //Create a nice text intro
            GUILayout.BeginVertical("Gaia Resource", m_boxStyle);
            GUILayout.Space(20);
            //EditorGUILayout.LabelField("The resource manager allows you to manage the resources used by your terrain and spawner. To see what every setting does you can hover over it.\n\nGet From Terrain - Pick up resources and settings from the current terrain.\n\nUpdate DNA - Updates DNA for all resources and automatically calculate sizes.\n\nApply To Terrain - Apply terrain specific settings such as texture, detail and tree prototypes back into the terrain. Prefab this settings to save time creating your next terrain.", m_wrapStyle);
            EditorGUILayout.LabelField("These are the resources used by the Spawning & Stamping system. Create a terrain, add textures, details and trees, then press Get Resources From Terrain to load. To see how the settings influence the system you can hover over them.", m_wrapStyle);
            GUILayout.EndVertical();

            float oldSeaLevel = m_resource.m_seaLevel;
            float oldHeight   = m_resource.m_terrainHeight;

            EditorGUI.BeginChangeCheck();

            DrawDefaultInspector();

            DropAreaGUI();

            GUILayout.BeginVertical("Resource Controller", m_boxStyle);
            GUILayout.Space(20);

            if (GUILayout.Button(GetLabel("Set Asset Associations")))
            {
                if (EditorUtility.DisplayDialog("Set Asset Associations", "This will update your asset associations and can not be undone ! Here temporarily until hidden.", "Yes", "No"))
                {
                    if (m_resource.SetAssetAssociations())
                    {
                        EditorUtility.SetDirty(m_resource);
                    }
                }
            }


            if (GUILayout.Button(GetLabel("Associate Assets")))
            {
                if (EditorUtility.DisplayDialog("Associate Assets", "This will locate and associate the first resource found that matches your asset and can not be undone !", "Yes", "No"))
                {
                    if (m_resource.AssociateAssets())
                    {
                        EditorUtility.SetDirty(m_resource);
                    }
                }
            }


            if (GUILayout.Button(GetLabel("Get Resources From Terrain")))
            {
                if (EditorUtility.DisplayDialog("Get Resources from Terrain ?", "Are you sure you want to get / update your resource prototypes from the terrain ? This will update your settings and can not be undone !", "Yes", "No"))
                {
                    m_resource.UpdatePrototypesFromTerrain();
                    EditorUtility.SetDirty(m_resource);
                }
            }

            if (GUILayout.Button(GetLabel("Replace Resources In Terrains")))
            {
                if (EditorUtility.DisplayDialog("Replace Resources in ALL Terrains ?", "Are you sure you want to replace the resources in ALL terrains with these? This can not be undone !", "Yes", "No"))
                {
                    m_resource.ApplyPrototypesToTerrain();
                }
            }

            if (GUILayout.Button(GetLabel("Add Missing Resources To Terrains")))
            {
                if (EditorUtility.DisplayDialog("Add Missing Resources to ALL Terrains ?", "Are you sure you want to add your missing resource prototypes to ALL terrains ? This can not be undone !", "Yes", "No"))
                {
                    m_resource.AddMissingPrototypesToTerrain();
                }
            }


            if (m_resource.m_texturePrototypes.GetLength(0) == 0)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button(GetLabel("Create Coverage Texture Spawner")))
            {
                m_resource.CreateCoverageTextureSpawner(GetRangeFromTerrain(), GetTextureIncrementFromTerrain());
            }
            GUI.enabled = true;


            if (m_resource.m_detailPrototypes.GetLength(0) == 0)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button(GetLabel("Create Clustered Grass Spawner")))
            {
                m_resource.CreateClusteredDetailSpawner(GetRangeFromTerrain(), GetDetailIncrementFromTerrain());
            }
            if (GUILayout.Button(GetLabel("Create Coverage Grass Spawner")))
            {
                m_resource.CreateCoverageDetailSpawner(GetRangeFromTerrain(), GetDetailIncrementFromTerrain());
            }
            GUI.enabled = true;


            if (m_resource.m_treePrototypes.GetLength(0) == 0)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button(GetLabel("Create Clustered Terrain Tree Spawner")))
            {
                m_resource.CreateClusteredTreeSpawner(GetRangeFromTerrain());
            }
            if (GUILayout.Button(GetLabel("Create Coverage Terrain Tree Spawner")))
            {
                m_resource.CreateCoverageTreeSpawner(GetRangeFromTerrain());
            }
            GUI.enabled = true;

            if (m_resource.m_gameObjectPrototypes.GetLength(0) == 0)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button(GetLabel("Create Clustered Prefab Spawner")))
            {
                m_resource.CreateClusteredGameObjectSpawner(GetRangeFromTerrain());
            }
            if (GUILayout.Button(GetLabel("Create Coverage Prefab Spawner")))
            {
                m_resource.CreateCoverageGameObjectSpawner(GetRangeFromTerrain());
            }
            GUI.enabled = true;

            if (GUILayout.Button(GetLabel("Visualise")))
            {
                GameObject gaiaObj = GameObject.Find("Gaia");
                if (gaiaObj == null)
                {
                    gaiaObj = new GameObject("Gaia");
                }
                GameObject visualiserObj = GameObject.Find("Visualiser");
                if (visualiserObj == null)
                {
                    visualiserObj = new GameObject("Visualiser");
                    visualiserObj.AddComponent <ResourceVisualiser>();
                    visualiserObj.transform.parent = gaiaObj.transform;
                }
                ResourceVisualiser visualiser = visualiserObj.GetComponent <ResourceVisualiser>();
                visualiser.m_resources     = m_resource;
                Selection.activeGameObject = visualiserObj;
            }

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

            //Check for changes, make undo record, make changes and let editor know we are dirty
            if (EditorGUI.EndChangeCheck())
            {
                if (oldHeight != m_resource.m_terrainHeight)
                {
                    m_resource.ChangeHeight(oldHeight, m_resource.m_terrainHeight);
                }

                if (oldSeaLevel != m_resource.m_seaLevel)
                {
                    m_resource.ChangeSeaLevel(oldSeaLevel, m_resource.m_seaLevel);
                }
                Undo.RecordObject(m_resource, "Made resource changes");
                EditorUtility.SetDirty(m_resource);

                //Stop the save from going nuts
                if ((DateTime.Now - m_lastSaveDT).Seconds > 5)
                {
                    m_lastSaveDT = DateTime.Now;
                    AssetDatabase.SaveAssets();
                }
            }
        }