diff --git a/sk_demo/src/Mono.cs b/sk_demo/src/Mono.cs index 5e3c9a2..19d67a8 100644 --- a/sk_demo/src/Mono.cs +++ b/sk_demo/src/Mono.cs @@ -1,4 +1,4 @@ -using System.Security.AccessControl; +using System.Security.AccessControl; namespace Main; public class Mono { @@ -19,8 +19,6 @@ public class Mono { Model model_room = Model.FromFile("room.glb"); Mesh mesh_room = Mesh.Quad; - int last_xi = 0; - public class Character { public string character; public int[] dots; @@ -212,6 +210,10 @@ public class Mono { return layouts[Layout.Colemak][index]; } + Matrix keyboard_m4 = Matrix.Identity; + Rig.Btn keyboard_btn = new(); + Vec3 index_pos = Vec3.Zero; + int index_i = 0; public void Init() { rig.Init(); @@ -226,7 +228,11 @@ public class Mono { public void Run() { rig.Run(); - mat.Run(); // run here or at the end?... or maybe both? + keyboard_m4 = Matrix.TRS( + V.XYZ(-0.18f, 1.15f, -0.5f), + Quat.FromAngles(0, 180, 0) * Quat.FromAngles(40, 0, 0), + V.XYZ(-1, -1, 1) * 0.04f + ); // thumb extension // Matrix palm_ori = rig.hand_1.palm.ToMatrix(); @@ -241,54 +247,47 @@ public class Mono { monoNet.send = true; } - int xi = (int)(rig.hand_1.palm.position.x * 25f); - xi = Math.Clamp(xi + 4, 0, 26); + Vec3 index_tip = keyboard_m4.Inverse * rig.hand_1.Get(FingerId.Index, JointId.Tip).position; + // press + index_pos.z = Math.Clamp(index_tip.z, -0.3f, 0.1f); + keyboard_btn.Frame(index_pos.z > 0.0f, index_pos.z < -0.2f); - Matrix palm_ori = rig.hand_1.palm.ToMatrix(); - Vec3 thumb_tip = palm_ori.Inverse * rig.hand_1.Get(FingerId.Thumb, JointId.Tip).position; - Vec3 thumb_minor = palm_ori.Inverse * rig.hand_1.Get(FingerId.Thumb, JointId.KnuckleMinor).position; - if (thumb_tip.x < thumb_minor.x - 0f * U.cm) { - // Log.Info("thumb is out"); - xi = 0; + if (!keyboard_btn.held) { + // snap to grid + index_pos.x = (float)Math.Clamp(Math.Round(index_tip.x), 0, 10-1); + index_pos.y = (float)Math.Clamp(Math.Round(index_tip.y), 0, 3-1); + + index_i = (int)(index_pos.y * 10 + index_pos.x); } - if (xi != last_xi) { - Log.Info($"xi: {xi}"); - last_xi = xi; - monoNet.value = xi; + if (keyboard_btn.frameDown) { + Log.Info($"index_i: {index_i}"); + monoNet.value = index_i; monoNet.send = true; } // keyboard - Matrix keyboard_m4 = Matrix.TRS( - rig.head_pos + V.XYZ(0, -0.1f, -0.2f), - rig.head_ori * Quat.FromAngles(0, 180, 0), - 0.2f - ); Hierarchy.Push(keyboard_m4); // 3 rows of 10 keys - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 10; j++) { + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 10; x++) { + Vec3 pos = V.XYZ(x, y, 0); // background quad Mesh.Quad.Draw( mat.unlit_clear, - Matrix.TRS( - V.XYZ(j * -0.1f, i * -0.1f, 0.0f) + V.XYZ(0.45f, 0.0f, 0.0f), - Quat.FromAngles(0, 0, 0), - 0.09f - ), - Color.Hex(0x00000020) + Matrix.TS(pos, 0.9f), + Color.Hex(0x00000040) ); - int index = i * 10 + j; + int index = y * 10 + x; // string text = characters[index].character; string keychar = KeyToChar(index); Text.Add( keychar, Matrix.TS( - V.XYZ(j * -0.1f, i * -0.1f, 0.0f) + V.XYZ(0.45f, 0.0f, 0.0f), - 1.0f + pos + V.XYZ(-0.3f, -0.3f, 0), + V.XYZ(-1, -1, 0) * 8.0f ), TextAlign.Center, TextAlign.Center, @@ -296,17 +295,17 @@ public class Mono { ); // show the braille dots(spheres) above the text for (int k = 0; k < 6; k++) { - float spacing = 0.02f; + float spacing = 0.2f; float x_offset = (k % 2) * spacing; float y_offset = (k / 2) * -spacing; if (char_cell[keychar][k] == 1) { Mesh.Sphere.Draw( mat.unlit_clear, Matrix.TS( - V.XYZ(j * -0.1f - x_offset, i * -0.1f + y_offset, 0.0f) + V.XYZ(0.45f + 0.01f, 0.018f, 0.0f), - 0.01f + V.XYZ(pos.x + x_offset, pos.y - y_offset, 0) + V.XYZ(-0.1f, -0.18f, 0), + 0.1f ), - Color.Hex(0x80808040) + Color.Hex(0xFFFFFFFF) ); } } @@ -314,6 +313,16 @@ public class Mono { } Hierarchy.Pop(); + // keyboard button + Mesh.Cube.Draw( + mat.unlit_clear, + Matrix.TS( + index_pos * V.XYZ(1, 1, 0.25f), + V.XYZ(0.8f, 0.8f, -index_pos.z * 0.5f) + ) * keyboard_m4, + keyboard_btn.held ? Color.Hex(0x00000090) : Color.Hex(0x80808090) + ); + // WORLD mesh_room.Draw( mat_room, @@ -335,5 +344,7 @@ public class Mono { V.XYZ(2, 2f, 2f) * U.cm, V.XYZ(0, 0, 0) }; + + mat.Run(); } } \ No newline at end of file