Skip to content

Instantly share code, notes, and snippets.

@maluoi
Last active April 29, 2022 23:11
Show Gist options
  • Save maluoi/59fd1e8b28903c1edcd250701d76246a to your computer and use it in GitHub Desktop.
Save maluoi/59fd1e8b28903c1edcd250701d76246a to your computer and use it in GitHub Desktop.
A quick example of lerp-based animations with StereoKit
// This demo can be seen in motion here:
// https://twitter.com/koujaku/status/1407538105070997504
using StereoKit;
using System;
class Program {
static void Main(string[] args) {
// Initialize StereoKit
if (!SK.Initialize(new SKSettings { appName = "LerpAnim" }))
Environment.Exit(1);
Pose window = new Pose(0,0,-0.4f, Quat.LookDir(0,0,1));
float start=0, length=0;
float curr =0, duration=0;
while (SK.Step(() =>
{
UI.WindowBegin("Press things here!", ref window);
if (UI.Button("Start/Length")) {
start = Time.Totalf;
length = 0.5f;
}
if (UI.Button("Duration/Curr")) {
curr = 0;
duration = 0.5f;
}
UI.WindowEnd();
curr += Time.Elapsedf;
float cdPercent = duration == 0 ? 0 : MathF.Min(1, curr / duration);
Default.MeshSphere.Draw(Default.Material,
Matrix.TS(V.XYZ(-0.3f, 0, -0.5f), SKMath.Lerp(0.15f, 0.25f, cdPercent)));
float slPercent = length == 0 ? 0 : MathF.Min(1, (Time.Totalf - start) / length);
Default.MeshSphere.Draw(Default.Material,
Matrix.TS(V.XYZ( 0.3f, 0, -0.5f), SKMath.Lerp(0.15f, 0.25f, slPercent)));
// Remap them both to bounces, see https://github.com/Michaelangel007/easing
Func<float, float> OutBack = (p) => {
float m = p - 1;
float k = 1.70158f;
return 1 + m * m * (m * (k + 1) + k);
};
Default.MeshSphere.Draw(Default.Material,
Matrix.TS(V.XYZ(-0.3f, 0.3f, -0.5f), SKMath.Lerp(0.15f, 0.25f, OutBack(cdPercent))));
Default.MeshSphere.Draw(Default.Material,
Matrix.TS(V.XYZ( 0.3f, 0.3f, -0.5f), SKMath.Lerp(0.15f, 0.25f, OutBack(slPercent))));
})) ;
SK.Shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment