Skip to content

Instantly share code, notes, and snippets.

@hiepnd
Created October 4, 2016 10:26
Show Gist options
  • Save hiepnd/414a1f4d10b0d6b245880d92742eca6e to your computer and use it in GitHub Desktop.
Save hiepnd/414a1f4d10b0d6b245880d92742eca6e to your computer and use it in GitHub Desktop.
TransformState p0, p1;
void Update2 (Transform transform, float dt) {
time += dt;
if (time < playOutDelay)
return;
if (!interpolating) {
if (buffer.Count >= 1) {
p0 = buffer [0];
interpolationStartTime = time;
interpolationEndTime = interpolationStartTime;
interpolationStartSequence = interpolationEndSequence = p0.sequence;
interpolating = true;
} else {
Debug.LogError ("No frame to go !!!");
}
}
if (!interpolating) {
return;
}
// Find other frame
if (time >= interpolationEndTime ) {
buffer.RemoveAll (x => x.sequence < interpolationEndSequence);
interpolationStartTime = interpolationEndTime;
interpolationStartSequence = interpolationEndSequence;
p0 = buffer [0];
p1 = buffer.Find (x => x.sequence > p0.sequence);
Assert.IsTrue (interpolationStartSequence == p0.sequence, "WRONG");
if (p1 != null) {
interpolationEndTime = interpolationStartTime + p1.time - p0.time;
interpolationEndSequence = p1.sequence;
} else {
Debug.Log ("No frame to GO");
}
}
if (time >= interpolationEndTime + 0.00001f) {
return;
}
Assert.IsTrue (p0 != null && p1 != null, "Must not be NULL");
float t = Mathf.Clamp01 ((time - interpolationStartTime) /(interpolationEndTime - interpolationStartTime));
Interpolate (transform, t, interpolationEndTime - interpolationStartTime);
}
void Interpolate (Transform transform, float t, float duration) {
transform.rotation = Quaternion.Slerp (p0.rotation, p1.rotation, t);
if (mode == Interpolation.NONE) {
if (t >= 1) {
transform.position = p1.position;
}
} else if (mode == Interpolation.LINEAR) {
transform.position = LinearInterpolation (p0.position, p1.position, t);
} else if (mode == Interpolation.HERMITE) {
transform.position = HermiteSpline (p0.position, p1.position,
p0.velocity * duration,
p1.velocity * duration,
t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment