Skip to content

Commit

Permalink
Merge pull request #3 from paralleltree/write-defaults
Browse files Browse the repository at this point in the history
各AnimatorStateにWriteDefaultsを設定できるようにした
  • Loading branch information
paralleltree authored Nov 9, 2024
2 parents de36467 + 0292b8a commit 108d5fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 68 deletions.
82 changes: 19 additions & 63 deletions Editor/FaceEmoteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected AnimatorControllerLayer GenerateIdleLayer(Runtime.FaceEmoteInstaller i
};

var state = layer.stateMachine.AddState("Idle");
state.writeDefaultValues = false;
state.writeDefaultValues = installer.WriteDefaultsValues;
state.motion = installer.IdleMotion;

return layer;
Expand All @@ -80,79 +80,35 @@ protected AnimatorControllerLayer GenerateHandLayer(Runtime.FaceEmoteInstaller i
stateMachine = new AnimatorStateMachine(),
};

var idleState = new AnimatorState()
{
name = "Idle",
motion = EmptyAnimation,
writeDefaultValues = false,
};
var idleState = GenerateAnimatorState("Idle", EmptyAnimation);
layer.stateMachine.AddState(idleState, new Vector3(-100, 0));
layer.stateMachine.defaultState = idleState;

if (installer.Definitions == null) installer.Definitions = new List<Runtime.ExpressionSetDefinition>();
for (int i = 0; i < installer.Definitions.Count; i++)
AnimatorState GenerateAnimatorState(string name, AnimationClip motion)
{
var def = installer.Definitions[i];
var initialState = new AnimatorState()
{
name = $"Idle {i}",
motion = EmptyAnimation,
writeDefaultValues = false,
};

var fistState = new AnimatorState()
{
name = $"Fist {i}",
motion = def.Fist ?? EmptyAnimation,
writeDefaultValues = false,
};

var openState = new AnimatorState()
return new AnimatorState()
{
name = $"Open {i}",
motion = def.Open ?? EmptyAnimation,
writeDefaultValues = false,
};

var pointState = new AnimatorState()
{
name = $"Point {i}",
motion = def.Point ?? EmptyAnimation,
writeDefaultValues = false,
};

var peaceState = new AnimatorState()
{
name = $"Peace {i}",
motion = def.Peace ?? EmptyAnimation,
writeDefaultValues = false,
};

var rockNRollState = new AnimatorState()
{
name = $"RockNRoll {i}",
motion = def.RockNRoll ?? EmptyAnimation,
writeDefaultValues = false,
};

var gunState = new AnimatorState()
{
name = $"Gun {i}",
motion = def.Gun ?? EmptyAnimation,
writeDefaultValues = false,
name = name,
motion = motion ?? EmptyAnimation,
writeDefaultValues = installer.WriteDefaultsValues,
};
}

var thumbsUpState = new AnimatorState()
{
name = $"Thumbs up {i}",
motion = def.ThumbsUp ?? EmptyAnimation,
writeDefaultValues = false,
};
if (installer.Definitions == null) installer.Definitions = new List<Runtime.ExpressionSetDefinition>();
for (int i = 0; i < installer.Definitions.Count; i++)
{
var def = installer.Definitions[i];
var initialState = GenerateAnimatorState($"Idle {i}", EmptyAnimation);

layer.stateMachine.AddState(initialState, new Vector3(i * 100, 0));
var gestureStates = new AnimatorState[]
{
fistState, openState, pointState, peaceState, rockNRollState, gunState, thumbsUpState,
GenerateAnimatorState($"Fist {i}", def.Fist),
GenerateAnimatorState($"Open {i}", def.Open),
GenerateAnimatorState( $"Point {i}", def.Point),
GenerateAnimatorState($"Peace {i}", def.Peace),
GenerateAnimatorState($"RockNRoll {i}", def.RockNRoll),
GenerateAnimatorState($"Thumbs up {i}",def.ThumbsUp),
};

// ExpressionSet切り替えの遷移
Expand Down
2 changes: 2 additions & 0 deletions Runtime/FaceEmoteInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public sealed class FaceEmoteInstaller : MonoBehaviour, IEditorOnly
public float TransitionSeconds = 0.1f;
[SerializeField]
public AnimationClip IdleMotion;
[SerializeField]
public bool WriteDefaultsValues;

[SerializeField]
public List<ExpressionSetDefinition> Definitions;
Expand Down
13 changes: 8 additions & 5 deletions Tests/FaceEmoteProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ public void TestProcess_GeneratesAnimatorWithParameters()
}

[Test]
public void TestProcess_GeneratesAnimatorBasedOnFaceEmoteDefinition()
[TestCase(false)]
[TestCase(true)]
public void TestProcess_GeneratesAnimatorBasedOnFaceEmoteDefinition(bool writeDefaultValues)
{

var gameObject = new GameObject("Test Target");
var installerComponent = gameObject.AddComponent<Runtime.FaceEmoteInstaller>();
installerComponent.WriteDefaultsValues = writeDefaultValues;

var idleAnim = new AnimationClip();
var idleAnimEvent = new AnimationEvent() { stringParameter = "testFist", time = 10f, floatParameter = 0f };
Expand Down Expand Up @@ -145,6 +147,7 @@ void ValidateIdleLayer(AnimatorControllerLayer layer)

var idleState = layer.stateMachine.defaultState;
Assert.AreEqual(idleAnim, idleState.motion);
Assert.AreEqual(writeDefaultValues, idleState.writeDefaultValues);
}

void ValidateHandLayer(AnimatorControllerLayer layer, string handSide)
Expand All @@ -158,7 +161,7 @@ void ValidateHandLayer(AnimatorControllerLayer layer, string handSide)
void ValidateEmoteSetStateMachineRoot(AnimatorState root, string handSide)
{
Assert.IsTrue(((AnimationClip)root.motion).empty);
Assert.AreEqual(false, root.writeDefaultValues);
Assert.AreEqual(writeDefaultValues, root.writeDefaultValues);

foreach (var trans in root.transitions)
{
Expand Down Expand Up @@ -188,7 +191,7 @@ void ValidateEmoteSetStateMachineRoot(AnimatorState root, string handSide)
void ValidateEmoteSetStateMachineSubSetRoot(AnimatorState node, int setIndex, string handSide)
{
Assert.IsTrue(((AnimationClip)node.motion).empty);
Assert.AreEqual(false, node.writeDefaultValues);
Assert.AreEqual(writeDefaultValues, node.writeDefaultValues);

// assert each expression set Idle to layer root
var childIdleToGlobalIdle = node.transitions.Where(trans => trans.destinationState.name == "Idle").Single();
Expand All @@ -215,7 +218,7 @@ void ValidateEmoteSetStateMachineSubSetRoot(AnimatorState node, int setIndex, st

void ValidateEmoteSetStateMachineSubSetEmote(AnimatorState node, int setIndex, int gestureIndex, string handSide)
{
Assert.AreEqual(false, node.writeDefaultValues);
Assert.AreEqual(writeDefaultValues, node.writeDefaultValues);

// assert selected motion
var def = installerComponent.Definitions[setIndex];
Expand Down

0 comments on commit 108d5fe

Please sign in to comment.