2023-04-03 11:04:31 +08:00

201 lines
5.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
using BF;
namespace BFEditor.Resource
{
public class GenBlurTextureWindow : EditorWindow
{
int cameraCount = 1;
int iteration = 4;
int scaleSize = 4;
bool bloom = false;
string outputPath = "";
string textureName = "";
bool needDestroy = false;
List<Camera> cameras = new List<Camera>() { null, null, null };
GenBlurTextureWindow()
{
this.titleContent = new GUIContent("生成模糊texture");
}
void OnGUI()
{
GUILayout.Space(10);
EditorGUILayout.LabelField("相机数量 (范围1-3)");
ChangableIntField("", ref cameraCount, 1, 3);
EditorGUILayout.LabelField("从场景中添加相机");
for (int i = 0; i < 3; i++)
{
if (i < cameraCount)
cameras[i] = (Camera)EditorGUILayout.ObjectField(cameras[i], typeof(Camera), true, GUILayout.Width(200));
else
cameras[i] = null;
}
GUILayout.Space(15);
EditorGUILayout.LabelField("高斯迭代次数 (范围1-16)");
ChangableIntField("", ref iteration, 1, 16);
GUILayout.Space(15);
EditorGUILayout.LabelField("缩小系数 相对于屏幕宽高 (范围1-8)");
ChangableIntField("", ref scaleSize, 1, 8);
GUILayout.Space(15);
EditorGUILayout.LabelField("是否需要bloom ? ");
bloom = EditorGUILayout.Toggle(bloom, GUILayout.Width(100));
GUILayout.Space(15);
EditorGUILayout.LabelField("产出名称");
textureName = EditorGUILayout.TextField(textureName, GUILayout.Width(180));
GUILayout.Space(15);
EditorGUILayout.LabelField("产出路径");
EditorGUILayout.BeginHorizontal();
outputPath = EditorGUILayout.TextField(outputPath, GUILayout.Width(180));
if (GUILayout.Button("选择", GUILayout.Width(50)))
{
OnClickSelectOutputPath();
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(15);
if (GUILayout.Button("生成"))
{
OnClickGenerate();
}
}
void ChangableIntField(string label, ref int valueInt, int min, int max, int fieldWidth = 80, int buttonWidth = 50)
{
GUILayout.BeginHorizontal();
valueInt = EditorGUILayout.IntField(label, valueInt, GUILayout.Width(fieldWidth));
if (GUI.changed)
{
int oldValueInt = valueInt;
if (valueInt < min)
{
valueInt = min;
}
if (valueInt > max)
{
valueInt = max;
}
}
if (GUILayout.Button("-", GUILayout.Width(buttonWidth)))
{
valueInt--;
if (valueInt < min)
valueInt = min;
}
if (GUILayout.Button("+", GUILayout.Width(buttonWidth)))
{
valueInt++;
if (valueInt > max)
valueInt = max;
}
GUILayout.EndHorizontal();
}
void OnClickSelectOutputPath()
{
outputPath = EditorUtility.OpenFolderPanel("select output path", Application.dataPath, "");
}
void OnClickGenerate()
{
if (string.IsNullOrEmpty(outputPath))
{
EditorUtility.DisplayDialog("提示", "产出路径不能为空", "ok");
return;
}
if (!Directory.Exists(outputPath))
{
EditorUtility.DisplayDialog("提示", "产出路径不存在", "ok");
return;
}
if (string.IsNullOrEmpty(textureName))
{
EditorUtility.DisplayDialog("提示", "产出名称不能为空", "ok");
return;
}
var path = outputPath + "/" + textureName + ".png";
if (File.Exists(path))
{
EditorUtility.DisplayDialog("提示", "文件已存在", "ok");
return;
}
for (int i = cameras.Count - 1; i >= 0; i--)
{
if (cameras[i] == null)
{
cameras.RemoveAt(i);
}
}
var gameWidth = 0;
var gameHeight = 0;
GameViewUtils.GetCurrentGameViewSize(out gameWidth, out gameHeight);
if (gameWidth == 0 || gameHeight == 0)
{
EditorUtility.DisplayDialog("提示", "GameView分辨率设置异常", "ok");
return;
}
RenderTexture rt;
var width = gameWidth / scaleSize;
var height = gameHeight / scaleSize;
if (bloom)
{
rt = BFMain.Instance.RenderMgr.GetBlurBgWithBloom(cameras.ToArray(), width, height, iteration);
}
else
{
rt = BFMain.Instance.RenderMgr.GetBlurBg(cameras.ToArray(), width, height, iteration);
}
var tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, true);
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false);
File.WriteAllBytes(path, tex.EncodeToPNG());
RenderTexture.ReleaseTemporary(rt);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
cameras = new List<Camera>() { null, null, null };
needDestroy = true;
EditorUtility.DisplayDialog("提示", "生成成功", "ok");
}
void OnDestroy()
{
if (needDestroy)
{
var obj = GameObject.Find("BFMain");
DestroyImmediate(obj);
}
}
public static void ShowWindow()
{
var window = (GenBlurTextureWindow)GetWindowWithRect(typeof(GenBlurTextureWindow),
new Rect(Screen.width / 2, Screen.height / 2, 330, 480), true);
window.Show();
}
}
}