cursor refactor and networking core inserted

This commit is contained in:
spatialfree 2021-11-09 00:57:57 -05:00
parent 836dfefb8a
commit 103c872b7b
6 changed files with 186 additions and 95 deletions

43
Assets/colorcube.hlsl Normal file
View file

@ -0,0 +1,43 @@
#include "stereokit.hlsli"
//--name = dofdev/colorcube
// float4 color;
float _height;
float _ypos;
struct vsIn {
float4 pos : SV_POSITION;
float3 norm : NORMAL0;
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
struct psIn {
float4 pos : SV_POSITION;
float3 world : NORMAL0;
float3 norm : NORMAL1;
float2 uv : TEXCOORD0;
float4 color : COLOR0;
uint view_id : SV_RenderTargetArrayIndex;
};
psIn vs(vsIn input, uint id : SV_InstanceID) {
psIn o;
o.view_id = id % sk_view_count;
id = id / sk_view_count;
o.world = mul(input.pos, sk_inst[id].world).xyz;
o.pos = mul(float4(o.world, 1), sk_viewproj[o.view_id]);
o.norm = normalize(mul(input.norm, (float3x3)sk_inst[id].world));
o.uv = input.uv;
o.color = input.col;
float lighting = dot(o.norm, normalize(float3(-0.3, 0.6, 0.1)));
lighting = (clamp(lighting, 0, 1) * 0.8) + 0.2;
o.color.rgb = o.color.rgb * lighting;
return o;
}
float4 ps(psIn input) : SV_TARGET {
input.color.r = 1;
return input.color;
}

View file

@ -2,7 +2,8 @@ using System;
using StereoKit;
class ColorCube {
static Material orbMat = Default.MaterialUnlit.Copy();
static Shader shaderColorCube = Shader.FromFile("colorcube.hlsl");
static Material orbMat = Default.MaterialUnlit.Copy();
static Model orb = new Model(Default.MeshSphere, orbMat);
static Model colorCube = Model.FromFile("colorcube.glb", Shader.UIBox);
Bounds bounds = new Bounds(Vec3.Zero, Vec3.One * 1.25f);
@ -65,6 +66,11 @@ class ColorCube {
orb.Draw(Matrix.TS(matrix * orbPos, _thiccness * 2));
PullRequest.BoundsDraw(bounds, Color.White);
PullRequest.BoundsDraw(bounds, _thiccness, Color.White);
// can't change shader lol
// Default.MaterialUnlit.Shader = shaderColorCube;
// Rods time it is!
// spatialize the stereokit line shader
}
}

View file

@ -1,5 +1,9 @@
using System;
using StereoKit;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
class Program {
static void Main(string[] args) {
@ -21,6 +25,25 @@ public static class Mono {
public static Controller offHand, mainHand;
public static void Run() {
void GetIPs()
{
string localIP;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address.ToString();
}
// Console.WriteLine("Your local IP is: " + localIP);
string publicIP = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd();
// Console.WriteLine("Your IP is: " + publicIP);
}
Peer peer0 = new Peer("peer0");
Peer peer1 = new Peer("peer1");
peer0.Start(1, true);
peer1.Start(2, false);
ColorCube cube = new ColorCube();
OrbitalView.strength = 4;
OrbitalView.distance = 0.4f;
@ -28,12 +51,10 @@ public static class Mono {
ReachCursor reachCursor = new ReachCursor();
SupineCursor supineCursor = new SupineCursor();
BallsCursor ballsCursor = new BallsCursor();
ClawCursor clawCursor = new ClawCursor();
Oriel oriel = new Oriel();
Pose p = new Pose(Vec3.One, Quat.Identity); // ACTUALLY COOL
oriel.Start();
while (SK.Step(() => {
@ -46,20 +67,65 @@ public static class Mono {
// reachCursor.Step();
// supineCursor.Step(
// offHand.aim.orientation,
// mainHand.aim.position,
// mainHand.aim.orientation,
// Mono.mainHand.IsStickClicked
// new Pose(Vec3.Zero, offHand.aim.orientation),
// new Pose(mainHand.aim.position, mainHand.aim.orientation),
// mainHand.IsStickClicked
// );
// clawCursor.Step(
// Input.Head.position - Vec3.Up * 0.2f,
// new Pose(offHand.aim.position, offHand.aim.orientation),
// new Pose(mainHand.aim.position, mainHand.aim.orientation),
// mainHand.IsStickClicked
// );
oriel.Step();
// oriel.Step();
// cursor.Draw(Matrix.S(0.1f));
})) ;
}));
SK.Shutdown();
}
}
public class Peer {
public string name;
public Peer(string name) {
this.name = name;
}
public async void Start(int increment, bool log) {
int port = 1234;
string serverIP = "139.177.201.219";
// try connecting to the server
if (log) Console.WriteLine("{0} attempting to connect to server...", name);
// create a new socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// server endpoint
EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), port);
socket.Connect(serverEndPoint);
// send a message to the server
if (log) Console.WriteLine("{0} sending message to server...", name);
int test = 0;
// send every 0.1 seconds
while (true) {
// send a message to the server
test += increment;
socket.SendTo(BitConverter.GetBytes(test), serverEndPoint);
// receive a message from the server
byte[] data = new byte[1024];
socket.ReceiveFrom(data, ref serverEndPoint);
if (log) Console.WriteLine("{0} received from server: {1}", name, BitConverter.ToInt32(data, 0));
// sleep for 0.1 seconds
await Task.Delay(100);
}
}
}
public class Oriel {
public Bounds bounds;
@ -86,16 +152,18 @@ public class Oriel {
}
public static class PullRequest {
public static Vec3 VecMulti(Vec3 a, Vec3 b) { return new Vec3(a.x * b.x, a.y * b.y, a.z * b.z); }
public static void BoundsDraw(Bounds b, Color color) {
public static void BoundsDraw(Bounds b, float thickness, Color color) {
Vec3 c = Vec3.One / 2;
Vec3 ds = b.dimensions;
for (int i = 0; i < 4; i++) {
Quat q = Quat.FromAngles(i * 90, 0, 0);
Lines.Add(q * VecMulti(new Vec3(0, 0, 0) - c, ds), q * VecMulti(new Vec3(0, 1, 0) - c, ds), color, color, 0.01f);
Lines.Add(q * VecMulti(new Vec3(0, 1, 0) - c, ds), q * VecMulti(new Vec3(1, 1, 0) - c, ds), color, color, 0.01f);
Lines.Add(q * VecMulti(new Vec3(1, 1, 0) - c, ds), q * VecMulti(new Vec3(1, 0, 0) - c, ds), color, color, 0.01f);
Lines.Add(q * (new Vec3(0, 0, 0) - c) * ds, q * (new Vec3(0, 1, 0) - c) * ds, color, color, thickness);
Lines.Add(q * (new Vec3(0, 1, 0) - c) * ds, q * (new Vec3(1, 1, 0) - c) * ds, color, color, thickness);
Lines.Add(q * (new Vec3(1, 1, 0) - c) * ds, q * (new Vec3(1, 0, 0) - c) * ds, color, color, thickness);
// convert to linepoints
}
}
// amplify quaternions (q * q * lerp(q.i, q, %))
}

View file

@ -1,5 +1,5 @@
## oriels
*a dof oriented os*
*a dof driven os*
<img src='oriel.gif'>
<https://dofdev.org>
@ -16,3 +16,17 @@
- implement all dofs from site
- merge networking prototypes
- architect interfaces for navigation
## stream
- ~~refactor cursors to pose data ~30m~~
- ~~claw cursor ~30m~~
- ~~refactor networking code to async based on video ~1hr~~
- ~~haul the networking into stereokit ~30m~~
pb = 2:30 (end at 1:30)
- do something spatial with the data
- make a batch script for niko so he can test things easily
- connect dof site data to oriels (networking will help with capturing gifs)

View file

@ -9,31 +9,19 @@ public abstract class SpatialCursor {
public static Model model = Model.FromFile("cursor.glb", Shader.Default);
}
// : SpatialCursor
public class ReachCursor {
static Material unlitMat = Default.MaterialUnlit.Copy();
static Model modelCursor = Model.FromFile("cursor.glb", Shader.Default);
static Model modelSphere = new Model(Default.MeshSphere, unlitMat);
// this is just a stretch cursor derivative
public class ReachCursor : SpatialCursor {
static Vec3 origin;
public void Step(Vec3 mainPos, bool calibrate) {
float stretch = (origin - mainPos).Length;
Vec3 dir = (mainPos - origin).Normalized;
Vec3 pos = mainPos + dir * stretch * 3;
model.Draw(Matrix.TS(pos, 0.06f));
Lines.Add(origin, pos, Color.White, 0.01f);
model.Draw(Matrix.TS(origin, 0.04f));
static Vec3[] pullPoints = new Vec3[2];
public void Step() {
for (int h = 0; h < (int)Handed.Max; h++) {
// Get the pose for the index fingertip
Hand hand = Input.Hand((Handed)h);
Vec3 indexTip = hand[FingerId.Index, JointId.Tip].Pose.position;
Vec3 thumbTip = hand[FingerId.Thumb, JointId.Tip].Pose.position;
Vec3 pinchPos = Vec3.Lerp(indexTip, thumbTip, 0.5f);
if (hand.IsPinched) {
pullPoints[h] = pinchPos;
}
float stretch = (pullPoints[h] - pinchPos).Length;
Vec3 dir = (pinchPos - pullPoints[h]).Normalized;
Vec3 pos = pinchPos + dir * stretch * 3;
modelCursor.Draw(Matrix.TS(pos, 0.06f));
Lines.Add(pullPoints[h], pos, Color.White, 0.01f);
modelSphere.Draw(Matrix.TS(pullPoints[h], 0.04f));
if (calibrate) {
origin = mainPos;
}
}
}
@ -48,75 +36,45 @@ public class TwistCursor : SpatialCursor {
}
}
// a more symmetrical one would be cool
public class SupineCursor : SpatialCursor {
float calibStr;
Quat calibQuat;
public void Step(Quat offQuat, Vec3 mainPos, Quat mainQuat, bool calibrate = false) {
Quat rel = Quat.LookAt(Vec3.Zero, offQuat * Vec3.Forward);
float twist = (Vec3.Dot(rel * -Vec3.Right, offQuat * Vec3.Up) + 1) / 2;
public void Step(Pose offPose, Pose mainPose, bool calibrate) {
Quat rel = Quat.LookAt(Vec3.Zero, offPose.orientation * Vec3.Forward);
float twist = (Vec3.Dot(rel * -Vec3.Right, offPose.orientation * Vec3.Up) + 1) / 2;
pos = mainPos + mainQuat * calibQuat * Vec3.Forward * calibStr * twist;
pos = mainPose.position + mainPose.orientation * calibQuat * Vec3.Forward * calibStr * twist;
if (calibrate) {
Vec3 target = Input.Head.position + Input.Head.Forward;
calibStr = Vec3.Distance(mainPos, target) * 2;
calibStr = Vec3.Distance(mainPose.position, target) * 2;
Quat calibAlign = Quat.LookAt(mainPos, target);
calibQuat = mainQuat.Inverse * calibAlign;
Quat calibAlign = Quat.LookAt(mainPose.position, target);
calibQuat = mainPose.orientation.Inverse * calibAlign;
}
model.Draw(Matrix.TS(pos, 0.06f));
}
}
public class BallsCursor : SpatialCursor {
Vec3 offPivot, mainPivot;
Quat offBall, mainBall;
Quat oldOffQuat, oldMainQuat;
float offRadius, mainRadius;
bool offPress, mainPress;
public void Step(
Vec3 offPos, Quat offQuat,
Vec3 mainPos, Quat mainQuat,
bool offGrip, bool mainGrip, bool calibrate = false
) {
// for fun
public class ClawCursor : SpatialCursor {
Quat calibOff, calibMain;
public void Step(Vec3 chest, Pose offPose, Pose mainPose, bool calibrate) {
float wingspan = 0.5f;
Quat offQuat = calibOff * offPose.orientation;
Quat mainQuat = calibMain * mainPose.orientation;
Vec3 elbow = chest + mainQuat * mainQuat * Vec3.Forward * wingspan;
pos = elbow + offQuat * offQuat * Vec3.Forward * wingspan;
Function(offPos, ref offPivot, ref offBall, offQuat, ref oldOffQuat, ref offRadius, offGrip, ref offPress);
Function(mainPos, ref mainPivot, ref mainBall, mainQuat, ref oldMainQuat, ref mainRadius, mainGrip, ref mainPress);
void Function(Vec3 handPos, ref Vec3 pivot, ref Quat ball, Quat quat, ref Quat oldQuat, ref float radius, bool grip, ref bool press) {
if (grip) {
if (!press) {
pivot = pos + ball * Vec3.Forward * radius;
oldQuat = quat;
press = true;
}
ball = Quat.Difference(oldQuat, quat) * ball;
pos = pivot + (ball * -Vec3.Forward * radius);
oldQuat = quat;
}
else {
press = false;
}
// Sphere sphere = new Sphere(pos, mainDiameter);
Default.MeshSphere.Draw(
clearMat,
Matrix.TS(pos + ball * Vec3.Forward * radius, radius * 2)
);
}
if (calibrate)
{
pos = Vec3.Lerp(offPos, mainPos, 0.5f);
offRadius = mainRadius = Vec3.Distance(offPos, mainPos) / 2;
offBall = Quat.LookAt(mainPos, offPos);
mainBall = Quat.LookAt(offPos, mainPos);
if (calibrate) {
calibOff = offPose.orientation.Inverse;
calibMain = mainPose.orientation.Inverse;
}
Lines.Add(chest, elbow, Color.White, 0.01f);
Lines.Add(elbow, pos, Color.White, 0.01f);
model.Draw(Matrix.TS(pos, 0.06f));
clearMat.SetColor("color", new Color(1, 1, 1, 0.1f));
}
Material clearMat = Default.MaterialHand;
}

View file

@ -20,11 +20,13 @@
<ItemGroup>
<None Remove="Assets/example.hlsl" />
<None Remove="Assets/oriel.hlsl" />
<None Remove="Assets/colorcube.hlsl" />
</ItemGroup>
<ItemGroup>
<SKShader Include="Assets/example.hlsl" />
<SKShader Include="Assets/oriel.hlsl" />
<SKShader Include="Assets/colorcube.hlsl" />
</ItemGroup>
</Project>