diff --git a/add/oriel.hlsl b/add/oriel.hlsl index 2bf1d3c..d6723a6 100644 --- a/add/oriel.hlsl +++ b/add/oriel.hlsl @@ -53,12 +53,12 @@ float sdBox(float3 p, float3 b) { return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); } -float raymarch(float3 ro, float3 rd) { - ro = mul(float4(ro, 1), _matrix).xyz; - rd = mul(float4(rd, 0), _matrix).xyz; +float raymarch(float3 origin, float3 direction) { + origin = mul(float4(origin, 1), _matrix).xyz; + direction = mul(float4(direction, 0), _matrix).xyz; float dist = 0.0; for (int i = 0; i < 256; i++) { - float3 pos = ro + dist * rd; + float3 pos = origin + dist * direction; float step = sdBox(pos, _dimensions / 2.0); if (step < 0.0001 || dist > 100) break; // 100 == distmax dist += step; @@ -72,15 +72,15 @@ psOut ps(psIn input) { o.color = input.color; o.depth = input.position.z; - float3 ro = input.campos; - float3 rd = normalize(input.world - ro); - // ro = ; - float ol = raymarch(ro, rd); + float3 origin = input.campos; + float3 direction = normalize(input.world - origin); + // origin = ; + float ol = raymarch(origin, direction); clip(100 - (ol + 1)); - ro += ol * rd; + origin += ol * direction; - clip(distance(input.campos, input.world) - distance(input.campos, ro)); + clip(distance(input.campos, input.world) - distance(input.campos, origin)); // normalize(input.world - _center) float t = 1 - (1 + dot(input.normal, _light)) / 2; @@ -90,7 +90,7 @@ psOut ps(psIn input) { // backface - if (dot(rd, input.normal) > 0) { + if (dot(direction, input.normal) > 0) { o.color = float4(0.5, 0.5, 0.5, 1); } return o; diff --git a/add/shed/shed.glb b/add/shed/shed.glb new file mode 100644 index 0000000..16e11f6 Binary files /dev/null and b/add/shed/shed.glb differ diff --git a/add/shed/shed.jpg b/add/shed/shed.jpg new file mode 100644 index 0000000..2341881 Binary files /dev/null and b/add/shed/shed.jpg differ diff --git a/app/App.cs b/app/App.cs new file mode 100644 index 0000000..51300f9 --- /dev/null +++ b/app/App.cs @@ -0,0 +1,12 @@ +using System; +using StereoKit; + +interface IApp { + void Step(); +} + +public class App { + public App() { + + } +} \ No newline at end of file diff --git a/app/Monolith.cs b/app/Monolith.cs index b73968e..56ba688 100644 --- a/app/Monolith.cs +++ b/app/Monolith.cs @@ -48,6 +48,8 @@ public struct Btn { } public class Monolith { + public PullRequest.Noise noise; + public MonoNet net; public Scene scene; public Mic mic; @@ -90,6 +92,10 @@ public class Monolith { public void Run() { Renderer.SetClip(0.02f, 1000f); + noise = new PullRequest.Noise(978235461); + + Console.WriteLine("noise = " + noise.value); + scene = new Scene(this); net = new MonoNet(this); net.Start(); @@ -333,7 +339,6 @@ public class Monolith { } - // colorCube.Step(); @@ -499,4 +504,56 @@ public static class PullRequest { public static Pose WorldPose(this Pose pose, float scale = 1) { return pose; } + + [Serializable] + public class Noise { + const uint CAP = 4294967295; + const uint BIT_NOISE1 = 0xB5297A4D; + const uint BIT_NOISE2 = 0x68E31DA4; + const uint BIT_NOISE3 = 0x1B56C4E9; + + public uint seed; + + public Noise(uint seed) { + this.seed = seed; + } + + int position; + public float value { + get { + float v = RNG(position, seed) / (float)CAP; + position++; + return v; + } + } + + public float D1(int position) { + return RNG(position, seed) / (float)CAP; + } + + public float D2(int x, int y) { + // large prime number with non-boring bits + const int PRIME = 198491317; + return RNG(x + (PRIME * y), seed) / (float)CAP; + } + + public float D3(int x, int y, int z) { + // large prime number with non-boring bits + const int PRIME1 = 198491317; + const int PRIME2 = 6542989; + return RNG(x + (PRIME1 * y) + (PRIME2 * z), seed) / (float)CAP; + } + + public uint RNG(int position, uint seed) { + uint mangled = (uint)position; + mangled *= BIT_NOISE1; + mangled += seed; + mangled ^= mangled >> 8; + mangled += BIT_NOISE2; + mangled ^= mangled << 8; + mangled *= BIT_NOISE3; + mangled ^= mangled >> 8; + return mangled; + } + } } diff --git a/app/Oriel.cs b/app/Oriel.cs index a612c31..195b193 100644 --- a/app/Oriel.cs +++ b/app/Oriel.cs @@ -71,7 +71,7 @@ public class Oriel { // edge detection = (2 axis) // corner detection = (3 axis) // Pose pose = new Pose(); - + Vec3 localPos = ori.Inverse * (rGlovePos - bounds.center); @@ -106,7 +106,7 @@ public class Oriel { lOffset = ori.Inverse * vOffset; qOffset = (ori.Inverse * rGloveRot).Normalized; mOffset = matrix; - anchor = bounds.center + ori * -(detect * bounds.dimensions / 2); + anchor = bounds.center + ori * -(detect * bounds.dimensions / 2); adjusting = false; } else { @@ -190,48 +190,55 @@ public class Oriel { matOriel["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(matrix); - - Matrix orielSimMatrix = Matrix.TRS(new Vec3(0, -bounds.dimensions.y / 2, 0), Quat.Identity, Vec3.One * 0.5f * bounds.dimensions.y).Inverse; + + // APP + Vec3 playerWorldPos = playerPos * 0.5f * bounds.dimensions.y; + Matrix orielSimMatrix = Matrix.TRS( + new Vec3(0, -bounds.dimensions.y / 2, -playerWorldPos.z), + Quat.Identity, + Vec3.One * 0.5f * bounds.dimensions.y + ); if (drawAxis) { meshAxis.Draw(matOriel, - Matrix.TRS(Vec3.Zero, Quat.Identity, Vec3.One * 1f) * orielSimMatrix.Inverse * matrix.Inverse, + Matrix.TRS(Vec3.Zero, Quat.Identity, Vec3.One * 1f) * orielSimMatrix * matrix.Inverse, Color.White ); } Mesh.Quad.Draw(matOriel, - Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 100f) * orielSimMatrix.Inverse * matrix.Inverse, + Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 100f) * orielSimMatrix * matrix.Inverse, new Color(1.0f, 1.0f, 1.0f) * 0.3f ); - meshCube.Draw(matOriel, + meshCube.Draw(matOriel, mono.rGlove.virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 3 * 1.05f), new Color(0.3f, 0.3f, 0.6f) ); - // draw relative to oriel matrix - // for (int i = 0; i < asteroids.Count; i++) { - // Asteroid asteroid = asteroids[i]; - // asteroid.pose.orientation = Quat.FromAngles(Time.Elapsedf * 10 * asteroid.scale, 0, 0) * asteroid.pose.orientation; - // meshAsteroid.Draw(matOriel, - // asteroid.pose.ToMatrix(Vec3.One * asteroid.scale) * matrix.Inverse, - // Color.White * 0.32f - // ); - // } - playerPos += new Vec3(-mono.lCon.device.stick.x, 0, -mono.lCon.device.stick.y) * Time.Elapsedf; - player.Move(playerPos + simOffset, Quat.Identity); + + + + float fwd = Input.Key(Key.W).IsActive() ? 1 : 0; + playerPos += new Vec3(-mono.lCon.device.stick.x, 0, -mono.lCon.device.stick.y + fwd) * Time.Elapsedf; meshCube.Draw(matOriel, - Matrix.TRS(player.GetPose().position + (player.GetPose().orientation * Vec3.Up * 0.5f) - simOffset, player.GetPose().orientation, new Vec3(0.4f, 1f, 0.2f)) * orielSimMatrix.Inverse * matrix.Inverse, + Matrix.TRS(playerPos, Quat.Identity, new Vec3(0.4f, 1f, 0.2f)) * orielSimMatrix * matrix.Inverse, new Color(1.0f, 0.0f, 0.05f) ); + // destroy enemies that are too close to the playerPos + for (int i = 0; i < enemies.Count; i++) { + if (Vec3.Distance(enemies[i], playerPos) < 0.5f) { + enemies.RemoveAt(i); + i--; + } + } // FULLSTICK // Vec3 Fullstick() { @@ -243,77 +250,61 @@ public class Oriel { // Vec3 fullstick = Fullstick(); // sword.Move(playerPos + simOffset + fullstick, Quat.LookAt(Vec3.Zero, fullstick, Vec3.Up)); // meshCube.Draw(matOriel, - // Matrix.TRS(sword.GetPose().position + (Vec3.Up * 0.7f) + (sword.GetPose().orientation * Vec3.Forward * -0.5f) - simOffset, sword.GetPose().orientation, new Vec3(0.1f, 0.03f, 1f)) * orielSimMatrix.Inverse * matrix.Inverse, + // Matrix.TRS(sword.GetPose().position + (Vec3.Up * 0.7f) + (sword.GetPose().orientation * Vec3.Forward * -0.5f) - simOffset, sword.GetPose().orientation, new Vec3(0.1f, 0.03f, 1f)) * orielSimMatrix * matrix.Inverse, // new Color(0.9f, 0.5f, 0.5f) // ); + if (Time.Totalf > spawnTime) { + enemies.Add(playerPos + Quat.FromAngles(0, mono.noise.value * 360f, 0) * Vec3.Forward * 8); + spawnTime = Time.Totalf + 1; + } for (int i = 0; i < enemies.Count; i++) { - Solid enemy = enemies[i]; - Pose pose = enemy.GetPose(); + // move towards player - enemy.Move(pose.position + (playerPos - pose.position).Normalized * Time.Elapsedf * 0.5f, pose.orientation); + Vec3 toPlayer = (playerPos - enemies[i]).Normalized; + Vec3 newPos = enemies[i] + toPlayer * Time.Elapsedf * 0.5f; + + // if far enough away from other enemies than set new pos + bool setNewPos = true; + for (int j = 0; j < enemies.Count; j++) { + if (i == j) continue; + if ((newPos - enemies[j]).Length < 0.5f) { + setNewPos = false; + break; + } + } + + if (setNewPos) { + enemies[i] = newPos; + } + + + + meshCube.Draw(matOriel, - Matrix.TRS(pose.position - simOffset, pose.orientation, new Vec3(0.4f, 1f, 0.2f) * 0.99f) * orielSimMatrix.Inverse * matrix.Inverse, + Matrix.TRS(enemies[i], + Quat.LookAt(enemies[i], playerPos, Vec3.Up), + new Vec3(0.4f, 1f, 0.2f) + ) * orielSimMatrix * matrix.Inverse, Color.White * 0.62f ); } + } - Solid ground; - Solid player; Vec3 playerPos; - Solid sword; - List enemies = new List(); - Vec3 simOffset = new Vec3(0, 100, 0); + // Custom > Physics + // issue that we are having is we don't have enough access to the physics sim + // and because of that we run into issues where solutions we've learned of in Unity + // will not carry over simply due to what we have current access to. - Mesh meshAsteroid, meshAxis; + // getting over these physics hurdles is worthwhile, but not when we have a viable alternate solution + Vec3 playerPos; + List enemies = new List(); + float spawnTime; - static Model modelAsteroids = Model.FromFile("asteroids.glb"); - class Asteroid { - public Pose pose; - public float scale; - - public Asteroid(Pose pose, float scale) { - this.pose = pose; - this.scale = scale; - } - } - List asteroids = new List(); + Mesh meshAxis; void Gen() { meshAxis = model.GetMesh("Axis"); - meshAsteroid = modelAsteroids.GetMesh("1 Meteor"); - asteroids.Clear(); - Random random = new Random(); - - // for (int i = 0; i < 128; i++) { - // Pose pose = new Pose( - // PullRequest.RandomInCube(Vec3.Up * bounds.dimensions.y * 1, bounds.dimensions.x * 2), - // Quat.FromAngles( - // random.Next(360), - // random.Next(360), - // random.Next(360) - // ) - // ); - // asteroids.Add(new Asteroid(pose, 0.1f + random.NextSingle() * 0.4f)); - // } - - ground = new Solid(simOffset, Quat.Identity, SolidType.Immovable); - ground.AddBox(new Vec3(20, 1, 20), 1, new Vec3(0, -0.5f, 0)); - - player = new Solid(simOffset + Vec3.Up, Quat.Identity, SolidType.Normal); - player.AddBox(new Vec3(0.4f, 1f, 0.2f), 1, new Vec3(0, 0.5f, 0)); - - sword = new Solid(simOffset + Vec3.Up, Quat.Identity, SolidType.Normal); - sword.AddBox(new Vec3(0.1f, 0.03f, 1f), 1, new Vec3(0, 0, -0.5f)); - - for (int i = 0; i < 32; i++) { - Solid solid = new Solid( - simOffset + Vec3.Up * i, - Quat.Identity, - SolidType.Normal - ); - solid.AddBox(new Vec3(0.4f, 1f, 0.2f), 1); - enemies.Add(solid); - } } } \ No newline at end of file diff --git a/app/Scene.cs b/app/Scene.cs index 0889134..fce6e86 100644 --- a/app/Scene.cs +++ b/app/Scene.cs @@ -16,7 +16,8 @@ public class Scene { BufferData data = new BufferData(); Material matFloor = new Material(Shader.Default); - Model room = Model.FromFile("room/room.glb", Shader.FromFile("room.hlsl")); + // Model room = Model.FromFile("room/room.glb", Shader.FromFile("room.hlsl")); + Model room = Model.FromFile("shed/shed.glb", Shader.FromFile("room.hlsl")); Solid floor; @@ -71,7 +72,7 @@ public class Scene { // room.RootNode.Material["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix); // Shader. - - room.Draw(Matrix.TRS(new Vec3(0, World.BoundsPose.position.y, -1), Quat.Identity, Vec3.One)); + // World.BoundsPose.position.y + room.Draw(Matrix.TRS(new Vec3(0, -1.6f, 0), Quat.Identity, Vec3.One)); } } \ No newline at end of file