From 3c2061f7882b5158a50c3089d9adcea7e334d8b1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 7 Sep 2025 23:23:01 +0300 Subject: [PATCH] add hud.default_hand_controller --- res/scripts/hud.lua | 68 +++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/res/scripts/hud.lua b/res/scripts/hud.lua index cf2153a2..90883d94 100644 --- a/res/scripts/hud.lua +++ b/res/scripts/hud.lua @@ -22,6 +22,39 @@ local function configure_SSAO() -- for test purposes end +local function update_hand() + local skeleton = gfx.skeletons + local pid = hud.get_player() + local invid, slot = player.get_inventory(pid) + local itemid = inventory.get(invid, slot) + + local cam = cameras.get("core:first-person") + local bone = skeleton.index("hand", "item") + + local offset = vec3.mul(vec3.sub(cam:get_pos(), {player.get_pos(pid)}), -1) + + local rotation = cam:get_rot() + + local angle = player.get_rot(pid) - 90 + local cos = math.cos(angle / (180 / math.pi)) + local sin = math.sin(angle / (180 / math.pi)) + + local newX = offset[1] * cos - offset[3] * sin + local newZ = offset[1] * sin + offset[3] * cos + + offset[1] = newX + offset[3] = newZ + + local mat = mat4.translate(mat4.idt(), {0.06, 0.035, -0.1}) + mat4.scale(mat, {0.1, 0.1, 0.1}, mat) + mat4.mul(rotation, mat, mat) + mat4.rotate(mat, {0, 1, 0}, -90, mat) + mat4.translate(mat, offset, mat) + + skeleton.set_matrix("hand", bone, mat) + skeleton.set_model("hand", bone, item.model_name(itemid)) +end + function on_hud_open() input.add_callback("player.pick", function () if hud.is_paused() or hud.is_inventory_open() then @@ -63,7 +96,7 @@ function on_hud_open() player.set_noclip(pid, true) end end) - + input.add_callback("player.flight", function () if hud.is_paused() or hud.is_inventory_open() then return @@ -81,39 +114,8 @@ function on_hud_open() end) configure_SSAO() -end -local function update_hand() - local skeleton = gfx.skeletons - local pid = hud.get_player() - local invid, slot = player.get_inventory(pid) - local itemid = inventory.get(invid, slot) - - local cam = cameras.get("core:first-person") - local bone = skeleton.index("hand", "item") - - local offset = vec3.mul(vec3.sub(cam:get_pos(), {player.get_pos(pid)}), -1) - - local rotation = cam:get_rot() - - local angle = player.get_rot(pid) - 90 - local cos = math.cos(angle / (180 / math.pi)) - local sin = math.sin(angle / (180 / math.pi)) - - local newX = offset[1] * cos - offset[3] * sin - local newZ = offset[1] * sin + offset[3] * cos - - offset[1] = newX - offset[3] = newZ - - local mat = mat4.translate(mat4.idt(), {0.06, 0.035, -0.1}) - mat4.scale(mat, {0.1, 0.1, 0.1}, mat) - mat4.mul(rotation, mat, mat) - mat4.rotate(mat, {0, 1, 0}, -90, mat) - mat4.translate(mat, offset, mat) - - skeleton.set_matrix("hand", bone, mat) - skeleton.set_model("hand", bone, item.model_name(itemid)) + hud.default_hand_controller = update_hand end function on_hud_render()