Skip to content

Commit

Permalink
Merge pull request #14 from Adjerry91/Write-Defaults-Switch
Browse files Browse the repository at this point in the history
Fix write defaults ON breaking in VRC stations
  • Loading branch information
regzo2 authored Dec 31, 2022
2 parents ac9c273 + 1b594ad commit eaa61ad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
64 changes: 55 additions & 9 deletions Assets/OSCmooth/Editor/OSCmoothAnimationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,32 @@ public void CreateSmoothAnimationLayer()

// Looking for existing animation layer, and will delete it to replace with a new one. Will look into
// creating a more thorough solution to much more effectively overwrite the existing layer for a future update.
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen_", _animatorController);
if(_writeDefaults)
{
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
}
else
{
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
}

// Creating a Direct BlendTree that will hold all of the smooth driver animations. This is to effectively create a 'sublayer'
// system within the Direct BlendTree to tidy up the animator base layers from bloating up visually.
AnimatorState[] state = new AnimatorState[2];
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local", new Vector3(30, 170, 0));
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net", new Vector3(30, 170 + 60, 0));

if(_writeDefaults)
{
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local_WD", new Vector3(30, 170, 0));
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net_WD", new Vector3(30, 170 + 60, 0));
}
else
{
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local", new Vector3(30, 170, 0));
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net", new Vector3(30, 170 + 60, 0));
}


state[0].writeDefaultValues = _writeDefaults;
state[1].writeDefaultValues = _writeDefaults;
Expand All @@ -51,23 +70,33 @@ public void CreateSmoothAnimationLayer()
toLocalState.AddCondition(AnimatorConditionMode.If, 0, "IsLocal");

// Creating BlendTree objects to better customize them in the AC Editor
var nameLocalWD = "OSCm_Local";
var nameRemoteWD = "OSCm_Remote";

if (_writeDefaults)
{
nameLocalWD = "OSCm_Local_WD";
nameRemoteWD = "OSCm_Remote_WD";
}

var basisLocalBlendTree = new BlendTree()
{
blendType = BlendTreeType.Direct,
hideFlags = HideFlags.HideInHierarchy,
name = "OSCm_Local",
name = nameLocalWD,
useAutomaticThresholds = false

};

var basisRemoteBlendTree = new BlendTree()
{
blendType = BlendTreeType.Direct,
hideFlags = HideFlags.HideInHierarchy,
name = "OSCm_Remote",
name = nameRemoteWD,
useAutomaticThresholds = false
};


// Stuffing the BlendTrees into their designated state. Also stuffing them so that they
// retain serialization.
state[0].motion = basisLocalBlendTree;
Expand All @@ -76,7 +105,16 @@ public void CreateSmoothAnimationLayer()
AssetDatabase.AddObjectToAsset(basisRemoteBlendTree, AssetDatabase.GetAssetPath(animLayer.stateMachine));

// Creating a '1Set' parameter that holds a value of one at all times for the Direct BlendTree
ParameterUtil.CheckAndCreateParameter("OSCm/BlendSet", _animatorController, AnimatorControllerParameterType.Float, 1f / (float)_parameters.Count);

if (_writeDefaults)
{
ParameterUtil.CheckAndCreateParameter("OSCm/BlendSet", _animatorController, AnimatorControllerParameterType.Float, 1f);
}
else
{
ParameterUtil.CheckAndCreateParameter("OSCm/BlendSet", _animatorController, AnimatorControllerParameterType.Float, 1f / (float)_parameters.Count);
}


List<ChildMotion> localChildMotion = new List<ChildMotion>();
List<ChildMotion> remoteChildMotion = new List<ChildMotion>();
Expand All @@ -89,17 +127,25 @@ public void CreateSmoothAnimationLayer()
AnimUtil.RenameAllStateMachineInstancesOfBlendParameter(_animatorController, p.paramName, "OSCm/Proxy/" + p.paramName);
}

var motionLocal = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.localSmoothness, p.paramName, p.flipInputOutput, (float)_parameters.Count, _animExportDirectory, "OSCm/Local/", "Smoother", "OSCm/Proxy/", "Proxy");
var motionRemote = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.remoteSmoothness, p.paramName, p.flipInputOutput, (float)_parameters.Count, _animExportDirectory, "OSCm/Remote/", "SmootherRemote", "OSCm/Proxy/", "Proxy");
if (_writeDefaults)
{
motionLocal = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.localSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Local/", "SmootherWD", "OSCm/Proxy/", "Proxy");
motionRemote = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.remoteSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Remote/", "SmootherRemoteWD", "OSCm/Proxy/", "Proxy");
}

localChildMotion.Add(new ChildMotion
{
directBlendParameter = "OSCm/BlendSet",
motion = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.localSmoothness, p.paramName, p.flipInputOutput, (float)_parameters.Count, _animExportDirectory, "OSCm/Local/", "Smoother", "OSCm/Proxy/", "Proxy"),
motion = motionLocal,
timeScale = 1
});

remoteChildMotion.Add(new ChildMotion
{
directBlendParameter = "OSCm/BlendSet",
motion = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.remoteSmoothness, p.paramName, p.flipInputOutput, (float)_parameters.Count, _animExportDirectory, "OSCm/Remote/", "SmootherRemote", "OSCm/Proxy/", "Proxy"),
motion = motionRemote,
timeScale = 1,
});
}
Expand Down
12 changes: 12 additions & 0 deletions Assets/OSCmooth/Editor/OSCmoothUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public static void RenameAllStateMachineInstancesOfBlendParameter(AnimatorContro
}
}

public static void RemoveAnimLayerInController(string layerName, AnimatorController animatorController)
{
for (int i = 0; i < animatorController.layers.Length; i++)
{
if (animatorController.layers[i].name == layerName)
{
animatorController.RemoveLayer(i);
}
}

}

public static AnimatorControllerLayer CreateAnimLayerInController(string layerName, AnimatorController animatorController)
{
for (int i = 0; i < animatorController.layers.Length; i++)
Expand Down

0 comments on commit eaa61ad

Please sign in to comment.