-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationCompress.cs
More file actions
460 lines (415 loc) · 18.6 KB
/
Copy pathAnimationCompress.cs
File metadata and controls
460 lines (415 loc) · 18.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace XXXEditor
{
public static class SETTING
{
public static class FILTER
{
//缩放属性剔除(变化误差范围 x 分之一)
public readonly static int ERR_RANGE_SCALE_PROPERTY = 1000;
//相同关键帧剔(除误差范围 X 分之一)
public readonly static int ERR_RANGE_SAME_FRAME = 10000;
}
//精度压缩(根据曲线变化坡度压缩,坡度越大精度越高,坡度越小精度越小)
public static class ACCURACY
{
//精度1级 坡度阀值
public readonly static float THRESHOLD1 = 0;
//精度2级 坡度阀值
public readonly static float THRESHOLD2 = 0.1f;
//精度1级(小数点后3位)
public readonly static string LEVEL1 = "f3";
//精度2级(小数点后4位)
public readonly static string LEVEL2 = "f4";
//精度3级(小数点后5位)
public readonly static string LEVEL3 = "f5";
}
}
public class CompressOpt
{
public AnimationClip AnimClip { private set; get; }
public string AnimClipPath { private set; get; }
private HashSet<string> mScaleBonePaths;
private Dictionary<string, float> mGradientVals;
public CompressOpt(AnimationClip animClip, string animClipPath)
{
AnimClip = animClip;
AnimClipPath = animClipPath;
mGradientVals = new Dictionary<string, float>();
}
public void SetScaleBonePaths(HashSet<string> scaleBonePaths)
{
mScaleBonePaths = scaleBonePaths;
}
private bool Approximately(Keyframe a, Keyframe b)
{
return Mathf.Abs(a.value - b.value) * SETTING.FILTER.ERR_RANGE_SAME_FRAME < 1f &&
Mathf.Abs(a.inTangent - b.inTangent) * SETTING.FILTER.ERR_RANGE_SAME_FRAME < 1f &&
Mathf.Abs(a.outTangent - b.outTangent) * SETTING.FILTER.ERR_RANGE_SAME_FRAME < 1f &&
Mathf.Abs(a.inWeight - b.inWeight) * SETTING.FILTER.ERR_RANGE_SAME_FRAME < 1f &&
Mathf.Abs(a.outWeight - b.outWeight) * SETTING.FILTER.ERR_RANGE_SAME_FRAME < 1f;
}
private string GetCurveKey(string path, string propertyName)
{
var splits = propertyName.Split('.');
var name = splits[0];
return string.Format("{0}/{1}", path, name);
}
//获取曲线坡度
private float GetCurveThreshold(string path, string propertyName)
{
var curveKey = GetCurveKey(path, propertyName);
float threshold = 0;
mGradientVals.TryGetValue(curveKey, out threshold);
return threshold;
}
//设置曲线坡度
private void SetCurveThreshold(string path, string propertyName, float threshold)
{
var curveKey = GetCurveKey(path, propertyName);
if (!mGradientVals.ContainsKey(curveKey))
mGradientVals.Add(curveKey, threshold);
else
mGradientVals[curveKey] = threshold;
}
//获取曲线压缩精度
private string GetCompressAccuracy(string path, string propertyName)
{
var threshold = GetCurveThreshold(path, propertyName);
if (threshold <= SETTING.ACCURACY.THRESHOLD1)
return SETTING.ACCURACY.LEVEL1;
else if (threshold <= SETTING.ACCURACY.THRESHOLD2)
return SETTING.ACCURACY.LEVEL2;
return SETTING.ACCURACY.LEVEL3;
}
public void Compress()
{
if (AnimClip != null)
{
var curveBindings = AnimationUtility.GetCurveBindings(AnimClip);
for (int i = 0; i < curveBindings.Length; i++)
{
EditorCurveBinding curveBinding = curveBindings[i];
float threshold = GetCurveThreshold(curveBinding.path, curveBinding.propertyName);
string name = curveBinding.propertyName.ToLower();
var curve = AnimationUtility.GetEditorCurve(AnimClip, curveBinding);
var keys = curve.keys;
if (name.Contains("scale"))
{
//优化scale曲线
if (!mScaleBonePaths.Contains(curveBinding.path))
{
AnimationUtility.SetEditorCurve(AnimClip, curveBinding, null);
continue;
}
}
float bottomVal = 999999;
float topVal = -999999;
//优化采样点数量
List<Keyframe> newFrames = new List<Keyframe>();
if (keys.Length > 0)
{
newFrames.Add(keys[0]);
var lastSameFrameIndex = 0;
var comparerFrameIndex = 0;
for (int j = 1; j < keys.Length; j++)
{
var curFrame = keys[j];
var comparerFrame = keys[comparerFrameIndex];
if (Approximately(curFrame, comparerFrame))
{
lastSameFrameIndex = j;
}
else
{
if (lastSameFrameIndex > comparerFrameIndex)
newFrames.Add(keys[lastSameFrameIndex]);
newFrames.Add(keys[j]);
comparerFrameIndex = j;
}
bottomVal = Mathf.Min(bottomVal, keys[j].value);
topVal = Mathf.Max(topVal, keys[j].value);
}
if (newFrames.Count == 1)
newFrames.Add(keys[keys.Length - 1]);//最少两帧
if (newFrames.Count != keys.Length)
{
curve.keys = newFrames.ToArray();
//Debug.LogFormat("{0}=>{1}", keys.Length, newFrames.Count);
AnimationUtility.SetEditorCurve(AnimClip, curveBinding, curve);
}
}
SetCurveThreshold(curveBinding.path, curveBinding.propertyName, Mathf.Max(threshold, topVal - bottomVal));
}
//优化精度
AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(AnimClip);
if (curves != null && curves.Length > 0)
{
for (int i = 0; i < curves.Length; i++)
{
AnimationClipCurveData curveDate = curves[i];
if (curveDate.curve == null || curveDate.curve.keys == null)
continue;
string accuracy = GetCompressAccuracy(curveDate.path, curveDate.propertyName);
Keyframe[] keyFrames = curveDate.curve.keys;
for (int j = 0; j < keyFrames.Length; j++)
{
Keyframe key = keyFrames[j];
key.value = float.Parse(key.value.ToString(accuracy));
//切线固定精度
key.inTangent = float.Parse(key.inTangent.ToString("f3"));
key.outTangent = float.Parse(key.outTangent.ToString("f3"));
keyFrames[j] = key;
}
curveDate.curve.keys = keyFrames;
AnimClip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
}
}
}
}
}
public class AnimClipDirectory
{
public string Path { get; }
public List<string> AnimClipPaths { get; private set; }
public List<CompressOpt> CompressOpts { get; private set; }
public AnimClipDirectory(string directory)
{
Path = directory;
AnimClipPaths = new List<string>();
CompressOpts = new List<CompressOpt>();
}
public void AddAnimClipPath(string animClipPath)
{
AnimClipPaths.Add(animClipPath);
}
//分析被缩放的所有骨骼路径
public void Analyse()
{
HashSet<string> scaleBonePaths = new HashSet<string>();
for (int i = 0; i < AnimClipPaths.Count; i++)
{
var assetPath = AnimClipPaths[i];
AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(assetPath);
CompressOpts.Add(new CompressOpt(clip, assetPath));
AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(clip);
if (curves != null && curves.Length > 0)
{
for (int j = 0; j < curves.Length; j++)
{
string name = curves[j].propertyName.ToLower();
if (name.Contains("scale"))
{
AnimationClipCurveData curveDate = curves[j];
if (curveDate.curve == null || curveDate.curve.keys == null)
continue;
var keyFrames = curveDate.curve.keys;
bool isScaleChanged = false;
if (keyFrames.Length > 0)
{
var frist = keyFrames[0].value;
if (Mathf.Abs(frist - 1f) * SETTING.FILTER.ERR_RANGE_SCALE_PROPERTY > 1f) //如果第一帧大小变了
isScaleChanged = true;
else
{
for (int k = 1; k < keyFrames.Length; k++)
{
if (Mathf.Abs(keyFrames[k].value - frist) * SETTING.FILTER.ERR_RANGE_SCALE_PROPERTY > 1f) //如果差异超过千分之一,则不可删除
{
isScaleChanged = true;
break;
}
}
}
}
if (isScaleChanged)
scaleBonePaths.Add(curves[j].path);
}
}
}
}
for (int i = 0; i < CompressOpts.Count; i++)
{
CompressOpts[i].SetScaleBonePaths(scaleBonePaths);
}
}
}
public class AnimClipCompressTool
{
private enum ProcessType
{
Analyse,
Compress,
Finish,
}
private static ProcessType mCurProcess;
private static List<AnimClipDirectory> mAnimClipDirectoryList;
private static List<CompressOpt> mCompressOptList;
private static int mIndex = 0;
[MenuItem("Assets/动画资源处理", false, 1006)]
public static void Optimize()
{
Dictionary<string, AnimClipDirectory> animClipPaths = new Dictionary<string, AnimClipDirectory>();
string[] assetGUIDs = Selection.assetGUIDs;
if (assetGUIDs.Length == 0)
{
Debug.LogError("请选择文件夹或者文件");
return;
}
if (assetGUIDs != null && assetGUIDs.Length > 0)
{
for (int i = 0; i < assetGUIDs.Length; i++)
{
GetAllAnimClipPaths(assetGUIDs[i], ref animClipPaths);
}
}
mAnimClipDirectoryList = new List<AnimClipDirectory>();
mAnimClipDirectoryList.AddRange(animClipPaths.Values);
mCompressOptList = new List<CompressOpt>();
mIndex = 0;
mCurProcess = ProcessType.Analyse;
if (mAnimClipDirectoryList.Count > 0)
EditorApplication.update = Update;
else
EditorUtility.DisplayDialog("Tips", "can not found AnimationClip file!", "ok");
}
public static void Optimize(string[] guids)
{
Dictionary<string, AnimClipDirectory> animClipPaths = new Dictionary<string, AnimClipDirectory>();
string[] assetGUIDs = guids;
if (assetGUIDs.Length == 0)
{
Debug.LogError("请选择文件夹或者文件");
return;
}
if (assetGUIDs != null && assetGUIDs.Length > 0)
{
for (int i = 0; i < assetGUIDs.Length; i++)
{
GetAllAnimClipPaths(assetGUIDs[i], ref animClipPaths);
}
}
mAnimClipDirectoryList = new List<AnimClipDirectory>();
mAnimClipDirectoryList.AddRange(animClipPaths.Values);
mCompressOptList = new List<CompressOpt>();
mIndex = 0;
mCurProcess = ProcessType.Analyse;
if (mAnimClipDirectoryList.Count > 0)
EditorApplication.update = Update;
else
EditorUtility.DisplayDialog("Tips", "can not found AnimationClip file!", "ok");
}
public static bool IsFolderByGUID(string guid,out string floderPath)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
floderPath = path;
return AssetDatabase.IsValidFolder(path);
}
private static void Update()
{
if (mCurProcess == ProcessType.Analyse)
{
AnimClipDirectory animClipDirectory = mAnimClipDirectoryList[mIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format("正在读取AnimationClip文件夹信息[{0}/{1}])", mIndex, mAnimClipDirectoryList.Count), animClipDirectory.Path, (float)mIndex / (float)mAnimClipDirectoryList.Count);
if (isCancel)
mCurProcess = ProcessType.Compress;
else
{
animClipDirectory.Analyse();
mIndex++;
if (mIndex >= mAnimClipDirectoryList.Count)
{
for (int i = 0; i < mAnimClipDirectoryList.Count; i++)
mCompressOptList.AddRange(mAnimClipDirectoryList[i].CompressOpts);
if (mCompressOptList.Count > 0)
mCurProcess = ProcessType.Compress;
else
mCurProcess = ProcessType.Finish;
mIndex = 0;
}
}
}
else if (mCurProcess == ProcessType.Compress)
{
CompressOpt compressOpt = mCompressOptList[mIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format("正在压缩AnimationClip文件[{0}/{1}]", mIndex, mCompressOptList.Count), compressOpt.AnimClipPath, (float)mIndex / (float)mCompressOptList.Count);
if (isCancel)
mCurProcess = ProcessType.Finish;
else
{
compressOpt.Compress();
mIndex++;
if (mIndex >= mCompressOptList.Count)
mCurProcess = ProcessType.Finish;
}
}
else if (mCurProcess == ProcessType.Finish)
{
mAnimClipDirectoryList = null;
mCompressOptList = null;
mIndex = 0;
EditorUtility.ClearProgressBar();
Resources.UnloadUnusedAssets();
GC.Collect();
AssetDatabase.SaveAssets();
EditorApplication.update = null;
if (AnimationSizeCheck.instance)
AnimationSizeCheck.instance.Refresh();
}
}
private static void GetAllAnimClipPaths(string assetGuid, ref Dictionary<string, AnimClipDirectory> animClipPaths)
{
string floderPath = "";
if (IsFolderByGUID(assetGuid,out floderPath))
{
string[] paths = System.IO.Directory.GetFiles(floderPath,"*.*",SearchOption.AllDirectories);
for (int i = 0; i < paths.Length; i++)
{
var path = paths[i];
if (IsDirectory(path))
{
GetAllAnimClipPaths(path, ref animClipPaths);
}
else
{
if (path.EndsWith(".anim"))
{
var directoryPath = GetFileDirectoryPath(path);
if (!animClipPaths.ContainsKey(directoryPath))
animClipPaths.Add(directoryPath, new AnimClipDirectory(directoryPath));
animClipPaths[directoryPath].AddAnimClipPath(path);
}
}
}
}
else
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
if (assetPath.EndsWith(".anim"))
{
var directoryPath = GetFileDirectoryPath(assetPath);
if (!animClipPaths.ContainsKey(directoryPath))
animClipPaths.Add(directoryPath, new AnimClipDirectory(directoryPath));
animClipPaths[directoryPath].AddAnimClipPath(assetPath);
}
}
}
private static bool IsDirectory(string assetPath)
{
Debug.Log(System.IO.File.GetAttributes(assetPath));
return System.IO.File.GetAttributes(assetPath) == System.IO.FileAttributes.Directory;
}
private static string GetFileDirectoryPath(string filePath)
{
var fileName = System.IO.Path.GetFileName(filePath);
var directoryPath = filePath.Replace(fileName, "");
return directoryPath;
}
}
}