Project_WL/AgentScripts/WL762_ClipMotion.cs

101 lines
6.2 KiB
C#

// WL762_ClipMotion.cs — PD 지시 #762 실측 도구 (에디트 모드 · Assets 무수정)
// unity command run_script --file AgentScripts/WL762_ClipMotion.cs --entry WL762_ClipMotion.MeasureAll
// unity command run_script --file AgentScripts/WL762_ClipMotion.cs --entry WL762_ClipMotion.Measure --args '["Assets/Res_Addr/Animations/Animation/Paladin/attack.FBX", "Sword&Shield_Attack1"]'
// 🔴 PM 실측(2026-09-06): Humanoid 클립을 아바타 없는 FBX 인스턴스에 SampleAnimationClip 하면 전부 0 이 나온다(리타게팅 불가).
// → 이 버전은 **PC 프리팹(Ai01 · Animator + Humanoid Avatar)** 에 샘플링하고 `HumanBodyBones.Hips` 본을 읽는다.
// 그래도 0 이면(AnimationMode 가 Humanoid 를 지원하지 않는 경우) Play 중 `WL760_Seq` 류로 골반 본 위치를 프레임마다 기록해 실측할 것.
// 목적: "위치는 그대로인데 모션이 크다" 의 크기(골반의 캐릭터-전방 이동량)를 수치로 잡는다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
public static class WL762_ClipMotion
{
const string PcPrefab = "Assets/Res_Addr/PC/Ai01.prefab";
public static object MeasureAll()
{
var sb = new StringBuilder();
var targets = new[]
{
new[] { "Assets/Res_Addr/Animations/Animation/Paladin/attack.FBX", "Sword&Shield_Attack1" },
new[] { "Assets/Res_Addr/Animations/Animation/Paladin/Stander@Sword&Shield_Attack2.FBX", "Sword&Shield_Attack2" },
new[] { "Assets/Res_Addr/Animations/Animation/Paladin/Stander@Sword&Shield_Attack3.FBX", "Sword&Shield_Attack3" },
new[] { "Assets/Res_Addr/Animations/Animation/OneHand/Knight@Attack1_S.FBX", "Attack1" },
new[] { "Assets/Res_Addr/Animations/Animation/OneHand/Knight@Attack2_S.FBX", "Attack2" },
new[] { "Assets/Res_Addr/Animations/Animation/OneHand/Knight@Attack3_S.FBX", "Attack3" },
};
foreach (var t in targets) sb.AppendLine(Measure(t[0], t[1]).ToString());
return sb.ToString();
}
public static object Measure(string fbxPath, string clipName)
{
var all = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
var clip = all.OfType<AnimationClip>().FirstOrDefault(c => c.name == clipName && !c.name.StartsWith("__preview__"));
if (clip == null) return "clip not found: " + clipName + " in " + fbxPath + " (clips: " + string.Join(",", all.OfType<AnimationClip>().Select(c => c.name)) + ")";
var pcPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(PcPrefab);
if (pcPrefab == null) return "PC prefab not found " + PcPrefab;
var go = (GameObject)UnityEngine.Object.Instantiate(pcPrefab);
go.hideFlags = HideFlags.HideAndDontSave;
go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity;
var animator = go.GetComponentInChildren<Animator>();
if (animator == null) { UnityEngine.Object.DestroyImmediate(go); return "PC prefab has no Animator"; }
var animGo = animator.gameObject;
var hips = animator.isHuman ? animator.GetBoneTransform(HumanBodyBones.Hips) : null;
var lfoot = animator.isHuman ? animator.GetBoneTransform(HumanBodyBones.LeftFoot) : null;
var sb = new StringBuilder();
var importer = AssetImporter.GetAtPath(fbxPath) as ModelImporter;
string rm = "?";
if (importer != null)
{
var ca = importer.clipAnimations.FirstOrDefault(c => c.name == clipName);
if (ca != null) rm = "bakeXZ=" + ca.lockRootPositionXZ + " bakeY=" + ca.lockRootHeightY + " bakeRot=" + ca.lockRootRotation + " keepOrigXZ=" + ca.keepOriginalPositionXZ;
rm += " animType=" + importer.animationType;
}
sb.AppendLine("== " + clipName + " len=" + clip.length.ToString("F3") + "s hasRootCurves=" + clip.hasRootCurves + " hasMotionCurves=" + clip.hasMotionCurves + " humanMotion=" + clip.humanMotion
+ " " + rm + " sampledOn=" + animGo.name + " isHuman=" + animator.isHuman + " avatar=" + (animator.avatar != null ? animator.avatar.name : "null") + " hips=" + (hips != null ? hips.name : "?"));
if (hips == null) { UnityEngine.Object.DestroyImmediate(go); sb.AppendLine(" (no humanoid hips — cannot sample)"); return sb.ToString(); }
AnimationMode.StartAnimationMode();
try
{
AnimationMode.BeginSampling();
AnimationMode.SampleAnimationClip(animGo, clip, 0f);
AnimationMode.EndSampling();
Vector3 root0 = animGo.transform.position; Vector3 hip0 = hips.position; Vector3 foot0 = lfoot != null ? lfoot.position : Vector3.zero;
float maxHipZ = 0f, minHipZ = 0f, tMax = 0f, maxHipX = 0f;
var rows = new List<string>();
int n = 12;
for (int i = 0; i <= n; i++)
{
float t = clip.length * i / n;
AnimationMode.BeginSampling();
AnimationMode.SampleAnimationClip(animGo, clip, t);
AnimationMode.EndSampling();
Vector3 dr = animGo.transform.position - root0;
Vector3 dh = hips.position - hip0;
Vector3 df = lfoot != null ? lfoot.position - foot0 : Vector3.zero;
if (dh.z > maxHipZ) { maxHipZ = dh.z; tMax = t; }
if (dh.z < minHipZ) minHipZ = dh.z;
if (Mathf.Abs(dh.x) > Mathf.Abs(maxHipX)) maxHipX = dh.x;
rows.Add(" t=" + t.ToString("F2") + " root(z)=" + dr.z.ToString("F3") + " hips(dx,dy,dz)=(" + dh.x.ToString("F2") + "," + dh.y.ToString("F2") + "," + dh.z.ToString("F2") + ")"
+ " lfoot(dz)=" + df.z.ToString("F2"));
}
foreach (var r in rows) sb.AppendLine(r);
sb.AppendLine(" => hips forward travel: max +" + maxHipZ.ToString("F2") + " m at t=" + tMax.ToString("F2") + "s, min " + minHipZ.ToString("F2") + " m, lateral max " + maxHipX.ToString("F2") + " m, model scale=" + animGo.transform.lossyScale.x.ToString("F2"));
}
finally
{
AnimationMode.StopAnimationMode();
UnityEngine.Object.DestroyImmediate(go);
}
return sb.ToString();
}
}