Skip to content

Commit

Permalink
Enable to change face expression gradually #88
Browse files Browse the repository at this point in the history
Set count of steps from current face to the target face to `FaceFadeStep`. The interval for each step is 10 msec. (Not exactly 10 msec. The accuracy depends on `Task.Delay(10)`)

If you set 0 to `FaceFadeStep`, the face expression will be changed immediately.
  • Loading branch information
uezo committed Nov 30, 2020
1 parent da72297 commit fd8348e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions ChatdollKit/Scripts/Model/ModelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ModelController : MonoBehaviour
public FaceClipConfiguration FaceClipConfiguration;
private Dictionary<string, FaceClip> faceClips = new Dictionary<string, FaceClip>();
private FaceRequest DefaultFace;
public int FaceFadeStep = 5;

// History recorder for debug and test
public ActionHistoryRecorder History;
Expand Down Expand Up @@ -611,10 +612,39 @@ public async Task SetFace(FaceRequest request)
History?.Add(face);
if (faceClips.ContainsKey(face.Name))
{
foreach (var blendShapeValue in faceClips[face.Name].Values)
if (FaceFadeStep == 0)
{
// Change immediately when step is 0
foreach (var blendShapeValue in faceClips[face.Name].Values)
{
SkinnedMeshRenderer.SetBlendShapeWeight(blendShapeValue.Index, blendShapeValue.Weight);
}
}
else
{
// Calculate the weights to be added at each steps
var weightsForEachSteps = new Dictionary<int, List<float>>();
foreach (var blendShapeValue in faceClips[face.Name].Values)
{
var currentWeight = SkinnedMeshRenderer.GetBlendShapeWeight(blendShapeValue.Index);
weightsForEachSteps.Add(blendShapeValue.Index, new List<float>());
var weightToAdd = (blendShapeValue.Weight - currentWeight) / FaceFadeStep;
for (var i = 0; i < FaceFadeStep - 1; i++)
{
weightsForEachSteps[blendShapeValue.Index].Add(currentWeight + weightToAdd * (i + 1));
}
weightsForEachSteps[blendShapeValue.Index].Add(blendShapeValue.Weight);
}

// Apply weights
SkinnedMeshRenderer.SetBlendShapeWeight(blendShapeValue.Index, blendShapeValue.Weight);
for (var i = 0; i < FaceFadeStep; i++)
{
foreach (var blendShapeValue in faceClips[face.Name].Values)
{
SkinnedMeshRenderer.SetBlendShapeWeight(blendShapeValue.Index, weightsForEachSteps[blendShapeValue.Index][i]);
}
await Task.Delay(10);
}
}
await Task.Delay((int)(face.Duration * 1000));
}
Expand Down

0 comments on commit fd8348e

Please sign in to comment.