Skip to content

Commit

Permalink
some additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vyshnovka committed Feb 26, 2024
1 parent e96408e commit 303f881
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 54 deletions.
6 changes: 0 additions & 6 deletions Assets/Plugins/SimpleFileBrowser/README.txt

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Plugins/SimpleFileBrowser/README.txt.meta

This file was deleted.

2 changes: 1 addition & 1 deletion Assets/Scenes/Generation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
document: {fileID: 829112317}
terrainGenerator: {fileID: 1703777238}
terrainManager: {fileID: 1703777238}
saveManager: {fileID: 724454390}
--- !u!114 &829112317
MonoBehaviour:
Expand Down
29 changes: 11 additions & 18 deletions Assets/Scripts/Algorithms/PerlinNoise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,19 @@ private int[] GetPermutationTable(int size)
return permutationTable;
}

private float Grad(int hash, float x, float y)
#region Perlin-specific Helper Methods
private float Grad(int hash, float x, float y) => (hash & 3) switch
{
return (hash & 3) switch
{
0 => x + y,
1 => -x + y,
2 => x - y,
3 => -x - y,
_ => 0,
};
}
0 => x + y,
1 => -x + y,
2 => x - y,
3 => -x - y,
_ => 0,
};

private float Smooth(float t)
{
return t * t * t * (t * (t * 6f - 15f) + 10f);
}
private float Smooth(float t) => t * t * t * (t * (t * 6f - 15f) + 10f);

private float Lerp(float a, float b, float t)
{
return a + (b - a) * t;
}
private float Lerp(float a, float b, float t) => a + (b - a) * t;
#endregion
}
}
6 changes: 3 additions & 3 deletions Assets/Scripts/Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void SaveFloatArray()
private IEnumerator ShowLoadDialogCoroutine(Action<bool> LoadResultCallback)
{
yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.Files, false, null, fileName, "Load Height Map from...", "Select");
bool success = FileBrowser.Success;

if (success)
bool isSuccess = FileBrowser.Success;
if (isSuccess)
{
var filePath = FileBrowser.Result[0];

Expand All @@ -58,7 +58,7 @@ private IEnumerator ShowLoadDialogCoroutine(Action<bool> LoadResultCallback)
}
}

LoadResultCallback?.Invoke(success);
LoadResultCallback?.Invoke(isSuccess);
}
}
}
20 changes: 11 additions & 9 deletions Assets/Scripts/Managers/TerrainGenerationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TerrainGenerationManager : MonoBehaviour
[SerializeField]
private Texture2D texture;

[Header("Gradients")]
[Header("Color Schemes")]
[SerializeField]
private List<Gradient> gradients;

Expand Down Expand Up @@ -85,6 +85,15 @@ public void DisplayResult()
NeedToGenerate = true;
}

#region Terrain Visual Representation
/// <summary>Set terrain heights.</summary>
private void GenerateTerrain()
{
terrain.terrainData.heightmapResolution = SelectedSizeAsNumber;
terrain.terrainData.size = new Vector3(SelectedSizeAsNumber, SelectedSizeAsNumber / 10, SelectedSizeAsNumber);
terrain.terrainData.SetHeights(0, 0, HeightMap);
}

/// <summary>Apply generated texture pixel by pixel.</summary>
private void ApplyTexture()
{
Expand All @@ -105,14 +114,6 @@ private void ApplyTexture()
texture.Apply();
}

/// <summary>Set terrain heights.</summary>
private void GenerateTerrain()
{
terrain.terrainData.heightmapResolution = SelectedSizeAsNumber;
terrain.terrainData.size = new Vector3(SelectedSizeAsNumber, SelectedSizeAsNumber / 10, SelectedSizeAsNumber);
terrain.terrainData.SetHeights(0, 0, HeightMap);
}

/// <summary>Paint terrain pixel by pixel depending on its height.</summary>
private void PaintTerrain()
{
Expand Down Expand Up @@ -176,5 +177,6 @@ private void ResetTerrain()
AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
TerrainSizeChanged?.Invoke(SelectedSizeAsNumber);
}
#endregion
}
}
18 changes: 9 additions & 9 deletions Assets/Scripts/Managers/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UIManager : MonoBehaviour
[SerializeField]
private UIDocument document;
[SerializeField]
private TerrainGenerationManager terrainGenerator;
private TerrainGenerationManager terrainManager;
[SerializeField]
private SaveManager saveManager;

Expand All @@ -19,10 +19,10 @@ public class UIManager : MonoBehaviour
void OnEnable()
{
root = document.rootVisualElement;
root.Q<Button>("Generate").clicked += () => terrainGenerator.DisplayResult();
root.Q<Button>("Generate").clicked += () => terrainManager.DisplayResult();
root.Q<Button>("Success").clicked += () =>
{
terrainGenerator.DisplayResult();
terrainManager.DisplayResult();
root.Q<VisualElement>("Popup").style.opacity = 0;
};
root.Q<Button>("Save").clicked += () => saveManager.SaveFloatArray();
Expand All @@ -35,16 +35,16 @@ void OnEnable()
};

var algorithmDropdown = root.Q<EnumField>("AlgorithmEnum");
algorithmDropdown.RegisterValueChangedCallback(_ => terrainGenerator.SelectedAlgorithmTypeAsName = _.newValue.ToString());
algorithmDropdown.value = (Enum)Enum.Parse(algorithmDropdown.value.GetType(), terrainGenerator.SelectedAlgorithmTypeAsName);
algorithmDropdown.RegisterValueChangedCallback(_ => terrainManager.SelectedAlgorithmTypeAsName = _.newValue.ToString());
algorithmDropdown.value = (Enum)Enum.Parse(algorithmDropdown.value.GetType(), terrainManager.SelectedAlgorithmTypeAsName);

var sizeDropdown = root.Q<EnumField>("SizeEnum");
sizeDropdown.RegisterValueChangedCallback(_ => terrainGenerator.SelectedSizeAsNumber = Convert.ToInt32(_.newValue));
sizeDropdown.value = (Enum)Enum.ToObject(sizeDropdown.value.GetType(), terrainGenerator.SelectedSizeAsNumber);
sizeDropdown.RegisterValueChangedCallback(_ => terrainManager.SelectedSizeAsNumber = Convert.ToInt32(_.newValue));
sizeDropdown.value = (Enum)Enum.ToObject(sizeDropdown.value.GetType(), terrainManager.SelectedSizeAsNumber);

var gradientDropdown = root.Q<EnumField>("GradientEnum");
gradientDropdown.RegisterValueChangedCallback(_ => terrainGenerator.SelectedColorSchemeAsNumber = Convert.ToInt32(_.newValue));
gradientDropdown.value = (Enum)Enum.ToObject(gradientDropdown.value.GetType(), terrainGenerator.SelectedColorSchemeAsNumber);
gradientDropdown.RegisterValueChangedCallback(_ => terrainManager.SelectedColorSchemeAsNumber = Convert.ToInt32(_.newValue));
gradientDropdown.value = (Enum)Enum.ToObject(gradientDropdown.value.GetType(), terrainManager.SelectedColorSchemeAsNumber);
}
}
}

0 comments on commit 303f881

Please sign in to comment.