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

267 lines
9.2 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
public class EarthTextureGenerator : EditorWindow
{
public Texture2D sourceTexture;
public string outputFolder = "Assets/Textures/Earth";
[MenuItem("Tools/Earth Texture Generator")]
public static void ShowWindow()
{
GetWindow<EarthTextureGenerator>("Earth 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", GenerateTexture);
}
EditorGUILayout.Space(5);
if (GUILayout.Button("Çãåíåðóâàòè -- Ñòðàòîñôåðà (UV îï³ê)", GUILayout.Height(35)))
{
if (sourceTexture == null) { EditorUtility.DisplayDialog("Ïîìèëêà", "Ïåðåòÿãíè òåêñòóðó!", "OK"); return; }
GenerateAllTextures("Strato", GenerateTextureUV);
}
EditorGUILayout.Space(5);
if (GUILayout.Button("Çãåíåðóâàòè -- Òåðìîñôåðà (ïîëþñè ãîðÿòü)", GUILayout.Height(35)))
{
if (sourceTexture == null) { EditorUtility.DisplayDialog("Ïîìèëêà", "Ïåðåòÿãíè òåêñòóðó!", "OK"); return; }
GenerateAllTextures("Thermo", GenerateTextureThermo);
}
}
Texture2D GenerateTextureThermo(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 poleGlow = new Color(0.9f, 0.5f, 0.1f);
Color poleBurn = new Color(0.8f, 0.2f, 0.05f);
Color poleFinal = new Color(0.6f, 0.15f, 0.05f);
for (int p = 0; p < pixels.Length; p++)
{
int x = p % width;
int y = p / width;
float v = (float)y / height;
float poleStrength = Mathf.Clamp01((Mathf.Abs(v - 0.5f) * 2f - 0.5f) * 2f);
Color col = pixels[p];
Color result_col = col;
if (alive < 1.0f)
{
float t = Mathf.Clamp01((1.0f - alive) / 0.3f);
result_col = Color.Lerp(result_col, poleGlow, poleStrength * t * 0.5f);
}
if (alive < 0.7f)
{
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
result_col = Color.Lerp(result_col, poleGlow, poleStrength * t);
}
if (alive < 0.5f)
{
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
result_col = Color.Lerp(result_col, poleBurn, poleStrength * t);
}
if (alive < 0.3f)
{
float t = Mathf.Clamp01((0.3f - alive) / 0.3f);
result_col = Color.Lerp(result_col, poleFinal, poleStrength * 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");
}
Texture2D GenerateTexture(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 greenYellow = new Color(0.68f, 0.60f, 0.12f);
Color greenDead = new Color(0.45f, 0.35f, 0.18f);
Color oceanLight = new Color(0.82f, 0.92f, 0.94f);
Color oceanBoiling = new Color(0.94f, 0.96f, 0.97f);
Color oceanGone = new Color(0.55f, 0.48f, 0.35f);
Color landDead = new Color(0.52f, 0.42f, 0.28f);
Color finalDead = new Color(0.38f, 0.30f, 0.20f);
for (int p = 0; p < pixels.Length; p++)
{
Color col = pixels[p];
float isOcean = Mathf.Clamp01(col.b - col.r * 0.5f - col.g * 0.3f);
float isGreen = Mathf.Clamp01((col.g - col.r * 0.5f - col.b * 0.4f) * 2f);
float isForest = Mathf.Clamp01((col.g - col.r * 0.6f - col.b * 0.5f) * 3f);
float isLand = Mathf.Clamp01(1f - isOcean);
Color result_col = col;
if (alive < 1.0f)
{
float t = Mathf.Clamp01((1.0f - alive) / 0.3f);
result_col = Color.Lerp(result_col, greenYellow, isGreen * t);
}
if (alive < 0.7f)
{
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
result_col = Color.Lerp(result_col, greenDead, (isGreen + isForest) * 0.5f * t);
result_col = Color.Lerp(result_col, oceanLight, isOcean * t * 0.5f);
}
if (alive < 0.5f)
{
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
result_col = Color.Lerp(result_col, oceanBoiling, isOcean * t);
}
if (alive < 0.3f)
{
float t = Mathf.Clamp01((0.3f - alive) / 0.2f);
result_col = Color.Lerp(result_col, oceanGone, isOcean * t);
result_col = Color.Lerp(result_col, landDead, isLand * t * 0.4f);
}
if (alive < 0.1f)
{
float t = Mathf.Clamp01((0.1f - alive) / 0.1f);
result_col = Color.Lerp(result_col, finalDead, t);
}
output[p] = result_col;
}
result.SetPixels(output);
result.Apply();
return result;
}
Texture2D GenerateTextureUV(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 burnYellow = new Color(0.80f, 0.70f, 0.05f);
Color burnOrange = new Color(0.70f, 0.40f, 0.05f);
Color burnBrown = new Color(0.50f, 0.28f, 0.05f);
Color oceanGray = new Color(0.45f, 0.45f, 0.45f);
Color finalAsh = new Color(0.35f, 0.30f, 0.25f);
for (int p = 0; p < pixels.Length; p++)
{
Color col = pixels[p];
float isOcean = Mathf.Clamp01(col.b - col.r * 0.5f - col.g * 0.3f);
float isGreen = Mathf.Clamp01((col.g - col.r * 0.5f - col.b * 0.4f) * 2f);
float isForest = Mathf.Clamp01((col.g - col.r * 0.6f - col.b * 0.5f) * 3f);
float isLand = Mathf.Clamp01(1f - isOcean);
Color result_col = col;
if (alive < 1.0f)
{
float t = Mathf.Clamp01((1.0f - alive) / 0.3f);
result_col = Color.Lerp(result_col, burnYellow, isGreen * t * 0.7f);
}
if (alive < 0.7f)
{
float t = Mathf.Clamp01((0.7f - alive) / 0.2f);
result_col = Color.Lerp(result_col, burnOrange, (isGreen + isForest) * 0.5f * t);
}
if (alive < 0.5f)
{
float t = Mathf.Clamp01((0.5f - alive) / 0.2f);
result_col = Color.Lerp(result_col, burnBrown, isLand * t * 0.8f);
result_col = Color.Lerp(result_col, oceanGray, isOcean * t);
}
if (alive < 0.3f)
{
float t = Mathf.Clamp01((0.3f - alive) / 0.3f);
result_col = Color.Lerp(result_col, finalAsh, t);
}
output[p] = result_col;
}
result.SetPixels(output);
result.Apply();
return result;
}
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();
}
}
}