回答編集履歴
1
OnEnabledでのruntimeAnimatorController無効化が抜けていたため追加、改行を調整
test
CHANGED
@@ -72,444 +72,434 @@
|
|
72
72
|
|
73
73
|
{Equation.Linear, t => t},
|
74
74
|
|
75
|
+
{Equation.EaseInOutQuad, t => t < 0.5f ? (t *= 2.0f) * t * 0.5f : 1.0f - ((t = (1.0f - t) * 2.0f) * t * 0.5f)},
|
76
|
+
|
77
|
+
{Equation.EaseOutCirc, t => Mathf.Sqrt(1.0f - ((t -= 1.0f) * t))},
|
78
|
+
|
79
|
+
{Equation.EaseInOutBounce, t => t < 0.5f ? (1.0f - EaseOutBounce(1.0f - (t * 2.0f))) * 0.5f : (EaseOutBounce((t * 2.0f) - 1.0f) * 0.5f) + 0.5f}
|
80
|
+
|
81
|
+
};
|
82
|
+
|
83
|
+
private static float EaseOutBounce(float v)
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
if (v < (1 / 2.75f))
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
return 7.5625f * v * v;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
if (v < (2 / 2.75f))
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
return (7.5625f * (v -= 1.5f / 2.75f) * v) + 0.75f;
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
if (v < (2.5 / 2.75))
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
return (7.5625f * (v -= 2.25f / 2.75f) * v) + 0.9375f;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
return (7.5625f * (v -= 2.625f / 2.75f) * v) + 0.984375f;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
// どのトランジションに対してどのイージングを適用するかを記憶しておくためのフィールド
|
124
|
+
|
125
|
+
// 本来ならDictionaryのようなコレクションを使いたいところではあるが、シリアライズするために
|
126
|
+
|
127
|
+
// キー(トランジションまたはステートのフルパスハッシュ)と値をそれぞれListとして保持する
|
128
|
+
|
129
|
+
// リストは2組用意し、前者はステート間トランジション、後者はAny Stateからの場合のような
|
130
|
+
|
131
|
+
// ソースステートを持たないトランジションに使う
|
132
|
+
|
133
|
+
// ソースなしトランジションでもAnimatorTransitionInfo.fullPathHashはそれっぽい値を返すので、
|
134
|
+
|
135
|
+
// ソースなしの場合のハッシュの元となる文字列の作り方がわかればリストは1組ですむはずなのだが...
|
136
|
+
|
137
|
+
// リファレンスには載っておらず、勘で試してもハズレばかりであきらめた
|
138
|
+
|
139
|
+
[SerializeField] private List<int> equationKeys = new List<int>();
|
140
|
+
|
141
|
+
[SerializeField] private List<Equation> equationValues = new List<Equation>();
|
142
|
+
|
143
|
+
[SerializeField] private List<int> equationWithoutSourceKeys = new List<int>();
|
144
|
+
|
145
|
+
[SerializeField] private List<Equation> equationWithoutSourceValues = new List<Equation>();
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
private PlayableGraph animationGraph;
|
150
|
+
|
151
|
+
private Animator animator;
|
152
|
+
|
153
|
+
private RuntimeAnimatorController runtimeAnimatorController;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// ハッシュからEquationを引っ張ってくるのに使うヘルパーメソッド
|
158
|
+
|
159
|
+
// TransitionModifierEditorが使う
|
160
|
+
|
161
|
+
public Equation GetEquation(int hash, bool hasSource, out int index)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
index = (hasSource ? this.equationKeys : this.equationWithoutSourceKeys).FindIndex(i => i == hash);
|
166
|
+
|
167
|
+
return index < 0 ? Equation.Linear : (hasSource ? this.equationValues : this.equationWithoutSourceValues)[index];
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
private void Start()
|
174
|
+
|
175
|
+
{
|
176
|
+
|
177
|
+
// まずPlayableGraphを新規作成する
|
178
|
+
|
179
|
+
this.animator = this.GetComponent<Animator>();
|
180
|
+
|
181
|
+
this.runtimeAnimatorController = this.animator.runtimeAnimatorController;
|
182
|
+
|
183
|
+
this.animationGraph = PlayableGraph.Create("Animation");
|
184
|
+
|
185
|
+
this.animationGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
|
186
|
+
|
187
|
+
var animatorOutput = AnimationPlayableOutput.Create(this.animationGraph, "AnimatorOutput", this.animator);
|
188
|
+
|
189
|
+
var animatorController = AnimatorControllerPlayable.Create(
|
190
|
+
|
191
|
+
this.animationGraph,
|
192
|
+
|
193
|
+
this.runtimeAnimatorController);
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// ハッシュ値とイージングファンクションを対応づけたDictionaryを作り...
|
198
|
+
|
199
|
+
var transitionFunctions = new Dictionary<int, Func<float, float>>();
|
200
|
+
|
201
|
+
foreach (var (key, value) in this.equationKeys.Zip(this.equationValues, (key, value) => (key, value)))
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
transitionFunctions.Add(key, TransitionFunctions[value]);
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
var transitionWithoutSourceFunctions = new Dictionary<int, Func<float, float>>();
|
210
|
+
|
211
|
+
foreach (var (key, value) in this.equationWithoutSourceKeys.Zip(
|
212
|
+
|
213
|
+
this.equationWithoutSourceValues,
|
214
|
+
|
215
|
+
(key, value) => (key, value)))
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
transitionWithoutSourceFunctions.Add(key, TransitionFunctions[value]);
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// メインの処理を担当するTransitionModifierPlayableを作成、必要なデータを投入する
|
226
|
+
|
227
|
+
var modifierPlayable = ScriptPlayable<TransitionModifierPlayable>.Create(this.animationGraph, 1);
|
228
|
+
|
229
|
+
modifierPlayable.GetBehaviour().Initialize(
|
230
|
+
|
231
|
+
modifierPlayable,
|
232
|
+
|
233
|
+
animatorController,
|
234
|
+
|
235
|
+
this.animator,
|
236
|
+
|
237
|
+
transitionFunctions,
|
238
|
+
|
239
|
+
transitionWithoutSourceFunctions);
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
// AnimatorのruntimeAnimatorControllerはnullにして黙らせ、代わりに
|
244
|
+
|
245
|
+
// できあがったPlayableGraphを起動してアニメーションを行わせる
|
246
|
+
|
247
|
+
animatorOutput.SetSourcePlayable(modifierPlayable);
|
248
|
+
|
249
|
+
this.animator.runtimeAnimatorController = null;
|
250
|
+
|
251
|
+
this.animationGraph.Play();
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
private void OnEnable()
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
this.runtimeAnimatorController = this.animator.runtimeAnimatorController;
|
262
|
+
|
263
|
+
this.animator.runtimeAnimatorController = null;
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
if (this.animationGraph.IsValid())
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
this.animationGraph.Play();
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
private void OnDisable()
|
280
|
+
|
281
|
+
{
|
282
|
+
|
283
|
+
if (this.animationGraph.IsValid())
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
this.animationGraph.Stop();
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
if ((this.animator != null) && (this.runtimeAnimatorController != null))
|
294
|
+
|
295
|
+
{
|
296
|
+
|
297
|
+
this.animator.runtimeAnimatorController = this.runtimeAnimatorController;
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
private void OnDestroy()
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
if (this.animationGraph.IsValid())
|
310
|
+
|
311
|
+
{
|
312
|
+
|
313
|
+
// 生成したPlayableGraphは明示的に破棄しないとクラッシュするらしい...!
|
314
|
+
|
315
|
+
// http://tsubakit1.hateblo.jp/entry/2017/07/30/032008#%E6%B3%A8%E6%84%8F%E7%82%B9
|
316
|
+
|
317
|
+
this.animationGraph.Destroy();
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
private class TransitionModifierPlayable : PlayableBehaviour
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
private Dictionary<int, Func<float, float>> transitionFunctions;
|
330
|
+
|
331
|
+
private Dictionary<int, Func<float, float>> transitionWithoutSourceFunctions;
|
332
|
+
|
333
|
+
private Animator animator;
|
334
|
+
|
335
|
+
private PlayableGraph animationGraph;
|
336
|
+
|
337
|
+
private AnimationMixerPlayable referenceMixer;
|
338
|
+
|
339
|
+
private AnimationMixerPlayable actualMixer;
|
340
|
+
|
341
|
+
private AnimationClipPlayable mirrorClip0;
|
342
|
+
|
343
|
+
private AnimationClipPlayable mirrorClip1;
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
public void Initialize(
|
348
|
+
|
349
|
+
ScriptPlayable<TransitionModifierPlayable> self,
|
350
|
+
|
351
|
+
AnimatorControllerPlayable animatorController,
|
352
|
+
|
353
|
+
Animator animator,
|
354
|
+
|
355
|
+
Dictionary<int, Func<float, float>> transitionFunctions,
|
356
|
+
|
357
|
+
Dictionary<int, Func<float, float>> transitionWithoutSourceFunctions)
|
358
|
+
|
359
|
+
{
|
360
|
+
|
361
|
+
// AnimatorControllerによるノードツリー内の、トランジション前後のモーション混合を
|
362
|
+
|
363
|
+
// 行っているミキサーを監視し、そのブレンド比をイージングファンクションで変化させ、
|
364
|
+
|
365
|
+
// それを別途用意したミキサーに適用する作戦
|
366
|
+
|
367
|
+
// AnimatorControllerのノードツリーは編集不許可のようなので、こんな回りくどい方法をとった
|
368
|
+
|
369
|
+
this.transitionFunctions = transitionFunctions;
|
370
|
+
|
371
|
+
this.transitionWithoutSourceFunctions = transitionWithoutSourceFunctions;
|
372
|
+
|
373
|
+
this.animator = animator;
|
374
|
+
|
375
|
+
this.referenceMixer = (AnimationMixerPlayable)animatorController.GetInput(0).GetInput(0);
|
376
|
+
|
377
|
+
this.animationGraph = this.referenceMixer.GetGraph();
|
378
|
+
|
379
|
+
var referenceClip0 = (AnimationClipPlayable)this.referenceMixer.GetInput(0).GetInput(0);
|
380
|
+
|
381
|
+
var referenceClip1 = (AnimationClipPlayable)this.referenceMixer.GetInput(1).GetInput(0);
|
382
|
+
|
383
|
+
this.mirrorClip0 = AnimationClipPlayable.Create(this.animationGraph, referenceClip0.GetAnimationClip());
|
384
|
+
|
385
|
+
this.mirrorClip1 = AnimationClipPlayable.Create(this.animationGraph, referenceClip1.GetAnimationClip());
|
386
|
+
|
387
|
+
this.actualMixer = AnimationMixerPlayable.Create(this.animationGraph, 3);
|
388
|
+
|
389
|
+
this.actualMixer.ConnectInput(0, this.mirrorClip0, 0, 1.0f);
|
390
|
+
|
391
|
+
this.actualMixer.ConnectInput(1, this.mirrorClip1, 0);
|
392
|
+
|
393
|
+
this.actualMixer.ConnectInput(2, animatorController, 0);
|
394
|
+
|
395
|
+
self.ConnectInput(0, this.actualMixer, 0, 1.0f);
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
public override void PrepareFrame(Playable playable, FrameData info)
|
402
|
+
|
403
|
+
{
|
404
|
+
|
405
|
+
// アニメーションクリップが変わっていたらノードを差し替え、その後再生位置を同期させる
|
406
|
+
|
407
|
+
this.UpdateMirror(
|
408
|
+
|
409
|
+
(AnimationClipPlayable)this.referenceMixer.GetInput(0).GetInput(0),
|
410
|
+
|
411
|
+
ref this.mirrorClip0,
|
412
|
+
|
413
|
+
0);
|
414
|
+
|
415
|
+
this.UpdateMirror(
|
416
|
+
|
417
|
+
(AnimationClipPlayable)this.referenceMixer.GetInput(1).GetInput(0),
|
418
|
+
|
419
|
+
ref this.mirrorClip1,
|
420
|
+
|
421
|
+
1);
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
// 遷移中ならミキサーの混合比を同期させる
|
426
|
+
|
427
|
+
// このときイージング処理を施し緩急をつける
|
428
|
+
|
429
|
+
var transitionInfo = this.animator.GetAnimatorTransitionInfo(0);
|
430
|
+
|
431
|
+
var functions = transitionInfo.anyState
|
432
|
+
|
433
|
+
? this.transitionWithoutSourceFunctions
|
434
|
+
|
435
|
+
: this.transitionFunctions;
|
436
|
+
|
437
|
+
var transitionHash = transitionInfo.anyState
|
438
|
+
|
439
|
+
? this.animator.GetNextAnimatorStateInfo(0).fullPathHash
|
440
|
+
|
441
|
+
: transitionInfo.fullPathHash;
|
442
|
+
|
443
|
+
if (transitionHash != 0)
|
444
|
+
|
75
445
|
{
|
76
446
|
|
447
|
+
var weight0 = this.referenceMixer.GetInputWeight(0);
|
448
|
+
|
449
|
+
var weight1 = this.referenceMixer.GetInputWeight(1);
|
450
|
+
|
451
|
+
var weightSum = weight0 + weight1;
|
452
|
+
|
77
|
-
|
453
|
+
if (weightSum > 0.0f)
|
454
|
+
|
78
|
-
|
455
|
+
{
|
456
|
+
|
457
|
+
var normalizedWeight1 = weight1 / weightSum;
|
458
|
+
|
79
|
-
|
459
|
+
if (functions.TryGetValue(transitionHash, out var function))
|
460
|
+
|
80
|
-
|
461
|
+
{
|
462
|
+
|
463
|
+
normalizedWeight1 = function(normalizedWeight1);
|
464
|
+
|
81
|
-
}
|
465
|
+
}
|
466
|
+
|
467
|
+
|
468
|
+
|
82
|
-
|
469
|
+
var normalizedWeight0 = 1.0f - normalizedWeight1;
|
470
|
+
|
83
|
-
|
471
|
+
this.actualMixer.SetInputWeight(0, normalizedWeight0);
|
472
|
+
|
473
|
+
this.actualMixer.SetInputWeight(1, normalizedWeight1);
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
base.PrepareFrame(playable, info);
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
private void UpdateMirror(AnimationClipPlayable reference, ref AnimationClipPlayable mirror, int port)
|
488
|
+
|
489
|
+
{
|
490
|
+
|
491
|
+
if (mirror.GetAnimationClip() != reference.GetAnimationClip())
|
84
492
|
|
85
493
|
{
|
86
494
|
|
87
|
-
|
495
|
+
mirror.Destroy();
|
88
|
-
|
89
|
-
|
496
|
+
|
90
|
-
|
91
|
-
|
497
|
+
mirror = AnimationClipPlayable.Create(this.animationGraph, reference.GetAnimationClip());
|
92
|
-
|
498
|
+
|
93
|
-
|
499
|
+
this.actualMixer.ConnectInput(port, mirror, 0, port == 0 ? 1.0f : 0.0f);
|
94
500
|
|
95
501
|
}
|
96
502
|
|
97
|
-
};
|
98
|
-
|
99
|
-
private static float EaseOutBounce(float v)
|
100
|
-
|
101
|
-
{
|
102
|
-
|
103
|
-
if (v < (1 / 2.75f))
|
104
|
-
|
105
|
-
{
|
106
|
-
|
107
|
-
return 7.5625f * v * v;
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
if (v < (2 / 2.75f))
|
114
|
-
|
115
|
-
{
|
116
|
-
|
117
|
-
return (7.5625f * (v -= 1.5f / 2.75f) * v) + 0.75f;
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
if (v < (2.5 / 2.75))
|
124
|
-
|
125
|
-
{
|
126
|
-
|
127
|
-
return (7.5625f * (v -= 2.25f / 2.75f) * v) + 0.9375f;
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
return (7.5625f * (v -= 2.625f / 2.75f) * v) + 0.984375f;
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
// どのトランジションに対してどのイージングを適用するかを記憶しておくためのフィールド
|
140
|
-
|
141
|
-
// 本来ならDictionaryのようなコレクションを使いたいところではあるが、シリアライズするために
|
142
|
-
|
143
|
-
// キー(トランジションまたはステートのフルパスハッシュ)と値をそれぞれListとして保持する
|
144
|
-
|
145
|
-
// リストは2組用意し、前者はステート間トランジション、後者はAny Stateからの場合のような
|
146
|
-
|
147
|
-
// ソースステートを持たないトランジションに使う
|
148
|
-
|
149
|
-
// ソースなしトランジションでもAnimatorTransitionInfo.fullPathHashはそれっぽい値を返すので、
|
150
|
-
|
151
|
-
// ソースなしの場合のハッシュの元となる文字列の作り方がわかればリストは1組ですむはずなのだが...
|
152
|
-
|
153
|
-
// リファレンスには載っておらず、勘で試してもハズレばかりであきらめた
|
154
|
-
|
155
|
-
[SerializeField] private List<int> equationKeys = new List<int>();
|
156
|
-
|
157
|
-
[SerializeField] private List<Equation> equationValues = new List<Equation>();
|
158
|
-
|
159
|
-
[SerializeField] private List<int> equationWithoutSourceKeys = new List<int>();
|
160
|
-
|
161
|
-
[SerializeField] private List<Equation> equationWithoutSourceValues = new List<Equation>();
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
private PlayableGraph animationGraph;
|
166
|
-
|
167
|
-
private Animator animator;
|
168
|
-
|
169
|
-
private RuntimeAnimatorController runtimeAnimatorController;
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
// ハッシュからEquationを引っ張ってくるのに使うヘルパーメソッド
|
174
|
-
|
175
|
-
// TransitionModifierEditorが使う
|
176
|
-
|
177
|
-
public Equation GetEquation(int hash, bool hasSource, out int index)
|
178
|
-
|
179
|
-
{
|
180
|
-
|
181
|
-
index = (hasSource ? this.equationKeys : this.equationWithoutSourceKeys).FindIndex(i => i == hash);
|
182
|
-
|
183
|
-
return index < 0 ? Equation.Linear : (hasSource ? this.equationValues : this.equationWithoutSourceValues)[index];
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
private void Start()
|
190
|
-
|
191
|
-
{
|
192
|
-
|
193
|
-
// まずPlayableGraphを新規作成する
|
194
|
-
|
195
|
-
this.animator = this.GetComponent<Animator>();
|
196
|
-
|
197
|
-
this.runtimeAnimatorController = this.animator.runtimeAnimatorController;
|
198
|
-
|
199
|
-
this.animationGraph = PlayableGraph.Create("Animation");
|
200
|
-
|
201
|
-
this.animationGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
|
202
|
-
|
203
|
-
var animatorOutput = AnimationPlayableOutput.Create(this.animationGraph, "AnimatorOutput", this.animator);
|
204
|
-
|
205
|
-
var animatorController = AnimatorControllerPlayable.Create(
|
206
|
-
|
207
|
-
this.animationGraph,
|
208
|
-
|
209
|
-
this.runtimeAnimatorController);
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
// ハッシュ値とイージングファンクションを対応づけたDictionaryを作り...
|
214
|
-
|
215
|
-
var transitionFunctions = new Dictionary<int, Func<float, float>>();
|
216
|
-
|
217
|
-
foreach (var (key, value) in this.equationKeys.Zip(this.equationValues, (key, value) => (key, value)))
|
218
|
-
|
219
|
-
{
|
220
|
-
|
221
|
-
transitionFunctions.Add(key, TransitionFunctions[value]);
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
var transitionWithoutSourceFunctions = new Dictionary<int, Func<float, float>>();
|
226
|
-
|
227
|
-
foreach (var (key, value) in this.equationWithoutSourceKeys.Zip(
|
228
|
-
|
229
|
-
this.equationWithoutSourceValues,
|
230
|
-
|
231
|
-
(key, value) => (key, value)))
|
232
|
-
|
233
|
-
{
|
234
|
-
|
235
|
-
transitionWithoutSourceFunctions.Add(key, TransitionFunctions[value]);
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
// メインの処理を担当するTransitionModifierPlayableを作成、必要なデータを投入する
|
242
|
-
|
243
|
-
var modifierPlayable = ScriptPlayable<TransitionModifierPlayable>.Create(this.animationGraph, 1);
|
244
|
-
|
245
|
-
modifierPlayable.GetBehaviour().Initialize(
|
246
|
-
|
247
|
-
modifierPlayable,
|
248
|
-
|
249
|
-
animatorController,
|
250
|
-
|
251
|
-
this.animator,
|
252
|
-
|
253
|
-
transitionFunctions,
|
254
|
-
|
255
|
-
transitionWithoutSourceFunctions);
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
// AnimatorのruntimeAnimatorControllerはnullにして黙らせ、代わりに
|
260
|
-
|
261
|
-
// できあがったPlayableGraphを起動してアニメーションを行わせる
|
262
|
-
|
263
|
-
animatorOutput.SetSourcePlayable(modifierPlayable);
|
264
|
-
|
265
|
-
this.animator.runtimeAnimatorController = null;
|
266
|
-
|
267
|
-
this.animationGraph.Play();
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
private void OnEnable()
|
274
|
-
|
275
|
-
{
|
276
|
-
|
277
|
-
if (this.animationGraph.IsValid())
|
278
|
-
|
279
|
-
{
|
280
|
-
|
281
|
-
this.animationGraph.Play();
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
}
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
private void OnDisable()
|
290
|
-
|
291
|
-
{
|
292
|
-
|
293
|
-
if (this.animationGraph.IsValid())
|
294
|
-
|
295
|
-
{
|
296
|
-
|
297
|
-
this.animationGraph.Stop();
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
if ((this.animator != null) && (this.runtimeAnimatorController != null))
|
304
|
-
|
305
|
-
{
|
306
|
-
|
307
|
-
this.animator.runtimeAnimatorController = this.runtimeAnimatorController;
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
private void OnDestroy()
|
316
|
-
|
317
|
-
{
|
318
|
-
|
319
|
-
if (this.animationGraph.IsValid())
|
320
|
-
|
321
|
-
{
|
322
|
-
|
323
|
-
// 生成したPlayableGraphは明示的に破棄しないとクラッシュするらしい...!
|
324
|
-
|
325
|
-
// http://tsubakit1.hateblo.jp/entry/2017/07/30/032008#%E6%B3%A8%E6%84%8F%E7%82%B9
|
326
|
-
|
327
|
-
this.animationGraph.Destroy();
|
328
|
-
|
329
|
-
}
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
private class TransitionModifierPlayable : PlayableBehaviour
|
336
|
-
|
337
|
-
{
|
338
|
-
|
339
|
-
private Dictionary<int, Func<float, float>> transitionFunctions;
|
340
|
-
|
341
|
-
private Dictionary<int, Func<float, float>> transitionWithoutSourceFunctions;
|
342
|
-
|
343
|
-
private Animator animator;
|
344
|
-
|
345
|
-
private PlayableGraph animationGraph;
|
346
|
-
|
347
|
-
private AnimationMixerPlayable referenceMixer;
|
348
|
-
|
349
|
-
private AnimationMixerPlayable actualMixer;
|
350
|
-
|
351
|
-
private AnimationClipPlayable mirrorClip0;
|
352
|
-
|
353
|
-
private AnimationClipPlayable mirrorClip1;
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
public void Initialize(
|
358
|
-
|
359
|
-
ScriptPlayable<TransitionModifierPlayable> self,
|
360
|
-
|
361
|
-
AnimatorControllerPlayable animatorController,
|
362
|
-
|
363
|
-
Animator animator,
|
364
|
-
|
365
|
-
Dictionary<int, Func<float, float>> transitionFunctions,
|
366
|
-
|
367
|
-
Dictionary<int, Func<float, float>> transitionWithoutSourceFunctions)
|
368
|
-
|
369
|
-
{
|
370
|
-
|
371
|
-
// AnimatorControllerによるノードツリー内の、トランジション前後のモーション混合を
|
372
|
-
|
373
|
-
// 行っているミキサーを監視し、そのブレンド比をイージングファンクションで変化させ、
|
374
|
-
|
375
|
-
// それを別途用意したミキサーに適用する作戦
|
376
|
-
|
377
|
-
// AnimatorControllerのノードツリーは編集不許可のようなので、こんな回りくどい方法をとった
|
378
|
-
|
379
|
-
this.transitionFunctions = transitionFunctions;
|
380
|
-
|
381
|
-
this.transitionWithoutSourceFunctions = transitionWithoutSourceFunctions;
|
382
|
-
|
383
|
-
this.animator = animator;
|
384
|
-
|
385
|
-
this.referenceMixer = (AnimationMixerPlayable)animatorController.GetInput(0).GetInput(0);
|
386
|
-
|
387
|
-
this.animationGraph = this.referenceMixer.GetGraph();
|
388
|
-
|
389
|
-
var referenceClip0 = (AnimationClipPlayable)this.referenceMixer.GetInput(0).GetInput(0);
|
390
|
-
|
391
|
-
var referenceClip1 = (AnimationClipPlayable)this.referenceMixer.GetInput(1).GetInput(0);
|
392
|
-
|
393
|
-
this.mirrorClip0 = AnimationClipPlayable.Create(this.animationGraph, referenceClip0.GetAnimationClip());
|
394
|
-
|
395
|
-
this.mirrorClip1 = AnimationClipPlayable.Create(this.animationGraph, referenceClip1.GetAnimationClip());
|
396
|
-
|
397
|
-
this.actualMixer = AnimationMixerPlayable.Create(this.animationGraph, 3);
|
398
|
-
|
399
|
-
this.actualMixer.ConnectInput(0, this.mirrorClip0, 0, 1.0f);
|
400
|
-
|
401
|
-
this.actualMixer.ConnectInput(1, this.mirrorClip1, 0);
|
402
|
-
|
403
|
-
this.actualMixer.ConnectInput(2, animatorController, 0);
|
404
|
-
|
405
|
-
self.ConnectInput(0, this.actualMixer, 0, 1.0f);
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
public override void PrepareFrame(Playable playable, FrameData info)
|
412
|
-
|
413
|
-
{
|
414
|
-
|
415
|
-
// アニメーションクリップが変わっていたらノードを差し替え、その後再生位置を同期させる
|
416
|
-
|
417
|
-
this.UpdateMirror(
|
418
|
-
|
419
|
-
(AnimationClipPlayable)this.referenceMixer.GetInput(0).GetInput(0),
|
420
|
-
|
421
|
-
ref this.mirrorClip0,
|
422
|
-
|
423
|
-
0);
|
424
|
-
|
425
|
-
this.UpdateMirror(
|
426
|
-
|
427
|
-
(AnimationClipPlayable)this.referenceMixer.GetInput(1).GetInput(0),
|
428
|
-
|
429
|
-
ref this.mirrorClip1,
|
430
|
-
|
431
|
-
1);
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
// 遷移中ならミキサーの混合比を同期させる
|
436
|
-
|
437
|
-
// このときイージング処理を施し緩急をつける
|
438
|
-
|
439
|
-
var transitionInfo = this.animator.GetAnimatorTransitionInfo(0);
|
440
|
-
|
441
|
-
var functions = transitionInfo.anyState
|
442
|
-
|
443
|
-
? this.transitionWithoutSourceFunctions
|
444
|
-
|
445
|
-
: this.transitionFunctions;
|
446
|
-
|
447
|
-
var transitionHash = transitionInfo.anyState
|
448
|
-
|
449
|
-
? this.animator.GetNextAnimatorStateInfo(0).fullPathHash
|
450
|
-
|
451
|
-
: transitionInfo.fullPathHash;
|
452
|
-
|
453
|
-
if (transitionHash != 0)
|
454
|
-
|
455
|
-
{
|
456
|
-
|
457
|
-
var weight0 = this.referenceMixer.GetInputWeight(0);
|
458
|
-
|
459
|
-
var weight1 = this.referenceMixer.GetInputWeight(1);
|
460
|
-
|
461
|
-
var weightSum = weight0 + weight1;
|
462
|
-
|
463
|
-
if (weightSum > 0.0f)
|
464
|
-
|
465
|
-
{
|
466
|
-
|
467
|
-
var normalizedWeight1 = weight1 / weightSum;
|
468
|
-
|
469
|
-
if (functions.TryGetValue(transitionHash, out var function))
|
470
|
-
|
471
|
-
{
|
472
|
-
|
473
|
-
normalizedWeight1 = function(normalizedWeight1);
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
var normalizedWeight0 = 1.0f - normalizedWeight1;
|
480
|
-
|
481
|
-
this.actualMixer.SetInputWeight(0, normalizedWeight0);
|
482
|
-
|
483
|
-
this.actualMixer.SetInputWeight(1, normalizedWeight1);
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
base.PrepareFrame(playable, info);
|
492
|
-
|
493
|
-
}
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
private void UpdateMirror(AnimationClipPlayable reference, ref AnimationClipPlayable mirror, int port)
|
498
|
-
|
499
|
-
{
|
500
|
-
|
501
|
-
if (mirror.GetAnimationClip() != reference.GetAnimationClip())
|
502
|
-
|
503
|
-
{
|
504
|
-
|
505
|
-
mirror.Destroy();
|
506
|
-
|
507
|
-
mirror = AnimationClipPlayable.Create(this.animationGraph, reference.GetAnimationClip());
|
508
|
-
|
509
|
-
this.actualMixer.ConnectInput(port, mirror, 0, port == 0 ? 1.0f : 0.0f);
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
503
|
|
514
504
|
|
515
505
|
mirror.SetTime(reference.GetTime());
|