-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagalong.cs
More file actions
54 lines (44 loc) · 2.35 KB
/
Tagalong.cs
File metadata and controls
54 lines (44 loc) · 2.35 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
/// Billboard and Tagalong
/// Calculates the pose of an object so that it follows the user and always faces the user
namespace SKTagalong
{
using StereoKit;
public static class UIExtension
{
public static void TagAlongBegin(ref Pose pose, ref Vec3 targetPos, ref Vec3 nextTargetPos, float forwardDistance = 0.5f, float lerpBlend = 0.05f)
{
Pose head = Input.Head;
// The next target position will always be in front of the user.
nextTargetPos = head.position + head.Forward * forwardDistance;
// Tagalong, move the window along with user at the previous target.
pose.position = Vec3.Lerp(pose.position, targetPos, lerpBlend);
// Billboarding always faces the user.
pose.orientation = Quat.LookAt(pose.position, head.position);
}
public static void TagAlongBegin(ref Pose pose, ref Vec3 targetPos, ref Vec3 nextTargetPos, Vec2 offset, float forwardDistance = 0.5f, float lerpBlend = 0.05f)
{
Pose head = Input.Head;
// The next target position will always be in front of the user.
nextTargetPos = head.position + head.Forward * forwardDistance + head.Right * offset.x + head.Up * offset.y;
// Tagalong, move the window along with user at the previous target.
pose.position = Vec3.Lerp(pose.position, targetPos, lerpBlend);
// Billboarding always faces the user.
pose.orientation = Quat.LookAt(pose.position, head.position);
}
public static void TagAlongEnd(ref Pose pose, ref Vec3 targetPos, ref Vec3 nextTargetPos, float collisionRadius = 0.2f)
{
// Set the new target position for when it is outside the user's FOV
// so that we lerp only when the new window position is out of range.
if (!pose.position.InRadius(nextTargetPos, collisionRadius))
{
targetPos = nextTargetPos;
}
#if DEBUG
Material transparentMat = Material.Default.Copy();
transparentMat.Transparency = Transparency.Add;
Mesh.Sphere.Draw(transparentMat, Matrix.TS(targetPos, collisionRadius), Color.Hex(0x0000FFFF));
Mesh.Sphere.Draw(transparentMat, Matrix.TS(nextTargetPos, collisionRadius), Color.Hex(0xFF0000FF));
#endif
}
}
}