From 533e712c3f37666e05a1e840a1a0a56a38c245f9 Mon Sep 17 00:00:00 2001 From: spatialfree Date: Thu, 2 Dec 2021 05:52:05 -0500 Subject: [PATCH] Network Solids --- Assets/oriel.hlsl | 11 ++++-- MonoNet.cs | 55 ++++++++++++++++++++++++++- Program.cs | 96 ++++++++++++++++++++++++++++------------------- README.md | 7 +++- 4 files changed, 123 insertions(+), 46 deletions(-) diff --git a/Assets/oriel.hlsl b/Assets/oriel.hlsl index f58fdb1..9d0b817 100644 --- a/Assets/oriel.hlsl +++ b/Assets/oriel.hlsl @@ -4,6 +4,8 @@ // float4 color; float _height; float _ypos; +Texture2D tex : register(t0); +SamplerState tex_s : register(s0); struct vsIn { float4 pos : SV_POSITION; @@ -35,7 +37,7 @@ psIn vs(vsIn input, uint id : SV_InstanceID) { 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; + o.color.rgb = o.color.rgb * lighting; // * sk_inst[id].color; return o; } @@ -47,9 +49,8 @@ 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 tri_raycast(float3 origin, float3 dir, float3 v0) { 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; @@ -89,8 +90,10 @@ float4 ps(psIn input) : SV_TARGET { // input.color.a = 0.5; // raycast or raymarch + float4 col = tex.Sample(tex_s, float2(0.01, 0.01)); + float3 v0 = float3(0, col.r, 0); float3 ray = normalize(input.world - input.campos); - input.color = float4(float3(1,1,1) * max(tri_raycast(input.world, ray), 0.0), 1); + input.color = float4(float3(1,1,1) * max(tri_raycast(input.world, ray, v0), 0.0), 1); return input.color; diff --git a/MonoNet.cs b/MonoNet.cs index 8048f45..0b5dad5 100644 --- a/MonoNet.cs +++ b/MonoNet.cs @@ -14,6 +14,7 @@ public class MonoNet { this.mono = mono; Random rnd = new Random(); me = new Peer(rnd.Next(1, 256)); // let the server determine these + me.block = new Block(new Vec3((float)rnd.NextDouble() * 0.5f, 10, -4), Quat.Identity, SolidType.Normal, Color.White); } public Socket socket; int bufferSize = 1024; @@ -51,6 +52,7 @@ public class MonoNet { Thread writeThread = new Thread(Write); writeThread.Start(); + // socket.Close(); } @@ -65,7 +67,7 @@ public class MonoNet { rHead = 0; int id = ReadInt(); - if (id != 0) { + if (id != 0 && id != me.id) { int index = -1; for (int i = 0; i < peers.Length; i++) { if (peers[i] != null) { @@ -90,6 +92,7 @@ public class MonoNet { peers[index].headset = ReadPose(); peers[index].offHand = ReadPose(); peers[index].mainHand = ReadPose(); + ReadBlock(ref peers[index].block); } } } @@ -107,6 +110,7 @@ public class MonoNet { WritePose(me.headset); WritePose(me.offHand); WritePose(me.mainHand); + WriteBlock(me.block); socket.Send(wData); Thread.Sleep(60); @@ -178,6 +182,20 @@ public class MonoNet { WriteQuat(pose.orientation); } + void ReadBlock(ref Block b) { // update instead of replace + Pose pose = ReadPose(); + if (b == null) { + b = new Block(pose.position, pose.orientation, SolidType.Immovable, Color.White * 0.5f); // read up on unaffected + // b.solid.Enabled = false; + } else { + b.solid.Teleport(pose.position, pose.orientation); + // b.solid.Enabled = false; + } + } + void WriteBlock(Block block) { + WritePose(block.solid.GetPose()); + } + string localIP, publicIP; void GetIPs() { using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { @@ -194,18 +212,51 @@ public class MonoNet { meshCube.Draw(matCube, m); } + public class Block { + public static Mesh mesh = Default.MeshCube; + public static Material mat = Default.Material; + + public Solid solid; + + public Color color; + + // if you grab someone else's it becomes your own + // how to communicate to the other peer that you have grabbed it? + // public int request; // request ownership + // public int owner; // then if owner continue as usual + // public bool busy; // marked as held so no fighting + + public Block(Vec3 pos, Quat rot, SolidType type, Color color) { + this.solid = new Solid(pos, rot, type); + this.solid.AddBox(Vec3.One, 1); + this.color = color; + } + + public void Draw() { + mesh.Draw(mat, solid.GetPose().ToMatrix(), color); + } + } + public class Peer { + + + + // to do this we need to assign fixed id's to each peer from the server + // ++ make a peer timeout on the client side as well + + + public int id; public Vec3 cursorA, cursorB, cursorC, cursorD; public Pose headset; public Pose offHand; public Pose mainHand; + public Block block; // public Sound voice; // public SoundInst voiceInst; // update position public Peer(int id) { this.id = id; - // voice = Sound.CreateStream(0.5f); // voiceInst = voice.Play(Vec3.Zero, 0.5f); } diff --git a/Program.cs b/Program.cs index 2d23119..3ee41d2 100644 --- a/Program.cs +++ b/Program.cs @@ -23,8 +23,6 @@ public class Mono { public Vec3 dragStart, posStart; public float railT; - Block[] blocks; - Mesh ball = Default.MeshSphere; Material mat = Default.Material; Mesh cube = Default.MeshCube; @@ -33,18 +31,17 @@ public class Mono { // mic = new Mic(); Vec3 pos = new Vec3(0, 0, 0); + Solid floor = new Solid(Vec3.Up * -1.5f, Quat.Identity, SolidType.Immovable); + Vec3 floorScale = new Vec3(10, 0.1f, 10); + floor.AddBox(floorScale); + Cursors cursors = new Cursors(this); Oriel oriel = new Oriel(); oriel.Start(); - blocks = new Block[] { - new Block(new Bounds(new Vec3(-1, 0, -4), Vec3.One * 0.5f), Color.White), - new Block(new Bounds(new Vec3(0, 0, -4), Vec3.One * 0.5f), Color.White), - new Block(new Bounds(new Vec3(1, 0, -4), Vec3.One * 0.5f), Color.White), - }; - int blockIndex = -1; - Vec3 blockOffset = Vec3.Zero; + // int blockIndex = -1; + // Vec3 blockOffset = Vec3.Zero; MonoNet net = new MonoNet(this); net.Start(); @@ -136,25 +133,27 @@ public class Mono { // Solid solid = new Solid(Vec3.Up * -2, Quat.Identity, SolidType.Immovable); // make some blocks you can move around with cursor.p0 // no collision detection, just a visual example - if (domCon.grip > 0.5f) { - if (blockIndex < 0) { - for (int i = 0; i < blocks.Length; i++) { - if (blocks[i].bounds.Contains(cursor.p0)) { - blockIndex = i; - blockOffset = cursor.p0 - blocks[i].bounds.center; - blocks[i].color = colorCube.color; - break; - } - } - } else { - blocks[blockIndex].bounds.center = cursor.p0 - blockOffset; - } - } else { - blockIndex = -1; - } - for (int i = 0; i < blocks.Length; i++) { - cube.Draw(mat, Matrix.TS(blocks[i].bounds.center, blocks[i].bounds.dimensions), blocks[i].color); - } + // if (domCon.grip > 0.5f) { + // if (blockIndex < 0) { + // for (int i = 0; i < net.me.blocks.Length; i++) { + // if (net.me.blocks[i].bounds.Contains(cursor.p0)) { + // blockIndex = i; + // blockOffset = cursor.p0 - net.me.blocks[i].bounds.center; + // net.me.blocks[i].color = colorCube.color; + // break; + // } + // } + // } else { + // net.me.blocks[blockIndex].bounds.center = cursor.p0 - blockOffset; + // } + // } else { + // blockIndex = -1; + // } + cube.Draw(mat, floor.GetPose().ToMatrix(floorScale)); + // for (int i = 0; i < net.me.blocks.Length; i++) { + // cube.Draw(mat, net.me.blocks[i].solid.GetPose().ToMatrix(), net.me.blocks[i].color); + // } + net.me.block.Draw(); // cursor.Step(lHand.aim, rHand.aim); cursor.DrawSelf(); // net.me.cursorA = Vec3.Up * (float)Math.Sin(Time.Total); @@ -170,10 +169,11 @@ public class Mono { net.Cubee(peer.offHand.ToMatrix(Vec3.One * 0.1f)); net.Cubee(peer.mainHand.ToMatrix(Vec3.One * 0.1f)); // cubicFlow.Draw(peer.cursorA, peer.cursorB, peer.cursorC, peer.cursorD); + if (peer.block != null){ peer.block.Draw(); } } } - oriel.Step(); + // oriel.Step(); // Matrix orbitMatrix = OrbitalView.transform; // cube.Step(Matrix.S(Vec3.One * 0.2f) * orbitMatrix); @@ -185,16 +185,6 @@ public class Mono { } } -public class Block { - public Bounds bounds; - public Color color; - - public Block(Bounds bounds, Color color) { - this.bounds = bounds; - this.color = color; - } -} - public class Mic { public float[] bufferRaw = new float[0]; public int bufferRawSize = 0; @@ -381,6 +371,34 @@ public class Oriel { if (bounds.Contains(head.position, quadPos)) { quad.Draw(mat, Matrix.TRS(quadPos, Quat.LookAt(quadPos, head.position), Vec3.One * 0.5f)); } + + // instead of a quad, just slap the rendered cube mesh to the head + + Vec3 vertex = new Vec3(0, ((float)Math.Sin(Time.Totalf) + 1) / 3, 0); + + int w = 3, h = 1; + Tex tex = new Tex(TexType.ImageNomips, TexFormat.Rgba32); + tex.SampleMode = TexSample.Point; + tex.SetSize(w, h); + Color32[] colors = new Color32[w * h]; + // tex.GetColors(ref colors); + for (int i = 0; i < colors.Length; i++) { + // convert vertex to color + int vv = (int)Math.Floor(vertex.y * 255); + + colors[i] = new Color( + Math.Clamp(vv, 0, 255), + Math.Clamp(vv - 256, 0, 255), + Math.Clamp(vv - 256 - 256, 0, 255), + Math.Clamp(vv - 256 - 256 - 256, 0, 255) + ); + // colors[i] = new Color32(0, 128, 0, 0); + } + tex.SetColors(w, h, colors); + + mat.SetTexture("tex", tex); + // mat.SetMatrix("_matrix", Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions)); + // mat.Wireframe } } diff --git a/README.md b/README.md index 45f5efb..61e162d 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,14 @@ movement: - ~~teleport and drag~~ - ~~bezier rails~~ +interact: ~~blocks you can manipulate with spatial cursors~~ (trackballer) -color them with the color cube (player colors +~~color them with the color cube (player colors)~~ +- trackballer +- network the new stuff + +software: oriel + orbital view (control a shooty guy with fullstick and spatial cursor) ### POLISH