Skip to content

Commit

Permalink
mocopi受信データを補間してガクガク揺れないように
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-akira committed Jan 30, 2023
1 parent 675fde4 commit c0b6654
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/VirtualMotionCapture.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2430,7 +2430,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
IsBeta: 0
IsPreRelease: 0
VersionString: v0.54f1r1b7
VersionString: v0.54f1r1b8
LeftWristTransform: {fileID: 0}
RightWristTransform: {fileID: 0}
CalibrationCamera: {fileID: 1312323656}
Expand Down
62 changes: 45 additions & 17 deletions Assets/Scripts/MocopiConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class MocopiConnector : MonoBehaviour

private Animator modelAnimator;
private Animator mocopiAnimator;
private Transform cloneRootTransform;
private Animator filteredAnimator;

private VRIK vrik;
private IKSolver ikSolver;

private List<(HumanBodyBones humanBodyBone, Transform source, Transform target)> boneTransformCache;
private List<(HumanBodyBones humanBodyBone, Transform source, Transform filtered, Transform target)> boneTransformCache;

private Vector3 centerOffsetPosition;
private float centerOffsetRotationY;
Expand Down Expand Up @@ -55,7 +55,6 @@ private void OnEnable()

private void OnDisable()
{
isFrameArrived = false;
StopUdpReceiver();
}
private void Server_Received(object sender, DataReceivedEventArgs e)
Expand Down Expand Up @@ -143,13 +142,24 @@ private void OnCurrentModelChanged(GameObject model)
currentModel = model;
modelAnimator = model.GetComponent<Animator>();

var (cloneAvatar, cloneRoot) = CreateCopyAvatar(model, transform);
mocopiAnimator = gameObject.AddComponent<Animator>();
var filteredAvatarObject = new GameObject("filteredAvatar");
filteredAvatarObject.transform.SetParent(transform, false);

var (filteredAvatar, _) = CreateCopyAvatar(model, filteredAvatarObject.transform);

filteredAnimator = filteredAvatarObject.AddComponent<Animator>();
filteredAnimator.avatar = filteredAvatar;

var mocopiAvatarObject = new GameObject("mocopiAvatar");
mocopiAvatarObject.transform.SetParent(transform, false);

var (cloneAvatar, _) = CreateCopyAvatar(model, mocopiAvatarObject.transform);

mocopiAnimator = mocopiAvatarObject.AddComponent<Animator>();
mocopiAnimator.avatar = cloneAvatar;
cloneRootTransform = cloneRoot;

mocopiAvatar = gameObject.AddComponent<MocopiAvatar>();
mocopiAvatar.MotionSmoothness = 1.0f;
mocopiAvatar = mocopiAvatarObject.AddComponent<MocopiAvatar>();
mocopiAvatar.MotionSmoothness = 0.0f; // mocopiAvatarのモーションスムージングは使用せず自前で補正する
boneTransformCache = null;

if (enabled)
Expand All @@ -163,14 +173,12 @@ private void OnModelUnloading(GameObject model)
//前回の生成物の削除
if (currentModel != null)
{
DestroyImmediate(cloneRootTransform.gameObject);
DestroyImmediate(mocopiAnimator.gameObject);
// Destroy SkeletonRoot
foreach (Transform child in transform)
{
DestroyImmediate(child.gameObject);
}
DestroyImmediate(mocopiAvatar);
DestroyImmediate(mocopiAnimator);

if (ikSolver != null)
{
Expand All @@ -180,6 +188,7 @@ private void OnModelUnloading(GameObject model)
vrik = null;
currentModel = null;
isFrameArrived = false;
boneTransformCache = null;
}
}

Expand Down Expand Up @@ -230,6 +239,7 @@ private void StopUdpReceiver()
}
isFrameArrived = false;
udpReceiver = null;
boneTransformCache = null;
}
public void InitializeSkeleton(int[] boneIds, int[] parentBoneIds, float[] rotationsX, float[] rotationsY, float[] rotationsZ, float[] rotationsW, float[] positionsX, float[] positionsY, float[] positionsZ)
{
Expand Down Expand Up @@ -276,7 +286,7 @@ private void ApplyMotion()

if (boneTransformCache == null)
{
boneTransformCache = new List<(HumanBodyBones humanBodyBone, Transform source, Transform target)>();
boneTransformCache = new List<(HumanBodyBones humanBodyBone, Transform source, Transform filtered, Transform target)>();

var ReverseBodyBones = new HumanBodyBones[] {
HumanBodyBones.Head ,
Expand Down Expand Up @@ -310,10 +320,17 @@ private void ApplyMotion()
var mocopiBone = mocopiAnimator.GetBoneTransform(bone);
if (mocopiBone == null) continue;

var filteredBone = filteredAnimator.GetBoneTransform(bone);
if (filteredBone == null) continue;

// initialize filtered bone
filteredBone.localPosition = mocopiBone.localPosition;
filteredBone.localRotation = mocopiBone.localRotation;

var modelBone = modelAnimator.GetBoneTransform(bone);
if (modelBone == null) continue;

boneTransformCache.Add((bone, mocopiBone, modelBone));
boneTransformCache.Add((bone, mocopiBone, filteredBone, modelBone));
}
}

Expand All @@ -323,21 +340,32 @@ private void ApplyMotion()
Vector3 defaultHeadPosition = headBone.position;
Quaternion defaultHeadRotation = headBone.rotation;

foreach ((var bone, var source, var target) in boneTransformCache)


float lerpValue = 0.5f;
foreach ((var bone, var source, var filtered, var target) in boneTransformCache)
{
//source to filtered
if (bone == HumanBodyBones.Hips)
{
filtered.localPosition = Vector3.Lerp(filtered.localPosition, source.localPosition, lerpValue);
}
filtered.localRotation = Quaternion.Lerp(filtered.localRotation, source.localRotation, lerpValue);

// filtered to target
bool apply = false;
switch (bone)
{
case HumanBodyBones.Hips:
hipBone = target;
if (Settings.Current.mocopi_ApplyRootRotation)
{
target.localRotation = source.localRotation;
target.localRotation = filtered.localRotation;
target.Rotate(new Vector3(0, centerOffsetRotationY, 0), Space.World);
}
if (Settings.Current.mocopi_ApplyRootPosition)
{
target.localPosition = source.localPosition + centerOffsetPosition; //Root位置だけは同期
target.localPosition = filtered.localPosition + centerOffsetPosition; //Root位置だけは同期
}
break;
case HumanBodyBones.Spine:
Expand Down Expand Up @@ -386,7 +414,7 @@ private void ApplyMotion()
break;
}

if (apply) target.localRotation = source.localRotation;
if (apply) target.localRotation = filtered.localRotation;
}

if (Settings.Current.mocopi_ApplyHead == false && hipBone != null && spineBone != null)
Expand Down

0 comments on commit c0b6654

Please sign in to comment.