Skip to content

Commit

Permalink
Merge pull request #2445 from ousttrue/fix/vrm10_lookat_initialization
Browse files Browse the repository at this point in the history
lookAt は root と同じ向きが初期姿勢
  • Loading branch information
ousttrue authored Sep 25, 2024
2 parents af06834 + 4196b2d commit d6e920b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Vrm10Runtime(Vrm10Instance instance, bool useControlRig, IVrm10SpringBone
ControlRig = new Vrm10RuntimeControlRig(instance.Humanoid, m_instance.transform);
}
Constraints = instance.GetComponentsInChildren<IVrm10Constraint>();
LookAt = new Vrm10RuntimeLookAt(instance.Vrm.LookAt, instance.Humanoid, ControlRig);
LookAt = new Vrm10RuntimeLookAt(instance, instance.Humanoid, ControlRig);
Expression = new Vrm10RuntimeExpression(instance, LookAt.EyeDirectionApplicable);
SpringBone = springBoneRuntime;
}
Expand Down
45 changes: 35 additions & 10 deletions Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ public sealed class Vrm10RuntimeLookAt : ILookAtEyeDirectionProvider
/// </summary>
public Transform LookAtOriginTransform { get; }

internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookAt, UniHumanoid.Humanoid humanoid, Vrm10RuntimeControlRig controlRig)
internal Vrm10RuntimeLookAt(Vrm10Instance instance, UniHumanoid.Humanoid humanoid, Vrm10RuntimeControlRig controlRig)
{
_lookAt = lookAt;
_lookAt = instance.Vrm.LookAt;

LookAtOriginTransform = InitializeLookAtOriginTransform(
humanoid.Head,
controlRig != null ? controlRig.GetBoneTransform(HumanBodyBones.Head) : humanoid.Head,
_lookAt.OffsetFromHead);
humanoid,
controlRig,
_lookAt.OffsetFromHead,
instance.transform.rotation);

_lookAtOriginTransformLocalPosition = LookAtOriginTransform.localPosition;
_lookAtOriginTransformLocalRotation = LookAtOriginTransform.localRotation;

Expand Down Expand Up @@ -96,13 +99,35 @@ public void SetYawPitchManually(float yaw, float pitch)
return (yaw, pitch);
}

private static Transform InitializeLookAtOriginTransform(Transform rawHead, Transform actualHead, Vector3 eyeOffsetValue)
/// <summary>
/// Generate empty object for gaze calculation.
/// NOTE: このメソッドを実行するとき、モデル全体は初期姿勢(T-Pose)でなければならない。
/// NOTE: Vrm10Instance.Runtime 呼び出しによりトリガーされる。
/// From v0.127.0: VRM Root( Vrm10Instance が Add されている)GameObject は初期姿勢でなくてもよい #2445
/// </summary>
/// <param name="humanoid"></param>
/// <param name="controlRig">if provided parent is controlrig head else humanoid head</param>
/// <param name="eyeOffsetValue">A humanoid head local offset</param>
/// <param name="rootRotation">world rotation of vrm model</param>
/// <returns></returns>
private static Transform InitializeLookAtOriginTransform(UniHumanoid.Humanoid humanoid, Vrm10RuntimeControlRig controlRig,
Vector3 eyeOffsetValue, Quaternion rootRotation)
{
// NOTE: このメソッドを実行するとき、モデル全体は初期姿勢(T-Pose)でなければならない。
var lookAtOrigin = new GameObject("_look_at_origin_").transform;
lookAtOrigin.SetParent(actualHead);
lookAtOrigin.position = rawHead.TransformPoint(eyeOffsetValue);
lookAtOrigin.rotation = Quaternion.identity;
if (controlRig != null)
{
// controlRig のHeadに連結
lookAtOrigin.SetParent(controlRig.GetBoneTransform(HumanBodyBones.Head));
}
else
{
// humanoid のHeadに連結
lookAtOrigin.SetParent(humanoid.Head);
}

lookAtOrigin.position = humanoid.Head.TransformPoint(eyeOffsetValue);
// v0.127.0
lookAtOrigin.rotation = rootRotation;

return lookAtOrigin;
}
Expand Down

0 comments on commit d6e920b

Please sign in to comment.