forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuaternionInterpolated.cs
More file actions
92 lines (82 loc) · 2.74 KB
/
QuaternionInterpolated.cs
File metadata and controls
92 lines (82 loc) · 2.74 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
using System;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Class to encapsulate an interpolating Quaternion property.
/// TODO: Remove if redundant to InterpolatedQuaternion.cs
/// </summary>
[Serializable]
public class QuaternionInterpolated
{
/// <summary>
/// Speed of change in magnitude.
/// </summary>
public float DeltaSpeed = 360f;
/// <summary>
/// Current value of the property.
/// </summary>
public Quaternion Value { get; private set; }
/// <summary>
/// Target value of the property.
/// </summary>
public Quaternion TargetValue { get; private set; }
public Quaternion StartValue { get; private set; }
public float Duration { get; private set; }
public float Counter { get; private set; }
public QuaternionInterpolated()
{
Reset(Quaternion.identity);
}
public QuaternionInterpolated(Quaternion initialValue)
{
Reset(initialValue);
}
/// <summary>
/// Resets property to zero interpolation and set value.
/// </summary>
/// <param name="value">Desired value to reset</param>
public void Reset(Quaternion value)
{
Value = value;
TargetValue = value;
StartValue = value;
Duration = 0f;
Counter = 0f;
}
/// <summary>
/// Set a target for property to interpolate to.
/// </summary>
/// <param name="targetValue">Targeted value.</param>
public void SetTarget(Quaternion targetValue)
{
TargetValue = targetValue;
StartValue = Value;
Duration = Quaternion.Angle(StartValue, TargetValue) / DeltaSpeed;
Counter = 0f;
}
/// <summary>
/// Returns whether there are further updates required to get the target value.
/// </summary>
/// <returns>True if updates are required. False otherwise.</returns>
public bool HasUpdate()
{
return Quaternion.Angle(TargetValue, Value) > 0.05f;
}
/// <summary>
/// Performs and fets the updated value.
/// </summary>
/// <param name="deltaTime">Tick delta.</param>
/// <returns>Updated value.</returns>
public Quaternion GetUpdate(float deltaTime)
{
Counter += deltaTime;
Value = Quaternion.Slerp(StartValue, TargetValue, Counter / Duration);
return Value;
}
}
}