Zoom / pan on arkit in unity - v1.4
camera movement script is not working in ARkit.
@vinita18 could you give me some more details on the issue? Which AR scene are you using?
I am using tabletop AR scene (using arkit) in v1.4. and I am using CameraMovement script on Camera to zoom and pan. But it's not working in AR scene.
@VinitaSawant Tabletop AR scene is a starting point for users to create their applications. Camera and interactions are application specific so we do not ship the scene with a camera script.
We have used LeanTouch asset from Unity asset store in the past to set up interaction with the tabletop AR example.
Also, another alternative is to switch the Extent Options to RangeAroundCenter and use the following script - this is a modified QuadTreeCameraMovement . Please note, this is an example script to give you an idea of how to use map api for zoom/pan ( not intended for production use. )
namespace Mapbox.Examples
{
using Mapbox.Unity.Map;
using Mapbox.Unity.Utilities;
using Mapbox.Utils;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
public class ARTableTopCameraMovement : MonoBehaviour
{
[SerializeField]
[Range(1, 20)]
public float _panSpeed = 1.0f;
[SerializeField]
float _zoomSpeed = 0.25f;
[SerializeField]
public Camera _referenceCamera;
[SerializeField]
AbstractMap _mapManager;
private Vector3 _origin;
private Vector3 _mousePosition;
private Vector3 _mousePositionPrevious;
private bool _shouldDrag;
private bool _isInitialized = false;
void Awake()
{
if (null == _referenceCamera)
{
_referenceCamera = GetComponent<Camera>();
if (null == _referenceCamera) { Debug.LogErrorFormat("{0}: reference camera not set", this.GetType().Name); }
}
_mapManager.OnInitialized += () =>
{
_isInitialized = true;
};
}
private void LateUpdate()
{
if (!_isInitialized) { return; }
if (Input.touchSupported && Input.touchCount > 0)
{
HandleTouch();
}
else
{
HandleMouseAndKeyBoard();
}
}
void HandleMouseAndKeyBoard()
{
// zoom
float scrollDelta = 0.0f;
scrollDelta = Input.GetAxis("Mouse ScrollWheel");
ZoomMapUsingTouchOrMouse(scrollDelta);
//pan keyboard
float xMove = Input.GetAxis("Horizontal");
float zMove = Input.GetAxis("Vertical");
PanMapUsingKeyBoard(xMove, zMove);
//pan mouse
PanMapUsingTouchOrMouse();
}
void HandleTouch()
{
float zoomFactor = 0.0f;
//pinch to zoom.
switch (Input.touchCount)
{
case 1:
{
PanMapUsingTouchOrMouse();
}
break;
case 2:
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
zoomFactor = 0.01f * (touchDeltaMag - prevTouchDeltaMag);
}
ZoomMapUsingTouchOrMouse(zoomFactor);
break;
default:
break;
}
}
void ZoomMapUsingTouchOrMouse(float zoomFactor)
{
var zoom = Mathf.Max(0.0f, Mathf.Min(_mapManager.Zoom + zoomFactor * _zoomSpeed, 21.0f));
_mapManager.UpdateMap(_mapManager.CenterLatitudeLongitude, zoom);
}
void PanMapUsingKeyBoard(float xMove, float zMove)
{
if (Math.Abs(xMove) > 0.0f || Math.Abs(zMove) > 0.0f)
{
// Get the number of degrees in a tile at the current zoom level.
// Divide it by the tile width in pixels ( 256 in our case)
// to get degrees represented by each pixel.
// Keyboard offset is in pixels, therefore multiply the factor with the offset to move the center.
float factor = _panSpeed * (Conversions.GetTileScaleInDegrees((float)_mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom));
//MapLocationOptions locationOptions = new MapLocationOptions
//{
var latitudeLongitude = new Vector2d(_mapManager.CenterLatitudeLongitude.x + zMove * factor * 2.0f, _mapManager.CenterLatitudeLongitude.y + xMove * factor * 4.0f);
//};
_mapManager.UpdateMap(latitudeLongitude, _mapManager.Zoom);
}
}
void PanMapUsingTouchOrMouse()
{
UseMeterConversion();
}
void UseMeterConversion()
{
if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
var mousePosScreen = Input.mousePosition;
_mousePosition = mousePosScreen; //_referenceCamera.ScreenToWorldPoint(mousePosScreen);
if (_shouldDrag == false)
{
_shouldDrag = true;
_origin = mousePosScreen;//_referenceCamera.ScreenToWorldPoint(mousePosScreen);
}
}
else
{
_shouldDrag = false;
}
if (_shouldDrag == true)
{
var changeFromPreviousPosition = _mousePositionPrevious - _mousePosition;
if (Mathf.Abs(changeFromPreviousPosition.x) > 0.0f || Mathf.Abs(changeFromPreviousPosition.y) > 0.0f)
{
_mousePositionPrevious = _mousePosition;
var offset = _origin - _mousePosition;
if (Mathf.Abs(offset.x) > 0.0f || Mathf.Abs(offset.z) > 0.0f)
{
PanMapUsingKeyBoard(offset.x, offset.y);
}
_origin = _mousePosition;
}
else
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
_mousePositionPrevious = _mousePosition;
_origin = _mousePosition;
}
}
}
}
}
@VinitaSawant Closing this ticket. Let us know if you have questions.