ThreeKingdomsSlingshot/Tools/fx_catalog.cs

198 lines
8.8 KiB
C#
Raw Normal View History

using System.IO;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 이펙트 그림 목록 + 실측 크기(2026-11-11 · 액티브 스킬 연출 본작업 준비) — Assets/VFX(또는 Resources/Fx 사본) 프리팹을 게임과 같은 방식(SkillVfx.SpawnPrefab ·
// 직교 카메라 · 모래색 바탕 · 빌트인)으로 RenderTexture 에 직접 그려 ① 가장 많이 보이는 시점의 옆모습(tilt 0)·위에서 본 모습(tilt −90) 그림 ② 배율 1 일 때 그림 경계(월드 단위)를 잰다.
// 설정 = <outDir>/job.txt(1줄 = 찾을 폴더 ; 로 여러 개 | 한 번에 처리할 수 | 제외할 경로 조각 ; 여러 개) · 진행 = <outDir>/state.txt(다음 순번) · 결과 = <outDir>/NNNN.png + catalog.csv
// 실행(끝날 때까지 반복): unity command --project-path . --timeout 300 script eval --file Tools/fx_catalog.cs (출력 폴더 = 환경 변수 대신 Tools/fx_catalog_dir.txt 한 줄)
string outDir = File.ReadAllText("Tools/fx_catalog_dir.txt").Trim();
Directory.CreateDirectory(outDir);
var job = File.ReadAllText(outDir + "/job.txt").Trim().Split('|');
var folders = job[0].Split(';').Select(s => s.Trim()).Where(s => s.Length > 0).ToArray();
int batch = int.Parse(job[1].Trim());
var excludes = job.Length > 2 ? job[2].Split(';').Select(s => s.Trim()).Where(s => s.Length > 0).ToArray() : new string[0];
var paths = AssetDatabase.FindAssets("t:Prefab", folders).Select(AssetDatabase.GUIDToAssetPath)
.Where(p => !excludes.Any(e => p.Contains(e))).Distinct().OrderBy(p => p, System.StringComparer.Ordinal).ToList();
string statePath = outDir + "/state.txt";
int start = File.Exists(statePath) ? int.Parse(File.ReadAllText(statePath).Trim()) : 0;
string csvPath = outDir + "/catalog.csv";
if (start == 0) File.WriteAllText(csvPath, "n_idx,s_path,f_dur,b_loop,n_ps,f_peak,f_w0,f_h0,f_cx0,f_cy0,f_w90,f_h90,f_cx90,f_cy90,s_rgb\n");
const int Layer = 31;
const int Cell = 192;
Color bg = new Color(197f / 255f, 155f / 255f, 82f / 255f, 1f);
Vector2 origin = new Vector2(3000f, 3000f);
var camGo = new GameObject("FxCatalogCam") { hideFlags = HideFlags.HideAndDontSave };
var cam = camGo.AddComponent<Camera>();
cam.orthographic = true;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = bg;
cam.cullingMask = 1 << Layer;
cam.nearClipPlane = 0.01f;
cam.farClipPlane = 200f;
cam.enabled = false;
cam.depthTextureMode |= DepthTextureMode.Depth;
cam.allowHDR = false;
cam.allowMSAA = false;
var rtLow = new RenderTexture(128, 128, 24);
var rtHi = new RenderTexture(Cell, Cell, 24);
var texLow = new Texture2D(128, 128, TextureFormat.RGB24, false);
var texHi = new Texture2D(Cell, Cell, TextureFormat.RGB24, false);
void SetLayer(Transform t)
{
t.gameObject.layer = Layer;
t.gameObject.hideFlags = HideFlags.HideAndDontSave;
foreach (Transform c in t) SetLayer(c);
}
List<ParticleSystem> Roots(GameObject go)
{
var res = new List<ParticleSystem>();
foreach (var ps in go.GetComponentsInChildren<ParticleSystem>(true))
{
var p = ps.transform.parent;
bool has = false;
while (p != null)
{
if (p.GetComponent<ParticleSystem>() != null) { has = true; break; }
p = p.parent;
}
if (!has) res.Add(ps);
}
return res;
}
void Sim(List<ParticleSystem> roots, float t)
{
foreach (var ps in roots) ps.Simulate(t, true, true, true);
}
// 그리고 바탕과 다른 픽셀을 센다 → (개수 · 경계 xmin,ymin,xmax,ymax 픽셀 · 평균 색)
float[] Shoot(RenderTexture rt, Texture2D tex, Vector2 center, float half)
{
cam.transform.position = new Vector3(center.x, center.y, -50f);
cam.orthographicSize = half;
cam.targetTexture = rt;
cam.Render();
var prev = RenderTexture.active;
RenderTexture.active = rt;
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply(false);
RenderTexture.active = prev;
var px = tex.GetPixels32();
int w = rt.width, h = rt.height, n = 0, x0 = w, y0 = h, x1 = -1, y1 = -1;
double sr = 0, sg = 0, sbb = 0;
byte br = (byte)(bg.r * 255f), bgc = (byte)(bg.g * 255f), bb = (byte)(bg.b * 255f);
for (int i = 0; i < px.Length; i++)
{
int d = Mathf.Abs(px[i].r - br) + Mathf.Abs(px[i].g - bgc) + Mathf.Abs(px[i].b - bb);
if (d < 24) continue;
int x = i % w, y = i / w;
n++;
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
sr += px[i].r; sg += px[i].g; sbb += px[i].b;
}
return new float[] { n, x0, y0, x1, y1, n > 0 ? (float)(sr / n) : 0, n > 0 ? (float)(sg / n) : 0, n > 0 ? (float)(sbb / n) : 0 };
}
var sb = new System.Text.StringBuilder();
var csv = new System.Text.StringBuilder();
int end = Mathf.Min(paths.Count, start + batch);
float[] tilts = { 0f, -90f };
for (int idx = start; idx < end; idx++)
{
string path = paths[idx];
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (prefab == null) { csv.AppendLine(idx + "," + path + ",0,0,0,0,0,0,0,0,0,0,0,0,"); continue; }
var sheet = new Texture2D(Cell * 2, Cell, TextureFormat.RGB24, false);
string line = "";
float dur = 0f, peak = 0f;
bool loop = false;
int nps = 0;
string rgb = "";
for (int ti = 0; ti < tilts.Length; ti++)
{
GameObject go = null;
try
{
go = TKS.SkillVfx.SpawnPrefab(prefab, origin, 1f, tilts[ti], null, 0, 1f, null);
SetLayer(go.transform);
var roots = Roots(go);
var all = go.GetComponentsInChildren<ParticleSystem>(true);
nps = all.Length;
if (ti == 0)
{
float best = 0.3f;
foreach (var ps in all)
{
var m = ps.main;
if (m.loop) loop = true;
float t = m.startDelay.constantMax + (m.loop ? 0f : m.duration) + m.startLifetime.constantMax;
if (!m.loop && t > best) best = t;
}
dur = Mathf.Min(best, 6f);
float[] fr = loop ? new[] { 0.3f, 0.6f, 1.0f, 1.5f, 2.2f } : new[] { 0.06f, 0.15f, 0.28f, 0.45f, 0.65f };
float bestN = -1f;
foreach (var f in fr)
{
float t = loop ? f : Mathf.Max(0.05f, f * dur);
Sim(roots, t);
var r = Shoot(rtLow, texLow, origin, 10f);
if (r[0] > bestN) { bestN = r[0]; peak = t; }
}
}
Sim(roots, peak);
var lo = Shoot(rtLow, texLow, origin, 10f);
float upp = 20f / 128f; // 픽셀당 월드 단위(낮은 해상도 · 반높이 10)
float w = 0f, h = 0f, cx = 0f, cy = 0f;
if (lo[0] > 0)
{
w = (lo[3] - lo[1] + 1) * upp;
h = (lo[4] - lo[2] + 1) * upp;
cx = ((lo[1] + lo[3] + 1) * 0.5f - 64f) * upp;
cy = ((lo[2] + lo[4] + 1) * 0.5f - 64f) * upp;
}
float half = Mathf.Clamp(Mathf.Max(w, h) * 0.6f, 0.4f, 10f);
var hi = Shoot(rtHi, texHi, origin + new Vector2(cx, cy), half);
if (hi[0] > 0 && half < 10f) // 맞춘 화면에서 다시 정밀 측정
{
float u2 = half * 2f / Cell;
float w2 = (hi[3] - hi[1] + 1) * u2, h2 = (hi[4] - hi[2] + 1) * u2;
cx += ((hi[1] + hi[3] + 1) * 0.5f - Cell * 0.5f) * u2;
cy += ((hi[2] + hi[4] + 1) * 0.5f - Cell * 0.5f) * u2;
w = w2; h = h2;
}
if (ti == 0) rgb = ((int)hi[5]).ToString("X2") + ((int)hi[6]).ToString("X2") + ((int)hi[7]).ToString("X2");
sheet.SetPixels32(ti * Cell, 0, Cell, Cell, texHi.GetPixels32());
line += "," + w.ToString("0.00") + "," + h.ToString("0.00") + "," + cx.ToString("0.00") + "," + cy.ToString("0.00");
}
catch (System.Exception e)
{
line += ",0,0,0,0";
sb.AppendLine("ERR " + idx + " " + path + " : " + e.Message);
}
finally
{
if (go != null) Object.DestroyImmediate(go);
}
}
sheet.Apply(false);
File.WriteAllBytes(outDir + "/" + idx.ToString("0000") + ".png", sheet.EncodeToPNG());
Object.DestroyImmediate(sheet);
csv.AppendLine(idx + "," + path.Replace(",", ";") + "," + dur.ToString("0.00") + "," + (loop ? 1 : 0) + "," + nps + "," + peak.ToString("0.00") + line + "," + rgb);
}
File.AppendAllText(csvPath, csv.ToString());
File.WriteAllText(statePath, end.ToString());
cam.targetTexture = null;
Object.DestroyImmediate(camGo);
rtLow.Release(); rtHi.Release();
Object.DestroyImmediate(rtLow); Object.DestroyImmediate(rtHi);
Object.DestroyImmediate(texLow); Object.DestroyImmediate(texHi);
sb.AppendLine("done " + start + " ~ " + (end - 1) + " / " + paths.Count);
return sb.ToString();