o7 flatbuffers

This commit is contained in:
spatialfree 2021-11-27 01:55:23 -05:00
parent a687b25504
commit 14bddf6dd0
2 changed files with 163 additions and 106 deletions

View file

@ -16,10 +16,12 @@ public class MonoNet {
public Socket socket;
// public NetworkStream stream;
byte[] data;
int head;
public Peer[] peers;
public Vec3 cursor; // are these stored here???
public Pose head;
public Pose headset;
public Pose offHand;
public Pose mainHand;
@ -52,41 +54,50 @@ public class MonoNet {
bool running = true;
while (running) {
while (socket.Available > 0) {
socket.Receive(data, 0, data.Length, SocketFlags.None);
ByteBuffer bb = new ByteBuffer(data);
try {socket.Receive(data, 0, data.Length, SocketFlags.None);}
catch (Exception e) {
Console.WriteLine("can't connect to the server");
return;
}
// ByteBuffer bb = new ByteBuffer(data);
NetData.Peer peer = NetData.Peer.GetRootAsPeer(bb);
int id = peer.Id;
// NetData.Peer peer = NetData.Peer.GetRootAsPeer(bb);
// int id = peer.Id;
head = 0;
int id = ReadInt();
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);
// peers[i].cursor = NetVec3(peer.Cursor.Value);
peers[i].cursor = ReadVec3();
break;
}
} else {
if (peer.Cursor.HasValue) {
peers[i] = new Peer(id, NetVec3(peer.Cursor.Value));
// if (peer.Cursor.HasValue) {
// peers[i] = new Peer(id, NetVec3(peer.Cursor.Value));
// break;
// }
peers[i] = new Peer(id, ReadVec3());
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());
// FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
// NetData.Peer.StartPeer(fbb);
// 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());
data = new byte[1024];
head = 0;
WriteInt(myID);
WriteVec3(cursor);
socket.Send(data);
await Task.Delay(100);
}
@ -107,45 +118,55 @@ public class MonoNet {
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;
int ReadInt() {
int value = BitConverter.ToInt32(data, head);
head += 4;
return value;
} void WriteInt(int value) {
BitConverter.GetBytes(value).CopyTo(data, head);
head += 4;
}
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)
Vec3 ReadVec3() {
Vec3 value = new Vec3(
BitConverter.ToSingle(data, head),
BitConverter.ToSingle(data, head + 4),
BitConverter.ToSingle(data, head + 8)
);
} 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;
head += 12;
return value;
} void WriteVec3(Vec3 vec) {
BitConverter.GetBytes(vec.x).CopyTo(data, head);
BitConverter.GetBytes(vec.y).CopyTo(data, head + 4);
BitConverter.GetBytes(vec.z).CopyTo(data, head + 8);
head += 12;
}
Pose ReadPose(byte[] data, ref int dataPos) {
dataPos += 24;
Quat ReadQuat() {
Quat value = new Quat(
BitConverter.ToSingle(data, head),
BitConverter.ToSingle(data, head + 4),
BitConverter.ToSingle(data, head + 8),
BitConverter.ToSingle(data, head + 12)
);
head += 16;
return value;
} void WriteQuat(Quat quat) {
BitConverter.GetBytes(quat.x).CopyTo(data, head);
BitConverter.GetBytes(quat.y).CopyTo(data, head + 4);
BitConverter.GetBytes(quat.z).CopyTo(data, head + 8);
BitConverter.GetBytes(quat.w).CopyTo(data, head + 12);
head += 16;
}
Pose ReadPose() {
return new Pose(
ReadVec3(data, ref dataPos),
ReadQuat(data, ref dataPos)
ReadVec3(),
ReadQuat()
);
} void WritePose(ref byte[] data, ref int dataPos, Pose pose) {
WriteVec3(ref data, ref dataPos, pose.position);
WriteQuat(ref data, ref dataPos, pose.orientation);
} void WritePose(Pose pose) {
WriteVec3(pose.position);
WriteQuat(pose.orientation);
}
Mesh meshCube = Default.MeshCube;

View file

@ -11,7 +11,6 @@ class Program {
Environment.Exit(1);
// TextStyle style = Text.MakeStyle(Font.FromFile("DMMono-Regular.ttf"), 0.1f, Color.White);
Mono.Run();
}
}
@ -40,15 +39,6 @@ public static class Mono {
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);
@ -59,7 +49,7 @@ public static class Mono {
net.cursor = Vec3.Up * (float)Math.Sin(Time.Total);
net.head = Input.Head;
net.headset = Input.Head;
net.offHand = offHand.aim;
net.mainHand = mainHand.aim;
for (int i = 0; i < net.peers.Length; i++) {
@ -72,46 +62,6 @@ public static class Mono {
}
}
// 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
// if (offHand.trigger.) {
@ -145,6 +95,16 @@ public static class Mono {
}
}
public class DrawKey {
public int x, y;
public Key key;
public DrawKey(int x, int y, Key key) {
this.x = x;
this.y = y;
this.key = key;
}
}
public class Lerper
{
public float t = 0;
@ -211,6 +171,82 @@ public class Oriel {
}
}
public class Bitting {
Tex tex = new Tex(TexType.Image, TexFormat.Rgba32);
Material material = Default.Material;
Mesh quad = Default.MeshQuad;
int [,] bitchar = new int[,] {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
};
DrawKey[] drawKeys = new DrawKey[] {
new DrawKey(0, 0, Key.F), new DrawKey(0, 2, Key.D), new DrawKey(0, 4, Key.S), new DrawKey(0, 6, Key.A),
new DrawKey(2, 0, Key.J), new DrawKey(2, 2, Key.K), new DrawKey(2, 4, Key.L), new DrawKey(2, 6, Key.Semicolon),
}; DrawKey lastKey = null;
public void Start() {
tex.SetSize(128, 128);
tex.SampleMode = TexSample.Point;
material.SetTexture("diffuse", tex);
}
public void Step() {
// clear
if (Input.Key(Key.Space).IsJustActive()) {
for (int i = 0; i < bitchar.GetLength(0); i++) {
for (int j = 0; j < bitchar.GetLength(1); j++) {
bitchar[i, j] = 0;
}
}
lastKey = null;
}
for (int i = 0; i < drawKeys.Length; i++){
DrawKey drawKey = drawKeys[i];
if (Input.Key(drawKey.key).IsJustActive()) {
bitchar[drawKey.x, drawKey.y] = 1;
if (lastKey != null) {
// draw line between last and current
int x1 = lastKey.x;
int y1 = lastKey.y;
int x2 = drawKey.x;
int y2 = drawKey.y;
int dx = Math.Abs(x2 - x1);
int dy = Math.Abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
while (true) {
bitchar[x1, y1] = 1;
if (x1 == x2 && y1 == y2) break;
int e2 = 2 * err;
if (e2 > -dy) { err -= dy; x1 += sx; }
if (e2 < dx) { err += dx; y1 += sy; }
}
}
lastKey = drawKey;
break;
}
}
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 && bitchar[x, y] == 1) {
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)));
}
}
public static class PullRequest {
public static void BoundsDraw(Bounds b, float thickness, Color color) {
Vec3 c = Vec3.One / 2;