/// <summary> /// Copy the terrain's details and use it with the custom Foliage system. /// </summary> /// <param name="terrain"></param> public void InsertFoliageFromTerrain(Terrain terrain) { UNStandaloneUtility.AddPrototypesIfDontExist(terrain.terrainData.detailPrototypes); List <int[, ]> detailData = UNStandaloneUtility.GetTerrainDetails(terrain.terrainData); int terrainDetailWidth = terrain.terrainData.detailWidth; int terrainDetailHeight = terrain.terrainData.detailHeight; var detailPrototypes = terrain.terrainData.detailPrototypes; var uNaturePrototypes = FoliageDB.sortedPrototypes; float posX; float posZ; int chunkIndex; FoliageCore_Chunk chunk; FoliageManagerInstance managerInstance; int prototype; float interpolatedX; float interpolatedZ; float worldPosX; float worldPosZ; FoliageGrassMap grassMap; for (int x = 0; x < terrainDetailWidth; x++) { for (int z = 0; z < terrainDetailHeight; z++) { worldPosX = ((x * terrain.terrainData.size.x) / terrainDetailWidth) + terrain.transform.position.x; worldPosZ = ((z * terrain.terrainData.size.z) / terrainDetailHeight) + terrain.transform.position.z; posX = worldPosX - transform.position.x; posZ = worldPosZ - transform.position.z; chunkIndex = GetChunkID(posX, posZ); if (CheckChunkInBounds(chunkIndex)) // If isn't out of bounds { chunk = sector.foliageChunks[chunkIndex]; managerInstance = chunk.GetOrCreateFoliageManagerInstance(); interpolatedX = managerInstance.TransformCord(worldPosX, managerInstance.transform.position.x); interpolatedZ = managerInstance.TransformCord(worldPosZ, managerInstance.transform.position.z); for (int prototypeIndex = 0; prototypeIndex < detailData.Count; prototypeIndex++) { prototype = UNStandaloneUtility.TryGetPrototypeIndex(detailPrototypes[prototypeIndex]); grassMap = managerInstance.grassMaps[uNaturePrototypes[prototype]]; grassMap.mapPixels[(int)interpolatedX + (int)interpolatedZ * grassMap.mapWidth].b = (byte)detailData[prototypeIndex][z, x]; // reset details on the terrain. detailData[prototypeIndex][z, x] = 0; grassMap.SetPixels32Delayed(); grassMap.dirty = true; } } } } for (int i = 0; i < detailData.Count; i++) { terrain.terrainData.SetDetailLayer(0, 0, i, detailData[i]); } SaveDelayedMaps(); //apply delayed maps. FoliageGrassMap.SaveAllMaps(); CallInstancesChunksUpdate(); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif }
void DrawPaintWindow() { GUILayout.BeginVertical("Box", GUILayout.Width(450)); GUILayout.BeginHorizontal(); GUILayout.Label("Paint Tools:", EditorStyles.boldLabel); GUILayout.FlexibleSpace(); if (GUILayout.Button("Reload Brushes")) { FoliageDB.instance.brushes = null; } GUILayout.EndHorizontal(); GUILayout.Space(5); GUILayout.BeginHorizontal(); GUILayout.BeginVertical("Box"); string[] paintMethodsNames = Enum.GetNames(typeof(CurrentPaintMethod)); CurrentPaintMethod currentType; for (int i = 0; i < paintMethodsNames.Length; i++) { currentType = (Enum.GetValues(typeof(CurrentPaintMethod)) as CurrentPaintMethod[])[i]; if (UNEditorUtility.DrawHighlitableButton(UNStandaloneUtility.GetUIIcon(paintMethodsNames[i]), paintMethod == currentType, GUILayout.Width(40), GUILayout.Height(40))) { paintMethod = currentType; // select the paint method. } } GUILayout.EndVertical(); GUILayout.Space(3); GUILayout.BeginVertical("Box", GUILayout.Height(100)); GUILayout.Label("Paint Settings:", EditorStyles.boldLabel); GUILayout.Space(5); switch (paintMethod) { case CurrentPaintMethod.Normal_Paint: DrawNormalBrush(); break; case CurrentPaintMethod.Spline_Paint: DrawSplineBrush(); break; } GUILayout.Space(5); paintBrushSize = EditorGUILayout.IntSlider(new GUIContent("Paint Brush Size:", "The percentage from the brush that will be drawn"), paintBrushSize, 1, 30); paintDensity = (byte)EditorGUILayout.IntSlider(new GUIContent("Paint Brush Density:", "The percentage from the brush that will be drawn"), paintDensity, 0, 15); instaRemove = EditorGUILayout.Toggle(new GUIContent("Instant Remove On Shift:", "Instantely remove the grass when shift is pressed (instead of needing to put density to 0"), instaRemove); GUILayout.EndVertical(); GUILayout.EndHorizontal(); if (FoliageGrassMap.globalDirty) { GUILayout.Space(10); EditorGUILayout.HelpBox("Foliage hasn't been saved after painting on terrain. \nPlease save the data by clicking the save button.", MessageType.Warning); if (GUILayout.Button("Save Grass Maps")) { FoliageGrassMap.SaveAllMaps(); EditorUtility.SetDirty(FoliageCore_MainManager.instance); if (!Application.isPlaying) { #if UNITY_5_3_OR_NEWER UnityEditor.SceneManagement.EditorSceneManager.SaveScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene()); #else EditorApplication.SaveScene(); #endif } AssetDatabase.SaveAssets(); } } if (FoliageWorldMap.globalDirty) { GUILayout.Space(10); EditorGUILayout.HelpBox("World information isn't been saved. \nPlease save the data by clicking the save button or changes wont be applied.", MessageType.Warning); if (GUILayout.Button("Save World Map")) { FoliageWorldMap.SaveAllMaps(); EditorUtility.SetDirty(FoliageCore_MainManager.instance); if (!Application.isPlaying) { #if UNITY_5_3_OR_NEWER UnityEditor.SceneManagement.EditorSceneManager.SaveScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene()); #else EditorApplication.SaveScene(); #endif } AssetDatabase.SaveAssets(); } } GUILayout.EndVertical(); }