From ca4b715130128d46a3792f81423dbd5bd4df2d3c Mon Sep 17 00:00:00 2001 From: spatialfree Date: Sat, 7 Dec 2024 17:57:31 -0500 Subject: [PATCH] color cube --- Assets/colorcube.hlsl | 48 +++++++++++++++++++ Assets/meshes/assets.glb | 4 +- Platforms/Android/AndroidManifest.xml | 4 +- readme.md | 3 +- src/Arts.cs | 69 ++++++++++++++++++--------- src/Dofs.cs | 48 ++++++++++++++++++- src/Mono.cs | 5 +- 7 files changed, 151 insertions(+), 30 deletions(-) create mode 100644 Assets/colorcube.hlsl diff --git a/Assets/colorcube.hlsl b/Assets/colorcube.hlsl new file mode 100644 index 0000000..f17f183 --- /dev/null +++ b/Assets/colorcube.hlsl @@ -0,0 +1,48 @@ +#include "stereokit.hlsli" + +//--name = dofdev/colorcube +//--color:color = 1, 1, 1, 1 + +float4 color; + +struct vsIn { + float4 pos : SV_Position; + float3 norm : NORMAL0; +}; +struct psIn { + float4 pos : SV_POSITION; + float3 col_pos : TEXCOORD0; + float4 color : COLOR0; + uint view_id : SV_RenderTargetArrayIndex; +}; + +float f_inv(float x) { + if (x >= 0.04045) { + return pow((x + 0.055)/(1 + 0.055), 2.4); + } + + return x / 12.92; +} + +psIn vs(vsIn input, uint id : SV_InstanceID) { + psIn o; + o.view_id = id % sk_view_count; + id = id / sk_view_count; + + float3 world = mul(float4(input.pos.xyz, 1), sk_inst[id].world).xyz; + o.pos = mul(float4(world, 1), sk_viewproj[o.view_id]); + o.col_pos = input.pos.xyz + float3(0.5, 0.5, 0.5); + + float3 normal = normalize(mul(input.norm, (float3x3)sk_inst[id].world)); + + o.color = color * sk_inst[id].color; + return o; +} + +float4 ps(psIn input) : SV_TARGET { + // pos to color + float3 col = float3(input.col_pos); + + // linear to sRGB + return float4(f_inv(col.r), f_inv(col.g), f_inv(col.b), 1); +} \ No newline at end of file diff --git a/Assets/meshes/assets.glb b/Assets/meshes/assets.glb index f333627..4bb875f 100644 --- a/Assets/meshes/assets.glb +++ b/Assets/meshes/assets.glb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8102da697bdb6bb808f0fec44aa31221e008e9cb5ea69d607f0d63558cb4b03f -size 398664 +oid sha256:253c75378db0b495c212ddfc6181dce32432d7df5f3875e20f55bee6617a030d +size 6844 diff --git a/Platforms/Android/AndroidManifest.xml b/Platforms/Android/AndroidManifest.xml index bc82f06..c955c1d 100644 --- a/Platforms/Android/AndroidManifest.xml +++ b/Platforms/Android/AndroidManifest.xml @@ -2,8 +2,8 @@ diff --git a/readme.md b/readme.md index c6c35ce..ef49e7e 100644 --- a/readme.md +++ b/readme.md @@ -51,7 +51,8 @@ todo featuring: [x] stretch_cursor - [ ] color_cube // move cube around picker which is constrained by cube limits + [x] color_cube + [ ] reach_cursor [ ] orbital_view // same dis/mount system as snake in a box [ ] fullstick [ ] twist_cursor diff --git a/src/Arts.cs b/src/Arts.cs index 32a2474..21199d8 100644 --- a/src/Arts.cs +++ b/src/Arts.cs @@ -12,7 +12,8 @@ static class Arts static Material mat_both = new Material("unlit.hlsl"); static Material mat_backface = new Material("backface.hlsl"); static Material mat_justcolor = new Material("justcolor.hlsl"); - + static Material mat_colorcube = new Material("colorcube.hlsl"); + static Material mat_colorcursor = new Material("justcolor.hlsl"); public static Vec3 shake = new(0, 0, 0); static TextStyle text_style; @@ -39,11 +40,14 @@ static class Arts mat_backface.DepthWrite = false; mat_both.Chain = mat_backface; + + // draw ontop of everything + mat_colorcursor.DepthTest = DepthTest.Always; } public static void Frame() { - // Input.HandVisible(Handed.Max, false); + Input.HandVisible(Handed.Max, false); // world // Matrix m4_dof = Mono.dof_pose.ToMatrix(Mono.dof_scl); @@ -85,33 +89,52 @@ static class Arts ); } - // reach cursor - // pos = trackedPoint - // if down - // pullPoint = pos + // color_cube + { + meshes["color_cube"].Draw( + mat_colorcube, + ColorCube.grab.pose.ToMatrix(ColorCube.scl) + ); - // stretch = distance(pullPoint, pos) - // dir = (pos - pullPoint).normalized - // cursor = pos + dir * stretch * 3 + Hierarchy.Push(ColorCube.grab.pose.ToMatrix(ColorCube.scl)); + // ColorCube.cursor render ontop *degree symbol with color inside + Mesh.Sphere.Draw( + mat_colorcursor, + Matrix.TS( + ColorCube.cursor, + 24.0f * U.cm + ), + Color.White + ); + Mesh.Sphere.Draw( + mat_colorcursor, + Matrix.TS( + ColorCube.cursor, + 16.0f * U.cm + ), + ColorCube.color + ); + Hierarchy.Pop(); + } // Hierarchy.Pop(); // particles - Particle[] particles = VFX.particles; - for (int i = 0; i < particles.Length; i++) - { - Particle particle = particles[i]; - meshes["FoodParticle"].Draw( - mat_unlit, - Matrix.TRS( - particle.pos, - particle.ori, - particle.scl - ), - Color.Hex(0xC75A09FF).ToLinear() - ); - } + // Particle[] particles = VFX.particles; + // for (int i = 0; i < particles.Length; i++) + // { + // Particle particle = particles[i]; + // meshes["FoodParticle"].Draw( + // mat_unlit, + // Matrix.TRS( + // particle.pos, + // particle.ori, + // particle.scl + // ), + // Color.Hex(0xC75A09FF).ToLinear() + // ); + // } // menu Matrix m4_menu = Mono.menu_pose.ToMatrix(Mono.menu_scale); diff --git a/src/Dofs.cs b/src/Dofs.cs index 6fa7d65..76db3ea 100644 --- a/src/Dofs.cs +++ b/src/Dofs.cs @@ -29,4 +29,50 @@ static class Stretch // design static float deadzone = 0.1f; static float strength = 3; -} \ No newline at end of file +} + +static class ColorCube +{ + public static float scl = 6 * U.cm; + public static Grab grab; + static Vec3 last_position; + + public static Vec3 cursor; + public static Color color; + + public static void Init() + { + grab = new(); + } + + public static void Frame() + { + Vec3 delta = grab.pose.position - last_position; + // move cube around picker which is constrained by cube limits + cursor -= delta / scl; + + cursor = V.XYZ( + Maths.s_clamp(cursor.x, 0.5f), + Maths.s_clamp(cursor.y, 0.5f), + Maths.s_clamp(cursor.z, 0.5f) + ); + + color = new Color( + f_inv(cursor.x + 0.5f), + f_inv(cursor.y + 0.5f), + f_inv(cursor.z + 0.5f), + 1 + ); + + last_position = grab.pose.position; + } + + static float f_inv(float x) { + if (x >= 0.04045f) { + return MathF.Pow((x + 0.055f) / (1 + 0.055f), 2.4f); + } + + return x / 12.92f; + } +} + diff --git a/src/Mono.cs b/src/Mono.cs index 79507d6..0cd9d13 100644 --- a/src/Mono.cs +++ b/src/Mono.cs @@ -36,6 +36,7 @@ static class Mono // dof_pose = new(0, 0, 0); // new(0, -1, -2); // dof_scl = 1.0f; // 0.1f; Stretch.Init(); + ColorCube.Init(); } public static void Frame() @@ -99,7 +100,8 @@ static class Mono Grab[] grabs = new Grab[] { Stretch.to_grab, - Stretch.from_grab + Stretch.from_grab, + ColorCube.grab, }; foreach (var grab in grabs) { @@ -150,5 +152,6 @@ static class Mono } Stretch.Frame(); + ColorCube.Frame(); } }