-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimEditor.cs
More file actions
225 lines (184 loc) · 6.32 KB
/
AnimEditor.cs
File metadata and controls
225 lines (184 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
class Sampler
{
public string name;
//public
int count;
float frameRate;
float length;
float dt;
Vector3[] pos = null;
Quaternion[] quat =null;
Matrix4x4[] transMat = null;
public Sampler( string name,float frameRate,float length)
{
this.name = name;
count = Mathf.RoundToInt(frameRate * length)+1;
this.frameRate= frameRate;
this.length = length;
this.dt = 1 / frameRate;
pos = new Vector3[count];
quat = new Quaternion[count];
transMat = new Matrix4x4[count];
}
public void finish()
{
for(int idx = 0; idx < count; idx++)
{
transMat[idx] = Matrix4x4.TRS( pos[idx],quat[idx],Vector3.one );
}
}
void calcCurve(AnimationCurve curve)
{
int count = Mathf.RoundToInt(frameRate * length) + 1;
float dt = 1 / frameRate;
float[] v = new float[count];
for (int idx = 0; idx < count; idx++)
{
v[idx] = curve.Evaluate(dt * idx);
}
return;
}
public void addCurve(string name, AnimationCurve curve)
{
bool propPos = name.IndexOf("m_LocalPosition")>= 0;
char axis = name[name.Length - 1];
if( propPos )
{
switch (axis)
{
case 'x':
for (int idx = 0; idx < count; idx++)
{
pos[idx].x = curve.Evaluate(dt * idx);
}
break;
case 'y':
for (int idx = 0; idx < count; idx++)
{
pos[idx].y = curve.Evaluate(dt * idx);
}
break;
case 'z':
for (int idx = 0; idx < count; idx++)
{
pos[idx].z = curve.Evaluate(dt * idx);
}
break;
default:
Debug.LogError("unregnoize property:"+name);
break;
}
return;
}
bool propRotation = name.IndexOf("m_LocalRotation")>= 0;
if(propRotation)
{
switch (axis)
{
case 'x':
for (int idx = 0; idx < count; idx++)
{
quat[idx].x = curve.Evaluate(dt * idx);
}
break;
case 'y':
for (int idx = 0; idx < count; idx++)
{
quat[idx].y = curve.Evaluate(dt * idx);
}
break;
case 'z':
for (int idx = 0; idx < count; idx++)
{
quat[idx].z = curve.Evaluate(dt * idx);
}
break;
case 'w':
for (int idx = 0; idx < count; idx++)
{
quat[idx].w = curve.Evaluate(dt * idx);
}
break;
default:
Debug.LogError("unregnoize property:"+name);
break;
}
return;
}
}
}
[CustomEditor(typeof(AnimInfo))]
[CanEditMultipleObjects]
public class AnimEditor : Editor
{
AnimInfo anim;
Vector2 pos = Vector2.zero;//new Vector2(100,100);
Vector2 pos0 = Vector2.zero;
Dictionary<string, Sampler> animMap= new Dictionary<string, Sampler>();
void calcCurve(AnimationCurve curve,float frameRate,float length)
{
int count = Mathf.RoundToInt(frameRate * length)+1;
float dt = 1 / frameRate;
float[] v = new float[count];
for(int idx = 0; idx < count;idx++)
{
v[idx] = curve.Evaluate(dt*idx);
}
return;
}
public override void OnInspectorGUI()
{
anim = (AnimInfo)target;
base.OnInspectorGUI();
//EditorGUILayout.PropertyField(anim);
//EditorGUILayout.LabelField("abcd");
if(anim.clip != null)
{
EditorGUILayout.LabelField("frameRate:"+anim.clip.frameRate+" ,length:"+anim.clip.length);
pos0 = EditorGUILayout.BeginScrollView(pos0,GUILayout.Height(200));
foreach( var binding in AnimationUtility.GetCurveBindings(anim.clip))
{
AnimationCurve curve = AnimationUtility.GetEditorCurve(anim.clip, binding);
string s = string.Format("{0}({1}):{2}, KeyLen:{3}",binding.path,binding.GetHashCode(), binding.propertyName, curve.keys.Length);
if (!animMap.ContainsKey(binding.path))
{
animMap.Add(binding.path, new Sampler(binding.path,anim.clip.frameRate,anim.clip.length));
}
animMap[binding.path].addCurve(binding.propertyName, curve);
/*
if(binding.propertyName == "m_LocalPosition.x")
{
calcCurve(curve, anim.clip.frameRate, anim.clip.length);
}
*/
//EditorGUILayout.LabelField( binding.propertyName + ", Keys: " + curve.keys.Length);
EditorGUILayout.LabelField(s);
}
foreach(var kv in animMap)
{
kv.Value.finish();
}
EditorGUILayout.EndScrollView();
}
EditorGUILayout.Separator();
if(anim.obj != null)
{
var smr = anim.obj.GetComponentInChildren<SkinnedMeshRenderer>();
var bones = smr.bones;
var root = smr.rootBone;
pos = EditorGUILayout.BeginScrollView(pos);
for(int idx = 0; idx < bones.Length; idx++)
{
string s = string.Format("{0}->({7}).pos[{1},{2},{3}], localPos[{4},{5},{6}]",bones[idx].name,
bones[idx].position.x,bones[idx].position.y,bones[idx].position.z,
bones[idx].localPosition.x,bones[idx].localPosition.y,bones[idx].localPosition.z,bones[idx].name.GetHashCode());
EditorGUILayout.LabelField(s);
}
EditorGUILayout.EndScrollView();
}
}
}