ThreeKingdomsSlingshot/Tools/fx_style_preview.cs

164 lines
7.7 KiB
C#

using System.IO;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 화풍 층 미리보기(2026-11-11 · 에디터 정지 상태에서 값 맞추기) — 프리팹을 유닛 그림 옆에 띄워 ① 팩 원본 그대로 ② 화풍 층(TKS/FxResolve → TKS/FxStyle · fx_style.csv) 두 줄로 그린다.
// 일감 = <outDir>/style_job.txt · 한 줄 = 프리팹 경로|기울기|크기(유닛 키 배수)|순서(12 = 유닛 위 · -2 = 유닛 아래)|기준점(feet·body·head)|화면 반높이(월드)|색 바꾸기(#RRGGBB)|세기 배수|유닛 그림(Resources/Units/<이름> · 빈칸 guanyu)
// 결과 = <outDir>/style_NN.png(위 줄 = 원본 · 아래 줄 = 화풍 · 가로 = 시간 4점) · 실행: unity command --project-path . --timeout 300 script eval --file Tools/fx_style_preview.cs
string outDir = File.ReadAllText("Tools/fx_catalog_dir.txt").Trim();
var jobs = File.ReadAllLines(outDir + "/style_job.txt").Where(l => l.Trim().Length > 0 && !l.StartsWith("#")).ToList();
const int LFx = 31, LUnit = 30, Cell = 288, Cols = 4;
Color bg = new Color(197f / 255f, 155f / 255f, 82f / 255f, 1f);
Vector2 origin = new Vector2(3000f, 3000f);
float unitH = 1.5f;
TKS.FxStyleLayer.Reload();
bool styledOnly = File.Exists(outDir + "/style_only.txt"); // 있으면 화풍 줄만(한 줄 그림)
var camGo = new GameObject("FxStylePreviewCam") { hideFlags = HideFlags.HideAndDontSave };
var cam = camGo.AddComponent<Camera>();
cam.orthographic = true;
cam.nearClipPlane = 0.01f;
cam.farClipPlane = 200f;
cam.enabled = false;
cam.allowHDR = false;
cam.allowMSAA = false;
cam.transparencySortMode = TransparencySortMode.CustomAxis;
cam.transparencySortAxis = Vector3.up;
var resolveMat = new Material(Resources.Load<Shader>(TKS.FxStyleLayer.ResolveShader));
var styleMat = new Material(Resources.Load<Shader>(TKS.FxStyleLayer.StyleShader));
var rtRaw = new RenderTexture(Cell, Cell, 24);
var rtB = new RenderTexture(Cell, Cell, 24);
var rtW = new RenderTexture(Cell, Cell, 24);
var rtRes = new RenderTexture(Cell, Cell, 0, RenderTextureFormat.ARGBHalf);
var rtMask = new RenderTexture(Cell / 2, Cell / 2, 0);
var rtFin = new RenderTexture(Cell, Cell, 24);
var tex = new Texture2D(Cell, Cell, TextureFormat.RGB24, false);
void SetLayer(Transform t, int layer)
{
t.gameObject.layer = layer;
t.gameObject.hideFlags = HideFlags.HideAndDontSave;
foreach (Transform c in t) SetLayer(c, layer);
}
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 Render(RenderTexture rt, int mask, Color back, CameraClearFlags clear)
{
cam.targetTexture = rt;
cam.cullingMask = mask;
cam.clearFlags = clear;
cam.backgroundColor = back;
cam.Render();
}
Color32[] Read(RenderTexture rt)
{
var prev = RenderTexture.active;
RenderTexture.active = rt;
tex.ReadPixels(new Rect(0, 0, Cell, Cell), 0, 0);
tex.Apply(false);
RenderTexture.active = prev;
return tex.GetPixels32();
}
var sb = new System.Text.StringBuilder();
for (int ji = 0; ji < jobs.Count; ji++)
{
var c = jobs[ji].Split('|');
string path = c[0].Trim();
float tilt = c.Length > 1 && c[1].Trim().Length > 0 ? float.Parse(c[1], System.Globalization.CultureInfo.InvariantCulture) : 0f;
float scale = c.Length > 2 && c[2].Trim().Length > 0 ? float.Parse(c[2], System.Globalization.CultureInfo.InvariantCulture) : 1f;
int order = c.Length > 3 && c[3].Trim().Length > 0 ? int.Parse(c[3]) : 12;
string anchor = c.Length > 4 && c[4].Trim().Length > 0 ? c[4].Trim() : "body";
float half = c.Length > 5 && c[5].Trim().Length > 0 ? float.Parse(c[5], System.Globalization.CultureInfo.InvariantCulture) : 2.4f;
string hueHex = c.Length > 6 ? c[6].Trim() : "";
float alphaMul = c.Length > 7 && c[7].Trim().Length > 0 ? float.Parse(c[7], System.Globalization.CultureInfo.InvariantCulture) : 1f;
string unitArt = c.Length > 8 && c[8].Trim().Length > 0 ? c[8].Trim() : "guanyu";
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (prefab == null) prefab = Resources.Load<GameObject>("Fx/" + path);
if (prefab == null) { sb.AppendLine("없음 " + path); continue; }
// 유닛 그림(발 = 원점 · 키 1.5)
var sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Resources/Units/" + unitArt + "/front_idle/f000.png");
GameObject ugo = null;
if (sprite != null)
{
ugo = new GameObject("PreviewUnit");
var sr = ugo.AddComponent<SpriteRenderer>();
sr.sprite = sprite;
sr.sortingOrder = 0;
float k = unitH / sprite.bounds.size.y;
ugo.transform.localScale = new Vector3(k, k, 1f);
ugo.transform.position = new Vector3(origin.x, origin.y - sprite.bounds.min.y * k, 0f);
SetLayer(ugo.transform, LUnit);
}
Vector2 pos = origin + new Vector2(0f, anchor == "body" ? unitH * 0.45f : anchor == "head" ? unitH : 0f);
var go = TKS.SkillVfx.SpawnPrefab(prefab, pos, scale * unitH, tilt, null, order, 1f, null);
TKS.SkillVfx.Recolor(go, hueHex, 1f, alphaMul);
SetLayer(go.transform, LFx);
var roots = Roots(go);
bool loop = false;
float dur = 0.3f;
foreach (var ps in go.GetComponentsInChildren<ParticleSystem>(true))
{
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 > dur) dur = t;
}
dur = Mathf.Min(dur, 6f);
float[] fr = loop ? new[] { 0.3f, 0.7f, 1.2f, 1.9f } : new[] { 0.08f, 0.2f, 0.38f, 0.62f };
var sheet = new Texture2D(Cell * Cols, styledOnly ? Cell : Cell * 2, TextureFormat.RGB24, false);
cam.transform.position = new Vector3(origin.x, origin.y + unitH * 0.5f, -50f);
cam.orthographicSize = half;
TKS.FxStyleLayer.ApplyParams(resolveMat, styleMat, half * 2f / Cell);
for (int ti = 0; ti < Cols; ti++)
{
float t = loop ? fr[ti] : Mathf.Max(0.04f, fr[ti] * dur);
foreach (var ps in roots) ps.Simulate(t, true, true, true);
// ① 원본
if (!styledOnly)
{
Render(rtRaw, (1 << LFx) | (1 << LUnit), bg, CameraClearFlags.SolidColor);
sheet.SetPixels32(ti * Cell, Cell, Cell, Cell, Read(rtRaw));
}
// ② 화풍
Render(rtB, 1 << LFx, Color.black, CameraClearFlags.SolidColor);
Render(rtW, 1 << LFx, Color.white, CameraClearFlags.SolidColor);
TKS.FxStyleLayer.Resolve(rtB, rtW, rtRes, rtMask, resolveMat, half * 2f / Cell);
styleMat.SetTexture("_MaskTex", rtMask);
Render(rtFin, order < 0 ? 0 : (1 << LUnit), bg, CameraClearFlags.SolidColor);
Graphics.Blit(rtRes, rtFin, styleMat);
if (order < 0) Render(rtFin, 1 << LUnit, bg, CameraClearFlags.Nothing);
sheet.SetPixels32(ti * Cell, 0, Cell, Cell, Read(rtFin));
}
sheet.Apply(false);
File.WriteAllBytes(outDir + "/style_" + ji.ToString("00") + ".png", sheet.EncodeToPNG());
Object.DestroyImmediate(sheet);
Object.DestroyImmediate(go);
if (ugo != null) Object.DestroyImmediate(ugo);
sb.AppendLine(ji + " " + Path.GetFileNameWithoutExtension(path) + " dur " + dur.ToString("0.00") + (loop ? " loop" : ""));
}
cam.targetTexture = null;
Object.DestroyImmediate(camGo);
foreach (var rt in new[] { rtRaw, rtB, rtW, rtRes, rtMask, rtFin }) { rt.Release(); Object.DestroyImmediate(rt); }
Object.DestroyImmediate(tex);
Object.DestroyImmediate(resolveMat);
Object.DestroyImmediate(styleMat);
return sb.ToString();