Skip to content

Commit 5fd0251

Browse files
committed
Added generic interpolation and smoothing framework.
1 parent 4dd3068 commit 5fd0251

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed

Unity/OpenSeeInterpolation.cs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace OpenSee {
6+
public class InterpolatedMap<T, V, U> where V : InterpolatedValue<U>, new() {
7+
private Dictionary<T, V> map = new Dictionary<T, V>();
8+
private float clamp = 1f;
9+
private float smoothing = 0f;
10+
private bool gotDefault = false;
11+
private U defaultValue;
12+
13+
public void Clear() {
14+
map.Clear();
15+
}
16+
17+
public void SetSmoothing(float smoothing) {
18+
this.smoothing = smoothing;
19+
foreach (var entry in map) {
20+
if (entry.Value != null)
21+
entry.Value.SetSmoothing(smoothing);
22+
}
23+
}
24+
25+
public void SetClamp(float clamp) {
26+
this.clamp = clamp;
27+
foreach (var entry in map) {
28+
if (entry.Value != null)
29+
entry.Value.SetClamp(clamp);
30+
}
31+
}
32+
33+
void Prepare(T key) {
34+
if (!map.ContainsKey(key)) {
35+
map.Add(key, new V());
36+
map[key].SetSmoothing(smoothing);
37+
map[key].SetClamp(clamp);
38+
}
39+
}
40+
41+
public void SetValue(T key, U v) {
42+
Prepare(key);
43+
map[key].SetValue(v);
44+
}
45+
46+
public void Store(T key, U v) {
47+
Prepare(key);
48+
map[key].UpdateValue(v);
49+
}
50+
51+
public void Store(T key, U v, float nowT) {
52+
if (!map.ContainsKey(key)) {
53+
map.Add(key, new V());
54+
map[key].SetSmoothing(smoothing);
55+
map[key].SetClamp(clamp);
56+
}
57+
map[key].UpdateValue(v, nowT);
58+
}
59+
60+
public U GetDefault() {
61+
if (gotDefault)
62+
return defaultValue;
63+
V tmp = new V();
64+
defaultValue = tmp.GetDefault();
65+
return defaultValue;
66+
}
67+
68+
public bool Check(T key) {
69+
return map.ContainsKey(key);
70+
}
71+
72+
public U Get(T key) {
73+
if (!map.ContainsKey(key))
74+
return GetDefault();
75+
return map[key].Interpolate();
76+
}
77+
}
78+
79+
abstract public class InterpolatedValue<V> {
80+
private float interpolateT = 0f;
81+
private float updateT = 0f;
82+
private float lastT = 0f;
83+
private float currentT = 0f;
84+
private float clamp = 1f;
85+
private float smoothing = 0f;
86+
private int gotData = 0;
87+
88+
private bool haveDefault = false;
89+
private V defaultValue;
90+
91+
private bool valueSet = false;
92+
private V dataValue;
93+
private V dataValueLast;
94+
95+
abstract public V ValueLerpUnclamped(V a, V b, float t);
96+
abstract public V GetBuiltinDefault();
97+
98+
public void SetDefault(V defaultValue) {
99+
this.defaultValue = defaultValue;
100+
haveDefault = true;
101+
}
102+
103+
public void ResetDefault() {
104+
haveDefault = false;
105+
}
106+
107+
public V GetDefault() {
108+
if (haveDefault)
109+
return defaultValue;
110+
return GetBuiltinDefault();
111+
}
112+
113+
public void SetSmoothing(float smoothing) {
114+
this.smoothing = smoothing;
115+
}
116+
117+
public void SetClamp(float clamp) {
118+
this.clamp = clamp;
119+
}
120+
121+
public void SetValue(V v) {
122+
dataValueLast = v;
123+
dataValue = v;
124+
valueSet = true;
125+
}
126+
127+
public InterpolatedValue(float smoothing = 0f, float clamp = 1f) {
128+
this.smoothing = smoothing;
129+
this.clamp = clamp;
130+
}
131+
132+
public void UpdateValue(V v) {
133+
UpdateValue(v, Time.realtimeSinceStartup);
134+
}
135+
136+
public void UpdateValue(V v, float nowT) {
137+
if (nowT < 0f)
138+
return;
139+
lock (this) {
140+
if (nowT < currentT || gotData == 0) {
141+
// Reinitialize
142+
gotData = 1;
143+
updateT = Time.time;
144+
lastT = 0f;
145+
currentT = nowT;
146+
dataValueLast = GetDefault();
147+
dataValue = ValueLerpUnclamped(v, dataValueLast, smoothing);
148+
} else if (nowT == currentT) {
149+
// Change value
150+
dataValue = ValueLerpUnclamped(v, dataValueLast, smoothing);
151+
} else {
152+
// Regular operation
153+
updateT = Time.time;
154+
lastT = currentT;
155+
currentT = nowT;
156+
if (gotData < 2)
157+
gotData++;
158+
dataValueLast = dataValue;
159+
dataValue = ValueLerpUnclamped(v, dataValueLast, smoothing);
160+
}
161+
}
162+
}
163+
164+
public V Interpolate() {
165+
lock (this) {
166+
interpolateT = Time.time;
167+
if (gotData < 2) {
168+
if (valueSet)
169+
return dataValue;
170+
else
171+
return GetDefault();
172+
}
173+
float t = Mathf.Min((interpolateT - updateT) / (float)(currentT - lastT), clamp);
174+
return ValueLerpUnclamped(dataValueLast, dataValue, t);
175+
}
176+
}
177+
}
178+
179+
public class InterpolatedColor : InterpolatedValue<Color> {
180+
public override Color ValueLerpUnclamped(Color a, Color b, float t) {
181+
return Color.LerpUnclamped(a, b, t);
182+
}
183+
public override Color GetBuiltinDefault() {
184+
return Color.clear;
185+
}
186+
}
187+
188+
public class InterpolatedQuaternion : InterpolatedValue<Quaternion> {
189+
public override Quaternion ValueLerpUnclamped(Quaternion a, Quaternion b, float t) {
190+
return Quaternion.SlerpUnclamped(a, b, t);
191+
}
192+
public override Quaternion GetBuiltinDefault() {
193+
return Quaternion.identity;
194+
}
195+
}
196+
197+
public class InterpolatedVector2 : InterpolatedValue<Vector2> {
198+
public override Vector2 ValueLerpUnclamped(Vector2 a, Vector2 b, float t) {
199+
return Vector2.LerpUnclamped(a, b, t);
200+
}
201+
public override Vector2 GetBuiltinDefault() {
202+
return Vector2.zero;
203+
}
204+
}
205+
206+
public class InterpolatedVector3 : InterpolatedValue<Vector3> {
207+
public override Vector3 ValueLerpUnclamped(Vector3 a, Vector3 b, float t) {
208+
return Vector3.LerpUnclamped(a, b, t);
209+
}
210+
public override Vector3 GetBuiltinDefault() {
211+
return Vector3.zero;
212+
}
213+
}
214+
215+
public class InterpolatedVector4 : InterpolatedValue<Vector4> {
216+
public override Vector4 ValueLerpUnclamped(Vector4 a, Vector4 b, float t) {
217+
return Vector4.LerpUnclamped(a, b, t);
218+
}
219+
public override Vector4 GetBuiltinDefault() {
220+
return Vector4.zero;
221+
}
222+
}
223+
224+
public class InterpolatedFloat : InterpolatedValue<float> {
225+
public override float ValueLerpUnclamped(float a, float b, float t) {
226+
return Mathf.LerpUnclamped(a, b, t);
227+
}
228+
public override float GetBuiltinDefault() {
229+
return 0f;
230+
}
231+
}
232+
}

Unity/OpenSeeInterpolation.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)