Network Solids
This commit is contained in:
parent
8c7c2c5b43
commit
533e712c3f
4 changed files with 123 additions and 46 deletions
|
@ -4,6 +4,8 @@
|
||||||
// float4 color;
|
// float4 color;
|
||||||
float _height;
|
float _height;
|
||||||
float _ypos;
|
float _ypos;
|
||||||
|
Texture2D tex : register(t0);
|
||||||
|
SamplerState tex_s : register(s0);
|
||||||
|
|
||||||
struct vsIn {
|
struct vsIn {
|
||||||
float4 pos : SV_POSITION;
|
float4 pos : SV_POSITION;
|
||||||
|
@ -35,7 +37,7 @@ psIn vs(vsIn input, uint id : SV_InstanceID) {
|
||||||
o.color = input.col;
|
o.color = input.col;
|
||||||
float lighting = dot(o.norm, normalize(float3(-0.3, 0.6, 0.1)));
|
float lighting = dot(o.norm, normalize(float3(-0.3, 0.6, 0.1)));
|
||||||
lighting = (clamp(lighting, 0, 1) * 0.8) + 0.2;
|
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;
|
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;
|
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;
|
float final = -1;
|
||||||
float3 v0 = float3(0, 0, 0);
|
|
||||||
float3 v1 = float3(0, 0, 1);
|
float3 v1 = float3(0, 0, 1);
|
||||||
float3 v2 = float3(1, 0, 0);
|
float3 v2 = float3(1, 0, 0);
|
||||||
float3 e1 = v1 - v0;
|
float3 e1 = v1 - v0;
|
||||||
|
@ -89,8 +90,10 @@ float4 ps(psIn input) : SV_TARGET {
|
||||||
// input.color.a = 0.5;
|
// input.color.a = 0.5;
|
||||||
|
|
||||||
// raycast or raymarch
|
// 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);
|
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;
|
return input.color;
|
||||||
|
|
55
MonoNet.cs
55
MonoNet.cs
|
@ -14,6 +14,7 @@ public class MonoNet {
|
||||||
this.mono = mono;
|
this.mono = mono;
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
me = new Peer(rnd.Next(1, 256)); // let the server determine these
|
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;
|
public Socket socket;
|
||||||
int bufferSize = 1024;
|
int bufferSize = 1024;
|
||||||
|
@ -51,6 +52,7 @@ public class MonoNet {
|
||||||
Thread writeThread = new Thread(Write);
|
Thread writeThread = new Thread(Write);
|
||||||
writeThread.Start();
|
writeThread.Start();
|
||||||
|
|
||||||
|
|
||||||
// socket.Close();
|
// socket.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +67,7 @@ public class MonoNet {
|
||||||
|
|
||||||
rHead = 0;
|
rHead = 0;
|
||||||
int id = ReadInt();
|
int id = ReadInt();
|
||||||
if (id != 0) {
|
if (id != 0 && id != me.id) {
|
||||||
int index = -1;
|
int index = -1;
|
||||||
for (int i = 0; i < peers.Length; i++) {
|
for (int i = 0; i < peers.Length; i++) {
|
||||||
if (peers[i] != null) {
|
if (peers[i] != null) {
|
||||||
|
@ -90,6 +92,7 @@ public class MonoNet {
|
||||||
peers[index].headset = ReadPose();
|
peers[index].headset = ReadPose();
|
||||||
peers[index].offHand = ReadPose();
|
peers[index].offHand = ReadPose();
|
||||||
peers[index].mainHand = ReadPose();
|
peers[index].mainHand = ReadPose();
|
||||||
|
ReadBlock(ref peers[index].block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,6 +110,7 @@ public class MonoNet {
|
||||||
WritePose(me.headset);
|
WritePose(me.headset);
|
||||||
WritePose(me.offHand);
|
WritePose(me.offHand);
|
||||||
WritePose(me.mainHand);
|
WritePose(me.mainHand);
|
||||||
|
WriteBlock(me.block);
|
||||||
socket.Send(wData);
|
socket.Send(wData);
|
||||||
|
|
||||||
Thread.Sleep(60);
|
Thread.Sleep(60);
|
||||||
|
@ -178,6 +182,20 @@ public class MonoNet {
|
||||||
WriteQuat(pose.orientation);
|
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;
|
string localIP, publicIP;
|
||||||
void GetIPs() {
|
void GetIPs() {
|
||||||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) {
|
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) {
|
||||||
|
@ -194,18 +212,51 @@ public class MonoNet {
|
||||||
meshCube.Draw(matCube, m);
|
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 {
|
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 int id;
|
||||||
public Vec3 cursorA, cursorB, cursorC, cursorD;
|
public Vec3 cursorA, cursorB, cursorC, cursorD;
|
||||||
public Pose headset;
|
public Pose headset;
|
||||||
public Pose offHand;
|
public Pose offHand;
|
||||||
public Pose mainHand;
|
public Pose mainHand;
|
||||||
|
public Block block;
|
||||||
// public Sound voice;
|
// public Sound voice;
|
||||||
// public SoundInst voiceInst; // update position
|
// public SoundInst voiceInst; // update position
|
||||||
|
|
||||||
public Peer(int id) {
|
public Peer(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
||||||
// voice = Sound.CreateStream(0.5f);
|
// voice = Sound.CreateStream(0.5f);
|
||||||
// voiceInst = voice.Play(Vec3.Zero, 0.5f);
|
// voiceInst = voice.Play(Vec3.Zero, 0.5f);
|
||||||
}
|
}
|
||||||
|
|
96
Program.cs
96
Program.cs
|
@ -23,8 +23,6 @@ public class Mono {
|
||||||
public Vec3 dragStart, posStart;
|
public Vec3 dragStart, posStart;
|
||||||
public float railT;
|
public float railT;
|
||||||
|
|
||||||
Block[] blocks;
|
|
||||||
|
|
||||||
Mesh ball = Default.MeshSphere;
|
Mesh ball = Default.MeshSphere;
|
||||||
Material mat = Default.Material;
|
Material mat = Default.Material;
|
||||||
Mesh cube = Default.MeshCube;
|
Mesh cube = Default.MeshCube;
|
||||||
|
@ -33,18 +31,17 @@ public class Mono {
|
||||||
// mic = new Mic();
|
// mic = new Mic();
|
||||||
Vec3 pos = new Vec3(0, 0, 0);
|
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);
|
Cursors cursors = new Cursors(this);
|
||||||
|
|
||||||
Oriel oriel = new Oriel();
|
Oriel oriel = new Oriel();
|
||||||
oriel.Start();
|
oriel.Start();
|
||||||
|
|
||||||
blocks = new Block[] {
|
// int blockIndex = -1;
|
||||||
new Block(new Bounds(new Vec3(-1, 0, -4), Vec3.One * 0.5f), Color.White),
|
// Vec3 blockOffset = Vec3.Zero;
|
||||||
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;
|
|
||||||
|
|
||||||
MonoNet net = new MonoNet(this);
|
MonoNet net = new MonoNet(this);
|
||||||
net.Start();
|
net.Start();
|
||||||
|
@ -136,25 +133,27 @@ public class Mono {
|
||||||
// Solid solid = new Solid(Vec3.Up * -2, Quat.Identity, SolidType.Immovable);
|
// Solid solid = new Solid(Vec3.Up * -2, Quat.Identity, SolidType.Immovable);
|
||||||
// make some blocks you can move around with cursor.p0
|
// make some blocks you can move around with cursor.p0
|
||||||
// no collision detection, just a visual example
|
// no collision detection, just a visual example
|
||||||
if (domCon.grip > 0.5f) {
|
// if (domCon.grip > 0.5f) {
|
||||||
if (blockIndex < 0) {
|
// if (blockIndex < 0) {
|
||||||
for (int i = 0; i < blocks.Length; i++) {
|
// for (int i = 0; i < net.me.blocks.Length; i++) {
|
||||||
if (blocks[i].bounds.Contains(cursor.p0)) {
|
// if (net.me.blocks[i].bounds.Contains(cursor.p0)) {
|
||||||
blockIndex = i;
|
// blockIndex = i;
|
||||||
blockOffset = cursor.p0 - blocks[i].bounds.center;
|
// blockOffset = cursor.p0 - net.me.blocks[i].bounds.center;
|
||||||
blocks[i].color = colorCube.color;
|
// net.me.blocks[i].color = colorCube.color;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
blocks[blockIndex].bounds.center = cursor.p0 - blockOffset;
|
// net.me.blocks[blockIndex].bounds.center = cursor.p0 - blockOffset;
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
blockIndex = -1;
|
// blockIndex = -1;
|
||||||
}
|
// }
|
||||||
for (int i = 0; i < blocks.Length; i++) {
|
cube.Draw(mat, floor.GetPose().ToMatrix(floorScale));
|
||||||
cube.Draw(mat, Matrix.TS(blocks[i].bounds.center, blocks[i].bounds.dimensions), blocks[i].color);
|
// 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();
|
// cursor.Step(lHand.aim, rHand.aim); cursor.DrawSelf();
|
||||||
// net.me.cursorA = Vec3.Up * (float)Math.Sin(Time.Total);
|
// 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.offHand.ToMatrix(Vec3.One * 0.1f));
|
||||||
net.Cubee(peer.mainHand.ToMatrix(Vec3.One * 0.1f));
|
net.Cubee(peer.mainHand.ToMatrix(Vec3.One * 0.1f));
|
||||||
// cubicFlow.Draw(peer.cursorA, peer.cursorB, peer.cursorC, peer.cursorD);
|
// 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;
|
// Matrix orbitMatrix = OrbitalView.transform;
|
||||||
// cube.Step(Matrix.S(Vec3.One * 0.2f) * orbitMatrix);
|
// 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 class Mic {
|
||||||
public float[] bufferRaw = new float[0];
|
public float[] bufferRaw = new float[0];
|
||||||
public int bufferRawSize = 0;
|
public int bufferRawSize = 0;
|
||||||
|
@ -381,6 +371,34 @@ public class Oriel {
|
||||||
if (bounds.Contains(head.position, quadPos)) {
|
if (bounds.Contains(head.position, quadPos)) {
|
||||||
quad.Draw(mat, Matrix.TRS(quadPos, Quat.LookAt(quadPos, head.position), Vec3.One * 0.5f));
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,9 +29,14 @@ movement:
|
||||||
- ~~teleport and drag~~
|
- ~~teleport and drag~~
|
||||||
- ~~bezier rails~~
|
- ~~bezier rails~~
|
||||||
|
|
||||||
|
interact:
|
||||||
~~blocks you can manipulate with spatial cursors~~ (trackballer)
|
~~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)
|
oriel + orbital view (control a shooty guy with fullstick and spatial cursor)
|
||||||
|
|
||||||
### POLISH
|
### POLISH
|
||||||
|
|
Loading…
Add table
Reference in a new issue