Files
PrinceOfGlory/Assets/Editor/ShirakaTools/ExportSplatMaps.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

63 lines
2.0 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
public class ExportSplatMaps : EditorWindow
{
private Terrain terrain;
[MenuItem("Tools/ShirakaTool/Export Terrain SplatMaps")]
public static void ShowWindow()
{
GetWindow<ExportSplatMaps>("Export SplatMaps");
}
private void OnGUI()
{
GUILayout.Label("导出 Terrain SplatMaps", EditorStyles.boldLabel);
terrain = EditorGUILayout.ObjectField("选择 Terrain", terrain, typeof(Terrain), true) as Terrain;
if (terrain == null)
{
EditorGUILayout.HelpBox("请在场景中选择一个 Terrain 对象。", MessageType.Warning);
return;
}
if (GUILayout.Button("导出 SplatMaps"))
{
ExportSplatMapsToPNG(terrain);
}
}
private void ExportSplatMapsToPNG(Terrain terrain)
{
TerrainData terrainData = terrain.terrainData;
Texture2D[] splatMaps = terrainData.alphamapTextures;
string folderPath = EditorUtility.SaveFolderPanel("选择导出文件夹", "", "");
if (string.IsNullOrEmpty(folderPath))
return;
for (int i = 0; i < splatMaps.Length; i++)
{
Texture2D splatMap = splatMaps[i];
RenderTexture rt = RenderTexture.GetTemporary(splatMap.width, splatMap.height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(splatMap, rt);
RenderTexture.active = rt;
Texture2D readableTex = new Texture2D(splatMap.width, splatMap.height, TextureFormat.ARGB32, false);
readableTex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
readableTex.Apply();
byte[] bytes = readableTex.EncodeToPNG();
File.WriteAllBytes(Path.Combine(folderPath, $"SplatMap_{i}.png"), bytes);
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(rt);
DestroyImmediate(readableTex);
}
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("导出完成", "SplatMaps 已成功导出为 PNG 文件。", "确定");
}
}