Tanoda
SmoothedFloat.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 SmoothedFloat {
20 public float value = 0f; // Filtered value
21 public float delay = 0f; // Mean delay
22 public bool reset = true; // Reset on Next Update
23
24 public SmoothedFloat(float blend = 0f, float deltaTime = 1f) { SetBlend(blend); }
25
26 public void SetBlend(float blend, float deltaTime = 1f) {
27 delay = deltaTime * blend / (1f - blend);
28 }
29
30 public float Update(float input, float deltaTime = 1f) {
31 if (deltaTime > 0f && !reset) {
32 float alpha = delay / deltaTime;
33 float blend = alpha / (1f + alpha);
34 // NOTE: If delay -> 0 then blend -> 0,
35 // reducing the filter to this.value = value.
36 // NOTE: If deltaTime -> 0 blend -> 1,
37 // so the change in the filtered value will be suppressed
38 value = Mathf.Lerp(value, input, 1f - blend);
39 } else {
40 value = input;
41 reset = false;
42 }
43 return value;
44 }
45 }
46}
Time-step independent exponential smoothing.
void SetBlend(float blend, float deltaTime=1f)
SmoothedFloat(float blend=0f, float deltaTime=1f)
float Update(float input, float deltaTime=1f)