Files
ScienceLab.AtmosphericPressure/Assets/Editor/MarsTextureGenerator.cs
2026-05-29 18:21:53 +03:00

204 lines
6.8 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
public class MarsTextureGenerator : EditorWindow
{
public Texture2D sourceTexture;
public string outputFolder = "Assets/Textures/Mars";
[MenuItem("Tools/Mars Texture Generator")]
public static void ShowWindow()
{
GetWindow<MarsTextureGenerator>("Mars 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("Tropo", GenerateTropoTexture);
}
if (GUILayout.Button("Çãåíåðóâàòè -- Òåðìîñôåðà (ñîíÿ÷íèé â³òåð + âòðàòà àòìîñôåðè)", GUILayout.Height(35)))
{
if (sourceTexture == null) { EditorUtility.DisplayDialog("Ïîìèëêà", "Ïåðåòÿãíè òåêñòóðó!", "OK"); return; }
GenerateAllTextures("Thermo", GenerateThermoTexture);
}
}
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 dustyRed = new Color(0.8f, 0.4f, 0.2f);
Color dryOrange = new Color(0.6f, 0.3f, 0.1f);
Color darkBrown = new Color(0.35f, 0.18f, 0.08f);
Color grayDead = new Color(0.25f, 0.22f, 0.20f);
Color blackDead = new Color(0.15f, 0.13f, 0.12f);
for (int p = 0; p < pixels.Length; p++)
{
int x = p % width;
int y = p / width;
Color col = pixels[p];
Color result_col = col;
float isRed = Mathf.Clamp01((col.r - col.b * 0.5f - col.g * 0.3f) * 2f);
float isBright = Mathf.Clamp01((col.r + col.g + col.b) / 3f);
if (alive < 1.0f)
{
float t = Mathf.Clamp01((1.0f - alive) / 0.3f);
result_col = Color.Lerp(result_col, dustyRed, isRed * t * 0.3f);
}
if (alive < 0.7f)
{
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
result_col = Color.Lerp(result_col, dryOrange, isRed * t * 0.5f);
result_col = Color.Lerp(result_col, darkBrown, isBright * t * 0.3f);
}
if (alive < 0.5f)
{
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
result_col = Color.Lerp(result_col, darkBrown, t * 0.6f);
}
if (alive < 0.3f)
{
float t = Mathf.Clamp01((0.3f - alive) / 0.2f);
result_col = Color.Lerp(result_col, grayDead, t * 0.7f);
}
if (alive < 0.1f)
{
float t = Mathf.Clamp01((0.1f - alive) / 0.1f);
result_col = Color.Lerp(result_col, blackDead, t);
}
output[p] = result_col;
}
result.SetPixels(output);
result.Apply();
return result;
}
Texture2D GenerateThermoTexture(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 burnedDark = new Color(0.2f, 0.15f, 0.12f);
Color coldGray = new Color(0.35f, 0.35f, 0.4f);
Color spaceBlack = new Color(0.05f, 0.05f, 0.07f);
Color solarGlow = new Color(1f, 0.6f, 0.2f);
Color ionBlue = new Color(0.4f, 0.7f, 1f);
for (int p = 0; p < pixels.Length; p++)
{
int x = p % width;
int y = p / width;
Color col = pixels[p];
Color result_col = col;
float heightFactor = (float)y / height;
float noise = Mathf.PerlinNoise(x * 0.02f, y * 0.02f);
if (alive < 0.8f)
{
float t = Mathf.Clamp01((0.8f - alive) / 0.3f);
result_col = Color.Lerp(result_col, ionBlue, heightFactor * t * 0.4f);
}
if (alive < 0.6f)
{
float t = Mathf.Clamp01((0.6f - alive) / 0.3f);
float glowMask = heightFactor * noise;
result_col = Color.Lerp(result_col, solarGlow, glowMask * t * 0.7f);
}
if (alive < 0.4f)
{
float t = Mathf.Clamp01((0.4f - alive) / 0.2f);
result_col = Color.Lerp(result_col, burnedDark, t * 0.6f);
}
if (alive < 0.2f)
{
float t = Mathf.Clamp01((0.2f - alive) / 0.2f);
result_col = Color.Lerp(result_col, coldGray, t * 0.7f);
}
if (alive < 0.05f)
{
float t = Mathf.Clamp01((0.05f - alive) / 0.05f);
result_col = Color.Lerp(result_col, spaceBlack, t);
}
output[p] = result_col;
}
result.SetPixels(output);
result.Apply();
return result;
}
void GenerateAllTextures(string prefix, System.Func<Texture2D, float, Texture2D> generator)
{
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("Ãåíåðàö³ÿ", prefix + "_" + names[i], (float)i / levels.Length);
Texture2D result = generator(sourceTexture, levels[i]);
SaveTexture(result, outputFolder + "/" + prefix + "_" + 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();
}
}
}