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

136 lines
4.7 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
public class MercuryTextureGenerator : EditorWindow
{
public Texture2D sourceTexture;
public string outputFolder = "Assets/Textures/Mercury";
[MenuItem("Tools/Mercury Texture Generator")]
public static void ShowWindow()
{
GetWindow<MercuryTextureGenerator>("Mercury 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("Mercury", GenerateTextureMercury);
}
}
Texture2D GenerateTextureMercury(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 craterDark = new Color(0.25f, 0.22f, 0.20f);
Color heatSide = new Color(0.65f, 0.35f, 0.15f);
Color coldSide = new Color(0.30f, 0.32f, 0.40f);
Color burnedOut = new Color(0.18f, 0.15f, 0.12f);
Color finalDead = new Color(0.10f, 0.08f, 0.07f);
System.Random rng = new System.Random(42);
for (int p = 0; p < pixels.Length; p++)
{
int x = p % width;
int y = p / width;
float u = (float)x / width;
float v = (float)y / height;
Color col = pixels[p];
Color result_col = col;
if (alive < 1.0f)
{
float t = Mathf.Clamp01((1.0f - alive) / 0.3f);
float noise = (float)rng.NextDouble();
if (noise < 0.03f * t)
result_col = Color.Lerp(result_col, craterDark, t * 0.8f);
}
if (alive < 0.7f)
{
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
float heatInfluence = Mathf.Clamp01(1f - u * 2f);
float coldInfluence = Mathf.Clamp01(u * 2f - 1f);
result_col = Color.Lerp(result_col, heatSide, heatInfluence * t * 0.6f);
result_col = Color.Lerp(result_col, coldSide, coldInfluence * t * 0.4f);
}
if (alive < 0.5f)
{
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
result_col = Color.Lerp(result_col, burnedOut, t * 0.7f);
float noise = (float)rng.NextDouble();
if (noise < 0.06f * t)
result_col = Color.Lerp(result_col, craterDark, 0.9f);
}
if (alive < 0.3f)
{
float t = Mathf.Clamp01((0.3f - alive) / 0.3f);
result_col = Color.Lerp(result_col, finalDead, 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();
}
}
}