From 9a687313096e6d4c1638de0b06184fbef3b245c3 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Thu, 25 Jan 2024 17:57:01 +0100 Subject: [PATCH] BitECS focus/track object menu support --- src/bit-systems/object-menu.ts | 34 ++++++++++++++++++++++++++++------ src/prefabs/object-menu.tsx | 4 ++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/bit-systems/object-menu.ts b/src/bit-systems/object-menu.ts index 0b181e8835..fcfb8ffc88 100644 --- a/src/bit-systems/object-menu.ts +++ b/src/bit-systems/object-menu.ts @@ -24,7 +24,9 @@ import { Inspected, Inspectable, Deletable, - MediaRefresh + MediaRefresh, + MyCameraTool, + CameraTool } from "../bit-components"; import { anyEntityWith, @@ -187,15 +189,31 @@ function cloneObject(world: HubsWorld, sourceEid: EntityID) { setMatrixWorld(clonedObj, clonedMatrixWorld); } +const tmpPos = new Vector3(); +function focus(world: HubsWorld, menu: EntityID, track: boolean = false) { + const myCam = anyEntityWith(APP.world, MyCameraTool); + if (!myCam) return; + + let target = ObjectMenu.targetRef[menu]; + if (track) { + const tracking = CameraTool.trackTarget[myCam]; + CameraTool.trackTarget[myCam] = tracking === target ? 0 : target; + } else { + const targetObj = world.eid2obj.get(target)!; + targetObj.getWorldPosition(tmpPos); + world.eid2obj.get(myCam)!.lookAt(tmpPos); + } +} + function handleClicks(world: HubsWorld, menu: EntityID, hubChannel: HubChannel) { if (clicked(world, ObjectMenu.pinButtonRef[menu])) { setPinned(hubChannel, world, ObjectMenu.targetRef[menu], true); } else if (clicked(world, ObjectMenu.unpinButtonRef[menu])) { setPinned(hubChannel, world, ObjectMenu.targetRef[menu], false); } else if (clicked(world, ObjectMenu.cameraFocusButtonRef[menu])) { - console.log("Clicked focus"); + focus(world, menu); } else if (clicked(world, ObjectMenu.cameraTrackButtonRef[menu])) { - console.log("Clicked track"); + focus(world, menu, true); } else if (clicked(world, ObjectMenu.removeButtonRef[menu])) { ObjectMenu.flags[menu] &= ~ObjectMenuFlags.Visible; deleteTheDeletableAncestor(world, ObjectMenu.targetRef[menu]); @@ -287,6 +305,7 @@ function updateVisibility(world: HubsWorld, menu: EntityID, frozen: boolean) { const isInspectable = hasComponent(world, Inspectable, target); const isInspected = hasComponent(world, Inspected, target); const isRefreshing = hasComponent(world, MediaRefresh, target); + const isCameraActive = !!anyEntityWith(APP.world, MyCameraTool); const openLinkButtonObj = world.eid2obj.get(ObjectMenu.openLinkButtonRef[menu])!; const mirrorButtonObj = world.eid2obj.get(ObjectMenu.mirrorButtonRef[menu])!; @@ -316,6 +335,9 @@ function updateVisibility(world: HubsWorld, menu: EntityID, frozen: boolean) { inspectButtonObj.position.fromArray(ObjectMenuPositions.inspect); } refreshButtonObj.position.fromArray(ObjectMenuPositions.refresh); + + world.eid2obj.get(ObjectMenu.cameraFocusButtonRef[menu])!.visible = isCameraActive; + world.eid2obj.get(ObjectMenu.cameraTrackButtonRef[menu])!.visible = isCameraActive; } else { [ ObjectMenu.unpinButtonRef[menu], @@ -324,7 +346,9 @@ function updateVisibility(world: HubsWorld, menu: EntityID, frozen: boolean) { ObjectMenu.cloneButtonRef[menu], ObjectMenu.rotateButtonRef[menu], ObjectMenu.scaleButtonRef[menu], - ObjectMenu.dropButtonRef[menu] + ObjectMenu.dropButtonRef[menu], + ObjectMenu.cameraFocusButtonRef[menu], + ObjectMenu.cameraTrackButtonRef[menu] ].forEach(ref => { world.eid2obj.get(ref)!.visible = false; }); @@ -347,8 +371,6 @@ function updateVisibility(world: HubsWorld, menu: EntityID, frozen: boolean) { // Hide unimplemented features for now. // TODO: Implement and show the buttons. - world.eid2obj.get(ObjectMenu.cameraFocusButtonRef[menu])!.visible = false; - world.eid2obj.get(ObjectMenu.cameraTrackButtonRef[menu])!.visible = false; world.eid2obj.get(ObjectMenu.deserializeDrawingButtonRef[menu])!.visible = false; } diff --git a/src/prefabs/object-menu.tsx b/src/prefabs/object-menu.tsx index eb1dcc4ec1..d2677a9b6b 100644 --- a/src/prefabs/object-menu.tsx +++ b/src/prefabs/object-menu.tsx @@ -240,8 +240,8 @@ export const ObjectMenuPositions = { background: [ 0, 0, uiZ - 0.0005] as ArrayVec3, pin: [ 0, 0.225, uiZ] as ArrayVec3, unpin: [ 0, 0.225, uiZ] as ArrayVec3, - focus: [-0.25, 0.575, uiZ] as ArrayVec3, - track: [ 0.25, 0.575, uiZ] as ArrayVec3, + focus: [ -0.2, 0.375, uiZ] as ArrayVec3, + track: [ 0.2, 0.375, uiZ] as ArrayVec3, remove: [ 0, -0.075, uiZ] as ArrayVec3, drop: [ 0, -0.225, uiZ] as ArrayVec3, inspect: [ 0, -0.225, uiZ] as ArrayVec3,