From 908c6c3db8d8658c8225a80c38cc1513e5dabb56 Mon Sep 17 00:00:00 2001 From: spatialfree Date: Mon, 21 Mar 2022 21:47:14 -0400 Subject: [PATCH] matrix is a reserved name --- add/oriel.hlsl | 9 ++-- add/room.hlsl | 62 ++++++++++++++++++++++++++-- app/Glove.cs | 8 ++-- app/MonoNet.cs | 2 +- app/Monolith.cs | 14 ++++--- app/Oriel.cs | 106 +++++++++++++++++++++++++++++++----------------- app/Scene.cs | 42 ++++++++++++++++++- oriels.csproj | 2 +- 8 files changed, 188 insertions(+), 57 deletions(-) diff --git a/add/oriel.hlsl b/add/oriel.hlsl index ee0fc8d..2bf1d3c 100644 --- a/add/oriel.hlsl +++ b/add/oriel.hlsl @@ -3,6 +3,7 @@ // --name = dofdev/oriel float3 _center; float3 _dimensions; +float3 _light; float4x4 _matrix; struct vsIn { @@ -81,9 +82,11 @@ psOut ps(psIn input) { clip(distance(input.campos, input.world) - distance(input.campos, ro)); - float t = 1 - (1 + dot(input.normal, normalize(input.world - _center))) / 2; - float3 light = lerp(float3(0.05, 0.05, 0.1), float3(0.95, 0.95, 0.9), t); - o.color = float4(o.color.rgb * light, 1); + // normalize(input.world - _center) + float t = 1 - (1 + dot(input.normal, _light)) / 2; + // float3 light = lerp(float3(0.05, 0.05, 0.1), float3(0.95, 0.95, 0.9), t); + // o.color = float4(o.color.rgb * light, 1); + o.color = float4(o.color.rgb * t, 1); // backface diff --git a/add/room.hlsl b/add/room.hlsl index 1044e5f..f6f5b3d 100644 --- a/add/room.hlsl +++ b/add/room.hlsl @@ -11,6 +11,12 @@ float tex_scale; Texture2D diffuse : register(t0); SamplerState diffuse_s : register(s0); +cbuffer BufferData : register(b3) { + float4x4 oriel_matrix; + float3 dimensions; + float time; +}; + struct vsIn { float4 pos : SV_Position; float3 norm : NORMAL0; @@ -19,8 +25,11 @@ struct vsIn { }; struct psIn { float4 pos : SV_Position; + float3 world : WORLD; float2 uv : TEXCOORD0; float4 color : COLOR0; + float3 camdir : TEXCOORD1; + float3 campos : TEXCOORD2; uint view_id : SV_RenderTargetArrayIndex; }; @@ -29,8 +38,11 @@ psIn vs(vsIn input, uint id : SV_InstanceID) { o.view_id = id % sk_view_count; id = id / sk_view_count; - float4 world = mul(input.pos, sk_inst[id].world); - o.pos = mul(world, sk_viewproj[o.view_id]); + o.camdir = sk_camera_dir[o.view_id].xyz; + 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]); float3 normal = normalize(mul(input.norm, (float3x3)sk_inst[id].world)); @@ -40,8 +52,50 @@ psIn vs(vsIn input, uint id : SV_InstanceID) { return o; } +float sdBox(float3 p, float3 b) { + float3 q = abs(p) - 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), oriel_matrix).xyz; + rd = mul(float4(rd, 0), oriel_matrix).xyz; + float dist = 0.0; + for (int i = 0; i < 256; i++) { + float3 pos = ro + dist * rd; + float step = sdBox(pos, dimensions / 2.0); + if (step < 0.0001 || dist > 100) break; // 100 == distmax + dist += step; + } + + return dist; +} + float4 ps(psIn input) : SV_TARGET { float4 col = diffuse.Sample(diffuse_s, input.uv); - float value = (col.r + col.r + col.g + col.g + col.g + col.b) / 6; - return float4(value, value, value, 1); + + + float3 ro = input.campos; + float3 rd = normalize(input.world - ro); + float ol = raymarch(ro, rd); + clip(-(100 - (ol + 1))); + // if ((100 - (ol + 1)) > 0) { + // col *= 0.1; + // } + + ro += ol * rd; + + // clip((distance(input.campos, input.world) - distance(input.campos, ro)) * -1); + // if ((distance(input.campos, input.world) - distance(input.campos, ro)) >= 0) { + // col *= 0.1; + // } + + // if (input.world.y < bufferCenter.y) { + // col *= 0.1; + // } + + + // float value = (col.r + col.r + col.g + col.g + col.g + col.b) / 6; + // return float4(value, value, value, 1); + return col; } \ No newline at end of file diff --git a/app/Glove.cs b/app/Glove.cs index f07931f..f854f0c 100644 --- a/app/Glove.cs +++ b/app/Glove.cs @@ -132,8 +132,8 @@ public class Glove { Material mat = Default.Material; Model model = Model.FromFile("skinned_test.glb", Shader.Default); public void Render(Pose glove, Pose virtualGlove, Pose wrist, float stretch, float twist, bool chirality) { - Lines.Add(pullPoint, glove.position, new Color(1, 0, 1), 0.005f); - Lines.Add(glove.position, virtualGlove.position, new Color(0, 1, 1), 0.005f); + Lines.Add(pullPoint, glove.position, new Color(1, 0, 1, 0.1f), 0.005f); + Lines.Add(glove.position, virtualGlove.position, new Color(0, 1, 1, 0.1f), 0.005f); // Twist float twistAbs = Math.Abs(twist); @@ -155,8 +155,8 @@ public class Glove { } Lines.Add(linePoints); - mesh.Draw(mat, glove.ToMatrix(new Vec3(0.02f, 0.08f, 0.08f) / 1)); - mesh.Draw(mat, virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 1)); + // mesh.Draw(mat, glove.ToMatrix(new Vec3(0.02f, 0.08f, 0.08f) / 1)); + mesh.Draw(mat, virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 3)); // ModelNode top = model.FindNode("Top"); diff --git a/app/MonoNet.cs b/app/MonoNet.cs index 50df711..ce436ab 100644 --- a/app/MonoNet.cs +++ b/app/MonoNet.cs @@ -367,7 +367,7 @@ public class Peer { mono.rCon.pos, mono.lCon.pos, mono.lGlove.virtualGlove.position, - Color.White + new Color(1, 1, 1, 0.1f) ); for (int i = 0; i < blocks.Length; i++) { diff --git a/app/Monolith.cs b/app/Monolith.cs index c9a97c7..b73968e 100644 --- a/app/Monolith.cs +++ b/app/Monolith.cs @@ -11,7 +11,7 @@ if (!SK.Initialize(settings)) Environment.Exit(1); Input.HandSolid(Handed.Max, false); -Input.HandVisible(Handed.Max, false); +Input.HandVisible(Handed.Max, true); // TextStyle style = Text.MakeStyle(Font.FromFile("DMMono-Regular.ttf"), 0.1f, Color.White); Monolith mono = new Monolith(); @@ -491,10 +491,12 @@ public static class PullRequest { ); } - static Bounds _bounds = new Bounds(); - public static bool ActuallyContains(this Bounds bounds, Quat ori, Vec3 pos, float radius) { - _bounds.dimensions = bounds.dimensions; - Vec3 p = ori.Inverse * (pos - bounds.center); - return _bounds.Contains(p, p, radius); + public static float Lerp(float a, float b, float t) { + return a + (b - a) * t; + } + + static Pose _pose = new Pose(); + public static Pose WorldPose(this Pose pose, float scale = 1) { + return pose; } } diff --git a/app/Oriel.cs b/app/Oriel.cs index 67551fd..a612c31 100644 --- a/app/Oriel.cs +++ b/app/Oriel.cs @@ -10,10 +10,11 @@ public class Oriel { static Model model = Model.FromFile("colorball.glb"); Mesh meshCube, meshFrame; - public Bounds bounds; Vec3 _dimensions; + public Bounds bounds; public Quat ori; + public Matrix matrix; public float crown = 0.0666f; - public bool drawAxis; + public bool drawAxis = false; bool adjusting = false; // bool scalingOriel = false; @@ -21,16 +22,16 @@ public class Oriel { // bool rotatingOriel = false; Quat qOffset = Quat.Identity; Vec3 vOffset = Vec3.Zero; + Vec3 lOffset = Vec3.Zero; Vec3 anchor = Vec3.Zero; Matrix mOffset = Matrix.Identity; public Oriel() { bounds = new Bounds( - Input.Head.position + new Vec3(0, 0f, -1.5f), + Input.Head.position + new Vec3(-0.5f, 0, -1f), new Vec3(0.8f, 0.5f, 0.5f) ); - _dimensions = bounds.dimensions; ori = Quat.Identity; matFrame.SetMat(102, Cull.None, true); @@ -43,49 +44,75 @@ public class Oriel { Gen(); } + public class Transform { + public string name; + public Pose pose; + public float scale; + + public Transform() { + + } + + public Vec3 LocalPos() { + return pose.position; + } + } + Vec3 detect = Vec3.Zero; int detectCount = 0; public void Step(Monolith mono) { - Matrix matrix = Matrix.TR(bounds.center, ori).Inverse; + matrix = Matrix.TR(bounds.center, ori).Inverse; Vec3 rGlovePos = mono.rGlove.virtualGlove.position; + Quat rGloveRot = mono.rGlove.virtualGlove.orientation; // Vec3 lGlovePos = mono.lGlove.virtualGlove.position; // face detection = (1 axis) // edge detection = (2 axis) // corner detection = (3 axis) + // Pose pose = new Pose(); + - Vec3 localPos = matrix.Transform(rGlovePos); + Vec3 localPos = ori.Inverse * (rGlovePos - bounds.center); if (!mono.rCon.triggerBtn.held) { + float margin = PullRequest.Lerp(0.03f, 0.5f, bounds.dimensions.y / 2); Vec3 newDetect = Vec3.Zero; - if ((_dimensions.x / 2) - MathF.Abs(localPos.x) < 0.1f) newDetect.x = 1 * MathF.Sign(localPos.x); - if ((_dimensions.y / 2) - MathF.Abs(localPos.y) < 0.1f) newDetect.y = 1 * MathF.Sign(localPos.y); - if ((_dimensions.z / 2) - MathF.Abs(localPos.z) < 0.1f) newDetect.z = 1 * MathF.Sign(localPos.z); + if ((bounds.dimensions.x / 2) - MathF.Abs(localPos.x) < 0) newDetect.x = 1 * MathF.Sign(localPos.x); + if ((bounds.dimensions.y / 2) - MathF.Abs(localPos.y) < 0) newDetect.y = 1 * MathF.Sign(localPos.y); + if ((bounds.dimensions.z / 2) - MathF.Abs(localPos.z) < 0) newDetect.z = 1 * MathF.Sign(localPos.z); if (newDetect.x != detect.x || newDetect.y != detect.y || newDetect.z != detect.z) { detect = newDetect; detectCount = (int)(MathF.Abs(detect.x) + MathF.Abs(detect.y) + MathF.Abs(detect.z)); - Console.WriteLine(detectCount + ": " + detect); + // Console.WriteLine(detectCount + ": " + detect); } - if (!bounds.ActuallyContains(ori, rGlovePos, 0.1f) || bounds.ActuallyContains(ori, rGlovePos, 0f)) { + Vec3 dim = new Vec3( + bounds.dimensions.x + 0.1f, + bounds.dimensions.y + 0.1f, + bounds.dimensions.z + 0.1f + ); + Bounds arounds = new Bounds(Vec3.Zero, dim); + if (!arounds.Contains(localPos) || bounds.Contains(bounds.center + localPos)) { detect = Vec3.Zero; detectCount = 0; } vOffset = rGlovePos - bounds.center; + lOffset = ori.Inverse * vOffset; + qOffset = (ori.Inverse * rGloveRot).Normalized; mOffset = matrix; - // qOffset = Quat.LookAt(Vec3.Zero, vOffset.Normalized); - anchor = bounds.center + ori * -(detect * bounds.dimensions / 2); + anchor = bounds.center + ori * -(detect * bounds.dimensions / 2); adjusting = false; } else { if (detectCount == 1) { // Move - bounds.center = rGlovePos - vOffset; + ori = (rGloveRot * qOffset.Inverse).Normalized; + bounds.center = rGlovePos - ori * lOffset; } else if (detectCount == 2) { // Rotate localPos = mOffset.Transform(rGlovePos); Vec3 dir = new Vec3( @@ -154,21 +181,22 @@ public class Oriel { matPanes["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(matrix); meshCube.Draw(matPanes, Matrix.TRS(bounds.center, ori, bounds.dimensions), - new Color(0.1f, 0.1f, 0.4f) + new Color(0.0f, 0.0f, 0.5f) ); matOriel.SetVector("_center", bounds.center); matOriel.SetVector("_dimensions", bounds.dimensions); + matOriel.SetVector("_light", ori * new Vec3(0.6f, -0.9f, 0.3f)); 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.1f).Inverse; + Matrix orielSimMatrix = Matrix.TRS(new Vec3(0, -bounds.dimensions.y / 2, 0), Quat.Identity, Vec3.One * 0.5f * bounds.dimensions.y).Inverse; if (drawAxis) { @@ -179,13 +207,13 @@ public class Oriel { } Mesh.Quad.Draw(matOriel, - Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 20f) * orielSimMatrix.Inverse * matrix.Inverse, - new Color(0.3f, 0.9f, 0.3f) + Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 100f) * orielSimMatrix.Inverse * matrix.Inverse, + new Color(1.0f, 1.0f, 1.0f) * 0.3f ); meshCube.Draw(matOriel, - mono.rGlove.virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) * 1.05f), - new Color(0.5f, 0.5f, 0.9f) + 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 @@ -201,29 +229,33 @@ public class Oriel { player.Move(playerPos + simOffset, Quat.Identity); 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, - new Color(0.9f, 0.5f, 0.5f) + new Color(1.0f, 0.0f, 0.05f) ); // FULLSTICK - Vec3 Fullstick() { - Controller con = mono.lCon.device; - Quat rot = Quat.FromAngles(con.stick.y * -90, 0, con.stick.x * 90); - Vec3 dir = Vec3.Up * (con.IsStickClicked ? -1 : 1); - return con.aim.orientation * rot * dir; - } - 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, - new Color(0.9f, 0.5f, 0.5f) - ); + // Vec3 Fullstick() { + // Controller con = mono.lCon.device; + // Quat rot = Quat.FromAngles(con.stick.y * -90, 0, con.stick.x * 90); + // Vec3 dir = Vec3.Up * (con.IsStickClicked ? -1 : 1); + // return con.aim.orientation * rot * dir; + // } + // 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, + // new Color(0.9f, 0.5f, 0.5f) + // ); + 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); meshCube.Draw(matOriel, - Matrix.TRS(enemy.GetPose().position - simOffset, enemy.GetPose().orientation, new Vec3(0.4f, 1f, 0.2f)) * orielSimMatrix.Inverse * matrix.Inverse, - Color.White * 0.32f + Matrix.TRS(pose.position - simOffset, pose.orientation, new Vec3(0.4f, 1f, 0.2f) * 0.99f) * orielSimMatrix.Inverse * matrix.Inverse, + Color.White * 0.62f ); } } @@ -272,7 +304,7 @@ public class Oriel { 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.5f, 0)); + 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( diff --git a/app/Scene.cs b/app/Scene.cs index 81f3044..0889134 100644 --- a/app/Scene.cs +++ b/app/Scene.cs @@ -1,14 +1,32 @@ using System; +using System.Runtime.InteropServices; using StereoKit; +[StructLayout(LayoutKind.Sequential)] +struct BufferData { + public Matrix matrix; + public Vec3 dimensions; + public float time; +} + public class Scene { Monolith mono; + + MaterialBuffer buffer; + BufferData data = new BufferData(); + Material matFloor = new Material(Shader.Default); Model room = Model.FromFile("room/room.glb", Shader.FromFile("room.hlsl")); + + Solid floor; public Scene(Monolith mono) { this.mono = mono; + // Shader.FromFile("room.hlsl") + buffer = new MaterialBuffer(3); // index + + floor = new Solid(World.BoundsPose.position, Quat.Identity, SolidType.Immovable); scale = 64f; floorScale = new Vec3(scale, 0.1f, scale); @@ -29,8 +47,30 @@ public class Scene { public void Step() { - // PullRequest.BlockOut(floor.GetPose().ToMatrix(floorScale), Color.White * 0.333f, matFloor); + data.matrix = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix); + data.dimensions = mono.oriel.bounds.dimensions; + + buffer.Set(data); + // PullRequest.BlockOut(floor.GetPose().ToMatrix(floorScale), Color.White * 0.333f, matFloor); + foreach (ModelNode node in room.Visuals) { + + // Console.WriteLine(i + " - " + node.Name); + + // node.Material.SetVector("_center", mono.oriel.bounds.center); + // node.Material.SetVector("_dimensions", mono.oriel.bounds.dimensions); + // node.Material["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix); + + // Console.WriteLine("Shader: " + node.Material.Shader.Name); + + // node.Mesh.Draw(matRoom, Matrix.TRS(new Vec3(0, World.BoundsPose.position.y, -1), Quat.Identity, Vec3.One)); + // Console.WriteLine(matRoom.ParamCount + " test " + node.Material.ParamCount); + } + // room.RootNode.Material.SetVector("_center", mono.oriel.bounds.center); + // room.RootNode.Material.SetVector("_dimensions", mono.oriel.bounds.dimensions); + // 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)); } diff --git a/oriels.csproj b/oriels.csproj index 649853e..5a9379a 100644 --- a/oriels.csproj +++ b/oriels.csproj @@ -7,7 +7,7 @@ - +