flatbuffers?
This commit is contained in:
parent
2791f26484
commit
a687b25504
9 changed files with 481 additions and 184 deletions
|
@ -13,8 +13,9 @@ struct vsIn {
|
|||
};
|
||||
struct psIn {
|
||||
float4 pos : SV_POSITION;
|
||||
float3 world : NORMAL0;
|
||||
float3 norm : NORMAL1;
|
||||
float3 campos : NORMAL0;
|
||||
float3 world : NORMAL1;
|
||||
float3 norm : NORMAL2;
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 color : COLOR0;
|
||||
uint view_id : SV_RenderTargetArrayIndex;
|
||||
|
@ -25,6 +26,7 @@ psIn vs(vsIn input, uint id : SV_InstanceID) {
|
|||
o.view_id = id % sk_view_count;
|
||||
id = id / sk_view_count;
|
||||
|
||||
o.campos = sk_camera_pos[o.view_id].xyz;
|
||||
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));
|
||||
|
@ -37,19 +39,59 @@ psIn vs(vsIn input, uint id : SV_InstanceID) {
|
|||
return o;
|
||||
}
|
||||
|
||||
float4 ps(psIn input) : SV_TARGET {
|
||||
if (input.world.y - _ypos > (_height / 2.0) - 0.06) {
|
||||
// brighten;
|
||||
input.color.r += (1.0 - input.color.r) / 2.0;
|
||||
input.color.g += (1.0 - input.color.g) / 2.0;
|
||||
input.color.b += (1.0 - input.color.b) / 2.0;
|
||||
return input.color;
|
||||
float3 cross(float3 a, float3 b) {
|
||||
return float3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
}
|
||||
|
||||
float dot(float3 a, float3 b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
float tri_raycast(float3 origin, float3 dir) {
|
||||
float final = -1;
|
||||
float3 v0 = float3(0, 0, 0);
|
||||
float3 v1 = float3(0, 0, 1);
|
||||
float3 v2 = float3(1, 0, 0);
|
||||
float3 e1 = v1 - v0;
|
||||
float3 e2 = v2 - v0;
|
||||
float3 h = cross(dir, e2);
|
||||
float a = dot(e1, h);
|
||||
if (a > -0.00001 && a < 0.00001) {} else{
|
||||
float f = 1 / a;
|
||||
float3 s = origin - v0;
|
||||
float u = f * dot(s, h);
|
||||
if (u < 0.0 || u > 1.0) {} else {
|
||||
float3 q = cross(s, e1);
|
||||
float v = f * dot(dir, q);
|
||||
if (v < 0.0 || u + v > 1.0) {} else {
|
||||
float t = f * dot(e2, q);
|
||||
if (t > 0.00001) { final = 1.0;} // t
|
||||
}
|
||||
}
|
||||
}
|
||||
return final;
|
||||
}
|
||||
|
||||
float4 ps(psIn input) : SV_TARGET {
|
||||
// if (input.world.y - _ypos > (_height / 2.0) - 0.06) {
|
||||
// // brighten;
|
||||
// input.color.r += (1.0 - input.color.r) / 2.0;
|
||||
// input.color.g += (1.0 - input.color.g) / 2.0;
|
||||
// input.color.b += (1.0 - input.color.b) / 2.0;
|
||||
// return input.color;
|
||||
// }
|
||||
|
||||
// clamp how dark the object is *hsv
|
||||
// float value = input.color.r * 0.3 + input.color.g * 0.59 + input.color.b * 0.11;
|
||||
// blue tint
|
||||
input.color.r /= 5.0;
|
||||
input.color.g /= 5.0;
|
||||
// input.color.a = 0.5;
|
||||
|
||||
// raycast or raymarch
|
||||
float3 ray = normalize(input.world - input.campos);
|
||||
input.color = float4(float3(1,1,1) * max(tri_raycast(input.world, ray), 0.0), 1);
|
||||
|
||||
|
||||
return input.color;
|
||||
}
|
167
MonoNet.cs
Normal file
167
MonoNet.cs
Normal file
|
@ -0,0 +1,167 @@
|
|||
using StereoKit;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using FlatBuffers;
|
||||
// using NetData;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
|
||||
public class MonoNet {
|
||||
public int myID;
|
||||
public MonoNet(int myID) {
|
||||
this.myID = myID;
|
||||
}
|
||||
public Socket socket;
|
||||
// public NetworkStream stream;
|
||||
byte[] data;
|
||||
public Peer[] peers;
|
||||
|
||||
public Vec3 cursor; // are these stored here???
|
||||
public Pose head;
|
||||
public Pose offHand;
|
||||
public Pose mainHand;
|
||||
|
||||
public async void Start() {
|
||||
string publicIP, localIP;
|
||||
// GetIPs();
|
||||
void GetIPs()
|
||||
{
|
||||
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);
|
||||
publicIP = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd();
|
||||
// Console.WriteLine("Your IP is: " + publicIP);
|
||||
}
|
||||
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
string ip = "192.168.1.70";
|
||||
// ip = "139.177.201.219";
|
||||
EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), 1234);
|
||||
socket.Connect(serverEndPoint);
|
||||
data = new byte[1024];
|
||||
peers = new Peer[4];
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
bool running = true;
|
||||
while (running) {
|
||||
while (socket.Available > 0) {
|
||||
socket.Receive(data, 0, data.Length, SocketFlags.None);
|
||||
ByteBuffer bb = new ByteBuffer(data);
|
||||
|
||||
NetData.Peer peer = NetData.Peer.GetRootAsPeer(bb);
|
||||
int id = peer.Id;
|
||||
if (id != 0) {
|
||||
for (int i = 0; i < peers.Length; i++) {
|
||||
if (peers[i] != null) {
|
||||
if (peers[i].id == id) {
|
||||
peers[i].cursor = NetVec3(peer.Cursor.Value);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (peer.Cursor.HasValue) {
|
||||
peers[i] = new Peer(id, NetVec3(peer.Cursor.Value));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
|
||||
NetData.Peer.StartPeer(fbb);
|
||||
// // fbb.AddStruct(NetPose(head.ToNetPose()));
|
||||
// NetData.Peer.AddCursor(fbb, NetVec3(cursor));
|
||||
// NetData.Peer.AddHead(fbb,
|
||||
// NetPose()
|
||||
// NetData.Vec3.CreateVec3(fbb, head.)
|
||||
// );
|
||||
NetData.Peer.AddId(fbb, myID);
|
||||
NetData.Peer.AddCursor(fbb, Vec3Net(cursor, ref fbb));
|
||||
var p = NetData.Peer.EndPeer(fbb);
|
||||
fbb.Finish(p.Value);
|
||||
socket.Send(fbb.SizedByteArray());
|
||||
|
||||
await Task.Delay(100);
|
||||
}
|
||||
socket.Close();
|
||||
}
|
||||
|
||||
public Vec3 NetVec3(NetData.Vec3 v) {
|
||||
return new Vec3(v.X, v.Y, v.Z);
|
||||
} Offset<NetData.Vec3> Vec3Net(Vec3 v, ref FlatBufferBuilder fbb) {
|
||||
return NetData.Vec3.CreateVec3(fbb, v.x, v.y, v.z);
|
||||
}
|
||||
|
||||
public Quat NetQuat(NetData.Quat q) {
|
||||
return new Quat(q.X, q.Y, q.Z, q.W);
|
||||
}
|
||||
|
||||
public Pose NetPose(NetData.Pose p) {
|
||||
return new Pose(NetVec3(p.Pos), NetQuat(p.Rot));
|
||||
}
|
||||
|
||||
Vec3 ReadVec3(byte[] data, ref int dataPos) {
|
||||
dataPos += 12;
|
||||
return new Vec3(
|
||||
BitConverter.ToSingle(data, dataPos),
|
||||
BitConverter.ToSingle(data, dataPos + 4),
|
||||
BitConverter.ToSingle(data, dataPos + 8)
|
||||
);
|
||||
} void WriteVec3(ref byte[] data, ref int dataPos, Vec3 vec) {
|
||||
BitConverter.GetBytes(vec.x).CopyTo(data, dataPos);
|
||||
BitConverter.GetBytes(vec.y).CopyTo(data, dataPos + 4);
|
||||
BitConverter.GetBytes(vec.z).CopyTo(data, dataPos + 8);
|
||||
dataPos += 12;
|
||||
}
|
||||
|
||||
Quat ReadQuat(byte[] data, ref int dataPos) {
|
||||
dataPos += 16;
|
||||
return new Quat(
|
||||
BitConverter.ToSingle(data, dataPos),
|
||||
BitConverter.ToSingle(data, dataPos + 4),
|
||||
BitConverter.ToSingle(data, dataPos + 8),
|
||||
BitConverter.ToSingle(data, dataPos + 12)
|
||||
);
|
||||
} void WriteQuat(ref byte[] data, ref int dataPos, Quat quat) {
|
||||
BitConverter.GetBytes(quat.x).CopyTo(data, dataPos);
|
||||
BitConverter.GetBytes(quat.y).CopyTo(data, dataPos + 4);
|
||||
BitConverter.GetBytes(quat.z).CopyTo(data, dataPos + 8);
|
||||
BitConverter.GetBytes(quat.w).CopyTo(data, dataPos + 12);
|
||||
dataPos += 16;
|
||||
}
|
||||
|
||||
Pose ReadPose(byte[] data, ref int dataPos) {
|
||||
dataPos += 24;
|
||||
return new Pose(
|
||||
ReadVec3(data, ref dataPos),
|
||||
ReadQuat(data, ref dataPos)
|
||||
);
|
||||
} void WritePose(ref byte[] data, ref int dataPos, Pose pose) {
|
||||
WriteVec3(ref data, ref dataPos, pose.position);
|
||||
WriteQuat(ref data, ref dataPos, pose.orientation);
|
||||
}
|
||||
|
||||
Mesh meshCube = Default.MeshCube;
|
||||
Material matCube = Default.Material;
|
||||
public void Cubee(Matrix m) {
|
||||
meshCube.Draw(matCube, m);
|
||||
}
|
||||
|
||||
public class Peer
|
||||
{
|
||||
public int id;
|
||||
public Vec3 cursor;
|
||||
|
||||
public Peer(int id, Vec3 cursor) {
|
||||
this.id = id;
|
||||
this.cursor = cursor;
|
||||
}
|
||||
}
|
||||
}
|
43
NetData/Peer.cs
Normal file
43
NetData/Peer.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
namespace NetData
|
||||
{
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::FlatBuffers;
|
||||
|
||||
public struct Peer : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_1_12_0(); }
|
||||
public static Peer GetRootAsPeer(ByteBuffer _bb) { return GetRootAsPeer(_bb, new Peer()); }
|
||||
public static Peer GetRootAsPeer(ByteBuffer _bb, Peer obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
|
||||
public Peer __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public int Id { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetInt(o + __p.bb_pos) : (int)0; } }
|
||||
public NetData.Pose? Head { get { int o = __p.__offset(6); return o != 0 ? (NetData.Pose?)(new NetData.Pose()).__assign(o + __p.bb_pos, __p.bb) : null; } }
|
||||
public NetData.Pose? LHand { get { int o = __p.__offset(8); return o != 0 ? (NetData.Pose?)(new NetData.Pose()).__assign(o + __p.bb_pos, __p.bb) : null; } }
|
||||
public NetData.Pose? RHand { get { int o = __p.__offset(10); return o != 0 ? (NetData.Pose?)(new NetData.Pose()).__assign(o + __p.bb_pos, __p.bb) : null; } }
|
||||
public NetData.Vec3? Cursor { get { int o = __p.__offset(12); return o != 0 ? (NetData.Vec3?)(new NetData.Vec3()).__assign(o + __p.bb_pos, __p.bb) : null; } }
|
||||
|
||||
public static void StartPeer(FlatBufferBuilder builder) { builder.StartTable(5); }
|
||||
public static void AddId(FlatBufferBuilder builder, int id) { builder.AddInt(0, id, 0); }
|
||||
public static void AddHead(FlatBufferBuilder builder, Offset<NetData.Pose> headOffset) { builder.AddStruct(1, headOffset.Value, 0); }
|
||||
public static void AddLHand(FlatBufferBuilder builder, Offset<NetData.Pose> lHandOffset) { builder.AddStruct(2, lHandOffset.Value, 0); }
|
||||
public static void AddRHand(FlatBufferBuilder builder, Offset<NetData.Pose> rHandOffset) { builder.AddStruct(3, rHandOffset.Value, 0); }
|
||||
public static void AddCursor(FlatBufferBuilder builder, Offset<NetData.Vec3> cursorOffset) { builder.AddStruct(4, cursorOffset.Value, 0); }
|
||||
public static Offset<NetData.Peer> EndPeer(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<NetData.Peer>(o);
|
||||
}
|
||||
public static void FinishPeerBuffer(FlatBufferBuilder builder, Offset<NetData.Peer> offset) { builder.Finish(offset.Value); }
|
||||
public static void FinishSizePrefixedPeerBuffer(FlatBufferBuilder builder, Offset<NetData.Peer> offset) { builder.FinishSizePrefixed(offset.Value); }
|
||||
};
|
||||
|
||||
|
||||
}
|
38
NetData/Pose.cs
Normal file
38
NetData/Pose.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
namespace NetData
|
||||
{
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::FlatBuffers;
|
||||
|
||||
public struct Pose : IFlatbufferObject
|
||||
{
|
||||
private Struct __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); }
|
||||
public Pose __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public NetData.Vec3 Pos { get { return (new NetData.Vec3()).__assign(__p.bb_pos + 0, __p.bb); } }
|
||||
public NetData.Quat Rot { get { return (new NetData.Quat()).__assign(__p.bb_pos + 12, __p.bb); } }
|
||||
|
||||
public static Offset<NetData.Pose> CreatePose(FlatBufferBuilder builder, float pos_X, float pos_Y, float pos_Z, float rot_X, float rot_Y, float rot_Z, float rot_W) {
|
||||
builder.Prep(4, 28);
|
||||
builder.Prep(4, 16);
|
||||
builder.PutFloat(rot_W);
|
||||
builder.PutFloat(rot_Z);
|
||||
builder.PutFloat(rot_Y);
|
||||
builder.PutFloat(rot_X);
|
||||
builder.Prep(4, 12);
|
||||
builder.PutFloat(pos_Z);
|
||||
builder.PutFloat(pos_Y);
|
||||
builder.PutFloat(pos_X);
|
||||
return new Offset<NetData.Pose>(builder.Offset);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
35
NetData/Quat.cs
Normal file
35
NetData/Quat.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
namespace NetData
|
||||
{
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::FlatBuffers;
|
||||
|
||||
public struct Quat : IFlatbufferObject
|
||||
{
|
||||
private Struct __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); }
|
||||
public Quat __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public float X { get { return __p.bb.GetFloat(__p.bb_pos + 0); } }
|
||||
public float Y { get { return __p.bb.GetFloat(__p.bb_pos + 4); } }
|
||||
public float Z { get { return __p.bb.GetFloat(__p.bb_pos + 8); } }
|
||||
public float W { get { return __p.bb.GetFloat(__p.bb_pos + 12); } }
|
||||
|
||||
public static Offset<NetData.Quat> CreateQuat(FlatBufferBuilder builder, float X, float Y, float Z, float W) {
|
||||
builder.Prep(4, 16);
|
||||
builder.PutFloat(W);
|
||||
builder.PutFloat(Z);
|
||||
builder.PutFloat(Y);
|
||||
builder.PutFloat(X);
|
||||
return new Offset<NetData.Quat>(builder.Offset);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
33
NetData/Vec3.cs
Normal file
33
NetData/Vec3.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
namespace NetData
|
||||
{
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::FlatBuffers;
|
||||
|
||||
public struct Vec3 : IFlatbufferObject
|
||||
{
|
||||
private Struct __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); }
|
||||
public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public float X { get { return __p.bb.GetFloat(__p.bb_pos + 0); } }
|
||||
public float Y { get { return __p.bb.GetFloat(__p.bb_pos + 4); } }
|
||||
public float Z { get { return __p.bb.GetFloat(__p.bb_pos + 8); } }
|
||||
|
||||
public static Offset<NetData.Vec3> CreateVec3(FlatBufferBuilder builder, float X, float Y, float Z) {
|
||||
builder.Prep(4, 12);
|
||||
builder.PutFloat(Z);
|
||||
builder.PutFloat(Y);
|
||||
builder.PutFloat(X);
|
||||
return new Offset<NetData.Vec3>(builder.Offset);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
255
Program.cs
255
Program.cs
|
@ -1,10 +1,5 @@
|
|||
using System;
|
||||
using StereoKit;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
using System;
|
||||
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
|
@ -25,54 +20,97 @@ public static class Mono {
|
|||
|
||||
public static Controller offHand, mainHand;
|
||||
|
||||
public static Model model = Model.FromFile("cursor.glb", Shader.Default);
|
||||
|
||||
public static void Run() {
|
||||
string publicIP, localIP;
|
||||
GetIPs();
|
||||
void GetIPs()
|
||||
{
|
||||
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);
|
||||
publicIP = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd();
|
||||
// Console.WriteLine("Your IP is: " + publicIP);
|
||||
}
|
||||
|
||||
// byte[] bytes = Encoding.UTF8.GetBytes(publicIP);
|
||||
// Console.WriteLine(bytes.Length);
|
||||
MonoNet net = new MonoNet(publicIP); // temp, until unique usernames
|
||||
net.Start(false);
|
||||
Random rnd = new Random();
|
||||
MonoNet net = new MonoNet(rnd.Next(1, 256)); // temp, until unique usernames
|
||||
net.Start();
|
||||
|
||||
ColorCube cube = new ColorCube();
|
||||
OrbitalView.strength = 4;
|
||||
OrbitalView.distance = 0.4f;
|
||||
cube.thickness = 0.01f;
|
||||
|
||||
StretchCursor stretchCursor = new StretchCursor();
|
||||
// StretchCursor stretchCursor = new StretchCursor();
|
||||
// ReachCursor reachCursor = new ReachCursor();
|
||||
// SupineCursor supineCursor = new SupineCursor();
|
||||
// ClawCursor clawCursor = new ClawCursor();
|
||||
|
||||
Oriel oriel = new Oriel();
|
||||
|
||||
oriel.Start();
|
||||
// Oriel oriel = new Oriel();
|
||||
// oriel.Start();
|
||||
|
||||
Lerper lerper = new Lerper();
|
||||
|
||||
Tex tex = new Tex(TexType.Image, TexFormat.Rgba32);
|
||||
tex.SetSize(128, 128);
|
||||
tex.SampleMode = TexSample.Point;
|
||||
|
||||
|
||||
Material material = Default.Material;
|
||||
material.SetTexture("diffuse", tex);
|
||||
Mesh quad = Default.MeshQuad;
|
||||
|
||||
while (SK.Step(() => {
|
||||
offHand = Input.Controller(Handed.Left);
|
||||
mainHand = Input.Controller(Handed.Right);
|
||||
|
||||
stretchCursor.Step(offHand.aim, mainHand.aim);
|
||||
net.cursor = stretchCursor.pos;
|
||||
mainHand.aim = Input.Hand(Handed.Right).palm;
|
||||
|
||||
// stretchCursor.Step(offHand.aim, mainHand.aim);
|
||||
|
||||
|
||||
net.cursor = Vec3.Up * (float)Math.Sin(Time.Total);
|
||||
net.head = Input.Head;
|
||||
net.offHand = offHand.aim;
|
||||
net.mainHand = mainHand.aim;
|
||||
for (int i = 0; i < net.peers.Length; i++) {
|
||||
MonoNet.Peer peer = net.peers[i];
|
||||
if (peer != null) {
|
||||
net.Cubee(Matrix.TRS(peer.cursor, Quat.Identity, Vec3.One * 0.05f));
|
||||
// Cubee(NetPose(peer.Head.Value).ToMatrix(Vec3.One * 0.3f));
|
||||
// Cubee(NetPose(peer.LHand.Value).ToMatrix(Vec3.One * 0.1f));
|
||||
// Cubee(NetPose(peer.RHand.Value).ToMatrix(Vec3.One * 0.1f));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// bool KeyDown(Key key) => Input.Key(key).IsActive();
|
||||
|
||||
// bool[,] bitting = new bool[,] {
|
||||
// {KeyDown(Key.F), false, KeyDown(Key.D), false, KeyDown(Key.S), false, KeyDown(Key.A)},
|
||||
// {false, false, false, false, false, false, false},
|
||||
// {KeyDown(Key.J), false, KeyDown(Key.K), false, KeyDown(Key.L), false, KeyDown(Key.Semicolon)},
|
||||
// };
|
||||
|
||||
|
||||
// bitting[1,0] = bitting[0,0] && bitting[2,0];
|
||||
// bitting[1,2] = bitting[0,2] && bitting[2,2];
|
||||
// bitting[1,4] = bitting[0,4] && bitting[2,4];
|
||||
// bitting[1,6] = bitting[0,6] && bitting[2,6];
|
||||
|
||||
// bitting[0, 1] = bitting[0, 0] && bitting[0, 2];
|
||||
// bitting[0, 3] = bitting[0, 2] && bitting[0, 4];
|
||||
// bitting[0, 5] = bitting[0, 4] && bitting[0, 6];
|
||||
|
||||
// bitting[2, 1] = bitting[2, 0] && bitting[2, 2];
|
||||
// bitting[2, 3] = bitting[2, 2] && bitting[2, 4];
|
||||
// bitting[2, 5] = bitting[2, 4] && bitting[2, 6];
|
||||
|
||||
// Color32[] pixels = new Color32[tex.Width * tex.Height];
|
||||
// tex.GetColors(ref pixels);
|
||||
// for (int i = 0; i < pixels.Length; i++) {
|
||||
// pixels[i] = new Color32(0, 0, 0, 0);
|
||||
// int x = i % tex.Width;
|
||||
// int y = i / tex.Width;
|
||||
// if (x < 3 && y < 7 && bitting[x, y]) {
|
||||
// pixels[i] = new Color32(0, 255, 255, 0);
|
||||
// }
|
||||
// }
|
||||
// tex.SetColors(tex.Width, tex.Height, pixels);
|
||||
|
||||
// quad.Draw(material, Matrix.TR(Vec3.Zero, Quat.FromAngles(0, 180, 0)));
|
||||
|
||||
|
||||
|
||||
|
||||
// domHand subHand ?? :3
|
||||
|
||||
|
@ -144,146 +182,14 @@ public class Lerper
|
|||
}
|
||||
}
|
||||
|
||||
public class Peer {
|
||||
public string id;
|
||||
public Vec3 cursor;
|
||||
public Pose head;
|
||||
public Pose offHand;
|
||||
public Pose mainHand;
|
||||
public Peer() {
|
||||
id = "";
|
||||
cursor = Vec3.Zero;
|
||||
head = new Pose();
|
||||
offHand = new Pose();
|
||||
mainHand = new Pose();
|
||||
}
|
||||
}
|
||||
|
||||
public class MonoNet {
|
||||
public string myID;
|
||||
public MonoNet(string name) {
|
||||
this.myID = name;
|
||||
}
|
||||
|
||||
public Vec3 cursor; // are these stored here???
|
||||
public Pose head;
|
||||
public Pose offHand;
|
||||
public Pose mainHand;
|
||||
public Peer[] peers;
|
||||
|
||||
public async void Start(bool log) {
|
||||
int port = 1234;
|
||||
string serverIP = "139.177.201.219";
|
||||
// serverIP = "192.168.1.70";
|
||||
// try connecting to the server
|
||||
if (log) Console.WriteLine($"{myID} attempting to connect to server...");
|
||||
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), port);
|
||||
socket.Connect(serverEndPoint);
|
||||
|
||||
peers = new Peer[1];
|
||||
peers[0] = new Peer();
|
||||
|
||||
// send every 0.1 seconds
|
||||
while (true) {
|
||||
byte[] data = new byte[1024];
|
||||
int dataPos;
|
||||
|
||||
// send a message to the server
|
||||
dataPos = 0;
|
||||
Encoding.UTF8.GetBytes(myID).CopyTo(data, dataPos); dataPos += 16;
|
||||
WriteVec3(ref data, ref dataPos, cursor);
|
||||
WritePose(ref data, ref dataPos, head);
|
||||
WritePose(ref data, ref dataPos, offHand);
|
||||
WritePose(ref data, ref dataPos, mainHand);
|
||||
socket.SendTo(data, serverEndPoint);
|
||||
|
||||
// receive a message from the server
|
||||
while (socket.Available > 0){
|
||||
dataPos = 0;
|
||||
socket.ReceiveFrom(data, ref serverEndPoint);
|
||||
string id = Encoding.UTF8.GetString(data, dataPos, 16); dataPos += 16;
|
||||
// recieve text
|
||||
for (int i = 0; i < peers.Length; i++){
|
||||
if (peers[i].id == id) {
|
||||
Peer peer = peers[i];
|
||||
peer.cursor = ReadVec3(data, ref dataPos);
|
||||
peer.head = ReadPose(data, ref dataPos);
|
||||
peer.offHand = ReadPose(data, ref dataPos);
|
||||
peer.mainHand = ReadPose(data, ref dataPos);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sleep for 0.1 seconds
|
||||
await Task.Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
Vec3 ReadVec3(byte[] data, ref int dataPos) {
|
||||
dataPos += 12;
|
||||
return new Vec3(
|
||||
BitConverter.ToSingle(data, dataPos),
|
||||
BitConverter.ToSingle(data, dataPos + 4),
|
||||
BitConverter.ToSingle(data, dataPos + 8)
|
||||
);
|
||||
} void WriteVec3(ref byte[] data, ref int dataPos, Vec3 vec) {
|
||||
BitConverter.GetBytes(vec.x).CopyTo(data, dataPos);
|
||||
BitConverter.GetBytes(vec.y).CopyTo(data, dataPos + 4);
|
||||
BitConverter.GetBytes(vec.z).CopyTo(data, dataPos + 8);
|
||||
dataPos += 12;
|
||||
}
|
||||
|
||||
Quat ReadQuat(byte[] data, ref int dataPos) {
|
||||
dataPos += 16;
|
||||
return new Quat(
|
||||
BitConverter.ToSingle(data, dataPos),
|
||||
BitConverter.ToSingle(data, dataPos + 4),
|
||||
BitConverter.ToSingle(data, dataPos + 8),
|
||||
BitConverter.ToSingle(data, dataPos + 12)
|
||||
);
|
||||
} void WriteQuat(ref byte[] data, ref int dataPos, Quat quat) {
|
||||
BitConverter.GetBytes(quat.x).CopyTo(data, dataPos);
|
||||
BitConverter.GetBytes(quat.y).CopyTo(data, dataPos + 4);
|
||||
BitConverter.GetBytes(quat.z).CopyTo(data, dataPos + 8);
|
||||
BitConverter.GetBytes(quat.w).CopyTo(data, dataPos + 12);
|
||||
dataPos += 16;
|
||||
}
|
||||
|
||||
Pose ReadPose(byte[] data, ref int dataPos) {
|
||||
dataPos += 24;
|
||||
return new Pose(
|
||||
ReadVec3(data, ref dataPos),
|
||||
ReadQuat(data, ref dataPos)
|
||||
);
|
||||
} void WritePose(ref byte[] data, ref int dataPos, Pose pose) {
|
||||
WriteVec3(ref data, ref dataPos, pose.position);
|
||||
WriteQuat(ref data, ref dataPos, pose.orientation);
|
||||
}
|
||||
|
||||
Mesh meshCube = Default.MeshCube;
|
||||
Material matCube = Default.Material;
|
||||
public void Render() {
|
||||
for (int i = 0; i < peers.Length; i++) {
|
||||
Peer p = peers[i];
|
||||
Cubee(Matrix.TRS(p.cursor, p.offHand.orientation, Vec3.One * 0.05f));
|
||||
Cubee(p.head.ToMatrix(Vec3.One * 0.3f));
|
||||
Cubee(p.offHand.ToMatrix(Vec3.One * 0.1f));
|
||||
Cubee(p.mainHand.ToMatrix(Vec3.One * 0.1f));
|
||||
}
|
||||
}
|
||||
void Cubee(Matrix m) {
|
||||
meshCube.Draw(matCube, m);
|
||||
}
|
||||
}
|
||||
|
||||
public class Oriel {
|
||||
public Bounds bounds;
|
||||
|
||||
// render
|
||||
Model model = Model.FromFile("oriel.glb", Shader.FromFile("oriel.hlsl"));
|
||||
// Model model = Model.FromFile("oriel.glb", Shader.FromFile("oriel.hlsl"));
|
||||
Material mat = new Material(Shader.FromFile("oriel.hlsl"));
|
||||
Mesh mesh = Default.MeshCube;
|
||||
Vec3 _dimensions;
|
||||
public void Start() {
|
||||
bounds = new Bounds(Vec3.Zero, new Vec3(1f, 0.5f, 0.5f));
|
||||
|
@ -292,15 +198,16 @@ public class Oriel {
|
|||
|
||||
public void Step() {
|
||||
// circle around center
|
||||
bounds.center = Quat.FromAngles(0, 0, Time.Totalf * 60) * Vec3.Up * 0.3f;
|
||||
// bounds.center = Quat.FromAngles(0, 0, Time.Totalf * 60) * Vec3.Up * 0.3f;
|
||||
|
||||
|
||||
bounds.dimensions = _dimensions * (1f + (MathF.Sin(Time.Totalf * 3) * 0.3f));
|
||||
// bounds.dimensions = _dimensions * (1f + (MathF.Sin(Time.Totalf * 3) * 0.3f));
|
||||
|
||||
model.GetMaterial(0).Transparency = Transparency.Blend;
|
||||
model.GetMaterial(0).SetFloat("_height", bounds.dimensions.y);
|
||||
model.GetMaterial(0).SetFloat("_ypos", bounds.center.y);
|
||||
model.Draw(Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions));
|
||||
mat.Transparency = Transparency.Blend;
|
||||
// mat.FaceCull = Cull.Front;
|
||||
mat.SetFloat("_height", bounds.dimensions.y);
|
||||
mat.SetFloat("_ypos", bounds.center.y);
|
||||
mesh.Draw(mat, Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FlatBuffers.Core" Version="1.12.0" />
|
||||
<PackageReference Include="StereoKit" Version="0.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
31
schema.fbs
Normal file
31
schema.fbs
Normal file
|
@ -0,0 +1,31 @@
|
|||
namespace NetData;
|
||||
|
||||
attribute "priority";
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
struct Quat {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
w:float;
|
||||
}
|
||||
|
||||
struct Pose {
|
||||
pos:Vec3;
|
||||
rot:Quat;
|
||||
}
|
||||
|
||||
table Peer {
|
||||
id:int;
|
||||
head:Pose;
|
||||
l_hand:Pose;
|
||||
r_hand:Pose;
|
||||
cursor:Vec3;
|
||||
}
|
||||
|
||||
root_type Peer;
|
Loading…
Add table
Reference in a new issue