ThreeKingdomsSlingshot/Assets/Scripts/Runtime/Skills/SkillVfxFade.cs

103 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
namespace TKS
{
/// <summary>
/// 이펙트 자연 소멸(2026-11-10 PD 「슬링샷 이동이 종료될 때 자연스럽게 소멸」): 방출을 멈추고 살아 있는 입자의 불투명도를 dur 초 동안 0 으로 낮춘 뒤 지운다.
/// 수명이 무한인 층(투사체 머리·꼬리)도 사라지게 입자 색 알파를 직접 줄인다(모든 이식 셰이더가 입자 색 알파를 곱함). 값 = 표 skill_vfx.csv f_fade.
/// </summary>
public class SkillVfxFade : MonoBehaviour
{
float dur;
float t;
ParticleSystem[] systems;
ParticleSystem.Particle[] buf = new ParticleSystem.Particle[64];
/// <summary>2D 평면 이펙트(b_flat)의 스프라이트 층 — 처음 불투명도에서 0 으로(트레일은 방출만 멈추면 제 길이만큼 줄어듦).</summary>
SpriteRenderer[] sprites;
float[] spriteAlpha;
/// <summary>꼬리(트레일) — 남은 꼬리 전체의 불투명도를 함께 낮춤(IB 속도 꼬리 0.6초가 멈춘 뒤 늦게 사라지던 문제 · PD 2026-11-10).</summary>
TrailRenderer[] trails;
Gradient[] trailGrad;
/// <summary>wait 초 뒤에 흐려지기 시작(한 번 재생 층의 수명 끝 · f_life − f_fade · 2026-11-12 PD 서서 바닥 그을음 「0.5초~1초 내외만 재생시키고 소멸」).</summary>
float wait;
public static void Begin(GameObject go, float seconds, float wait = 0f)
{
if (go == null) return;
var f = go.GetComponent<SkillVfxFade>();
if (f != null) return;
f = go.AddComponent<SkillVfxFade>();
f.dur = Mathf.Max(0.05f, seconds);
f.wait = wait;
if (wait <= 0f) f.Init();
}
void Init()
{
var go = gameObject;
var f = this;
f.systems = go.GetComponentsInChildren<ParticleSystem>(true);
foreach (var ps in f.systems) ps.Stop(false, ParticleSystemStopBehavior.StopEmitting);
f.trails = go.GetComponentsInChildren<TrailRenderer>(true);
f.trailGrad = new Gradient[f.trails.Length];
for (int i = 0; i < f.trails.Length; i++)
{
f.trails[i].emitting = false;
f.trailGrad[i] = f.trails[i].colorGradient;
}
f.sprites = go.GetComponentsInChildren<SpriteRenderer>(true);
f.spriteAlpha = new float[f.sprites.Length];
for (int i = 0; i < f.sprites.Length; i++) f.spriteAlpha[i] = f.sprites[i].color.a;
}
void LateUpdate()
{
if (systems == null)
{
wait -= Time.deltaTime;
if (wait > 0f) return;
Init();
}
float prev = Mathf.Clamp01(1f - t / dur);
t += Time.deltaTime;
float now = Mathf.Clamp01(1f - t / dur);
float k = prev > 1e-4f ? now / prev : 0f; // 이번 프레임 알파 배율(누적하면 1 - t/dur)
foreach (var ps in systems)
{
if (ps == null) continue;
int n = ps.particleCount;
if (n == 0) continue;
if (buf.Length < n) buf = new ParticleSystem.Particle[Mathf.NextPowerOfTwo(n)];
n = ps.GetParticles(buf, n);
for (int i = 0; i < n; i++)
{
var c = buf[i].startColor;
c.a = (byte)Mathf.Clamp(Mathf.RoundToInt(c.a * k), 0, 255);
buf[i].startColor = c;
}
ps.SetParticles(buf, n);
}
for (int i = 0; i < trails.Length; i++)
{
if (trails[i] == null) continue;
var g = trailGrad[i];
var ak = g.alphaKeys;
for (int j = 0; j < ak.Length; j++) ak[j].alpha *= now;
var ng = new Gradient();
ng.SetKeys(g.colorKeys, ak);
ng.mode = g.mode;
trails[i].colorGradient = ng;
}
for (int i = 0; i < sprites.Length; i++)
{
if (sprites[i] == null) continue;
var c = sprites[i].color;
c.a = spriteAlpha[i] * now;
sprites[i].color = c;
}
if (t >= dur) Destroy(gameObject);
}
}
}