78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
|
|
namespace BFEditor.Resource
|
|
{
|
|
public class CombineTextureWindow : EditorWindow
|
|
{
|
|
public CombineTextureWindow()
|
|
{
|
|
this.titleContent = new GUIContent("合并贴图");
|
|
}
|
|
|
|
Texture2D normalTex;
|
|
Texture2D metallTex;
|
|
|
|
private void OnEnable() { }
|
|
|
|
void OnGUI()
|
|
{
|
|
EditorGUILayout.BeginVertical("box");
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
EditorGUILayout.LabelField("选择法线贴图");
|
|
normalTex = (Texture2D)EditorGUILayout.ObjectField(normalTex, typeof(Texture2D), false);
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
EditorGUILayout.LabelField("选择金属度贴图");
|
|
metallTex = (Texture2D)EditorGUILayout.ObjectField(metallTex, typeof(Texture2D), false);
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
if (GUILayout.Button("合并"))
|
|
{
|
|
OnClickCombine();
|
|
}
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
EditorGUILayout.EndVertical();
|
|
}
|
|
|
|
void OnClickCombine()
|
|
{
|
|
if (normalTex == null || metallTex == null)
|
|
{
|
|
return;
|
|
}
|
|
Color32[] normalColors = normalTex.GetPixels32();
|
|
Color32[] metallColors = metallTex.GetPixels32();
|
|
Color32[] newColor = new Color32[normalColors.Length];
|
|
for (int i = 0; i < normalColors.Length; i++)
|
|
{
|
|
newColor[i] = new Color32(normalColors[i].r, normalColors[i].g, metallColors[i].r, metallColors[i].g);
|
|
}
|
|
Texture2D tex = new Texture2D(normalTex.width, normalTex.height, TextureFormat.ARGB32, false);
|
|
tex.SetPixels32(newColor);
|
|
tex.Apply();
|
|
|
|
string path = Application.dataPath + "/combin.png";
|
|
File.WriteAllBytes(path, tex.EncodeToPNG());
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
public static void ShowWindow()
|
|
{
|
|
var window = GetWindow<CombineTextureWindow>();
|
|
window.Show();
|
|
}
|
|
}
|
|
}
|