VRMLookAtBlendShapeApplyer.cs
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma warning disable 0414, 0649
using UnityEngine;
namespace VRM
{
public class VRMLookAtBlendShapeApplyer : MonoBehaviour, IVRMComponent
{
public bool DrawGizmo = true;
[SerializeField, Header("Degree Mapping")]
public CurveMapper Horizontal = new CurveMapper(90.0f, 1.0f);
[SerializeField]
public CurveMapper VerticalDown = new CurveMapper(90.0f, 1.0f);
[SerializeField]
public CurveMapper VerticalUp = new CurveMapper(90.0f, 1.0f);
[SerializeField]
public bool m_notSetValueApply;
public void OnImported(VRMImporterContext context)
{
var gltfFirstPerson = context.GLTF.extensions.VRM.firstPerson;
Horizontal.Apply(gltfFirstPerson.lookAtHorizontalOuter);
VerticalDown.Apply(gltfFirstPerson.lookAtVerticalDown);
VerticalUp.Apply(gltfFirstPerson.lookAtVerticalUp);
}
VRMLookAtHead m_head;
VRMBlendShapeProxy m_propxy;
private void Start()
{
m_head = GetComponent<VRMLookAtHead>();
m_propxy = GetComponent<VRMBlendShapeProxy>();
if (m_head == null)
{
enabled = false;
return;
}
m_head.YawPitchChanged += ApplyRotations;
}
void ApplyRotations(float yaw, float pitch)
{
#pragma warning disable 0618
if (yaw < 0)
{
// Left
m_propxy.SetValue(BlendShapePreset.LookRight, 0, !m_notSetValueApply); // clear first
m_propxy.SetValue(BlendShapePreset.LookLeft, Mathf.Clamp(Horizontal.Map(-yaw), 0, 1.0f), !m_notSetValueApply);
}
else
{
// Right
m_propxy.SetValue(BlendShapePreset.LookLeft, 0, !m_notSetValueApply); // clear first
m_propxy.SetValue(BlendShapePreset.LookRight, Mathf.Clamp(Horizontal.Map(yaw), 0, 1.0f), !m_notSetValueApply);
}
if (pitch < 0)
{
// Down
m_propxy.SetValue(BlendShapePreset.LookUp, 0, !m_notSetValueApply); // clear first
m_propxy.SetValue(BlendShapePreset.LookDown, Mathf.Clamp(VerticalDown.Map(-pitch), 0, 1.0f), !m_notSetValueApply);
}
else
{
// Up
m_propxy.SetValue(BlendShapePreset.LookDown, 0, !m_notSetValueApply); // clear first
m_propxy.SetValue(BlendShapePreset.LookUp, Mathf.Clamp(VerticalUp.Map(pitch), 0, 1.0f), !m_notSetValueApply);
}
#pragma warning restore 0618
}
}
}