69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
|
using System;
|
|||
|
using System.Buffers;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Experimental.Rendering;
|
|||
|
|
|||
|
namespace BitGames.Terrain
|
|||
|
{
|
|||
|
public class TerrainAsset : ScriptableObject
|
|||
|
{
|
|||
|
public int Size;
|
|||
|
public BiomesAsset BiomesAsset;
|
|||
|
public Texture2D ControlTexture;
|
|||
|
public Texture2D ShadowTexture;
|
|||
|
public Color ShadowColor = new(0.2f, 0.2f, 0.2f, 0.5f);
|
|||
|
|
|||
|
public static TerrainAsset Create(string path, int size)
|
|||
|
{
|
|||
|
var asset = CreateInstance<TerrainAsset>();
|
|||
|
|
|||
|
asset.Size = size;
|
|||
|
|
|||
|
var controlTexture = new Texture2D(size, size, TextureFormat.RG16, false);
|
|||
|
controlTexture.name = "Control Texture";
|
|||
|
controlTexture.filterMode = FilterMode.Point;
|
|||
|
|
|||
|
var shadowTexture = new Texture2D(size, size, TextureFormat.R8, true);
|
|||
|
shadowTexture.name = "Shadow Texture";
|
|||
|
controlTexture.filterMode = FilterMode.Bilinear;
|
|||
|
|
|||
|
var colorBuffer = ArrayPool<Color32>.Shared.Rent(size * size);
|
|||
|
try
|
|||
|
{
|
|||
|
Array.Fill(colorBuffer, GetControlMapDefault());
|
|||
|
controlTexture.SetPixels32(0, 0, size, size, colorBuffer);
|
|||
|
controlTexture.Apply();
|
|||
|
|
|||
|
Array.Fill(colorBuffer, GetShadowMapDefault());
|
|||
|
shadowTexture.SetPixels32(0, 0, size, size, colorBuffer);
|
|||
|
shadowTexture.Apply();
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
ArrayPool<Color32>.Shared.Return(colorBuffer);
|
|||
|
}
|
|||
|
|
|||
|
asset.ControlTexture = controlTexture;
|
|||
|
asset.ShadowTexture = shadowTexture;
|
|||
|
|
|||
|
AssetDatabase.CreateAsset(asset, path);
|
|||
|
AssetDatabase.AddObjectToAsset(asset.ControlTexture, asset);
|
|||
|
AssetDatabase.AddObjectToAsset(asset.ShadowTexture, asset);
|
|||
|
AssetDatabase.ImportAsset(path);
|
|||
|
|
|||
|
return asset;
|
|||
|
}
|
|||
|
|
|||
|
private static Color32 GetControlMapDefault()
|
|||
|
{
|
|||
|
return new Color32(0, (byte)ControlFlags.DEFAULT, 0, 0);
|
|||
|
}
|
|||
|
|
|||
|
private static Color32 GetShadowMapDefault()
|
|||
|
{
|
|||
|
return Color.black;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|