156 lines
5.1 KiB
C#
156 lines
5.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.IO;
|
|
|
|
public class UranusTextureGenerator : EditorWindow
|
|
{
|
|
public Texture2D sourceTexture;
|
|
public string outputFolder = "Assets/Textures/Uranus";
|
|
|
|
[MenuItem("Tools/Uranus Texture Generator")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<UranusTextureGenerator>("Uranus Texture Generator");
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUILayout.Label("Ãåíåðàòîð òåêñòóð Óðàíà", EditorStyles.boldLabel);
|
|
sourceTexture = (Texture2D)EditorGUILayout.ObjectField("Òåêñòóðà Óðàíà", sourceTexture, typeof(Texture2D), false);
|
|
outputFolder = EditorGUILayout.TextField("Ïàïêà çáåðåæåííÿ", outputFolder);
|
|
EditorGUILayout.Space(10);
|
|
|
|
if (GUILayout.Button("Çãåíåðóâàòè -- Òðîïîñôåðà", GUILayout.Height(35)))
|
|
{
|
|
if (sourceTexture == null) { EditorUtility.DisplayDialog("Ïîìèëêà", "Ïåðåòÿãíè òåêñòóðó Óðàíà!", "OK"); return; }
|
|
GenerateAllTextures();
|
|
}
|
|
|
|
EditorGUILayout.Space(5);
|
|
|
|
if (GUILayout.Button("Çãåíåðóâàòè -- Òåêñòóðà ñìåðò³", GUILayout.Height(35)))
|
|
{
|
|
if (sourceTexture == null) { EditorUtility.DisplayDialog("Ïîìèëêà", "Ïåðåòÿãíè òåêñòóðó Óðàíà!", "OK"); return; }
|
|
GenerateDeadTexture();
|
|
}
|
|
}
|
|
|
|
Texture2D GenerateTropoTexture(Texture2D source, float alive)
|
|
{
|
|
int width = source.width;
|
|
int height = source.height;
|
|
Texture2D result = new Texture2D(width, height, TextureFormat.RGB24, false);
|
|
Color[] pixels = source.GetPixels();
|
|
Color[] output = new Color[pixels.Length];
|
|
|
|
Color stage70 = new Color(0.75f, 0.82f, 0.72f);
|
|
Color stage50 = new Color(0.78f, 0.72f, 0.55f);
|
|
Color stage30 = new Color(0.6f, 0.52f, 0.38f);
|
|
Color stage0 = new Color(0.35f, 0.3f, 0.22f);
|
|
|
|
for (int p = 0; p < pixels.Length; p++)
|
|
{
|
|
Color col = pixels[p];
|
|
Color result_col = col;
|
|
|
|
if (alive <= 1f && alive > 0.7f)
|
|
{
|
|
result_col = col;
|
|
}
|
|
else if (alive <= 0.7f && alive > 0.5f)
|
|
{
|
|
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
|
|
result_col = Color.Lerp(col, stage70, t * 0.8f);
|
|
}
|
|
else if (alive <= 0.5f && alive > 0.3f)
|
|
{
|
|
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
|
|
result_col = Color.Lerp(stage70, stage50, t);
|
|
}
|
|
else if (alive <= 0.3f && alive > 0.0f)
|
|
{
|
|
float t = Mathf.Clamp01((0.3f - alive) / 0.3f);
|
|
result_col = Color.Lerp(stage50, stage30, t);
|
|
}
|
|
else if (alive <= 0f)
|
|
{
|
|
result_col = stage0;
|
|
}
|
|
|
|
output[p] = result_col;
|
|
}
|
|
|
|
result.SetPixels(output);
|
|
result.Apply();
|
|
return result;
|
|
}
|
|
|
|
void GenerateDeadTexture()
|
|
{
|
|
if (!Directory.Exists(outputFolder))
|
|
Directory.CreateDirectory(outputFolder);
|
|
|
|
MakeTextureReadable(sourceTexture);
|
|
|
|
int width = sourceTexture.width;
|
|
int height = sourceTexture.height;
|
|
Texture2D result = new Texture2D(width, height, TextureFormat.RGB24, false);
|
|
Color[] pixels = sourceTexture.GetPixels();
|
|
Color[] output = new Color[pixels.Length];
|
|
|
|
Color deadColor = new Color(0.35f, 0.3f, 0.22f);
|
|
Color darkColor = new Color(0.18f, 0.15f, 0.1f);
|
|
|
|
for (int p = 0; p < pixels.Length; p++)
|
|
{
|
|
float brightness = (pixels[p].r + pixels[p].g + pixels[p].b) / 3f;
|
|
output[p] = Color.Lerp(darkColor, deadColor, brightness);
|
|
}
|
|
|
|
result.SetPixels(output);
|
|
result.Apply();
|
|
|
|
SaveTexture(result, outputFolder + "/Dead.png");
|
|
AssetDatabase.Refresh();
|
|
EditorUtility.DisplayDialog("Ãîòîâî!", "Òåêñòóðà ñìåðò³ çáåðåæåíà!", "OK");
|
|
}
|
|
|
|
void GenerateAllTextures()
|
|
{
|
|
if (!Directory.Exists(outputFolder))
|
|
Directory.CreateDirectory(outputFolder);
|
|
|
|
MakeTextureReadable(sourceTexture);
|
|
|
|
float[] levels = { 1.0f, 0.7f, 0.5f, 0.3f, 0.0f };
|
|
string[] names = { "100", "70", "50", "30", "0" };
|
|
|
|
for (int i = 0; i < levels.Length; i++)
|
|
{
|
|
EditorUtility.DisplayProgressBar("Ãåíåðàö³ÿ", "Tropo_" + names[i], (float)i / levels.Length);
|
|
Texture2D result = GenerateTropoTexture(sourceTexture, levels[i]);
|
|
SaveTexture(result, outputFolder + "/Tropo_" + names[i] + ".png");
|
|
}
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
AssetDatabase.Refresh();
|
|
EditorUtility.DisplayDialog("Ãîòîâî!", "Òåêñòóðè çáåðåæåíî â " + outputFolder, "OK");
|
|
}
|
|
|
|
void SaveTexture(Texture2D tex, string path)
|
|
{
|
|
byte[] bytes = tex.EncodeToPNG();
|
|
File.WriteAllBytes(path, bytes);
|
|
}
|
|
|
|
void MakeTextureReadable(Texture2D tex)
|
|
{
|
|
string path = AssetDatabase.GetAssetPath(tex);
|
|
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
|
|
if (importer != null && !importer.isReadable)
|
|
{
|
|
importer.isReadable = true;
|
|
importer.SaveAndReimport();
|
|
}
|
|
}
|
|
} |