Search Unity

Extending the Terrain Detail Paint Editor?

Discussion in 'Editor & General Support' started by SomeGuy22, Mar 21, 2020.

  1. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    The Problem

    I've come across an issue when painting terrains after I upgraded to Unity 2019.3.4. It appears that this version of Unity (and all future versions as far as I'm aware) have adjusted the proportional "strength" of detail painting for terrains and also locked down the target strength setting to set intervals in the inspector. I have documented my findings in this post. This has posed a problem for me in regards to painting grass, as I can no longer paint small amounts of details at the same granularity that I used to.

    Here is a comparison to demonstrate what I'm dealing with:

    Unity 2019.2:


    Unity 2019.3:


    Essentially, the problem boils down to the fact that .0625 is now the lowest value allowed for target strength, and the "final" painted strength is much larger than .0625 used to be. In order to paint like I used to, the final paint strength must either be reverted back to how it was before, or the target strength value must become "unlocked" so that I can adjust the strength below .0625 to account for this extra weight.

    Potential Solution

    I have already submitted a bug report earlier this week (Case 1227935) but have not heard back from the testing team. Of course I don't blame them considering the virus situation.

    So instead of waiting on a Unity update to make an adjustment, my idea is to simply extend the paint tool to use a custom "target strength" value, which will be fully unlocked instead of set into intervals like the value is now. Either that or some sort of multiplier which adjusts the final target strength that is used for the paint operation.

    The problem is, I haven't done much Editor extension, and it appears that there is no way to access this specific piece of Terrain Inspector data. There is a SetDetailLayer() method for TerrainData. But in order to use it effectively I would need to reference the detail prototypes and do a lot of work matching them and adjusting the entire splatmap used by each.

    I found this experimental Terrain Paint Tool API, which seems to be way to add new terrain painting tools. But looking through it, it seems like it relies on TerrainPaintUtility which does not include any PaintContext helper method for editing Terrain details, only heightmap, holes, and textures. I'm not sure if it could be used for that purpose, and even if it could I would have to re-implement everything already offered by the detail paint tab--referencing prototypes, opacity painting, etc.

    Regardless, both of those options appear to necessitate rebuilding the detail paint tool. It would be much easier if I could just hook into the existing paint tool, but so far I've found nothing that could allow that. The Terrain editor UI only appears when the inspector is not in Debug mode, so there's no way to manually adjust the target strength like that either. I'm open to any suggestions or ideas on how I could go about unlocking this target strength, or adding an additional value to modify the detail paint tool instead of completely rebuilding it. Thanks!
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    It's seeming like SetDetailLayer is the only way to interact with the terrain's detail splatmap. Is this really true? There is no way to duplicate or modify the Terrain's special editor tools? I don't think I'll be able to recreate the entire detail paint tool using the experimental API so this is looking pretty hopeless...
     
  3. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Thanks, I didn't realize there was a "PaintTreesDetailsContext" which looks like the class I need to add a detail tool. However, as expected it's inaccessible due to protection level. I'm not sure if it's possible to recreate something similar using TerrainPaintUtility, but you're on to something with re-implementation. However I can't seem to find the PaintTreesDetailsContext class in the github you linked, and when I peek the definition from VS I get this:



    So the full implementation isn't exposed, which means I can't reference it to make it again for this new tool. I was able to get a duplicate of the tool to appear in the paint menu though, so that's a start. The downside is that it has no UI and no way to select the painted detail. Unless it pulls it from the other menu, I'm not really sure. I wasn't able to really test it since I had to comment out the context class and everything that used it.

    Is there some way I could read the PaintTreesDetailsContext class contents so that I can remake it? Otherwise, I'd have to really dig into how it works and see what I can do replicate the effects using what I have, which already sounds like maybe a waste of a ton of time for this kind of issue... this is really making me want to avoid using terrains ever again. I really wish there was some way to just unlock that target strength value without going through all these hoops.
     
  5. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    SomeGuy22 likes this.
  6. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Huh... you're right. I should've ran the advanced search in github instead of just searching the file names, no clue why they named it TerrainPaintToolUtility.cs instead of the actual class that's in it.

    Regardless, you are a lifesaver! Thanks to your direction and reference files I was able to painstakingly re-implement the entire Paint Details tool and (almost) everything regarding the Details Inspector UI, which is necessary to select the painted detail and set the target strength. This was all done through a deep dive into the github files and adding each missing method one-by-one as soon as the compiler errors came through. The reason for all the extra additions is because of access level and hidden functions used by UnityEngine internally, so it was necessary to follow down the chain of missing items and re-add them all into this one file.

    As a result there's a lot of horrible organization, tons of broken commented stuff, and some missing features from the real tool. But it works for painting on a selected detail with any chosen target strength. Here's the code for anyone interested (warning, it's pretty long and bloated):

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.Experimental.TerrainAPI;
    7.  
    8. namespace UnityEditor.Experimental.TerrainAPI
    9. {
    10.     internal class PaintTreesDetailsContext
    11.     {
    12.         private static Terrain[] s_Nbrs = new Terrain[8];
    13.         private static Vector2[] s_Uvs = new Vector2[8];
    14.  
    15.         public Terrain[] terrains = new Terrain[4];
    16.         public Vector2[] uvs = new Vector2[4];
    17.  
    18.         private PaintTreesDetailsContext() { }
    19.  
    20.         public static PaintTreesDetailsContext Create(Terrain terrain, Vector2 uv)
    21.         {
    22.             s_Nbrs[0] = terrain.leftNeighbor;
    23.             s_Nbrs[1] = terrain.leftNeighbor ? terrain.leftNeighbor.topNeighbor : (terrain.topNeighbor ? terrain.topNeighbor.leftNeighbor : null);
    24.             s_Nbrs[2] = terrain.topNeighbor;
    25.             s_Nbrs[3] = terrain.rightNeighbor ? terrain.rightNeighbor.topNeighbor : (terrain.topNeighbor ? terrain.topNeighbor.rightNeighbor : null);
    26.             s_Nbrs[4] = terrain.rightNeighbor;
    27.             s_Nbrs[5] = terrain.rightNeighbor ? terrain.rightNeighbor.bottomNeighbor : (terrain.bottomNeighbor ? terrain.bottomNeighbor.rightNeighbor : null);
    28.             s_Nbrs[6] = terrain.bottomNeighbor;
    29.             s_Nbrs[7] = terrain.leftNeighbor ? terrain.leftNeighbor.bottomNeighbor : (terrain.bottomNeighbor ? terrain.bottomNeighbor.leftNeighbor : null);
    30.  
    31.             s_Uvs[0] = new Vector2(uv.x + 1.0f, uv.y);
    32.             s_Uvs[1] = new Vector2(uv.x + 1.0f, uv.y - 1.0f);
    33.             s_Uvs[2] = new Vector2(uv.x, uv.y - 1.0f);
    34.             s_Uvs[3] = new Vector2(uv.x - 1.0f, uv.y - 1.0f);
    35.             s_Uvs[4] = new Vector2(uv.x - 1.0f, uv.y);
    36.             s_Uvs[5] = new Vector2(uv.x - 1.0f, uv.y + 1.0f);
    37.             s_Uvs[6] = new Vector2(uv.x, uv.y + 1.0f);
    38.             s_Uvs[7] = new Vector2(uv.x + 1.0f, uv.y + 1.0f);
    39.  
    40.             PaintTreesDetailsContext ctx = new PaintTreesDetailsContext();
    41.             ctx.terrains[0] = terrain;
    42.             ctx.uvs[0] = uv;
    43.  
    44.             bool left = uv.x < 0.5f;
    45.             bool right = !left;
    46.             bool bottom = uv.y < 0.5f;
    47.             bool top = !bottom;
    48.  
    49.             int t = 0;
    50.             if (right && top)
    51.                 t = 2;
    52.             else if (right && bottom)
    53.                 t = 4;
    54.             else if (left && bottom)
    55.                 t = 6;
    56.  
    57.             for (int i = 1; i < 4; ++i, t = (t + 1) % 8)
    58.             {
    59.                 ctx.terrains[i] = s_Nbrs[t];
    60.                 ctx.uvs[i] = s_Uvs[t];
    61.             }
    62.  
    63.             return ctx;
    64.         }
    65.     }
    66.  
    67.     internal class BrushRep
    68.     {
    69.         private const int kMinBrushSize = 3;
    70.  
    71.         private int m_Size;
    72.         private float[] m_Strength;
    73.         private Texture2D m_OldBrushTex;
    74.  
    75.         public float GetStrengthInt(int ix, int iy)
    76.         {
    77.             ix = Mathf.Clamp(ix, 0, m_Size - 1);
    78.             iy = Mathf.Clamp(iy, 0, m_Size - 1);
    79.  
    80.             float s = m_Strength[iy * m_Size + ix];
    81.  
    82.             return s;
    83.         }
    84.  
    85.         public void CreateFromBrush(Texture2D brushTex, int size)
    86.         {
    87.             if (size == m_Size && m_OldBrushTex == brushTex && m_Strength != null)
    88.                 return;
    89.  
    90.             Texture2D mask = brushTex;
    91.             if (mask != null)
    92.             {
    93.                 Texture2D readableTexture = null;
    94.                 if (!mask.isReadable)
    95.                 {
    96.                     readableTexture = new Texture2D(mask.width, mask.height, mask.format, mask.mipmapCount > 1);
    97.                     Graphics.CopyTexture(mask, readableTexture);
    98.                     readableTexture.Apply();
    99.                 }
    100.                 else
    101.                 {
    102.                     readableTexture = mask;
    103.                 }
    104.  
    105.                 float fSize = size;
    106.                 m_Size = size;
    107.                 m_Strength = new float[m_Size * m_Size];
    108.                 if (m_Size > kMinBrushSize)
    109.                 {
    110.                     for (int y = 0; y < m_Size; y++)
    111.                     {
    112.                         float v = y / fSize;
    113.                         for (int x = 0; x < m_Size; x++)
    114.                         {
    115.                             float u = x / fSize;
    116.                             m_Strength[y * m_Size + x] = readableTexture.GetPixelBilinear(u, v).r;
    117.                         }
    118.                     }
    119.                 }
    120.                 else
    121.                 {
    122.                     for (int i = 0; i < m_Strength.Length; i++)
    123.                         m_Strength[i] = 1.0F;
    124.                 }
    125.  
    126.                 if (readableTexture != mask)
    127.                     Object.DestroyImmediate(readableTexture);
    128.             }
    129.             else
    130.             {
    131.                 m_Strength = new float[1];
    132.                 m_Strength[0] = 1.0F;
    133.                 m_Size = 1;
    134.             }
    135.  
    136.             m_OldBrushTex = brushTex;
    137.         }
    138.     }
    139.  
    140.     internal class PaintDetailsUtils
    141.     {
    142.         public static int FindDetailPrototype(Terrain terrain, Terrain sourceTerrain, int sourceDetail)
    143.         {
    144.             if (sourceDetail == PaintDetailsTool.kInvalidDetail ||
    145.                 sourceDetail >= sourceTerrain.terrainData.detailPrototypes.Length)
    146.             {
    147.                 return PaintDetailsTool.kInvalidDetail;
    148.             }
    149.  
    150.             if (terrain == sourceTerrain)
    151.             {
    152.                 return sourceDetail;
    153.             }
    154.  
    155.             DetailPrototype sourceDetailPrototype = sourceTerrain.terrainData.detailPrototypes[sourceDetail];
    156.             for (int i = 0; i < terrain.terrainData.detailPrototypes.Length; ++i)
    157.             {
    158.                 if (sourceDetailPrototype.Equals(terrain.terrainData.detailPrototypes[i]))
    159.                     return i;
    160.             }
    161.  
    162.             return PaintDetailsTool.kInvalidDetail;
    163.         }
    164.  
    165.         public static int CopyDetailPrototype(Terrain terrain, Terrain sourceTerrain, int sourceDetail)
    166.         {
    167.             DetailPrototype sourceDetailPrototype = sourceTerrain.terrainData.detailPrototypes[sourceDetail];
    168.             DetailPrototype[] newDetailPrototypesArray = new DetailPrototype[terrain.terrainData.detailPrototypes.Length + 1];
    169.             System.Array.Copy(terrain.terrainData.detailPrototypes, newDetailPrototypesArray, terrain.terrainData.detailPrototypes.Length);
    170.             newDetailPrototypesArray[newDetailPrototypesArray.Length - 1] = new DetailPrototype(sourceDetailPrototype);
    171.             terrain.terrainData.detailPrototypes = newDetailPrototypesArray;
    172.             terrain.terrainData.RefreshPrototypes();
    173.             return newDetailPrototypesArray.Length - 1;
    174.         }
    175.     }
    176.  
    177.     internal class PaintDetailsTool : TerrainPaintTool<PaintDetailsTool>
    178.     {
    179.         public const int kInvalidDetail = -1;
    180.  
    181.         private DetailPrototype m_LastSelectedDetailPrototype;
    182.         private Terrain m_TargetTerrain;
    183.         private BrushRep m_BrushRep;
    184.  
    185.         public float detailOpacity { get; set; }
    186.         public float detailStrength { get; set; }
    187.         public int selectedDetail { get; set; }
    188.  
    189.         public override bool OnPaint(Terrain terrain, IOnPaint editContext)
    190.         {
    191.             if (m_TargetTerrain == null ||
    192.                 selectedDetail == kInvalidDetail ||
    193.                 selectedDetail >= m_TargetTerrain.terrainData.detailPrototypes.Length)
    194.             {
    195.                 return false;
    196.             }
    197.  
    198.             Texture2D brush = editContext.brushTexture as Texture2D;
    199.             if (brush == null)
    200.             {
    201.                 Debug.LogError("Brush texture is not a Texture2D.");
    202.                 return false;
    203.             }
    204.  
    205.             if (m_BrushRep == null)
    206.             {
    207.                 m_BrushRep = new BrushRep();
    208.             }
    209.  
    210.             PaintTreesDetailsContext ctx = PaintTreesDetailsContext.Create(terrain, editContext.uv);
    211.  
    212.             //PaintContext ctx = PaintContext.CreateFromBounds(terrain, editContext.uv);
    213.          
    214.             for (int t = 0; t < ctx.terrains.Length; ++t)
    215.             {
    216.                 Terrain ctxTerrain = ctx.terrains[t];
    217.                 if (ctxTerrain != null)
    218.                 {
    219.                     int detailPrototype = PaintDetailsUtils.FindDetailPrototype(ctxTerrain, m_TargetTerrain, selectedDetail);
    220.                     if (detailPrototype == kInvalidDetail)
    221.                     {
    222.                         detailPrototype = PaintDetailsUtils.CopyDetailPrototype(ctxTerrain, m_TargetTerrain, selectedDetail);
    223.                     }
    224.  
    225.                     TerrainData terrainData = ctxTerrain.terrainData;
    226.  
    227.                     //TerrainPaintUtilityEditor.UpdateTerrainDataUndo(terrainData, "Terrain - Detail Edit");
    228.                     UpdateTerrainDataUndo(terrainData, "Terrain - Detail Edit");
    229.  
    230.                     int size = (int)Mathf.Max(1.0f, editContext.brushSize * ((float)terrainData.detailResolution / terrainData.size.x));
    231.  
    232.                     m_BrushRep.CreateFromBrush(brush, size);
    233.  
    234.                     Vector2 ctxUV = ctx.uvs[t];
    235.  
    236.                     int xCenter = Mathf.FloorToInt(ctxUV.x * terrainData.detailWidth);
    237.                     int yCenter = Mathf.FloorToInt(ctxUV.y * terrainData.detailHeight);
    238.  
    239.                     int intRadius = Mathf.RoundToInt(size) / 2;
    240.                     int intFraction = Mathf.RoundToInt(size) % 2;
    241.  
    242.                     int xmin = xCenter - intRadius;
    243.                     int ymin = yCenter - intRadius;
    244.  
    245.                     int xmax = xCenter + intRadius + intFraction;
    246.                     int ymax = yCenter + intRadius + intFraction;
    247.  
    248.                     if (xmin >= terrainData.detailWidth || ymin >= terrainData.detailHeight || xmax <= 0 || ymax <= 0)
    249.                     {
    250.                         continue;
    251.                     }
    252.  
    253.                     xmin = Mathf.Clamp(xmin, 0, terrainData.detailWidth - 1);
    254.                     ymin = Mathf.Clamp(ymin, 0, terrainData.detailHeight - 1);
    255.  
    256.                     xmax = Mathf.Clamp(xmax, 0, terrainData.detailWidth);
    257.                     ymax = Mathf.Clamp(ymax, 0, terrainData.detailHeight);
    258.  
    259.                     int width = xmax - xmin;
    260.                     int height = ymax - ymin;
    261.  
    262.                     float targetStrength = detailStrength;
    263.                     if (Event.current.shift || Event.current.control)
    264.                         targetStrength = -targetStrength;
    265.  
    266.                     int[] layers = { detailPrototype };
    267.                     if (targetStrength < 0.0F && !Event.current.control)
    268.                         layers = terrainData.GetSupportedLayers(xmin, ymin, width, height);
    269.  
    270.                     for (int i = 0; i < layers.Length; i++)
    271.                     {
    272.                         int[,] alphamap = terrainData.GetDetailLayer(xmin, ymin, width, height, layers[i]);
    273.  
    274.                         for (int y = 0; y < height; y++)
    275.                         {
    276.                             for (int x = 0; x < width; x++)
    277.                             {
    278.                                 int xBrushOffset = (xmin + x) - (xCenter - intRadius + intFraction);
    279.                                 int yBrushOffset = (ymin + y) - (yCenter - intRadius + intFraction);
    280.                                 float opa = detailOpacity * m_BrushRep.GetStrengthInt(xBrushOffset, yBrushOffset);
    281.  
    282.                                 float targetValue = Mathf.Lerp(alphamap[y, x], targetStrength, opa);
    283.                                 alphamap[y, x] = Mathf.RoundToInt(targetValue - .5f + Random.value);
    284.                             }
    285.                         }
    286.  
    287.                         terrainData.SetDetailLayer(xmin, ymin, layers[i], alphamap);
    288.                     }
    289.                 }
    290.             }
    291.          
    292.  
    293.             return false;
    294.         }
    295.  
    296.         public override void OnEnterToolMode()
    297.         {
    298.             Terrain terrain = null;
    299.             if (Selection.activeGameObject != null)
    300.             {
    301.                 terrain = Selection.activeGameObject.GetComponent<Terrain>();
    302.             }
    303.  
    304.             if (terrain != null &&
    305.                 terrain.terrainData != null &&
    306.                 m_LastSelectedDetailPrototype != null)
    307.             {
    308.                 for (int i = 0; i < terrain.terrainData.detailPrototypes.Length; ++i)
    309.                 {
    310.                     if (m_LastSelectedDetailPrototype.Equals(terrain.terrainData.detailPrototypes[i]))
    311.                     {
    312.                         selectedDetail = i;
    313.                         break;
    314.                     }
    315.                 }
    316.             }
    317.  
    318.             m_TargetTerrain = terrain;
    319.  
    320.             m_LastSelectedDetailPrototype = null;
    321.         }
    322.  
    323.         public override void OnExitToolMode()
    324.         {
    325.             if (m_TargetTerrain != null &&
    326.                 m_TargetTerrain.terrainData != null &&
    327.                 selectedDetail != kInvalidDetail &&
    328.                 selectedDetail < m_TargetTerrain.terrainData.detailPrototypes.Length)
    329.             {
    330.                 m_LastSelectedDetailPrototype = new DetailPrototype(m_TargetTerrain.terrainData.detailPrototypes[selectedDetail]);
    331.             }
    332.  
    333.             selectedDetail = kInvalidDetail;
    334.         }
    335.  
    336.         public override string GetName()
    337.         {
    338.             return "Custom Paint Details";
    339.         }
    340.  
    341.         public override string GetDesc()
    342.         {
    343.             return "Paints the selected detail prototype onto the terrain";
    344.         }
    345.  
    346.  
    347.         private static int s_CurrentOperationUndoGroup = -1;
    348.         private static List<UnityEngine.Object> s_CurrentOperationUndoStack = new List<UnityEngine.Object>();
    349.  
    350.         internal static void UpdateTerrainDataUndo(TerrainData terrainData, string undoName)
    351.         {
    352.             // if we are in a new undo group (new operation) then start with an empty list
    353.             if (Undo.GetCurrentGroup() != s_CurrentOperationUndoGroup)
    354.             {
    355.                 s_CurrentOperationUndoGroup = Undo.GetCurrentGroup();
    356.                 s_CurrentOperationUndoStack.Clear();
    357.             }
    358.  
    359.             if (!s_CurrentOperationUndoStack.Contains(terrainData))
    360.             {
    361.                 s_CurrentOperationUndoStack.Add(terrainData);
    362.                 Undo.RegisterCompleteObjectUndo(terrainData, undoName);
    363.             }
    364.         }
    365.      
    366.  
    367.         GUIContent[] m_DetailContents;
    368.         float m_Size;
    369.  
    370.         public override void OnInspectorGUI(Terrain terrain, IOnInspectorGUI editContext)
    371.         {
    372.  
    373.             EditorGUI.BeginChangeCheck();
    374.  
    375.             styles = new Styles();
    376.  
    377.             //EditorGUILayout.BeginVertical();
    378.  
    379.             LoadDetailIcons();
    380.  
    381.             /*
    382.             RenderPipelineAsset renderPipelineAsset = GraphicsSettings.currentRenderPipeline;
    383.             if (renderPipelineAsset != null)
    384.             {
    385.                 if (SupportedRenderingFeatures.active.terrainDetailUnsupported)
    386.                 {
    387.                     EditorGUILayout.HelpBox(styles.detailShadersUnsupported.text, MessageType.Error);
    388.                 }
    389.                 else if (
    390.                     (renderPipelineAsset.terrainDetailLitShader == null) ||
    391.                     (renderPipelineAsset.terrainDetailGrassShader == null) ||
    392.                     (renderPipelineAsset.terrainDetailGrassBillboardShader == null))
    393.                 {
    394.                     EditorGUILayout.HelpBox(styles.detailShadersMissing.text, MessageType.Error);
    395.                 }
    396.             }
    397.             */
    398.  
    399.             //TerrainToolGUIHelper.DrawHeaderFoldoutForBrush();
    400.             //ShowBrushes(0, true, true, false, false, 0);
    401.  
    402.             // Detail picker
    403.             GUI.changed = false;
    404.  
    405.             //GUILayout.Label(styles.details, EditorStyles.boldLabel);
    406.             bool doubleClick;
    407.             //PaintDetailsTool.instance.selectedDetail = AspectSelectionGridImageAndText(PaintDetailsTool.instance.selectedDetail, m_DetailContents, 64, styles.gridListText, "No Detail Objects defined", out doubleClick);
    408.             selectedDetail = AspectSelectionGridImageAndText(selectedDetail, m_DetailContents, 64, styles.gridListText, "No Detail Objects defined", out doubleClick);
    409.             if (doubleClick)
    410.             {
    411.                 //TerrainDetailContextMenus.EditDetail(new MenuCommand(m_TargetTerrain, PaintDetailsTool.instance.selectedDetail));
    412.                 GUIUtility.ExitGUI();
    413.             }
    414.  
    415.             ShowDetailStats();
    416.  
    417.             GUILayout.BeginHorizontal();
    418.             GUILayout.FlexibleSpace();
    419.             //MenuButton(styles.editDetails, "CONTEXT/TerrainEngineDetails", PaintDetailsTool.instance.selectedDetail);
    420.             MenuButton(styles.editDetails, "CONTEXT/TerrainEngineDetails", selectedDetail);
    421.             ShowRefreshPrototypes();
    422.             GUILayout.EndHorizontal();
    423.  
    424.             GUILayout.Label(styles.settings, EditorStyles.boldLabel);
    425.  
    426.             // Brush size
    427.             m_Size = PowerSlider(styles.brushSize, m_Size, 1.0f, 100.0f, 4.0f);
    428.             detailOpacity = EditorGUILayout.Slider(styles.opacity, detailOpacity, 0, 1);
    429.  
    430.             // Strength
    431.             detailStrength = EditorGUILayout.Slider(styles.detailTargetStrength, detailStrength, 0, 1);
    432.  
    433.             //EditorGUILayout.EndVertical();
    434.  
    435.             //commonUI.OnInspectorGUI(terrain, editContext);
    436.  
    437.             //var s_showToolControls = TerrainToolGUIHelper.DrawHeaderFoldoutForBrush(Styles.controlHeader, s_showToolControls, () => { m_TargetHeight = 0; });
    438.  
    439.  
    440.             /*
    441.             if (s_showToolControls)
    442.             {
    443.                 EditorGUILayout.BeginVertical("GroupBox");
    444.                 {
    445. #if UNITY_2019_3_OR_NEWER
    446.                     EditorGUI.BeginChangeCheck();
    447.                     m_HeightSpace = (HeightSpace)EditorGUILayout.EnumPopup(Styles.space, m_HeightSpace);
    448.                     if (EditorGUI.EndChangeCheck())
    449.                     {
    450.                         if (m_HeightSpace == HeightSpace.Local)
    451.                             m_TargetHeight = Mathf.Clamp(m_TargetHeight, terrain.GetPosition().y, terrain.terrainData.size.y + terrain.GetPosition().y);
    452.                     }
    453.  
    454.                     if (m_HeightSpace == HeightSpace.Local)
    455.                     {
    456.                         m_TargetHeight = EditorGUILayout.Slider(Styles.height, m_TargetHeight - terrain.GetPosition().y, 0, terrain.terrainData.size.y) + terrain.GetPosition().y;
    457.                     }
    458.                     else
    459.                     {
    460.                         m_TargetHeight = EditorGUILayout.FloatField(Styles.height, m_TargetHeight);
    461.                     }
    462. #else
    463.                      m_TargetHeight = EditorGUILayout.Slider(Styles.height, m_TargetHeight, 0, terrain.terrainData.size.y);
    464. #endif
    465.                     GUILayout.BeginHorizontal();
    466.                     GUILayout.FlexibleSpace();
    467.                     if (GUILayout.Button(Styles.flatten, GUILayout.ExpandWidth(false)))
    468.                     {
    469.                         Flatten(terrain);
    470.                     }
    471.                     if (GUILayout.Button(Styles.flattenAll, GUILayout.ExpandWidth(false)))
    472.                     {
    473.                         FlattenAll(terrain);
    474.                     }
    475.                     GUILayout.EndHorizontal();
    476.  
    477.                     if (EditorGUI.EndChangeCheck())
    478.                     {
    479.                         Save(true);
    480.                         SaveSetting();
    481.                     }
    482.                 }
    483.                 EditorGUILayout.EndVertical();
    484.  
    485.             }
    486.             */
    487.  
    488.         }
    489.  
    490.         public void ShowDetailStats()
    491.         {
    492.             GUILayout.Space(3);
    493.  
    494.             EditorGUILayout.HelpBox(styles.detailResolutionWarning.text, MessageType.Warning);
    495.  
    496.             int maxMeshes = m_TargetTerrain.terrainData.detailPatchCount * m_TargetTerrain.terrainData.detailPatchCount;
    497.             EditorGUILayout.LabelField("Detail patches currently allocated: " + maxMeshes);
    498.  
    499.             //int maxDetails = maxMeshes * PaintDetailsUtils.GetMaxDetailInstances(m_TargetTerrain.terrainData);
    500.             //EditorGUILayout.LabelField("Detail instance density: " + maxDetails);
    501.             GUILayout.Space(3);
    502.         }
    503.  
    504.         public void MenuButton(GUIContent title, string menuName, int userData)
    505.         {
    506.             GUIContent t = new GUIContent(title.text, styles.settingsIcon, title.tooltip);
    507.             Rect r = GUILayoutUtility.GetRect(t, styles.largeSquare);
    508.             if (GUI.Button(r, t, styles.largeSquare))
    509.             {
    510.                 MenuCommand context = new MenuCommand(m_TargetTerrain, userData);
    511.                 EditorUtility.DisplayPopupMenu(new Rect(r.x, r.y, 0, 0), menuName, context);
    512.             }
    513.         }
    514.  
    515.         public void ShowRefreshPrototypes()
    516.         {
    517.             if (GUILayout.Button(styles.refresh, styles.largeSquare))
    518.             {
    519.                 //TerrainMenus.RefreshPrototypes();
    520.                 m_TargetTerrain.terrainData.RefreshPrototypes();
    521.             }
    522.         }
    523.  
    524.  
    525.         void LoadDetailIcons()
    526.         {
    527.             // Locate the proto types asset preview textures
    528.             DetailPrototype[] prototypes = m_TargetTerrain.terrainData.detailPrototypes;
    529.             m_DetailContents = new GUIContent[prototypes.Length];
    530.             for (int i = 0; i < m_DetailContents.Length; i++)
    531.             {
    532.                 m_DetailContents[i] = new GUIContent();
    533.  
    534.                 if (prototypes[i].usePrototypeMesh)
    535.                 {
    536.                     Texture tex = AssetPreview.GetAssetPreview(prototypes[i].prototype);
    537.                     if (tex != null)
    538.                         m_DetailContents[i].image = tex;
    539.  
    540.                     if (prototypes[i].prototype != null)
    541.                         m_DetailContents[i].text = prototypes[i].prototype.name;
    542.                     else
    543.                         m_DetailContents[i].text = "Missing";
    544.                 }
    545.                 else
    546.                 {
    547.                     Texture tex = prototypes[i].prototypeTexture;
    548.                     if (tex != null)
    549.                         m_DetailContents[i].image = tex;
    550.                     if (tex != null)
    551.                         m_DetailContents[i].text = tex.name;
    552.                     else
    553.                         m_DetailContents[i].text = "Missing";
    554.                 }
    555.             }
    556.         }
    557.  
    558.         static float PowerSlider(GUIContent content, float value, float minVal, float maxVal, float power, GUILayoutOption[] options = null)
    559.         {
    560.             value = Mathf.Clamp(value, minVal, maxVal);
    561.             EditorGUI.BeginChangeCheck();
    562.             //float newValue = EditorGUILayout.PowerSlider(content, value, minVal, maxVal, power, options);
    563.             float newValue = EditorGUILayout.Slider(value, minVal, maxVal, options);
    564.             if (EditorGUI.EndChangeCheck())
    565.             {
    566.                 return newValue;
    567.             }
    568.             return value;
    569.         }
    570.  
    571.         public static int AspectSelectionGridImageAndText(int selected, GUIContent[] textures, int approxSize, GUIStyle style, string emptyString, out bool doubleClick)
    572.         {
    573.             EditorGUILayout.BeginVertical(GUILayout.MinHeight(10));
    574.             int retval = 0;
    575.  
    576.             doubleClick = false;
    577.  
    578.             if (textures.Length != 0)
    579.             {
    580.                 int xCount = 0;
    581.                 Rect rect = GetBrushAspectRect(textures.Length, approxSize, 12, out xCount);
    582.  
    583.                 Event evt = Event.current;
    584.                 if (evt.type == EventType.MouseDown && evt.clickCount == 2 && rect.Contains(evt.mousePosition))
    585.                 {
    586.                     doubleClick = true;
    587.                     evt.Use();
    588.                 }
    589.                 retval = GUI.SelectionGrid(rect, System.Math.Min(selected, textures.Length - 1), textures, xCount, style);
    590.             }
    591.             else
    592.             {
    593.                 GUILayout.Label(emptyString);
    594.             }
    595.  
    596.             GUILayout.EndVertical();
    597.             return retval;
    598.         }
    599.  
    600.         static Rect GetBrushAspectRect(int elementCount, int approxSize, int extraLineHeight, out int xCount)
    601.         {
    602.             xCount = (int)Mathf.Ceil((EditorGUIUtility.currentViewWidth - 20) / approxSize);
    603.             int yCount = elementCount / xCount;
    604.             if (elementCount % xCount != 0)
    605.                 yCount++;
    606.             Rect r1 = GUILayoutUtility.GetAspectRect(xCount / (float)yCount);
    607.             Rect r2 = GUILayoutUtility.GetRect(10, extraLineHeight * yCount);
    608.             r1.height += r2.height;
    609.             return r1;
    610.         }
    611.  
    612.         class Styles
    613.         {
    614.             public GUIStyle gridListText = "GridListText";
    615.             public GUIStyle largeSquare = new GUIStyle("Button")
    616.             {
    617.                 fixedHeight = 22
    618.             };
    619.             public GUIStyle command = "Command";
    620.             public Texture settingsIcon = EditorGUIUtility.IconContent("SettingsIcon").image;
    621.  
    622.             // List of tools supported by the editor
    623.             public readonly GUIContent[] toolIcons =
    624.             {
    625.                 EditorGUIUtility.TrIconContent("TerrainInspector.TerrainToolAdd", "Create Neighbor Terrains"),
    626.                 EditorGUIUtility.TrIconContent("TerrainInspector.TerrainToolSplat", "Paint Terrain"),
    627.                 EditorGUIUtility.TrIconContent("TerrainInspector.TerrainToolTrees", "Paint Trees"),
    628.                 EditorGUIUtility.TrIconContent("TerrainInspector.TerrainToolPlants", "Paint Details"),
    629.                 EditorGUIUtility.TrIconContent("TerrainInspector.TerrainToolSettings", "Terrain Settings")
    630.             };
    631.  
    632.             public readonly GUIContent[] toolNames =
    633.             {
    634.                 EditorGUIUtility.TrTextContent("Create Neighbor Terrains", "Click the edges to create neighbor terrains"),
    635.                 EditorGUIUtility.TrTextContent("Paint Terrain", "Select a tool from the drop-down list"),
    636.                 EditorGUIUtility.TrTextContent("Paint Trees", "Click to paint trees.\n\nHold shift and click to erase trees.\n\nHold Ctrl and click to erase only trees of the selected type."),
    637.                 EditorGUIUtility.TrTextContent("Paint Details", "Click to paint details.\n\nHold shift and click to erase details.\n\nHold Ctrl and click to erase only details of the selected type."),
    638.                 EditorGUIUtility.TrTextContent("Terrain Settings")
    639.             };
    640.  
    641.             public readonly GUIContent brushSize = EditorGUIUtility.TrTextContent("Brush Size", "Size of the brush used to paint.");
    642.             public readonly GUIContent opacity = EditorGUIUtility.TrTextContent("Opacity", "Strength of the applied effect.");
    643.             public readonly GUIContent settings = EditorGUIUtility.TrTextContent("Settings");
    644.             //public readonly GUIContent mismatchedTerrainData = EditorGUIUtility.TextContentWithIcon(
    645.             //    "The TerrainData used by the TerrainCollider component is different from this terrain. Would you like to assign the same TerrainData to the TerrainCollider component?",
    646.             //    "console.warnicon");
    647.  
    648.             public readonly GUIContent assign = EditorGUIUtility.TrTextContent("Assign");
    649.             public readonly GUIContent duplicateTab = EditorGUIUtility.TrTextContent("This inspector tab is not the active Terrain inspector, paint functionality disabled.");
    650.             public readonly GUIContent makeMeActive = EditorGUIUtility.TrTextContent("Activate this inspector");
    651.             public readonly GUIContent gles2NotSupported = EditorGUIUtility.TrTextContentWithIcon("Terrain editting is not supported in GLES2.", MessageType.Info);
    652.  
    653.             // Trees
    654.             public readonly GUIContent trees = EditorGUIUtility.TrTextContent("Trees");
    655.             public readonly GUIContent editTrees = EditorGUIUtility.TrTextContent("Edit Trees...", "Add/remove tree types.");
    656.             public readonly GUIContent treeDensity = EditorGUIUtility.TrTextContent("Tree Density", "How dense trees are you painting");
    657.             public readonly GUIContent treeHeight = EditorGUIUtility.TrTextContent("Tree Height", "Height of the planted trees");
    658.             public readonly GUIContent treeHeightRandomLabel = EditorGUIUtility.TrTextContent("Random?", "Enable random variation in tree height (variation)");
    659.             public readonly GUIContent treeHeightRandomToggle = EditorGUIUtility.TrTextContent("", "Enable random variation in tree height (variation)");
    660.             public readonly GUIContent lockWidth = EditorGUIUtility.TrTextContent("Lock Width to Height", "Let the tree width be the same with height");
    661.             public readonly GUIContent treeWidth = EditorGUIUtility.TrTextContent("Tree Width", "Width of the planted trees");
    662.             public readonly GUIContent treeWidthRandomLabel = EditorGUIUtility.TrTextContent("Random?", "Enable random variation in tree width (variation)");
    663.             public readonly GUIContent treeWidthRandomToggle = EditorGUIUtility.TrTextContent("", "Enable random variation in tree width (variation)");
    664.             public readonly GUIContent treeColorVar = EditorGUIUtility.TrTextContent("Color Variation", "Amount of random shading applied to trees. This only works if the shader supports _TreeInstanceColor (for example, Speedtree shaders do not use this)");
    665.             public readonly GUIContent treeRotation = EditorGUIUtility.TrTextContent("Random Tree Rotation", "Randomize tree rotation. This only works when the tree has an LOD group.");
    666.             public readonly GUIContent treeRotationDisabled = EditorGUIUtility.TrTextContent("The selected tree does not have an LOD group, so it will use the default impostor system and will not support rotation.");
    667.             public readonly GUIContent massPlaceTrees = EditorGUIUtility.TrTextContent("Mass Place Trees", "The Mass Place Trees button is a very useful way to create an overall covering of trees without painting over the whole landscape. Following a mass placement, you can still use painting to add or remove trees to create denser or sparser areas.");
    668.             public readonly GUIContent treeContributeGI = EditorGUIUtility.TrTextContent("Tree Contribute Global Illumination", "The state of the Contribute GI flag for the tree prefab root GameObject. The flag can be changed on the prefab. When disabled, this tree will not be visible to the lightmapper. When enabled, any child GameObjects which also have the static flag enabled, will be present in lightmap calculations. Regardless of the value of the flag, each tree instance receives its own light probe and no lightmap texels.");
    669.  
    670.             // Details
    671.             public readonly GUIContent details = EditorGUIUtility.TrTextContent("Details");
    672.             public readonly GUIContent editDetails = EditorGUIUtility.TrTextContent("Edit Details...", "Add/remove detail meshes");
    673.             public readonly GUIContent detailTargetStrength = EditorGUIUtility.TrTextContent("Target Strength", "Target amount");
    674.  
    675.             // Heightmaps
    676.             public readonly GUIContent textures = EditorGUIUtility.TrTextContent("Texture Resolutions (On Terrain Data)");
    677.             public readonly GUIContent requireResampling = EditorGUIUtility.TrTextContent("Require resampling on change");
    678.             public readonly GUIContent importRaw = EditorGUIUtility.TrTextContent("Import Raw...", "The Import Raw button allows you to set the terrain's heightmap from an image file in the RAW grayscale format. RAW format can be generated by third party terrain editing tools (such as Bryce) and can also be opened, edited and saved by Photoshop. This allows for sophisticated generation and editing of terrains outside Unity.");
    679.             public readonly GUIContent exportRaw = EditorGUIUtility.TrTextContent("Export Raw...", "The Export Raw button allows you to save the terrain's heightmap to an image file in the RAW grayscale format. RAW format can be generated by third party terrain editing tools (such as Bryce) and can also be opened, edited and saved by Photoshop. This allows for sophisticated generation and editing of terrains outside Unity.");
    680.  
    681.             public readonly GUIContent bakeLightProbesForTrees = EditorGUIUtility.TrTextContent("Bake Light Probes For Trees", "If the option is enabled, Unity will create internal light probes at the position of each tree (these probes are internal and will not affect other renderers in the scene) and apply them to tree renderers for lighting. Otherwise trees are still affected by LightProbeGroups. The option is only effective for trees that have LightProbe enabled on their prototype prefab.");
    682.             public readonly GUIContent deringLightProbesForTrees = EditorGUIUtility.TrTextContent("Remove Light Probe Ringing", "When enabled, removes visible overshooting often observed as ringing on objects affected by intense lighting at the expense of reduced contrast.");
    683.             public readonly GUIContent refresh = EditorGUIUtility.TrTextContent("Refresh", "When you save a tree asset from the modelling app, you will need to click the Refresh button (shown in the inspector when the tree painting tool is selected) in order to see the updated trees on your terrain.");
    684.  
    685.             // Settings
    686.             public readonly GUIContent basicTerrain = EditorGUIUtility.TrTextContent("Basic Terrain");
    687.             public readonly GUIContent groupingID = EditorGUIUtility.TrTextContent("Grouping ID", "Grouping ID for auto connection");
    688.             public readonly GUIContent allowAutoConnect = EditorGUIUtility.TrTextContent("Auto Connect", "Allow the current terrain tile to automatically connect to neighboring tiles sharing the same grouping ID.");
    689.             public readonly GUIContent attemptReconnect = EditorGUIUtility.TrTextContent("Reconnect", "Will attempt to re-run auto connection");
    690.             public readonly GUIContent drawTerrain = EditorGUIUtility.TrTextContent("Draw", "Toggle the rendering of terrain");
    691.             public readonly GUIContent drawInstancedTerrain = EditorGUIUtility.TrTextContent("Draw Instanced", "Toggle terrain instancing rendering");
    692.             public readonly GUIContent pixelError = EditorGUIUtility.TrTextContent("Pixel Error", "The accuracy of the mapping between the terrain maps (heightmap, textures, etc.) and the generated terrain; higher values indicate lower accuracy but lower rendering overhead.");
    693.             public readonly GUIContent baseMapDist = EditorGUIUtility.TrTextContent("Base Map Dist.", "The maximum distance at which terrain textures will be displayed at full resolution. Beyond this distance, a lower resolution composite image will be used for efficiency.");
    694.             public readonly GUIContent castShadows = EditorGUIUtility.TrTextContent("Cast Shadows", "Does the terrain cast shadows?");
    695.             public readonly GUIContent createMaterial = EditorGUIUtility.TrTextContent("Create...", "Create a new Material asset to be used by the terrain by duplicating the current default Terrain material.");
    696.             public readonly GUIContent reflectionProbes = EditorGUIUtility.TrTextContent("Reflection Probes", "How reflection probes are used on terrain. Only effective when using built-in standard material or a custom material which supports rendering with reflection.");
    697.             //public readonly GUIContent preserveTreePrototypeLayers = EditorGUIUtility.TextContent("Preserve Tree Prototype Layers|Enable this option if you want your tree instances to take on the layer values of their prototype prefabs, rather than the terrain GameObject's layer.");
    698.             public readonly GUIContent treeAndDetails = EditorGUIUtility.TrTextContent("Tree & Detail Objects");
    699.             public readonly GUIContent drawTrees = EditorGUIUtility.TrTextContent("Draw", "Should trees, grass and details be drawn?");
    700.             public readonly GUIContent detailObjectDistance = EditorGUIUtility.TrTextContent("Detail Distance", "The distance (from camera) beyond which details will be culled.");
    701.             public readonly GUIContent detailObjectDensity = EditorGUIUtility.TrTextContent("Detail Density", "The number of detail/grass objects in a given unit of area. The value can be set lower to reduce rendering overhead.");
    702.             public readonly GUIContent treeDistance = EditorGUIUtility.TrTextContent("Tree Distance", "The distance (from camera) beyond which trees will be culled. For SpeedTree trees this parameter is controlled by the LOD group settings.");
    703.             public readonly GUIContent treeBillboardDistance = EditorGUIUtility.TrTextContent("Billboard Start", "The distance (from camera) at which 3D tree objects will be replaced by billboard images. For SpeedTree trees this parameter is controlled by the LOD group settings.");
    704.             public readonly GUIContent treeCrossFadeLength = EditorGUIUtility.TrTextContent("Fade Length", "Distance over which trees will transition between 3D objects and billboards. For SpeedTree trees this parameter is controlled by the LOD group settings.");
    705.             public readonly GUIContent treeMaximumFullLODCount = EditorGUIUtility.TrTextContent("Max Mesh Trees", "The maximum number of visible trees that will be represented as solid 3D meshes. Beyond this limit, trees will be replaced with billboards. For SpeedTree trees this parameter is controlled by the LOD group settings.");
    706.             public readonly GUIContent grassWindSettings = EditorGUIUtility.TrTextContent("Wind Settings for Grass (On Terrain Data)");
    707.             public readonly GUIContent wavingGrassStrength = EditorGUIUtility.TrTextContent("Speed", "The speed of the wind as it blows grass.");
    708.             public readonly GUIContent wavingGrassSpeed = EditorGUIUtility.TrTextContent("Size", "The size of the 'ripples' on grassy areas as the wind blows over them.");
    709.             public readonly GUIContent wavingGrassAmount = EditorGUIUtility.TrTextContent("Bending", "The degree to which grass objects are bent over by the wind.");
    710.             public readonly GUIContent wavingGrassTint = EditorGUIUtility.TrTextContent("Grass Tint", "Overall color tint applied to grass objects.");
    711.             public readonly GUIContent meshResolution = EditorGUIUtility.TrTextContent("Mesh Resolution (On Terrain Data)");
    712.             public readonly GUIContent detailResolutionWarning = EditorGUIUtility.TrTextContent("You may reduce CPU draw call overhead by setting the detail resolution per patch as high as possible, relative to detail resolution.");
    713.             public readonly GUIContent holesSettings = EditorGUIUtility.TrTextContent("Holes Settings (On Terrain Data)");
    714.             public readonly GUIContent holesCompressionToggle = EditorGUIUtility.TrTextContent("Compress Holes Texture", "If enabled, holes texture will be compressed at runtime if compression supported.");
    715.             public readonly GUIContent detailShadersMissing = EditorGUIUtility.TrTextContent("The current render pipeline does not have all Detail shaders");
    716.             public readonly GUIContent detailShadersUnsupported = EditorGUIUtility.TrTextContent("The current render pipeline does not support Detail shaders");
    717.  
    718.  
    719.             public static readonly GUIContent renderingLayerMask = EditorGUIUtility.TrTextContent("Rendering Layer Mask", "Mask that can be used with SRP DrawRenderers command to filter renderers outside of the normal layering system.");
    720.  
    721.             public static readonly GUIContent heightmapResolution = EditorGUIUtility.TrTextContent("Heightmap Resolution", "Pixel resolution of the terrain's heightmap (should be a power of two plus one, eg, 513 = 512 + 1)");
    722.             public static readonly GUIContent[] heightmapResolutionStrings =
    723.             {
    724.                 EditorGUIUtility.TrTextContent("33 x 33", "Pixels"),
    725.                 EditorGUIUtility.TrTextContent("65 x 65", "Pixels"),
    726.                 EditorGUIUtility.TrTextContent("129 x 129", "Pixels"),
    727.                 EditorGUIUtility.TrTextContent("257 x 257", "Pixels"),
    728.                 EditorGUIUtility.TrTextContent("513 x 513", "Pixels"),
    729.                 EditorGUIUtility.TrTextContent("1025 x 1025", "Pixels"),
    730.                 EditorGUIUtility.TrTextContent("2049 x 2049", "Pixels"),
    731.                 EditorGUIUtility.TrTextContent("4097 x 4097", "Pixels")
    732.             };
    733.             public static readonly int[] heightmapResolutionInts =
    734.             {
    735.                 33,
    736.                 65,
    737.                 129,
    738.                 257,
    739.                 513,
    740.                 1025,
    741.                 2049,
    742.                 4097
    743.             };
    744.  
    745.             public static readonly GUIContent alphamapResolution = EditorGUIUtility.TrTextContent("Control Texture Resolution", "Resolution of the \"splatmap\" that controls the blending of the different terrain materials.");
    746.             public static readonly GUIContent[] alphamapResolutionStrings =
    747.             {
    748.                 EditorGUIUtility.TrTextContent("16 x 16", "Pixels"),
    749.                 EditorGUIUtility.TrTextContent("32 x 32", "Pixels"),
    750.                 EditorGUIUtility.TrTextContent("64 x 64", "Pixels"),
    751.                 EditorGUIUtility.TrTextContent("128 x 128", "Pixels"),
    752.                 EditorGUIUtility.TrTextContent("256 x 256", "Pixels"),
    753.                 EditorGUIUtility.TrTextContent("512 x 512", "Pixels"),
    754.                 EditorGUIUtility.TrTextContent("1024 x 1024", "Pixels"),
    755.                 EditorGUIUtility.TrTextContent("2048 x 2048", "Pixels"),
    756.                 EditorGUIUtility.TrTextContent("4096 x 4096", "Pixels")
    757.             };
    758.             public static readonly int[] alphamapResolutionInts =
    759.             {
    760.                 16,
    761.                 32,
    762.                 64,
    763.                 128,
    764.                 256,
    765.                 512,
    766.                 1024,
    767.                 2048,
    768.                 4096
    769.             };
    770.  
    771.             public static readonly GUIContent basemapResolution = EditorGUIUtility.TrTextContent("Base Texture Resolution", "Resolution of the composite texture used on the terrain when viewed from a distance greater than the Basemap Distance.");
    772.             public static readonly GUIContent[] basemapResolutionStrings =
    773.             {
    774.                 EditorGUIUtility.TrTextContent("16 x 16", "Pixels"),
    775.                 EditorGUIUtility.TrTextContent("32 x 32", "Pixels"),
    776.                 EditorGUIUtility.TrTextContent("64 x 64", "Pixels"),
    777.                 EditorGUIUtility.TrTextContent("128 x 128", "Pixels"),
    778.                 EditorGUIUtility.TrTextContent("256 x 256", "Pixels"),
    779.                 EditorGUIUtility.TrTextContent("512 x 512", "Pixels"),
    780.                 EditorGUIUtility.TrTextContent("1024 x 1024", "Pixels"),
    781.                 EditorGUIUtility.TrTextContent("2048 x 2048", "Pixels"),
    782.                 EditorGUIUtility.TrTextContent("4096 x 4096", "Pixels")
    783.             };
    784.             public static readonly int[] basemapResolutionInts =
    785.             {
    786.                 16,
    787.                 32,
    788.                 64,
    789.                 128,
    790.                 256,
    791.                 512,
    792.                 1024,
    793.                 2048,
    794.                 4096
    795.             };
    796.         }
    797.         static Styles styles;
    798.  
    799.  
    800.         public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
    801.         {
    802.             // We're only doing painting operations, early out if it's not a repaint
    803.             if (Event.current.type != EventType.Repaint)
    804.                 return;
    805.  
    806.             if (editContext.hitValidTerrain)
    807.             {
    808.                 BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
    809.                 PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
    810.                 TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
    811.                 TerrainPaintUtility.ReleaseContextResources(ctx);
    812.             }
    813.         }
    814.     }
    815. }
    And a few caveats as a result of my hack job:
    • You may only be able to add/remove details from the actual detail tool, so don't try it from this tool. It's meant to be a selector only.
    • The brush size slider does nothing. I had no clue how to hook it up to the used brush since it is given as an IOnPaint variable that's passed into some editor function. I thought maybe the BrushRep variable would have it but I couldn't find how to get/set the size from it. So for now you just have to set your brush size using the hotkeys - left and right bracket.
    • The styles variable is a huge list of static params which are just for organization when drawing tons of terrain tools. But here you really only need a few variables. I just copied the whole thing to save time. This is in the OnInspectorUI function which I added to draw the UI.
    • It appears as though Undo isn't working right now, even though I tried to add it in. Even if I can add it though, it may be limited to actions only on a single terrain, that's my hypothesis anyways. I'm guessing if you paint over to another terrain it may not undo correctly since relies on a stack that gathers actions across multiple tools...? Not really sure, it's tough figuring out this kind of stuff without a scripting reference. *See Edit
    And as a result I'm able to paint at any density I want, for example lower than what is normally allowed in 2019.3:



    The screenshot you see above is the custom paint tool UI, with a paint action that shows grass at target density .049!

    This is just a quick fix for now since I wanted to share my results. Tomorrow I'll take another look at it and see if I can get Undo working, as well as brush size slider and maybe proper editing of the details and showing labels, etc. Will update once I make more progress. Thanks again to @KokkuHub , you may have just saved me hours of fiddling with the built-in detail paint!

    EDIT: Turns out I'm just dumb. Undo is working, I just forgot to uncomment the function call. I've edited the code with the fix and now Undo works as expected! Also, the image seems to have trouble loading. Here's a direct link.
     
    Last edited: Apr 5, 2020
    Neto_Kokku likes this.