Skip to content

Instantly share code, notes, and snippets.

@LittleFinix
Last active September 14, 2021 07:38
Show Gist options
  • Save LittleFinix/92c355efd3f935b2d20ffca2ea4c61c3 to your computer and use it in GitHub Desktop.
Save LittleFinix/92c355efd3f935b2d20ffca2ea4c61c3 to your computer and use it in GitHub Desktop.
OVRSharp Transformation Demo
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Numerics;
using OVRSharp;
using OVRSharp.Math;
using Valve.VR;
var app = new Application(Application.ApplicationType.Overlay);
while (!app.OVRSystem.IsTrackedDeviceConnected(OpenVR.k_unTrackedDeviceIndex_Hmd))
Thread.Sleep(100);
List<Overlay> overlays = new();
Overlay Create(string? name = null)
{
using var bmp = new Bitmap(100, 100);
name ??= "unknown." + Guid.NewGuid();
{
using var grph = Graphics.FromImage(bmp);
grph.Clear(Color.White);
var font = SystemFonts.CaptionFont ?? SystemFonts.DefaultFont;
font = new Font(font.FontFamily, 20f, FontStyle.Bold);
var size = grph.MeasureString(name, font);
grph.DrawString(name, font, Brushes.Black, new PointF((bmp.Width - size.Width) / 2, (bmp.Height - size.Height) / 2));
grph.Flush();
}
var path = Path.GetTempFileName();
using var stream = File.OpenWrite(path);
bmp.Save(stream, ImageFormat.Png);
Thread.Sleep(100);
var o = new Overlay("test." + name, "TEST") {
WidthInMeters = 0.25f,
};
Thread.Sleep(100);
o.SetTextureFromFile(path);
o.SetFlag(VROverlayFlags.SortWithNonSceneOverlays, true);
o.WidthInMeters = 0.25f;
o.Transform = default;
o.Show();
overlays.Add(o);
return o;
}
const float frameCount = 100;
void AnimateVector(int frame, int frameStart, out Vector3 mat, Vector3 from, Vector3 to)
{
frame -= frameStart;
if (frame <= 0)
mat = from;
else if (frame >= frameCount)
mat = to;
else
mat = Vector3.Lerp(from, to, frame / frameCount);
}
void AnimateTranslate(int frame, int frameStart, out Matrix4x4 mat, Vector3 from, Vector3 to)
{
AnimateVector(frame, frameStart, out var v, from, to);
mat = Matrix4x4.CreateTranslation(v);
}
void AnimateRotate(int frame, int frameStart, out Matrix4x4 mat, Vector3 from, Vector3 to)
{
AnimateVector(frame, frameStart, out var v, from, to);
mat = Matrix4x4.CreateFromYawPitchRoll(v.Y, v.X, v.Z);
}
var TEST = Create("TEST");
var PARENT = Create("PARENT");
try
{
var localPos = Vector3.UnitX * 0.25f;
var localRot = Vector3.UnitY * (MathF.PI / -2);
var parentPos = Vector3.UnitZ * -0.5f;
var parentRot = Vector3.UnitY * (MathF.PI / 4);
TEST.Transform = Matrix4x4.Identity.ToHmdMatrix3x4();
PARENT.Transform = Matrix4x4.Identity.ToHmdMatrix3x4();
for (int i = 0; i < 400; i++)
{
if (i % 100 == 0)
{
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey(intercept: true);
}
AnimateTranslate(i, 0, out var lt, Vector3.Zero, localPos);
AnimateRotate(i, 100, out var lr, Vector3.Zero, localRot);
AnimateRotate(i, 200, out var pr, Vector3.Zero, parentRot);
AnimateTranslate(i, 300, out var pt, Vector3.Zero, parentPos);
var local = lr * lt;
var parent = pr * pt;
var final = local * parent;
PARENT.Transform = parent.ToHmdMatrix34_t();
TEST.Transform = final.ToHmdMatrix34_t();
Console.Write($"\rFrame: {i}");
await Task.Delay(10);
}
Console.WriteLine();
Console.WriteLine("Press any key to quit.");
Console.ReadKey(intercept: true);
}
finally
{
foreach (var o in overlays)
o.Destroy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment