ThreeKingdomsSlingshot/Tools/vfx_capture/strip.cs

57 lines
3.4 KiB
C#

using System.IO;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 시각별 띠 그림 2: strip_job2.txt 한 줄 = 프리팹 경로|반높이|기울기|시각들(,)|출력 이름|이동 속도(+X · 월드/초 · 0 = 제자리)|바탕(dark = 어두운 바탕)
string dir = @"C:/Users/sw/AppData/Local/Temp/tks_vfx_capture/strips";
const int Layer = 31; const int Cell = 256;
Color bg = new Color(197f / 255f, 155f / 255f, 82f / 255f, 1f);
Vector2 origin = new Vector2(3000f, 3000f);
var camGo = new GameObject("StripCam") { 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.allowHDR = false; cam.allowMSAA = false;
var rt = new RenderTexture(Cell, Cell, 24);
void SetLayer(Transform t) { t.gameObject.layer = Layer; t.gameObject.hideFlags = HideFlags.HideAndDontSave; foreach (Transform c in t) SetLayer(c); }
var log = new System.Text.StringBuilder();
foreach (var line in File.ReadAllLines(dir + "/../strip_job2.txt"))
{
var c = line.Split('|');
if (c.Length < 5) continue;
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(c[0]);
if (prefab == null) { log.AppendLine("MISSING " + c[0]); continue; }
float half = float.Parse(c[1]); float tilt = float.Parse(c[2]);
var times = c[3].Split(',').Select(float.Parse).ToArray();
var sheet = new Texture2D(Cell * times.Length, Cell, TextureFormat.RGB24, false);
var go = TKS.SkillVfx.SpawnPrefab(prefab, origin, 1f, tilt, null, 0, 1f, null);
SetLayer(go.transform);
var roots = 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) roots.Add(ps);
ps.useAutoRandomSeed = false; ps.randomSeed = 1234;
}
float speed = c.Length > 5 && c[5].Length > 0 ? float.Parse(c[5]) : 0f;
cam.backgroundColor = c.Length > 6 && c[6] == "dark" ? new Color(0.05f, 0.07f, 0.08f, 1f) : bg;
float tNow = 0f;
if (speed != 0f) foreach (var ps in roots) ps.Simulate(0f, true, true, true);
for (int i = 0; i < times.Length; i++)
{
if (speed == 0f) foreach (var ps in roots) ps.Simulate(times[i], true, true, true);
else while (tNow < times[i]) { go.transform.position += new Vector3(speed / 60f, 0f, 0f); foreach (var ps in roots) ps.Simulate(1f / 60f, true, false, true); tNow += 1f / 60f; }
cam.transform.position = new Vector3(go.transform.position.x - (speed != 0f ? half * 0.5f : 0f), origin.y + (tilt > -80f && speed == 0f ? half * 0.5f : 0f), -50f);
cam.orthographicSize = half; cam.targetTexture = rt; cam.Render();
var prev = RenderTexture.active; RenderTexture.active = rt;
sheet.ReadPixels(new Rect(0, 0, Cell, Cell), Cell * i, 0); RenderTexture.active = prev;
}
sheet.Apply(false);
File.WriteAllBytes(dir + "/" + c[4] + ".png", sheet.EncodeToPNG());
Object.DestroyImmediate(sheet); Object.DestroyImmediate(go);
log.AppendLine("ok " + c[4]);
}
cam.targetTexture = null; Object.DestroyImmediate(rt); Object.DestroyImmediate(camGo);
return log.ToString();