Skip to content

Instantly share code, notes, and snippets.

@bahadir
Created November 25, 2020 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahadir/871bec46703d0f0fc5e78d6310693128 to your computer and use it in GitHub Desktop.
Save bahadir/871bec46703d0f0fc5e78d6310693128 to your computer and use it in GitHub Desktop.
VSP + A*
using System.Collections;
using System.Collections.Generic;
using AwesomeTechnologies.Vegetation;
using AwesomeTechnologies.VegetationSystem;
using AwesomeTechnologies.Vegetation.Masks;
using AwesomeTechnologies.ColliderSystem;
using Sirenix.OdinInspector;
using UnityEngine;
using System;
using System.IO;
[System.Serializable]
public class VegetationSplatmapLayer : System.Object
{
public int layer;
public float penalty;
}
public class VegetationSplatmapGenerator : MonoBehaviour
{
public Terrain terrain;
public int size = 512;
public List<VegetationSplatmapLayer> layers = new List<VegetationSplatmapLayer>();
public Texture2D obstacleTexture;
public string targetFile;
[ContextMenu("Save Vegetation Splatmap")]
void SaveVegetationSplatmap()
{
Dictionary<int, float> penalties = new Dictionary<int, float>();
foreach (var item in layers)
{
penalties.Add(item.layer, item.penalty);
}
float[,,] maps = terrain.terrainData.GetAlphamaps(0, 0, terrain.terrainData.alphamapWidth, terrain.terrainData.alphamapHeight);
int patches = terrain.terrainData.alphamapHeight / size;
Texture2D texture = new Texture2D(size, size, TextureFormat.ARGB32, false, true);
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
float layerValue = 0;
int layerIndex = 0;
for (int z = 0; z < terrain.terrainData.alphamapLayers; z++)
{
if (maps[x * patches, y * patches, z] > layerValue)
{
layerValue = maps[x * patches, y * patches, z];
layerIndex = z;
}
}
float color = 1;
if (penalties.ContainsKey(layerIndex))
{
color = penalties[layerIndex];
}
if (obstacleTexture != null)
{
texture.SetPixel(y, x, new Color(obstacleTexture.GetPixel(y, x).r, color, 0));
}
else
{
texture.SetPixel(y, x, new Color(0, color, 0));
}
}
}
texture.Apply();
byte[] data = texture.EncodeToPNG();
string targetPath = Path.Combine(Application.dataPath, targetFile);
System.IO.File.WriteAllBytes(targetPath, data);
Debug.Log(targetPath + " was saved.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment