Tanoda
SmoothedVector3.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10
11namespace Leap.Unity {
18 [System.Serializable]
19 public class SmoothedVector3 {
20 public Vector3 value = Vector3.zero; // Filtered value
21 public float delay = 0f; // Mean delay
22 public bool reset = true; // Reset on Next Update
23
24 public void SetBlend(float blend, float deltaTime = 1f) {
25 delay = deltaTime * blend / (1f - blend);
26 }
27
28 public Vector3 Update(Vector3 input, float deltaTime = 1f) {
29 if (deltaTime > 0f && !reset) {
30 float alpha = delay / deltaTime;
31 float blend = alpha / (1f + alpha);
32 // NOTE: If delay -> 0 then blend -> 0,
33 // reducing the filter to this.value = value.
34 // NOTE: If deltaTime -> 0 blend -> 1,
35 // so the change in the filtered value will be suppressed
36 value = Vector3.Lerp(this.value, input, 1f - blend);
37 } else {
38 value = input;
39 reset = false;
40 }
41 return value;
42 }
43 }
44}
Time-step independent exponential smoothing.
Vector3 Update(Vector3 input, float deltaTime=1f)
void SetBlend(float blend, float deltaTime=1f)