2022-09-09 11:40:52 +08:00
|
|
|
|
/*
|
|
|
|
|
* MIT License
|
2023-08-22 17:44:02 +08:00
|
|
|
|
*
|
2022-09-09 11:40:52 +08:00
|
|
|
|
* Copyright (c) 2022 bd_
|
2023-08-22 17:44:02 +08:00
|
|
|
|
*
|
2022-09-09 11:40:52 +08:00
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2023-08-22 17:44:02 +08:00
|
|
|
|
*
|
2022-09-09 11:40:52 +08:00
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2023-08-22 17:44:02 +08:00
|
|
|
|
*
|
2022-09-09 11:40:52 +08:00
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-19 20:32:44 +08:00
|
|
|
|
using nadena.dev.modular_avatar.editor.ErrorReporting;
|
2022-08-28 10:04:53 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEditor.Animations;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.Components;
|
2022-10-03 10:08:23 +08:00
|
|
|
|
using Object = UnityEngine.Object;
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
2022-11-11 12:39:58 +08:00
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2022-09-12 05:19:05 +08:00
|
|
|
|
internal class MergeAnimatorProcessor
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2022-09-18 00:52:59 +08:00
|
|
|
|
private const string SAMPLE_PATH_PACKAGE =
|
|
|
|
|
"Packages/com.vrchat.avatars/Samples/AV3 Demo Assets/Animation/Controllers";
|
|
|
|
|
|
2022-08-28 10:04:53 +08:00
|
|
|
|
private const string SAMPLE_PATH_LEGACY = "Assets/VRCSDK/Examples3/Animation/Controllers";
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2023-07-30 19:55:07 +08:00
|
|
|
|
private const string GUID_GESTURE_HANDSONLY_MASK = "b2b8bad9583e56a46a3e21795e96ad92";
|
|
|
|
|
|
2023-01-05 20:30:01 +08:00
|
|
|
|
private BuildContext _context;
|
|
|
|
|
|
2022-08-30 09:35:16 +08:00
|
|
|
|
private Dictionary<VRCAvatarDescriptor.AnimLayerType, AnimatorController> defaultControllers_ =
|
|
|
|
|
new Dictionary<VRCAvatarDescriptor.AnimLayerType, AnimatorController>();
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2022-10-03 10:08:23 +08:00
|
|
|
|
private Dictionary<VRCAvatarDescriptor.AnimLayerType, bool?> writeDefaults_ =
|
|
|
|
|
new Dictionary<VRCAvatarDescriptor.AnimLayerType, bool?>();
|
|
|
|
|
|
2022-08-28 10:04:53 +08:00
|
|
|
|
Dictionary<VRCAvatarDescriptor.AnimLayerType, AnimatorCombiner> mergeSessions =
|
|
|
|
|
new Dictionary<VRCAvatarDescriptor.AnimLayerType, AnimatorCombiner>();
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2023-01-05 20:30:01 +08:00
|
|
|
|
internal void OnPreprocessAvatar(GameObject avatarGameObject, BuildContext context)
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2023-01-05 20:30:01 +08:00
|
|
|
|
_context = context;
|
|
|
|
|
|
2022-09-10 11:36:04 +08:00
|
|
|
|
defaultControllers_.Clear();
|
|
|
|
|
mergeSessions.Clear();
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2022-08-28 10:04:53 +08:00
|
|
|
|
var descriptor = avatarGameObject.GetComponent<VRCAvatarDescriptor>();
|
|
|
|
|
|
2023-01-17 19:26:56 +08:00
|
|
|
|
if (descriptor.baseAnimationLayers != null) InitSessions(descriptor.baseAnimationLayers);
|
|
|
|
|
if (descriptor.specialAnimationLayers != null) InitSessions(descriptor.specialAnimationLayers);
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2022-08-30 05:00:11 +08:00
|
|
|
|
var toMerge = avatarGameObject.transform.GetComponentsInChildren<ModularAvatarMergeAnimator>(true);
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
|
|
|
|
foreach (var merge in toMerge)
|
|
|
|
|
{
|
2023-01-19 20:32:44 +08:00
|
|
|
|
BuildReport.ReportingObject(merge, () => ProcessMergeAnimator(avatarGameObject, context, merge));
|
|
|
|
|
}
|
2022-08-30 09:35:16 +08:00
|
|
|
|
|
2023-01-19 20:32:44 +08:00
|
|
|
|
descriptor.baseAnimationLayers = FinishSessions(descriptor.baseAnimationLayers);
|
|
|
|
|
descriptor.specialAnimationLayers = FinishSessions(descriptor.specialAnimationLayers);
|
2023-07-30 19:55:07 +08:00
|
|
|
|
descriptor.customizeAnimationLayers = true;
|
2023-01-19 20:32:44 +08:00
|
|
|
|
}
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2023-01-19 20:32:44 +08:00
|
|
|
|
private void ProcessMergeAnimator(GameObject avatarGameObject, BuildContext context,
|
|
|
|
|
ModularAvatarMergeAnimator merge)
|
|
|
|
|
{
|
|
|
|
|
if (merge.animator == null) return;
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
2023-01-19 20:32:44 +08:00
|
|
|
|
string basePath;
|
|
|
|
|
if (merge.pathMode == MergeAnimatorPathMode.Relative)
|
|
|
|
|
{
|
|
|
|
|
var relativePath = RuntimeUtil.RelativePath(avatarGameObject, merge.gameObject);
|
|
|
|
|
basePath = relativePath != "" ? relativePath + "/" : "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
basePath = "";
|
|
|
|
|
}
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2023-01-19 20:32:44 +08:00
|
|
|
|
if (!mergeSessions.TryGetValue(merge.layerType, out var session))
|
|
|
|
|
{
|
2023-07-29 23:35:07 +08:00
|
|
|
|
session = new AnimatorCombiner(context, merge.layerType.ToString() + " (merged)");
|
2023-01-19 20:32:44 +08:00
|
|
|
|
mergeSessions[merge.layerType] = session;
|
2023-07-29 23:35:07 +08:00
|
|
|
|
if (defaultControllers_.TryGetValue(merge.layerType, out var defaultController))
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2023-07-29 23:35:07 +08:00
|
|
|
|
session.AddController("", defaultController, null);
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 20:32:44 +08:00
|
|
|
|
bool? writeDefaults = merge.matchAvatarWriteDefaults ? writeDefaults_[merge.layerType] : null;
|
|
|
|
|
mergeSessions[merge.layerType]
|
|
|
|
|
.AddController(basePath, (AnimatorController) merge.animator, writeDefaults);
|
|
|
|
|
|
|
|
|
|
if (merge.deleteAttachedAnimator)
|
|
|
|
|
{
|
|
|
|
|
var animator = merge.GetComponent<Animator>();
|
|
|
|
|
if (animator != null) Object.DestroyImmediate(animator);
|
|
|
|
|
}
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private VRCAvatarDescriptor.CustomAnimLayer[] FinishSessions(
|
|
|
|
|
VRCAvatarDescriptor.CustomAnimLayer[] layers
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
layers = (VRCAvatarDescriptor.CustomAnimLayer[]) layers.Clone();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < layers.Length; i++)
|
|
|
|
|
{
|
2022-08-30 09:35:16 +08:00
|
|
|
|
if (mergeSessions.TryGetValue(layers[i].type, out var session))
|
|
|
|
|
{
|
2023-07-30 19:55:07 +08:00
|
|
|
|
if (layers[i].type == VRCAvatarDescriptor.AnimLayerType.Gesture && layers[i].isDefault)
|
|
|
|
|
{
|
|
|
|
|
// We need to set the mask field for the gesture layer on initial configuration
|
|
|
|
|
layers[i].mask = AssetDatabase.LoadAssetAtPath<AvatarMask>(
|
|
|
|
|
AssetDatabase.GUIDToAssetPath(GUID_GESTURE_HANDSONLY_MASK)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 09:35:16 +08:00
|
|
|
|
layers[i].isDefault = false;
|
2022-09-18 00:52:59 +08:00
|
|
|
|
layers[i].animatorController = session.Finish();
|
2022-08-30 09:35:16 +08:00
|
|
|
|
}
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return layers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitSessions(VRCAvatarDescriptor.CustomAnimLayer[] layers)
|
|
|
|
|
{
|
|
|
|
|
foreach (var layer in layers)
|
|
|
|
|
{
|
|
|
|
|
var controller = ResolveLayerController(layer);
|
|
|
|
|
if (controller == null) controller = new AnimatorController();
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2022-08-30 09:35:16 +08:00
|
|
|
|
defaultControllers_[layer.type] = controller;
|
2022-10-03 10:08:23 +08:00
|
|
|
|
writeDefaults_[layer.type] = ProbeWriteDefaults(controller);
|
2023-01-05 20:10:22 +08:00
|
|
|
|
if (!layer.isDefault)
|
|
|
|
|
{
|
|
|
|
|
// For non-default layers, ensure we always clone the controller for the benefit of subsequent
|
|
|
|
|
// processing phases
|
2023-07-29 23:35:07 +08:00
|
|
|
|
mergeSessions[layer.type] = new AnimatorCombiner(_context, layer.type.ToString());
|
2023-01-05 20:10:22 +08:00
|
|
|
|
mergeSessions[layer.type].AddController("", controller, null);
|
|
|
|
|
}
|
2022-10-03 10:08:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool? ProbeWriteDefaults(AnimatorController controller)
|
|
|
|
|
{
|
|
|
|
|
bool hasWDOn = false;
|
|
|
|
|
bool hasWDOff = false;
|
|
|
|
|
|
|
|
|
|
var stateMachineQueue = new Queue<AnimatorStateMachine>();
|
|
|
|
|
foreach (var layer in controller.layers)
|
|
|
|
|
{
|
|
|
|
|
stateMachineQueue.Enqueue(layer.stateMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (stateMachineQueue.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var stateMachine = stateMachineQueue.Dequeue();
|
|
|
|
|
foreach (var state in stateMachine.states)
|
|
|
|
|
{
|
|
|
|
|
if (state.state.writeDefaultValues) hasWDOn = true;
|
|
|
|
|
else hasWDOff = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var child in stateMachine.stateMachines)
|
|
|
|
|
{
|
|
|
|
|
stateMachineQueue.Enqueue(child.stateMachine);
|
|
|
|
|
}
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
2022-10-03 10:08:23 +08:00
|
|
|
|
|
|
|
|
|
if (hasWDOn == hasWDOff) return null;
|
|
|
|
|
return hasWDOn;
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
2022-09-18 00:52:59 +08:00
|
|
|
|
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
|
|
|
|
private static AnimatorController ResolveLayerController(VRCAvatarDescriptor.CustomAnimLayer layer)
|
|
|
|
|
{
|
|
|
|
|
AnimatorController controller = null;
|
|
|
|
|
|
|
|
|
|
if (!layer.isDefault && layer.animatorController != null &&
|
|
|
|
|
layer.animatorController is AnimatorController c)
|
|
|
|
|
{
|
|
|
|
|
controller = c;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
switch (layer.type)
|
|
|
|
|
{
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.Action:
|
|
|
|
|
name = "Action";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.Additive:
|
|
|
|
|
name = "Idle";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.Base:
|
|
|
|
|
name = "Locomotion";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.Gesture:
|
|
|
|
|
name = "Hands";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.Sitting:
|
|
|
|
|
name = "Sitting";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.FX:
|
|
|
|
|
name = "Face";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.TPose:
|
|
|
|
|
name = "UtilityTPose";
|
|
|
|
|
break;
|
|
|
|
|
case VRCAvatarDescriptor.AnimLayerType.IKPose:
|
|
|
|
|
name = "UtilityIKPose";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
name = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 05:00:11 +08:00
|
|
|
|
if (name != null)
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2022-08-30 05:00:11 +08:00
|
|
|
|
name = "/vrc_AvatarV3" + name + "Layer.controller";
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
|
|
|
|
controller = AssetDatabase.LoadAssetAtPath<AnimatorController>(SAMPLE_PATH_PACKAGE + name);
|
|
|
|
|
if (controller == null)
|
|
|
|
|
{
|
|
|
|
|
controller = AssetDatabase.LoadAssetAtPath<AnimatorController>(SAMPLE_PATH_LEGACY + name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return controller;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|