Skip to content

Instantly share code, notes, and snippets.

@cemkoker
Last active August 17, 2023 05:34
Show Gist options
  • Save cemkoker/240b3070f6127402c98c5420f0597792 to your computer and use it in GitHub Desktop.
Save cemkoker/240b3070f6127402c98c5420f0597792 to your computer and use it in GitHub Desktop.
Bakes the terrain into a TVE colormap (requires AllTerrainTextures & The Vegetation engine assets)
using UnityEngine;
using AmazingAssets.AllTerrainTextures;
#if THE_VEGETATION_ENGINE
using TheVegetationEngine;
#endif
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace OneFoxStudio.Utilities
{
[ExecuteInEditMode]
public class TerrainBaker : MonoBehaviour
{
[SerializeField] private int terrainSize = 128;
[SerializeField] private int terrainResolution = 1024;
[SerializeField] private Shader colorMapElementShader;
[SerializeField] private Color colormapColor;
[SerializeField] private Mesh QuadMesh;
[SerializeField] private string assetsPath = "Assets/_Game/Terrain/";
private Terrain[] terrains;
private void Clear()
{
terrains = FindObjectsOfType<Terrain>();
foreach (var terrain in terrains)
{
#if THE_VEGETATION_ENGINE
var elements = terrain.gameObject.GetComponentsInChildren<TVEElement>();
foreach (var e in elements)
DestroyImmediate(e.gameObject);
#endif
}
}
private void BakeTerrain()
{
#if UNITY_EDITOR
Clear();
terrains = FindObjectsOfType<Terrain>();
foreach (var terrain in terrains)
{
var terrainTransform = terrain.transform.position;
var terrainPosition = new Vector2(terrainTransform.x, terrainTransform.z);
var colorMapTexture = Bake(terrain);
SaveTextureAsPNG(colorMapTexture,
assetsPath + $"ColorMap/ColorMap[{terrainPosition.x},{terrainPosition.y}].png");
var colorMapMaterial = new Material(colorMapElementShader);
AssetDatabase.CreateAsset(colorMapMaterial,
assetsPath + $"ColorMap/Colormap[{terrainPosition.x},{terrainPosition.y}].mat");
colorMapMaterial.name = $"Colormap[{terrainPosition.x},{terrainPosition.y}]";
colorMapMaterial.SetTexture("_MainTex", colorMapTexture);
colorMapMaterial.SetColor("_MainColor", colormapColor);
colorMapMaterial.SetFloat("_MainTexAlphaMinValue", 0f);
colorMapMaterial.SetFloat("_MainTexAlphaMaxValue", 0f);
var colorMap = new GameObject();
colorMap.name = $"Colormap[{terrainPosition.x},{terrainPosition.y}]";
colorMap.AddComponent<MeshFilter>().mesh = QuadMesh;
colorMap.AddComponent<MeshRenderer>().sharedMaterial = colorMapMaterial;
colorMap.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
colorMap.GetComponent<MeshRenderer>().receiveShadows = false;
var t = (Texture2D)AssetDatabase.LoadAssetAtPath(
assetsPath + $"ColorMap/ColorMap[{terrainPosition.x},{terrainPosition.y}].png", typeof(Texture2D));
colorMap.AddComponent<TVEElement>();
colorMap.GetComponent<TVEElement>().elementRenderer.sharedMaterial.mainTexture = t;
colorMap.transform.parent = terrain.transform;
colorMap.transform.localPosition = new Vector3(terrainSize / 2, -0.01f, terrainSize / 2);
colorMap.transform.localScale = new Vector3(terrainSize, 1f, terrainSize);
AssetDatabase.SaveAssets();
}
AssetDatabase.Refresh();
#endif
}
private Texture2D Bake(Terrain terrain)
{
if (terrain == null) Debug.LogError("Terrain is null");
Texture2D colorMap;
colorMap = terrain.terrainData.AllTerrainTextures(false, false).Basemap.Diffuse(terrainResolution);
return colorMap;
}
private static void SaveTextureAsPNG(Texture2D _texture, string _fullPath)
{
var bytes = _texture.EncodeToPNG();
System.IO.File.WriteAllBytes(_fullPath, bytes);
}
public void Bake()
{
BakeTerrain();
}
}
}
using UnityEditor;
using UnityEngine;
namespace OneFoxStudio.Utilities
{
[CustomEditor(typeof(TerrainBaker))]
public class TerrainBakerEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var t = (TerrainBaker)target;
if (GUILayout.Button("Bake"))
t.Bake();
}
}
}
@cemkoker
Copy link
Author

Shader: Pick the ColorMap shader from TVE
Terain size & resolution: Adjust your terrain size & resolution
ColorMapColor: make it around grey (I tend to use 147 for R,G,B)
QuadMesh: pick any quad mesh (there's one in TVE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment